Skip to content

Commit 3fd8115

Browse files
committed
add more tests around possible hash collisions
1 parent e850d90 commit 3fd8115

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tests/hash.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ mod structs {
106106
field2: bool,
107107
}
108108

109+
#[derive(Hash)]
110+
struct PossibleCollision {
111+
a: &'static str,
112+
b: &'static str,
113+
}
114+
109115
#[test]
110116
fn assert() {
111117
assert_eq!(
@@ -136,6 +142,10 @@ mod structs {
136142
}),
137143
do_hash(&(42, true))
138144
);
145+
assert_ne!(
146+
do_hash(&PossibleCollision { a: "a", b: "bc" }),
147+
do_hash(&PossibleCollision { a: "ab", b: "c" })
148+
);
139149
}
140150
}
141151

@@ -188,6 +198,13 @@ mod enums {
188198
C,
189199
}
190200

201+
#[derive(Hash)]
202+
enum SameDataEnum {
203+
A(i32),
204+
B(i32),
205+
C(i32),
206+
}
207+
191208
#[derive(Hash)]
192209
enum StructEnum {
193210
A { x: i32 },
@@ -227,6 +244,12 @@ mod enums {
227244
do_hash(&SimpleEnum::C),
228245
do_hash(&core::mem::discriminant(&SimpleEnum::C))
229246
);
247+
let sa = SameDataEnum::A(42);
248+
assert_eq!(do_hash(&sa), do_hash(&(core::mem::discriminant(&sa), 42)));
249+
let sb = SameDataEnum::B(42);
250+
assert_eq!(do_hash(&sb), do_hash(&(core::mem::discriminant(&sb), 42)));
251+
let sc = SameDataEnum::C(42);
252+
assert_eq!(do_hash(&sc), do_hash(&(core::mem::discriminant(&sc), 42)));
230253
let ta = TupleEnum::A(42);
231254
let tb = TupleEnum::B("test", true);
232255
assert_eq!(do_hash(&ta), do_hash(&(core::mem::discriminant(&ta), 42)));

0 commit comments

Comments
 (0)