Skip to content

Commit 222a342

Browse files
committed
Some simple quality tests for EntityHasher
1 parent bc9e159 commit 222a342

File tree

1 file changed

+32
-0
lines changed
  • crates/bevy_ecs/src/entity

1 file changed

+32
-0
lines changed

crates/bevy_ecs/src/entity/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,4 +952,36 @@ mod tests {
952952
assert!(Entity::new(2, 2) > Entity::new(1, 2));
953953
assert!(Entity::new(2, 2) >= Entity::new(1, 2));
954954
}
955+
956+
// Feel free to change this test if needed, but it seemed like an important
957+
// part of the best-case performance changes in PR#9903.
958+
#[test]
959+
fn entity_hash_keeps_similar_ids_together() {
960+
use std::hash::BuildHasher;
961+
let hash = bevy_utils::EntityHash;
962+
963+
let first_id = 0xC0FFEE << 8;
964+
let first_hash = hash.hash_one(Entity::from_raw(first_id));
965+
966+
for i in 1..=255 {
967+
let id = first_id + i;
968+
let hash = hash.hash_one(Entity::from_raw(id));
969+
assert_eq!(hash.wrapping_sub(first_hash) as u32, i);
970+
}
971+
}
972+
973+
#[test]
974+
fn entity_hash_id_bitflip_affects_high_7_bits() {
975+
use std::hash::BuildHasher;
976+
let hash = bevy_utils::EntityHash;
977+
978+
let first_id = 0xC0FFEE;
979+
let first_hash = hash.hash_one(Entity::from_raw(first_id)) >> 57;
980+
981+
for bit in 0..u32::BITS {
982+
let id = first_id ^ (1 << bit);
983+
let hash = hash.hash_one(Entity::from_raw(id)) >> 57;
984+
assert_ne!(hash, first_hash);
985+
}
986+
}
955987
}

0 commit comments

Comments
 (0)