use super::*;
use crate::shading::culling::NO_CHAIN;
use wasm_bindgen_test::wasm_bindgen_test;
const REGIONS: u32 = 1000;
fn candidates() -> Candidates {
Candidates::new(&wgpu::Limits {
max_storage_buffer_binding_size: u64::from(2 * REGIONS)
* std::mem::size_of::<DrawInstance>() as u64,
max_buffer_size: u64::MAX,
..wgpu::Limits::default()
})
}
fn roomy() -> Candidates {
Candidates::new(&wgpu::Limits {
max_storage_buffer_binding_size: 1 << 30,
max_buffer_size: u64::MAX,
..wgpu::Limits::default()
})
}
fn mesh(material: usize, need: u32) -> CandidateMesh {
CandidateMesh {
material: MaterialId::issue(material, 1),
look: SetLook::default(),
blended: false,
need,
bounds: [Vec3::ZERO; 2],
word: 0,
chain: NO_CHAIN,
positions: (0, 0),
}
}
fn place(candidates: &mut Candidates, index: usize, meshes: &[CandidateMesh]) -> bool {
let model = ModelId::issue(0, 1);
candidates
.place(index, (model, meshes), false, None)
.is_ok()
}
fn regions(candidates: &Candidates) -> Vec<Range<u32>> {
candidates
.sets
.iter()
.map(|(_, _, region)| region)
.collect()
}
#[wasm_bindgen_test(unsupported = test)]
fn a_refused_placement_leaves_every_region_where_it_was() {
let mut candidates = candidates();
assert!(place(&mut candidates, 0, &[mesh(0, 100)]));
assert!(place(&mut candidates, 1, &[mesh(1, 500)]));
let before = regions(&candidates);
assert!(!place(&mut candidates, 2, &[mesh(0, 70)]));
assert_eq!(regions(&candidates), before);
assert!(candidates.sets.fit(REGIONS), "the regions fit the device");
assert_eq!(candidates.end(), 2, "no slot taken");
}
#[wasm_bindgen_test(unsupported = test)]
fn an_instance_placed_again_near_the_limit_fits() {
let mut candidates = candidates();
assert!(place(&mut candidates, 0, &[mesh(0, 600)]));
assert!(place(&mut candidates, 0, &[mesh(0, 600)]));
assert!(candidates.fit([(0, Candidates::keys(&[mesh(0, 600)], false, false))]));
}
#[wasm_bindgen_test(unsupported = test)]
fn a_dry_run_fits_exactly_when_the_placements_do() {
let mut midway = candidates();
assert!(place(&mut midway, 0, &[mesh(0, 100)]));
assert!(place(&mut midway, 1, &[mesh(1, 500)]));
let plan = [
(2, Candidates::keys(&[mesh(0, 60)], false, false)),
(0, Vec::new()),
];
assert!(!midway.fit(plan), "the plan passes the limit midway");
assert!(!place(&mut midway, 2, &[mesh(0, 60)]));
let mut state = 0x2545_f491u32;
let mut random = |bound: u32| {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
(state >> 8) % bound
};
let mut agreed = [0; 2];
for _ in 0..300 {
let history: Vec<(usize, Vec<CandidateMesh>)> = (0..random(12))
.map(|_| {
let meshes = (0..1 + random(3))
.map(|_| mesh(random(4) as usize, 1 + random(120)))
.collect();
(random(8) as usize, meshes)
})
.collect();
let first = random(8) as usize;
let plan: Vec<(usize, Vec<CandidateMesh>)> = (0..1 + random(4) as usize)
.map(|step| {
let meshes = (0..random(4))
.map(|_| mesh(random(4) as usize, 1 + random(200)))
.collect();
((first + step) % 8, meshes)
})
.collect();
let replay = || {
let mut candidates = candidates();
for (index, meshes) in &history {
place(&mut candidates, *index, meshes);
}
candidates
};
let dry = replay();
let keys = plan
.iter()
.map(|(index, meshes)| (*index, Candidates::keys(meshes, false, false)));
let fits = dry.fit(keys);
let mut placed = replay();
let mut succeeded = true;
for (index, meshes) in &plan {
succeeded = place(&mut placed, *index, meshes);
assert!(
placed.sets.fit(REGIONS),
"a placement leaves the regions bound"
);
if !succeeded {
break;
}
}
assert_eq!(fits, succeeded, "the dry run of {plan:?} after {history:?}");
agreed[usize::from(fits)] += 1;
}
assert!(
agreed.iter().all(|&count| count > 20),
"plans both fit and not: {agreed:?}"
);
}
#[wasm_bindgen_test(unsupported = test)]
fn many_placements_change_each_set_once() {
let mut candidates = roomy();
for index in 0..10_000 {
assert!(place(&mut candidates, index, &[mesh(index % 2, 1)]));
}
assert_eq!(candidates.sets.end(), 2);
assert!(
candidates.sets.changed() <= 2,
"{} set records marked changed",
candidates.sets.changed()
);
}