Skip to content

Commit a01f21d

Browse files
committed
Some simple quality tests for EntityHasher
1 parent 9c0fca0 commit a01f21d

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
@@ -998,4 +998,36 @@ mod tests {
998998
assert!(Entity::new(2, 2) > Entity::new(1, 2));
999999
assert!(Entity::new(2, 2) >= Entity::new(1, 2));
10001000
}
1001+
1002+
// Feel free to change this test if needed, but it seemed like an important
1003+
// part of the best-case performance changes in PR#9903.
1004+
#[test]
1005+
fn entity_hash_keeps_similar_ids_together() {
1006+
use std::hash::BuildHasher;
1007+
let hash = bevy_utils::EntityHash;
1008+
1009+
let first_id = 0xC0FFEE << 8;
1010+
let first_hash = hash.hash_one(Entity::from_raw(first_id));
1011+
1012+
for i in 1..=255 {
1013+
let id = first_id + i;
1014+
let hash = hash.hash_one(Entity::from_raw(id));
1015+
assert_eq!(hash.wrapping_sub(first_hash) as u32, i);
1016+
}
1017+
}
1018+
1019+
#[test]
1020+
fn entity_hash_id_bitflip_affects_high_7_bits() {
1021+
use std::hash::BuildHasher;
1022+
let hash = bevy_utils::EntityHash;
1023+
1024+
let first_id = 0xC0FFEE;
1025+
let first_hash = hash.hash_one(Entity::from_raw(first_id)) >> 57;
1026+
1027+
for bit in 0..u32::BITS {
1028+
let id = first_id ^ (1 << bit);
1029+
let hash = hash.hash_one(Entity::from_raw(id)) >> 57;
1030+
assert_ne!(hash, first_hash);
1031+
}
1032+
}
10011033
}

0 commit comments

Comments
 (0)