From 94dfde94b68392aa071952375709f3eb4813729b Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 5 Aug 2024 23:30:31 +0200 Subject: test(ecs): make component storage unit tests work --- ecs/src/component/storage.rs | 80 +++++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 30 deletions(-) (limited to 'ecs') 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::(), + is_optional: ComponentIsOptional::No, + }, + ComponentMetadata { + id: ComponentId::of::(), + 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::(), - is_optional: ComponentIsOptional::No, - }, - ComponentMetadata { - id: ComponentId::of::(), - 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::(), - ComponentId::of::(), - ComponentId::of::(), + component_ids: HashMap::from([ + (ComponentId::of::(), 0), + (ComponentId::of::(), 1), + (ComponentId::of::(), 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::(), - ComponentId::of::(), - ComponentId::of::(), - ComponentId::of::(), + component_ids: HashMap::from([ + (ComponentId::of::(), 0), + (ComponentId::of::(), 1), + (ComponentId::of::(), 2), + (ComponentId::of::(), 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::(), + is_optional: ComponentIsOptional::No, + }, + ComponentMetadata { + id: ComponentId::of::(), + 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::(), - is_optional: ComponentIsOptional::No, - }, - ComponentMetadata { - id: ComponentId::of::(), - 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" -- cgit v1.2.3-18-g5258