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
#[cfg(test)]
mod test_order_preservation {
use dcbor_parse::parse_dcbor_item;
use dcbor_pattern::{Matcher, Pattern};
#[test]
fn test_path_order_deterministic() {
let cbor_data = parse_dcbor_item("[42, 100, 200]").unwrap();
let pattern = Pattern::parse("[@item(number)]").unwrap();
// Run the same pattern multiple times to check for deterministic
// ordering
let mut all_results = Vec::new();
for _ in 0..10 {
let (paths, captures) = pattern.paths_with_captures(&cbor_data);
all_results.push((paths, captures));
}
// All results should be identical
let first_result = &all_results[0];
for result in &all_results[1..] {
assert_eq!(
result.0, first_result.0,
"Paths should be deterministic"
);
assert_eq!(
result.1, first_result.1,
"Captures should be deterministic"
);
}
// Verify we have exactly one path (not duplicates)
assert_eq!(
first_result.0.len(),
1,
"Should have exactly one deduplicated path"
);
// Verify we have the expected number of captures
if let Some(item_captures) = first_result.1.get("item") {
assert_eq!(item_captures.len(), 3, "Should have 3 captured items");
} else {
panic!("Should have 'item' captures");
}
}
#[test]
fn test_capture_order_deterministic() {
let cbor_data = parse_dcbor_item("[1, 2, 3, 1, 2, 3]").unwrap(); // Intentional duplicates
let pattern = Pattern::parse("[@num(number)]").unwrap();
// Run multiple times to check deterministic ordering
let mut all_results = Vec::new();
for _ in 0..10 {
let (paths, captures) = pattern.paths_with_captures(&cbor_data);
all_results.push((paths, captures));
}
// All results should be identical
let first_result = &all_results[0];
for result in &all_results[1..] {
assert_eq!(
result.0, first_result.0,
"Paths should be deterministic"
);
assert_eq!(
result.1, first_result.1,
"Captures should be deterministic"
);
}
// Check that captures are deduplicated properly
if let Some(num_captures) = first_result.1.get("num") {
// We should have captured paths [array,1], [array,2], [array,3]
// (deduplicated but order preserved) Since we have
// duplicate values 1,2,3 appearing twice, but they create identical
// paths, we should only see 3 unique captured paths,
// not 6
assert_eq!(
num_captures.len(),
3,
"Should capture 3 unique paths (deduplicated)"
);
} else {
panic!("Should have 'num' captures");
}
}
}