1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#[cfg(test)]
mod test_order_stability {
use dcbor_parse::parse_dcbor_item;
use dcbor_pattern::{Matcher, Pattern};
#[test]
fn test_deterministic_order_with_multiple_paths() {
// Create a scenario that would generate multiple paths in a predictable
// order
let cbor_data = parse_dcbor_item(r#"[[1], [2], [3], [1]]"#).unwrap();
let pattern = Pattern::parse("[@outer([@inner(number)])]").unwrap();
let (paths, captures) = pattern.paths_with_captures(&cbor_data);
// Record the exact order we get
let first_run_paths = paths.clone();
let first_run_captures = captures.clone();
// Run the same pattern many times and verify we always get the same
// order
for i in 0..100 {
let (test_paths, test_captures) =
pattern.paths_with_captures(&cbor_data);
assert_eq!(
test_paths, first_run_paths,
"Paths order differed on iteration {}",
i
);
assert_eq!(
test_captures, first_run_captures,
"Captures order differed on iteration {}",
i
);
}
// Additionally verify the structure makes sense
assert_eq!(
paths.len(),
1,
"Should have exactly one main path (the array)"
);
if let Some(outer_captures) = captures.get("outer") {
// Should have [1], [2], [3] captured (deduplicated, [1] appears
// twice but creates same path)
assert_eq!(
outer_captures.len(),
3,
"Should have 3 unique outer captures"
);
}
if let Some(inner_captures) = captures.get("inner") {
// Should have 1, 2, 3 captured (deduplicated)
assert_eq!(
inner_captures.len(),
3,
"Should have 3 unique inner captures"
);
}
}
#[test]
fn test_order_preserved_across_hash_boundaries() {
// Test with values that are likely to hash differently
let cbor_data =
parse_dcbor_item(r#"[1, 1000000, 2, 1000000, 3]"#).unwrap();
let pattern = Pattern::parse("[@item(number)]").unwrap();
let first_run = pattern.paths_with_captures(&cbor_data);
// Verify deterministic behavior across many runs
for i in 0..50 {
let test_run = pattern.paths_with_captures(&cbor_data);
assert_eq!(
test_run.0, first_run.0,
"Paths order changed on iteration {}",
i
);
assert_eq!(
test_run.1, first_run.1,
"Captures order changed on iteration {}",
i
);
}
// Verify we get the expected deduplication
if let Some(item_captures) = first_run.1.get("item") {
// Should capture: 1, 1000000, 2, 3 (in order of first appearance,
// duplicates removed)
assert_eq!(
item_captures.len(),
4,
"Should have 4 unique captured values"
);
}
}
}