Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions formal/catalogs/protocol-vectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,78 @@
]
]
},
{
"name": "bigint decimal text uses canonical numeric identity",
"input": {
"literalNegative": "-0001",
"literalPositive": "0001",
"literalZero": "-0"
},
"bigintArgs": {
"negative": "-0001",
"negativeZero": "-0",
"paddedZero": "000",
"positive": "0001",
"signedPaddedZero": "-000",
"zero": "0"
},
"expected": [
[
"literalNegative",
"-0001"
],
[
"literalPositive",
"0001"
],
[
"literalZero",
"-0"
],
[
"negative",
"-1"
],
[
"negativeZero",
"0"
],
[
"paddedZero",
"0"
],
[
"positive",
"1"
],
[
"signedPaddedZero",
"0"
],
[
"zero",
"0"
]
]
},
{
"name": "bigint decimal text retains magnitudes beyond fixed-width integers",
"input": {},
"bigintArgs": {
"negative": "-0001234567890123456789012345678901234567890123456789012345678901234567890",
"positive": "0001234567890123456789012345678901234567890123456789012345678901234567890"
},
"expected": [
[
"negative",
"-1234567890123456789012345678901234567890123456789012345678901234567890"
],
[
"positive",
"1234567890123456789012345678901234567890123456789012345678901234567890"
]
]
},
{
"name": "empty arguments",
"input": {},
Expand Down
2 changes: 1 addition & 1 deletion go/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ func TestProtocolRemainingVectors(t *testing.T) {
selection := os.Getenv("DIALCACHE_PROTOCOL_CORPUS")
expected := 0
if selection != "generated" {
expected = 191
expected = 193
}
if selection != "fixed" {
for _, rows := range generatedProtocolGroups(t) {
Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_protocol_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def key_from(vector):

def test_schemas_provenance_and_inventory():
assert all(corpus["schemaVersion"] == 3 for corpus in CORPORA)
expected_counts = [191, 457, 589, 297]
expected_counts = [193, 457, 589, 297]
for corpus, expected in zip(CORPORA, expected_counts, strict=True):
rows = [row for value in corpus.values() if isinstance(value, list) for row in value]
assert len(rows) == expected
Expand Down
12 changes: 11 additions & 1 deletion rust/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,17 @@ impl ArgValue {
"bigint text {text:?} is not a decimal integer"
)));
}
text
let digits = text
.strip_prefix('-')
.unwrap_or(&text)
.trim_start_matches('0');
if digits.is_empty() {
"0".to_owned()
} else if text.starts_with('-') {
format!("-{digits}")
} else {
digits.to_owned()
}
}
ArgValue::Str(text) => text,
}))
Expand Down
46 changes: 46 additions & 0 deletions rust/tests/protocol_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,56 @@ mod fixtures;
#[path = "formal/key_vectors.rs"]
mod key_vectors;

use dialcache::identity::{normalize_args, ArgValue, Identity};
use serde_json::{json, Value};

type Check = fn(&Value) -> Result<(), String>;

#[test]
fn bigint_text_uses_canonical_keys_but_strings_remain_literal() {
for (text, expected) in [
("1", "1"),
("0001", "1"),
("-1", "-1"),
("-0001", "-1"),
("0", "0"),
("-0", "0"),
("000", "0"),
("-000", "0"),
("0010200", "10200"),
("-0010200", "-10200"),
] {
let args = normalize_args([("n", ArgValue::BigInt(text.into()))]).unwrap();
assert_eq!(args, vec![("n".into(), expected.into())], "{text}");
for tracked in [false, true] {
let identity = Identity::new("id", text, "Get")
.namespace("urn")
.tracked(tracked);
let canonical = normalize_args([("n", ArgValue::Str(expected.into()))]).unwrap();
assert_eq!(
identity.clone().args(args.clone()).keys().unwrap(),
identity.clone().args(canonical).keys().unwrap(),
"{text}, tracked={tracked}"
);
assert_eq!(identity.id, text);
}
let literal = normalize_args([("n", ArgValue::Str(text.into()))]).unwrap();
assert_eq!(literal, vec![("n".into(), text.into())]);
}
}

#[test]
fn bigint_text_preserves_arbitrary_width_magnitudes() {
let magnitude = "12345678901234567890".repeat(100);
for sign in ["", "-"] {
let expected = format!("{sign}{magnitude}");
for text in [expected.clone(), format!("{sign}000{magnitude}")] {
let actual = normalize_args([("n", ArgValue::BigInt(text))]).unwrap();
assert_eq!(actual, vec![("n".into(), expected.clone())]);
}
}
}

#[test]
fn key_assertions_distinguish_library_rejections_from_bad_fixtures() {
let valid_shape = json!({
Expand Down
Loading