]> git.lizzy.rs Git - rust.git/blob - library/alloc/tests/btree_set_hash.rs
:arrow_up: rust-analyzer
[rust.git] / library / alloc / tests / btree_set_hash.rs
1 use crate::hash;
2 use std::collections::BTreeSet;
3
4 #[test]
5 fn test_hash() {
6     let mut x = BTreeSet::new();
7     let mut y = BTreeSet::new();
8
9     x.insert(1);
10     x.insert(2);
11     x.insert(3);
12
13     y.insert(3);
14     y.insert(2);
15     y.insert(1);
16
17     assert_eq!(hash(&x), hash(&y));
18 }
19
20 #[test]
21 fn test_prefix_free() {
22     let x = BTreeSet::from([1, 2, 3]);
23     let y = BTreeSet::<i32>::new();
24
25     // If hashed by iteration alone, `(x, y)` and `(y, x)` would visit the same
26     // order of elements, resulting in the same hash. But now that we also hash
27     // the length, they get distinct sequences of hashed data.
28     assert_ne!(hash(&(&x, &y)), hash(&(&y, &x)));
29 }