use archetype_ecs::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq)]
struct Position {
x: f32,
y: f32,
}
#[derive(Debug, Clone, Copy, PartialEq)]
struct Velocity {
x: f32,
y: f32,
}
#[test]
fn test_query_cache_basic() {
let mut world = World::new();
for i in 0..100 {
world.spawn_entity((
Position {
x: i as f32,
y: 0.0,
},
Velocity { x: 1.0, y: 1.0 },
));
}
let count1 = world.query::<(&Position, &Velocity)>().iter().count();
assert_eq!(count1, 100);
let stats = world.query_cache_stats();
assert!(
stats.num_cached_queries >= 1,
"Cache stats should be accessible"
);
let count2 = world.query::<(&Position, &Velocity)>().iter().count();
assert_eq!(count2, 100);
}
#[test]
fn test_query_cache_incremental_invalidation() {
let mut world = World::new();
for i in 0..50 {
world.spawn_entity((Position {
x: i as f32,
y: 0.0,
},));
}
let count1 = world.query::<&Position>().iter().count();
assert_eq!(count1, 50);
for i in 50..100 {
world.spawn_entity((Position {
x: i as f32,
y: 0.0,
},));
}
let count2 = world.query::<&Position>().iter().count();
assert_eq!(count2, 100);
let stats = world.query_cache_stats();
assert!(stats.num_cached_queries >= 1);
}
#[test]
fn test_query_cache_clear() {
let mut world = World::new();
for i in 0..50 {
world.spawn_entity((Position {
x: i as f32,
y: 0.0,
},));
}
let _count = world.query::<&Position>().iter().count();
world.clear_query_cache();
let stats_after = world.query_cache_stats();
assert_eq!(stats_after.num_cached_queries, 0);
}
#[test]
fn test_query_cache_performance() {
let mut world = World::new();
for i in 0..1000 {
world.spawn_entity((
Position {
x: i as f32,
y: 0.0,
},
Velocity { x: 1.0, y: 1.0 },
));
}
let _count = world.query::<(&Position, &Velocity)>().iter().count();
let start = std::time::Instant::now();
for _ in 0..100 {
let _count = world.query::<(&Position, &Velocity)>().iter().count();
}
let duration = start.elapsed();
assert!(
duration.as_millis() < 1000,
"100 cached queries took {:?}, expected <1000ms",
duration
);
}
#[test]
fn test_query_cache_stats() {
let mut world = World::new();
let stats = world.query_cache_stats();
assert_eq!(stats.num_cached_queries, 0);
assert_eq!(stats.total_cached_archetypes, 0);
for i in 0..100 {
world.spawn_entity((Position {
x: i as f32,
y: 0.0,
},));
}
let _count = world.query::<&Position>().iter().count();
let stats = world.query_cache_stats();
assert!(stats.num_cached_queries >= 1);
assert!(stats.total_cached_archetypes >= 1);
assert_eq!(stats.total_archetypes, world.archetype_count());
}