facett-map3d/tests/town_subset_selects_without_owning.rs
//! **Choosing which ways to extrude must not COPY them.**
//!
//! `view_town_subset` — and its camera-blind spelling `densest_cluster_subset` — has
//! three exits that hand the input straight back, and all three used to do it with
//! `ways.to_vec()`. `Way` is the most expensive type in the house to clone (`points` +
//! `lonlat` + `holes` + `name`: three allocations and 24 B per vertex), and the last of
//! those exits was the worst of them — it built the clipped `keep` set in full, threw it
//! away because it was too small, and then deep-cloned the whole input a second time. One
//! call, the set copied twice, to answer a question about SELECTION.
//!
//! The function returns `Cow<'_, [Way]>` now, so "keep everything" is a borrow by
//! construction. This asserts it the only way that cannot be faked: by **pointer
//! identity** with the caller's slice. An enum discriminant would be a state
//! round-trip; a pointer that is literally the input's is the applied output.
//!
//! Each arm carries its own RED — the same assertion run against `Cow::Owned(to_vec())`,
//! which is exactly what the code used to return — so a matcher that passes anything
//! cannot pass here.
mod common;
use facett_core::Theme;
use facett_map::Way;
use facett_map3d::extrude;
use std::borrow::Cow;
/// Did this reply hand back the caller's own memory, or a copy of it?
fn is_the_caller_s_own(reply: &[Way], ways: &[Way]) -> bool {
std::ptr::eq(reply.as_ptr(), ways.as_ptr()) && reply.len() == ways.len()
}
/// A handful of houses around one point — far too few to cluster, which is the
/// `TownChoice::Nothing` arm: no camera, and nothing to window.
fn a_few_houses(n: usize) -> Vec<Way> {
let theme = Theme::default();
let (clon, clat) = (18.07_f64, 59.33_f64);
let mut ways = Vec::new();
for i in 0..n {
let (dx, dy) = ((i % 5) as f64 * 3e-4, (i / 5) as f64 * 3e-4);
let (x, y) = (clon + dx, clat + dy);
let r = [(x, y), (x + 1e-4, y), (x + 1e-4, y + 1e-4), (x, y + 1e-4), (x, y)];
ways.extend(Way::from_lonlat(
(i + 1) as i64,
&r,
&[("building".to_string(), "yes".to_string())],
&theme,
));
}
ways
}
#[test]
fn the_keep_everything_exits_borrow_instead_of_copying() {
// ── arm 1: no camera, too sparse to cluster (`TownChoice::Nothing`) ──
let sparse = a_few_houses(12);
assert!(sparse.len() >= 10, "the fixture has ways at all: {}", sparse.len());
let reply = extrude::densest_cluster_subset(&sparse);
println!(
" sparse {:3} ways ⇒ {:9} same memory {}",
sparse.len(),
if matches!(reply, Cow::Borrowed(_)) { "Borrowed" } else { "Owned" },
is_the_caller_s_own(&reply, &sparse)
);
assert!(
is_the_caller_s_own(&reply, &sparse),
"the no-camera arm copied {} ways to say \"keep them all\"",
sparse.len()
);
// ── arm 2: already smaller than the window we would cut ──
// Smaller than 2·TOWN_HALF_MAX_M (1.16 km), so there is nothing left to cut.
let town = common::region_with_town(900.0, 8);
let span = extrude::world_span_metres(&town).expect("the town has a span");
assert!(
span < 2.0 * extrude::TOWN_HALF_MAX_M as f32,
"the fixture is already smaller than the window ({span:.0} m of {:.0} m)",
2.0 * extrude::TOWN_HALF_MAX_M
);
let reply = extrude::densest_cluster_subset(&town);
println!(
" town {:3} ways span {span:6.0} m ⇒ {:9} same memory {}",
town.len(),
if matches!(reply, Cow::Borrowed(_)) { "Borrowed" } else { "Owned" },
is_the_caller_s_own(&reply, &town)
);
assert!(
is_the_caller_s_own(&reply, &town),
"the already-a-town arm copied {} ways to hand them straight back",
town.len()
);
// ── RED: the shape the code used to return, through the same assertion ──
for (label, ways) in [("sparse", &sparse), ("town", &town)] {
let legacy: Cow<'_, [Way]> = Cow::Owned(ways.to_vec());
assert!(
!is_the_caller_s_own(&legacy, ways),
"{label}: a deep copy would have to be indistinguishable from the caller's own \
memory for the assertions above to mean anything — it is not, so they can see \
the copy"
);
}
}
/// The arm that DOES subset still owns its answer — a borrow there would be a lie, and
/// this is what stops the guard above from being satisfiable by borrowing everything.
#[test]
fn the_arm_that_actually_subsets_still_owns_its_answer() {
let region = common::region_with_town(55_000.0, 24);
let region_span = extrude::world_span_metres(®ion).expect("a region span");
assert!(region_span > 50_000.0, "the fixture is region-scale: {region_span:.0} m");
let reply = extrude::densest_cluster_subset(®ion);
let town_span = extrude::world_span_metres(&reply).expect("the town has a span");
println!(
" region {:5} ways span {region_span:7.0} m ⇒ {:5} ways span {town_span:6.0} m \
{:9}",
region.len(),
reply.len(),
if matches!(reply, Cow::Borrowed(_)) { "Borrowed" } else { "Owned" }
);
assert!(
matches!(reply, Cow::Owned(_)),
"the subsetting arm returned a borrow — it cannot have cut anything"
);
assert!(!is_the_caller_s_own(&reply, ®ion), "…and it is not the caller's slice");
assert!(reply.len() < region.len(), "it kept fewer ways than it was given");
assert!(town_span < 5_000.0, "and the survivors span a town, not a region ({town_span:.0} m)");
}