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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! Property-based tests for Pattern Hash/Eq consistency
//!
//! These tests use proptest to verify that pattern hashing is consistent with equality:
//! if p1 == p2, then hash(p1) == hash(p2) for all patterns p1, p2
//!
//! This is a fundamental requirement for correct HashMap/HashSet behavior.
use pattern_core::{Pattern, Symbol};
use proptest::prelude::*;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
// ============================================================================
// Helper Functions
// ============================================================================
/// Computes the hash of a pattern using the default hasher
fn hash_pattern<V: Hash>(p: &Pattern<V>) -> u64 {
let mut hasher = DefaultHasher::new();
p.hash(&mut hasher);
hasher.finish()
}
// ============================================================================
// Pattern Generators
// ============================================================================
/// Strategy for generating atomic patterns (no elements)
fn atomic_pattern() -> impl Strategy<Value = Pattern<String>> {
any::<String>().prop_map(Pattern::point)
}
/// Strategy for generating shallow patterns (1-2 levels, 1-5 elements)
fn shallow_pattern() -> impl Strategy<Value = Pattern<String>> {
(
any::<String>(),
prop::collection::vec(atomic_pattern(), 0..=5),
)
.prop_map(|(value, elements)| Pattern::pattern(value, elements))
}
/// Strategy for generating patterns with varying depths (0-10 levels)
fn varying_depth_pattern(max_depth: u32) -> BoxedStrategy<Pattern<String>> {
let leaf = any::<String>().prop_map(Pattern::point).boxed();
leaf.prop_recursive(
max_depth, // Max depth
256, // Max nodes
10, // Items per collection
|inner| {
(any::<String>(), prop::collection::vec(inner, 0..=5))
.prop_map(|(value, elements)| Pattern::pattern(value, elements))
.boxed()
},
)
.boxed()
}
/// Strategy for generating Symbol patterns
fn symbol_pattern() -> impl Strategy<Value = Pattern<Symbol>> {
any::<String>()
.prop_map(|s| Symbol(s))
.prop_map(Pattern::point)
}
// ============================================================================
// Hash/Eq Consistency Tests
// ============================================================================
proptest! {
/// Test that equal atomic patterns hash to the same value
#[test]
fn test_hash_eq_consistency_atomic(
value in any::<String>(),
) {
let p1 = Pattern::point(value.clone());
let p2 = Pattern::point(value);
let hash1 = hash_pattern(&p1);
let hash2 = hash_pattern(&p2);
prop_assert_eq!(p1, p2, "Patterns should be equal");
prop_assert_eq!(
hash1,
hash2,
"Equal patterns must hash to same value"
);
}
}
proptest! {
/// Test that equal shallow patterns hash to the same value
#[test]
fn test_hash_eq_consistency_shallow(
p1 in shallow_pattern(),
) {
let p2 = p1.clone();
let hash1 = hash_pattern(&p1);
let hash2 = hash_pattern(&p2);
prop_assert_eq!(p1, p2);
prop_assert_eq!(hash1, hash2);
}
}
proptest! {
/// Test that equal nested patterns hash to the same value
#[test]
fn test_hash_eq_consistency_nested(
p1 in varying_depth_pattern(10),
) {
let p2 = p1.clone();
// Compute hashes before comparison (which consumes values)
let hash1 = hash_pattern(&p1);
let hash2 = hash_pattern(&p2);
prop_assert_eq!(p1, p2);
prop_assert_eq!(
hash1,
hash2,
"Equal nested patterns must hash to same value"
);
}
}
proptest! {
/// Test hash consistency for Symbol patterns
#[test]
fn test_hash_eq_consistency_symbol(
value in any::<String>(),
) {
let p1 = Pattern::point(Symbol(value.clone()));
let p2 = Pattern::point(Symbol(value));
let hash1 = hash_pattern(&p1);
let hash2 = hash_pattern(&p2);
prop_assert_eq!(p1, p2);
prop_assert_eq!(hash1, hash2);
}
}
// ============================================================================
// Structure Distinguishes Hash Tests
// ============================================================================
proptest! {
/// Test that atomic and compound patterns with same value likely hash differently
///
/// Note: Hash collision is technically possible but extremely unlikely.
/// This test catches obvious implementation bugs where structure is ignored.
#[test]
fn test_structure_distinguishes_hash(
value in any::<String>(),
child_value in any::<String>(),
) {
let atomic = Pattern::point(value.clone());
let compound = Pattern::pattern(
value,
vec![Pattern::point(child_value)]
);
if atomic != compound {
// Different patterns should likely have different hashes
// (not guaranteed due to hash collisions, but expected)
let hash1 = hash_pattern(&atomic);
let hash2 = hash_pattern(&compound);
// We don't assert inequality (hash collisions are possible)
// but we do check that they're not equal patterns
prop_assert_ne!(atomic, compound, "Patterns should differ");
// For most inputs, hashes should differ
// This catches bugs where structure is completely ignored
// We accept this test might rarely pass even with correct implementation
if hash1 == hash2 {
// Collision detected - acceptable but rare
// Log but don't fail
}
}
}
}
proptest! {
/// Test that patterns with different element counts likely hash differently
#[test]
fn test_element_count_affects_hash(
value in any::<String>(),
child1 in any::<String>(),
child2 in any::<String>(),
) {
let p1 = Pattern::pattern(
value.clone(),
vec![Pattern::point(child1.clone())]
);
let p2 = Pattern::pattern(
value,
vec![Pattern::point(child1), Pattern::point(child2)]
);
// Compute hashes before comparison
let hash1 = hash_pattern(&p1);
let hash2 = hash_pattern(&p2);
prop_assert_ne!(p1, p2, "Patterns with different element counts should differ");
// We check they're not equal patterns (the important part)
// Hash inequality is expected but not required
if hash1 == hash2 {
// Collision - rare but acceptable
}
}
}
// ============================================================================
// Reflexive Hash Tests
// ============================================================================
proptest! {
/// Test that a pattern hashes to the same value when hashed multiple times
#[test]
fn test_hash_reflexive(
p in varying_depth_pattern(10),
) {
let hash1 = hash_pattern(&p);
let hash2 = hash_pattern(&p);
prop_assert_eq!(hash1, hash2, "Same pattern should always hash to same value");
}
}
proptest! {
/// Test hash reflexivity for deeply nested patterns
#[test]
fn test_hash_reflexive_deep(
p in varying_depth_pattern(50),
) {
let hash1 = hash_pattern(&p);
let hash2 = hash_pattern(&p);
prop_assert_eq!(hash1, hash2);
}
}