diff options
author | HampusM <hampus@hampusmat.com> | 2024-08-05 23:30:31 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-08-05 23:30:31 +0200 |
commit | 94dfde94b68392aa071952375709f3eb4813729b (patch) | |
tree | 7ec7ee7a434c5dbf6ec7dacb11ffb4ac7d1e7145 /ecs/src | |
parent | a2eb0a235b738864a107eb9d859878c68f7d5bbf (diff) |
test(ecs): make component storage unit tests work
Diffstat (limited to 'ecs/src')
-rw-r--r-- | ecs/src/component/storage.rs | 80 |
1 files changed, 50 insertions, 30 deletions
diff --git a/ecs/src/component/storage.rs b/ecs/src/component/storage.rs index 9c149ed..44addb7 100644 --- a/ecs/src/component/storage.rs +++ b/ecs/src/component/storage.rs @@ -259,7 +259,7 @@ impl<'component_storage> Iterator for ArchetypeRefIter<'component_storage> #[cfg(test)] mod tests { - use std::collections::HashSet; + use std::collections::HashMap; use ecs_macros::Component; @@ -270,6 +270,7 @@ mod tests IsOptional as ComponentIsOptional, Metadata as ComponentMetadata, }; + use crate::entity::Uid as EntityUid; use crate::lock::Lock; use crate::{self as ecs, EntityComponent}; @@ -331,18 +332,22 @@ mod tests assert_eq!(component_storage.archetype_lookup.len(), 1); + let mut components_metadata = [ + ComponentMetadata { + id: ComponentId::of::<HealthPotion>(), + is_optional: ComponentIsOptional::No, + }, + ComponentMetadata { + id: ComponentId::of::<Hookshot>(), + is_optional: ComponentIsOptional::No, + }, + ]; + + components_metadata.sort_by_key(|comp_metadata| comp_metadata.id); + let lookup = component_storage .archetype_lookup - .get(&ArchetypeId::from_components_metadata([ - ComponentMetadata { - id: ComponentId::of::<HealthPotion>(), - is_optional: ComponentIsOptional::No, - }, - ComponentMetadata { - id: ComponentId::of::<Hookshot>(), - is_optional: ComponentIsOptional::No, - }, - ])) + .get(&ArchetypeId::from_components_metadata(components_metadata)) .expect("Expected entry in archetype lookup map"); let first_archetype_index = lookup @@ -358,10 +363,15 @@ mod tests let mut component_storage = Storage::default(); component_storage.archetypes.push(Archetype { - component_ids: HashSet::from([ - ComponentId::of::<IronBoots>(), - ComponentId::of::<HealthPotion>(), - ComponentId::of::<Hookshot>(), + component_ids: HashMap::from([ + (ComponentId::of::<IronBoots>(), 0), + (ComponentId::of::<HealthPotion>(), 1), + (ComponentId::of::<Hookshot>(), 2), + ]), + entity_lookup: HashMap::from([ + (EntityUid::new_unique(), 0), + (EntityUid::new_unique(), 1), + (EntityUid::new_unique(), 2), ]), components: vec![ vec![EntityComponent { @@ -383,11 +393,17 @@ mod tests }); component_storage.archetypes.push(Archetype { - component_ids: HashSet::from([ - ComponentId::of::<DekuNut>(), - ComponentId::of::<IronBoots>(), - ComponentId::of::<Bow>(), - ComponentId::of::<Hookshot>(), + component_ids: HashMap::from([ + (ComponentId::of::<DekuNut>(), 0), + (ComponentId::of::<IronBoots>(), 1), + (ComponentId::of::<Bow>(), 2), + (ComponentId::of::<Hookshot>(), 3), + ]), + entity_lookup: HashMap::from([ + (EntityUid::new_unique(), 0), + (EntityUid::new_unique(), 1), + (EntityUid::new_unique(), 2), + (EntityUid::new_unique(), 3), ]), components: vec![ vec![EntityComponent { @@ -430,18 +446,22 @@ mod tests assert_eq!(component_storage.archetype_lookup.len(), 1); + let mut comps_metadata = [ + ComponentMetadata { + id: ComponentId::of::<IronBoots>(), + is_optional: ComponentIsOptional::No, + }, + ComponentMetadata { + id: ComponentId::of::<Hookshot>(), + is_optional: ComponentIsOptional::No, + }, + ]; + + comps_metadata.sort_by_key(|comp_metadata| comp_metadata.id); + let archetypes = component_storage .archetype_lookup - .get(&ArchetypeId::from_components_metadata([ - ComponentMetadata { - id: ComponentId::of::<IronBoots>(), - is_optional: ComponentIsOptional::No, - }, - ComponentMetadata { - id: ComponentId::of::<Hookshot>(), - is_optional: ComponentIsOptional::No, - }, - ])) + .get(&ArchetypeId::from_components_metadata(comps_metadata)) .expect(concat!( "Expected a archetype for IronBoots & Hookshot to be found in the ", "archetype lookup map" |