3 // The tests below compare the computed hashes to particular expected values
4 // in order to test that we produce the same results on different platforms,
5 // regardless of endianness and `usize` and `isize` size differences (this
6 // of course assumes we run these tests on platforms that differ in those
7 // ways). The expected values depend on the hashing algorithm used, so they
8 // need to be updated whenever StableHasher changes its hashing algorithm.
11 fn test_hash_integers() {
12 // Test that integers are handled consistently across platforms.
13 let test_u8 = 0xAB_u8;
14 let test_u16 = 0xFFEE_u16;
15 let test_u32 = 0x445577AA_u32;
16 let test_u64 = 0x01234567_13243546_u64;
17 let test_u128 = 0x22114433_66557788_99AACCBB_EEDDFF77_u128;
18 let test_usize = 0xD0C0B0A0_usize;
20 let test_i8 = -100_i8;
21 let test_i16 = -200_i16;
22 let test_i32 = -300_i32;
23 let test_i64 = -400_i64;
24 let test_i128 = -500_i128;
25 let test_isize = -600_isize;
27 let mut h = StableHasher::new();
29 test_u16.hash(&mut h);
30 test_u32.hash(&mut h);
31 test_u64.hash(&mut h);
32 test_u128.hash(&mut h);
33 test_usize.hash(&mut h);
35 test_i16.hash(&mut h);
36 test_i32.hash(&mut h);
37 test_i64.hash(&mut h);
38 test_i128.hash(&mut h);
39 test_isize.hash(&mut h);
41 // This depends on the hashing algorithm. See note at top of file.
42 let expected = (1784307454142909076, 11471672289340283879);
44 assert_eq!(h.finalize(), expected);
48 fn test_hash_usize() {
49 // Test that usize specifically is handled consistently across platforms.
50 let test_usize = 0xABCDEF01_usize;
52 let mut h = StableHasher::new();
53 test_usize.hash(&mut h);
55 // This depends on the hashing algorithm. See note at top of file.
56 let expected = (5798740672699530587, 11186240177685111648);
58 assert_eq!(h.finalize(), expected);
62 fn test_hash_isize() {
63 // Test that isize specifically is handled consistently across platforms.
64 let test_isize = -7_isize;
66 let mut h = StableHasher::new();
67 test_isize.hash(&mut h);
69 // This depends on the hashing algorithm. See note at top of file.
70 let expected = (2789913510339652884, 674280939192711005);
72 assert_eq!(h.finalize(), expected);
75 fn hash<T: HashStable<()>>(t: &T) -> u128 {
76 let mut h = StableHasher::new();
78 t.hash_stable(ctx, &mut h);
82 // Check that bit set hash includes the domain size.
84 fn test_hash_bit_set() {
85 use rustc_index::bit_set::BitSet;
86 let a: BitSet<usize> = BitSet::new_empty(1);
87 let b: BitSet<usize> = BitSet::new_empty(2);
89 assert_ne!(hash(&a), hash(&b));
92 // Check that bit matrix hash includes the matrix dimensions.
94 fn test_hash_bit_matrix() {
95 use rustc_index::bit_set::BitMatrix;
96 let a: BitMatrix<usize, usize> = BitMatrix::new(1, 1);
97 let b: BitMatrix<usize, usize> = BitMatrix::new(1, 2);
99 assert_ne!(hash(&a), hash(&b));
102 // Check that exchanging the value of two adjacent fields changes the hash.
104 fn test_attribute_permutation() {
105 macro_rules! test_type {
112 impl<CTX> HashStable<CTX> for Foo {
113 fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
114 self.a.hash_stable(hcx, hasher);
115 self.b.hash_stable(hcx, hasher);
119 #[allow(overflowing_literals)]
120 let mut item = Foo { a: 0xFF, b: 0xFF_FF };
121 let hash_a = hash(&item);
122 std::mem::swap(&mut item.a, &mut item.b);
123 let hash_b = hash(&item);
127 "The hash stayed the same after values were swapped for type `{}`!",
144 // Check that the `isize` hashing optimization does not produce the same hash when permuting two
147 fn test_isize_compression() {
148 fn check_hash(a: u64, b: u64) {
149 let hash_a = hash(&(a as isize, b as isize));
150 let hash_b = hash(&(b as isize, a as isize));
153 "The hash stayed the same when permuting values `{a}` and `{b}!",
157 check_hash(0xAA, 0xAAAA);
158 check_hash(0xFF, 0xFFFF);
159 check_hash(0xAAAA, 0xAAAAAA);
160 check_hash(0xAAAAAA, 0xAAAAAAAA);
161 check_hash(0xFF, 0xFFFFFFFFFFFFFFFF);