nativeview_test_support::install_counting_allocator!();
#[path = "support/cq_harness.rs"]
mod cq_harness;
use nativeview_test_support::count_allocs;
use cq_harness::{CardGrid, Harness, Leaves, VIEWPORT, build_card_grid, resize_column};
use nativeview_engine::Viewport;
#[derive(Clone, Copy, Debug)]
enum Stimulus {
Viewport(&'static [f32]),
Column(&'static [f32]),
}
const CROSSING: Stimulus = Stimulus::Viewport(&[900.0, 1000.0, 961.0, 959.0]);
const NO_BREAKPOINT: Stimulus = Stimulus::Viewport(&[1002.0, 1004.0, 1006.0, 1008.0]);
const CONTAINER_ONLY: Stimulus = Stimulus::Column(&[450.0, 500.0, 480.5, 479.5]);
struct Fixture {
h: Harness,
grid: CardGrid,
}
impl Fixture {
fn new(containers: bool, leaves: Leaves) -> Self {
let mut h = Harness::default();
let grid = build_card_grid(&mut h, 50, 3, containers, leaves);
Self { h, grid }
}
fn frame(&mut self, stimulus: Stimulus, i: usize) {
match stimulus {
Stimulus::Viewport(widths) => {
let width = widths[i % widths.len()];
assert!(self.h.relayout(Viewport { width, ..VIEWPORT }), "resize to {width}");
}
Stimulus::Column(widths) => resize_column(&mut self.h, &self.grid, widths[i % widths.len()]),
}
}
fn round(&mut self, stimulus: Stimulus) -> Vec<usize> {
let n = match stimulus {
Stimulus::Viewport(w) | Stimulus::Column(w) => w.len(),
};
(0..n).map(|i| count_allocs(|| self.frame(stimulus, i)).1).collect()
}
fn measure(containers: bool, leaves: Leaves, stimulus: Stimulus) -> (Vec<usize>, Vec<usize>) {
let mut f = Self::new(containers, leaves);
for _ in 0..3 {
f.round(stimulus);
}
(f.round(stimulus), f.round(stimulus))
}
}
fn assert_parity(stimulus: Stimulus, (c, l): (bool, Leaves), (tc, tl): (bool, Leaves)) {
let (a1, a2) = Fixture::measure(c, l, stimulus);
let (b1, b2) = Fixture::measure(tc, tl, stimulus);
assert_eq!(a1, a2, "{l:?} allocations drift between rounds under {stimulus:?}");
assert_eq!(b1, b2, "{tl:?} allocations drift between rounds under {stimulus:?}");
assert_eq!(
a1, b1,
"{l:?} (containers={c}) must allocate exactly what {tl:?} (containers={tc}) allocates \
under {stimulus:?}: the container-query machinery adds zero"
);
}
#[test]
fn counting_alloc_probe_fires_on_this_thread_only() {
nativeview_test_support::assert_counts_only_this_thread();
}
#[test]
fn query_machinery_overhead_is_zero_allocations() {
for stimulus in [CROSSING, NO_BREAKPOINT, CONTAINER_ONLY] {
assert_parity(stimulus, (true, Leaves::Identical), (false, Leaves::Unqueried));
}
}
#[test]
fn flipping_rules_allocate_like_their_unqueried_twin() {
for stimulus in [CROSSING, NO_BREAKPOINT, CONTAINER_ONLY] {
assert_parity(stimulus, (true, Leaves::Flipping), (true, Leaves::Unqueried));
}
}
#[test]
fn responsive_typography_allocates_like_plain_text() {
for stimulus in [CROSSING, CONTAINER_ONLY] {
assert_parity(stimulus, (true, Leaves::Typography), (true, Leaves::PlainText));
}
}
#[test]
fn queries_really_flip_and_clean_frames_allocate_nothing() {
let mut f = Fixture::new(true, Leaves::Flipping);
f.frame(CROSSING, 0); assert_eq!(f.h.width(f.grid.leaves[0]), 40.0);
f.frame(CROSSING, 1); assert_eq!(f.h.width(f.grid.leaves[0]), 120.0);
f.frame(CROSSING, 3); assert_eq!(f.h.width(f.grid.leaves[0]), 40.0);
resize_column(&mut f.h, &f.grid, 500.0);
assert_eq!(f.h.width(f.grid.leaves[0]), 120.0);
for _ in 0..3 {
f.round(CROSSING);
}
f.h.relayout(Viewport { width: 959.0, ..VIEWPORT });
let (_, clean) = count_allocs(|| {
for _ in 0..200 {
assert!(!f.h.relayout(Viewport { width: 959.0, ..VIEWPORT }));
}
});
assert_eq!(clean, 0, "clean container-query frames allocated {clean} times");
}