brood/world/
impl_eq.rs

1use super::World;
2use crate::registry;
3use core::cmp;
4
5impl<Registry, Resources> cmp::PartialEq for World<Registry, Resources>
6where
7    Registry: registry::PartialEq,
8    Resources: cmp::PartialEq,
9{
10    fn eq(&self, other: &Self) -> bool {
11        self.len == other.len
12            && self.archetypes == other.archetypes
13            && self.entity_allocator == other.entity_allocator
14            && self.resources == other.resources
15    }
16}
17
18impl<Registry, Resources> cmp::Eq for World<Registry, Resources>
19where
20    Registry: registry::Eq,
21    Resources: cmp::Eq,
22{
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28    use crate::{
29        entity,
30        resources,
31        Registry,
32        Resources,
33    };
34
35    #[derive(Debug, Eq, PartialEq)]
36    struct A(u32);
37
38    #[derive(Debug, Eq, PartialEq)]
39    struct B(char);
40
41    type Registry = Registry!(A, B);
42    type Resources = Resources!(A, B);
43
44    #[test]
45    fn empty_eq() {
46        assert_eq!(World::<Registry!()>::new(), World::<Registry!()>::new());
47    }
48
49    #[test]
50    fn with_entities_eq() {
51        let mut world_a = World::<Registry>::new();
52        let mut world_b = World::<Registry>::new();
53
54        world_a.insert(entity!(A(1), B('a')));
55        world_a.insert(entity!(A(2), B('b')));
56        world_a.insert(entity!(A(3), B('c')));
57        world_a.insert(entity!(A(4)));
58        world_a.insert(entity!(A(5)));
59        world_a.insert(entity!());
60
61        world_b.insert(entity!(A(1), B('a')));
62        world_b.insert(entity!(A(2), B('b')));
63        world_b.insert(entity!(A(3), B('c')));
64        world_b.insert(entity!(A(4)));
65        world_b.insert(entity!(A(5)));
66        world_b.insert(entity!());
67
68        assert_eq!(world_a, world_b);
69    }
70
71    #[test]
72    fn with_resources_eq() {
73        let world_a = World::<Registry, Resources>::with_resources(resources!(A(42), B('a')));
74        let world_b = World::<Registry, Resources>::with_resources(resources!(A(42), B('a')));
75
76        assert_eq!(world_a, world_b);
77    }
78
79    #[test]
80    fn with_entities_and_resources_eq() {
81        let mut world_a = World::<Registry, Resources>::with_resources(resources!(A(42), B('a')));
82        let mut world_b = World::<Registry, Resources>::with_resources(resources!(A(42), B('a')));
83
84        world_a.insert(entity!(A(1), B('a')));
85        world_a.insert(entity!(A(2), B('b')));
86        world_a.insert(entity!(A(3), B('c')));
87        world_a.insert(entity!(A(4)));
88        world_a.insert(entity!(A(5)));
89        world_a.insert(entity!());
90
91        world_b.insert(entity!(A(1), B('a')));
92        world_b.insert(entity!(A(2), B('b')));
93        world_b.insert(entity!(A(3), B('c')));
94        world_b.insert(entity!(A(4)));
95        world_b.insert(entity!(A(5)));
96        world_b.insert(entity!());
97
98        assert_eq!(world_a, world_b);
99    }
100
101    #[test]
102    fn archetypes_not_equal() {
103        let mut world_a = World::<Registry>::new();
104        let mut world_b = World::<Registry>::new();
105
106        world_a.insert(entity!(A(1), B('a')));
107        world_a.insert(entity!(A(2), B('b')));
108        world_a.insert(entity!(A(3), B('c')));
109
110        world_b.insert(entity!(A(1)));
111        world_b.insert(entity!(A(2)));
112        world_b.insert(entity!(A(3)));
113
114        assert_ne!(world_a, world_b);
115    }
116
117    #[test]
118    fn allocators_not_equal() {
119        let mut world_a = World::<Registry>::new();
120        let mut world_b = World::<Registry>::new();
121
122        world_a.insert(entity!(A(1), B('a')));
123
124        let entity_identifier = world_b.insert(entity!(A(1), B('a')));
125        world_b.remove(entity_identifier);
126        world_b.insert(entity!(A(1), B('a')));
127
128        // The generational index of the entities will be different.
129        assert_ne!(world_a, world_b);
130    }
131
132    #[test]
133    fn resources_not_equal() {
134        let world_a = World::<Registry, Resources>::with_resources(resources!(A(42), B('a')));
135        let world_b = World::<Registry, Resources>::with_resources(resources!(A(42), B('b')));
136
137        assert_ne!(world_a, world_b);
138    }
139}