Skip to content

Commit

Permalink
Fix bug when re-killing Entity after atomic killing followed by alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
torkleyy committed Jan 3, 2019
1 parent eb8510c commit f12546d
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions src/world/entity.rs
Expand Up @@ -70,6 +70,8 @@ impl Allocator {
}

self.alive.remove(entity.id());
// If the `Entity` was killed by `kill_atomic`, remove the bit set by it.
self.killed.remove(entity.id());

self.update_generation_length(id);

Expand Down Expand Up @@ -463,13 +465,6 @@ impl ZeroableGeneration {
}
}

#[test]
fn test_nonzero_optimization() {
use std::mem::size_of;
assert_eq!(size_of::<Option<Generation>>(), size_of::<Generation>());
assert_eq!(size_of::<Option<Entity>>(), size_of::<Entity>());
}

#[derive(Default, Debug)]
struct EntityCache {
cache: Vec<Index>,
Expand Down Expand Up @@ -529,3 +524,48 @@ fn atomic_decrement(i: &AtomicUsize) -> Option<usize> {
}
None
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_nonzero_optimization() {
use std::mem::size_of;
assert_eq!(size_of::<Option<Generation>>(), size_of::<Generation>());
assert_eq!(size_of::<Option<Entity>>(), size_of::<Entity>());
}

#[test]
fn kill_atomic_create_merge() {
let mut allocator = Allocator::default();

let entity = allocator.allocate();
assert_eq!(entity.id(), 0);

allocator.kill_atomic(entity).unwrap();

assert_ne!(allocator.allocate(), entity);

assert_eq!(allocator.killed.contains(entity.id()), true);
assert_eq!(allocator.merge(), vec![entity]);
}

#[test]
fn kill_atomic_kill_now_create_merge() {
let mut allocator = Allocator::default();

let entity = allocator.allocate();

allocator.kill_atomic(entity).unwrap();

assert_ne!(allocator.allocate(), entity);

allocator.kill(&[entity]).unwrap();

allocator.allocate();

assert_eq!(allocator.killed.contains(entity.id()), false);
assert_eq!(allocator.merge(), vec![]);
}
}

0 comments on commit f12546d

Please sign in to comment.