]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_data_structures/src/stable_hasher/tests.rs
Move `{core,std}::stream::Stream` to `{core,std}::async_iter::AsyncIterator`.
[rust.git] / compiler / rustc_data_structures / src / stable_hasher / tests.rs
1 use super::*;
2
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.
9
10 #[test]
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;
19
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;
26
27     let mut h = StableHasher::new();
28     test_u8.hash(&mut h);
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);
34     test_i8.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);
40
41     // This depends on the hashing algorithm. See note at top of file.
42     let expected = (1784307454142909076, 11471672289340283879);
43
44     assert_eq!(h.finalize(), expected);
45 }
46
47 #[test]
48 fn test_hash_usize() {
49     // Test that usize specifically is handled consistently across platforms.
50     let test_usize = 0xABCDEF01_usize;
51
52     let mut h = StableHasher::new();
53     test_usize.hash(&mut h);
54
55     // This depends on the hashing algorithm. See note at top of file.
56     let expected = (5798740672699530587, 11186240177685111648);
57
58     assert_eq!(h.finalize(), expected);
59 }
60
61 #[test]
62 fn test_hash_isize() {
63     // Test that isize specifically is handled consistently across platforms.
64     let test_isize = -7_isize;
65
66     let mut h = StableHasher::new();
67     test_isize.hash(&mut h);
68
69     // This depends on the hashing algorithm. See note at top of file.
70     let expected = (2789913510339652884, 674280939192711005);
71
72     assert_eq!(h.finalize(), expected);
73 }
74
75 fn hash<T: HashStable<()>>(t: &T) -> u128 {
76     let mut h = StableHasher::new();
77     let ctx = &mut ();
78     t.hash_stable(ctx, &mut h);
79     h.finish()
80 }
81
82 // Check that bit set hash includes the domain size.
83 #[test]
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);
88     assert_ne!(a, b);
89     assert_ne!(hash(&a), hash(&b));
90 }
91
92 // Check that bit matrix hash includes the matrix dimensions.
93 #[test]
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);
98     assert_ne!(a, b);
99     assert_ne!(hash(&a), hash(&b));
100 }
101
102 // Check that exchanging the value of two adjacent fields changes the hash.
103 #[test]
104 fn test_attribute_permutation() {
105     macro_rules! test_type {
106         ($ty: ty) => {{
107             struct Foo {
108                 a: $ty,
109                 b: $ty,
110             }
111
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);
116                 }
117             }
118
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);
124             assert_ne!(
125                 hash_a,
126                 hash_b,
127                 "The hash stayed the same after values were swapped for type `{}`!",
128                 stringify!($ty)
129             );
130         }};
131     }
132
133     test_type!(u16);
134     test_type!(u32);
135     test_type!(u64);
136     test_type!(u128);
137
138     test_type!(i16);
139     test_type!(i32);
140     test_type!(i64);
141     test_type!(i128);
142 }
143
144 // Check that the `isize` hashing optimization does not produce the same hash when permuting two
145 // values.
146 #[test]
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));
151         assert_ne!(
152             hash_a, hash_b,
153             "The hash stayed the same when permuting values `{a}` and `{b}!",
154         );
155     }
156
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);
162 }