From d6fde80cb4a769af72a5e50c8742c676627f24df Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 1 Oct 2021 12:29:09 -0700 Subject: Include the length in BTree hashes This change makes it consistent with `Hash` for all other collections. --- library/alloc/tests/btree_set_hash.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'library/alloc/tests') diff --git a/library/alloc/tests/btree_set_hash.rs b/library/alloc/tests/btree_set_hash.rs index e06a95ded94..ab275ac4353 100644 --- a/library/alloc/tests/btree_set_hash.rs +++ b/library/alloc/tests/btree_set_hash.rs @@ -1,9 +1,8 @@ +use crate::hash; use std::collections::BTreeSet; #[test] fn test_hash() { - use crate::hash; - let mut x = BTreeSet::new(); let mut y = BTreeSet::new(); @@ -17,3 +16,14 @@ fn test_hash() { assert_eq!(hash(&x), hash(&y)); } + +#[test] +fn test_prefix_free() { + let x = BTreeSet::from([1, 2, 3]); + let y = BTreeSet::::new(); + + // If hashed by iteration alone, `(x, y)` and `(y, x)` would visit the same + // order of elements, resulting in the same hash. But now that we also hash + // the length, they get distinct sequences of hashed data. + assert_ne!(hash(&(&x, &y)), hash(&(&y, &x))); +} -- cgit 1.4.1-3-g733a5