mirror of
https://github.com/zed-industries/zed.git
synced 2026-12-02 21:40:41 +01:00
gpui: Add BoundsTree test suite (#67443)
This PR primarily adds a test suite to `gpui::BoundsTree` that uses a naive implementation of a `BoundsTree` that is a `Vec<T>`, which iterates each element to find overlaps and maxes. The motivation for this change is having a clean test suite to test out the performance optimizations in a follow-up PR. This PR also fixes two extremely rare bugs within the `BoundsTree` caused by floats being imprecise after 24 bits and not handling some `NaN` cases within the `BoundsTree` float comparisons. These bugs are practically never hit because they rely on primitives overlapping by ~1/30,000th of a pixel (~0.00003 px), but they're necessary to fix to ensure the test suite will be working when I start working on performance improvements. Release Notes: - [GPUI] N/A - N/A
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crate::{Bounds, Half, scene::PrimitiveKind};
|
||||
use std::{
|
||||
cmp,
|
||||
cmp::{self, Ordering},
|
||||
fmt::Debug,
|
||||
ops::{Add, Sub},
|
||||
ptr::NonNull,
|
||||
@@ -43,13 +43,92 @@ where
|
||||
U: Clone + Debug + Default + PartialEq,
|
||||
{
|
||||
/// Bounding box containing this node and all descendants.
|
||||
bounds: Bounds<U>,
|
||||
bounds: NodeBounds<U>,
|
||||
/// Maximum key in this subtree.
|
||||
max_key: Key,
|
||||
/// Node-specific data.
|
||||
kind: NodeKind,
|
||||
}
|
||||
|
||||
/// Bounds stored as corners.
|
||||
///
|
||||
/// Storing a union as an origin and size would round its far corner, which
|
||||
/// could then fall short of a child's. The corners are also not reordered for
|
||||
/// negative sizes, so `intersects` agrees with [`Bounds::intersects`] for every
|
||||
/// input.
|
||||
#[derive(Debug, Clone)]
|
||||
struct NodeBounds<U> {
|
||||
min_x: U,
|
||||
min_y: U,
|
||||
max_x: U,
|
||||
max_y: U,
|
||||
}
|
||||
|
||||
impl<U> NodeBounds<U>
|
||||
where
|
||||
U: Clone + Debug + Default + PartialEq + PartialOrd + Add<U, Output = U> + Sub<Output = U>,
|
||||
{
|
||||
fn from_bounds(bounds: &Bounds<U>) -> Self {
|
||||
Self {
|
||||
min_x: bounds.origin.x.clone(),
|
||||
min_y: bounds.origin.y.clone(),
|
||||
max_x: bounds.origin.x.clone() + bounds.size.width.clone(),
|
||||
max_y: bounds.origin.y.clone() + bounds.size.height.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
fn intersects(&self, other: &Self) -> bool {
|
||||
self.min_x < other.max_x
|
||||
&& self.max_x > other.min_x
|
||||
&& self.min_y < other.max_y
|
||||
&& self.max_y > other.min_y
|
||||
}
|
||||
|
||||
fn union(&self, other: &Self) -> Self {
|
||||
Self {
|
||||
min_x: min_value(self.min_x.clone(), other.min_x.clone()),
|
||||
min_y: min_value(self.min_y.clone(), other.min_y.clone()),
|
||||
max_x: max_value(self.max_x.clone(), other.max_x.clone()),
|
||||
max_y: max_value(self.max_y.clone(), other.max_y.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
fn half_perimeter(&self) -> U {
|
||||
(self.max_x.clone() - self.min_x.clone()) + (self.max_y.clone() - self.min_y.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// A bounds with an unordered coordinate (such as an `f32` NaN) never intersects
|
||||
/// anything, so unions skip such coordinates rather than letting them spread into
|
||||
/// the bounds of every ancestor.
|
||||
fn min_value<U: PartialOrd>(a: U, b: U) -> U {
|
||||
match a.partial_cmp(&b) {
|
||||
Some(Ordering::Greater) => b,
|
||||
Some(_) => a,
|
||||
None => {
|
||||
if a.partial_cmp(&a).is_some() {
|
||||
a
|
||||
} else {
|
||||
b
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn max_value<U: PartialOrd>(a: U, b: U) -> U {
|
||||
match a.partial_cmp(&b) {
|
||||
Some(Ordering::Less) => b,
|
||||
Some(_) => a,
|
||||
None => {
|
||||
if a.partial_cmp(&a).is_some() {
|
||||
a
|
||||
} else {
|
||||
b
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The ordering of a bounds, then its kind, which orders the bounds that share
|
||||
/// an ordering.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
@@ -133,6 +212,7 @@ where
|
||||
kind: PrimitiveKind,
|
||||
max_shared_kind: PrimitiveKind,
|
||||
) -> u32 {
|
||||
let new_bounds = NodeBounds::from_bounds(&new_bounds);
|
||||
let ordering = match self.find_max_key(&new_bounds) {
|
||||
None => 1,
|
||||
Some(max_key) if max_key.kind <= max_shared_kind => max_key.ordering,
|
||||
@@ -154,7 +234,7 @@ where
|
||||
}
|
||||
|
||||
/// Finds the maximum key among all bounds that intersect with the query.
|
||||
fn find_max_key(&mut self, query: &Bounds<U>) -> Option<Key> {
|
||||
fn find_max_key(&mut self, query: &NodeBounds<U>) -> Option<Key> {
|
||||
let root_idx = self.root?;
|
||||
|
||||
// Fast path: check if the max-key leaf intersects
|
||||
@@ -211,7 +291,7 @@ where
|
||||
|
||||
/// Inserts a leaf node with the given bounds and key.
|
||||
/// Returns the index of the new leaf.
|
||||
fn insert_leaf(&mut self, bounds: Bounds<U>, key: Key) -> usize {
|
||||
fn insert_leaf(&mut self, bounds: NodeBounds<U>, key: Key) -> usize {
|
||||
let new_leaf_idx = self.nodes.len();
|
||||
self.nodes.push(Node {
|
||||
bounds: bounds.clone(),
|
||||
@@ -391,7 +471,7 @@ where
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{Bounds, Point, Size};
|
||||
use crate::{Bounds, Point, ScaledPixels, Size};
|
||||
use rand::{Rng, SeedableRng};
|
||||
|
||||
#[test]
|
||||
@@ -497,4 +577,636 @@ mod tests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ITEM_COUNT: usize = 2_500;
|
||||
const INVARIANT_CHECK_INTERVAL: usize = 97;
|
||||
|
||||
const ALL_KINDS: [PrimitiveKind; 9] = [
|
||||
PrimitiveKind::Shadow,
|
||||
PrimitiveKind::Quad,
|
||||
PrimitiveKind::Path,
|
||||
PrimitiveKind::Underline,
|
||||
PrimitiveKind::MonochromeSprite,
|
||||
PrimitiveKind::SubpixelSprite,
|
||||
PrimitiveKind::PolychromeSprite,
|
||||
PrimitiveKind::Surface,
|
||||
PrimitiveKind::NativeView,
|
||||
];
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum KindMode {
|
||||
Shared,
|
||||
MonochromeSpriteUnderline,
|
||||
QuadShadow,
|
||||
AllKinds,
|
||||
}
|
||||
|
||||
const KIND_MODES: [KindMode; 4] = [
|
||||
KindMode::Shared,
|
||||
KindMode::MonochromeSpriteUnderline,
|
||||
KindMode::QuadShadow,
|
||||
KindMode::AllKinds,
|
||||
];
|
||||
|
||||
impl KindMode {
|
||||
fn kinds(self, rng: &mut impl Rng) -> (PrimitiveKind, PrimitiveKind) {
|
||||
match self {
|
||||
KindMode::Shared => (PrimitiveKind::Quad, PrimitiveKind::Quad),
|
||||
KindMode::MonochromeSpriteUnderline => {
|
||||
(PrimitiveKind::MonochromeSprite, PrimitiveKind::Underline)
|
||||
}
|
||||
KindMode::QuadShadow => (PrimitiveKind::Quad, PrimitiveKind::Shadow),
|
||||
KindMode::AllKinds => (
|
||||
ALL_KINDS[rng.random_range(0..ALL_KINDS.len())],
|
||||
ALL_KINDS[rng.random_range(0..ALL_KINDS.len())],
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn bounds<U>(x: U, y: U, width: U, height: U) -> Bounds<U>
|
||||
where
|
||||
U: Clone + Debug + Default + PartialEq,
|
||||
{
|
||||
Bounds {
|
||||
origin: Point { x, y },
|
||||
size: Size { width, height },
|
||||
}
|
||||
}
|
||||
|
||||
fn expected_ordering<U>(
|
||||
inserted: &[(Bounds<U>, u32, PrimitiveKind)],
|
||||
new_bounds: &Bounds<U>,
|
||||
max_shared_kind: PrimitiveKind,
|
||||
) -> u32
|
||||
where
|
||||
U: Clone + Debug + Default + PartialEq + PartialOrd + Add<U, Output = U>,
|
||||
{
|
||||
let max_intersecting = inserted
|
||||
.iter()
|
||||
.filter(|(bounds, _, _)| bounds.intersects(new_bounds))
|
||||
.map(|(_, ordering, kind)| (*ordering, *kind))
|
||||
.max();
|
||||
match max_intersecting {
|
||||
None => 1,
|
||||
Some((ordering, max_kind)) if max_kind <= max_shared_kind => ordering,
|
||||
Some((ordering, _)) => ordering + 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// Inserts every item into `tree` and a brute-force oracle, asserting that
|
||||
/// they agree on every ordering and that the tree stays well-formed.
|
||||
fn assert_matches_oracle<U>(
|
||||
tree: &mut BoundsTree<U>,
|
||||
items: &[(Bounds<U>, PrimitiveKind, PrimitiveKind)],
|
||||
label: &str,
|
||||
) where
|
||||
U: Clone
|
||||
+ Debug
|
||||
+ PartialEq
|
||||
+ PartialOrd
|
||||
+ Add<U, Output = U>
|
||||
+ Sub<Output = U>
|
||||
+ Half
|
||||
+ Default,
|
||||
{
|
||||
let mut inserted: Vec<(Bounds<U>, u32, PrimitiveKind)> = Vec::with_capacity(items.len());
|
||||
for (item_index, (new_bounds, kind, max_shared_kind)) in items.iter().enumerate() {
|
||||
let expected = expected_ordering(&inserted, new_bounds, *max_shared_kind);
|
||||
let actual = tree.insert(new_bounds.clone(), *kind, *max_shared_kind);
|
||||
assert_eq!(
|
||||
actual, expected,
|
||||
"{label}: item {item_index} {new_bounds:?} kind {kind:?} max shared {max_shared_kind:?}"
|
||||
);
|
||||
inserted.push((new_bounds.clone(), expected, *kind));
|
||||
if item_index % INVARIANT_CHECK_INTERVAL == 0 {
|
||||
assert_invariants(tree, inserted.len(), label);
|
||||
}
|
||||
}
|
||||
assert_invariants(tree, inserted.len(), label);
|
||||
}
|
||||
|
||||
fn covers<U: PartialOrd>(outer: &NodeBounds<U>, inner: &NodeBounds<U>) -> bool {
|
||||
// Unordered coordinates never intersect anything, so ancestors skip them.
|
||||
let is_ordered = |value: &U| value.partial_cmp(value).is_some();
|
||||
(!is_ordered(&inner.min_x) || outer.min_x <= inner.min_x)
|
||||
&& (!is_ordered(&inner.max_x) || inner.max_x <= outer.max_x)
|
||||
&& (!is_ordered(&inner.min_y) || outer.min_y <= inner.min_y)
|
||||
&& (!is_ordered(&inner.max_y) || inner.max_y <= outer.max_y)
|
||||
}
|
||||
|
||||
fn assert_invariants<U>(tree: &BoundsTree<U>, inserted_count: usize, label: &str)
|
||||
where
|
||||
U: Clone + Debug + Default + PartialEq + PartialOrd,
|
||||
{
|
||||
let Some(root) = tree.root else {
|
||||
assert!(tree.nodes.is_empty(), "{label}");
|
||||
assert!(tree.max_leaf.is_none(), "{label}");
|
||||
assert_eq!(inserted_count, 0, "{label}");
|
||||
return;
|
||||
};
|
||||
|
||||
let mut visited = vec![false; tree.nodes.len()];
|
||||
let mut leaf_count = 0;
|
||||
let mut stack = vec![root];
|
||||
while let Some(node_index) = stack.pop() {
|
||||
assert!(!visited[node_index], "{label}: node {node_index} reused");
|
||||
visited[node_index] = true;
|
||||
let node = &tree.nodes[node_index];
|
||||
match &node.kind {
|
||||
NodeKind::Leaf { key } => {
|
||||
assert_eq!(node.max_key, *key, "{label}: leaf {node_index}");
|
||||
leaf_count += 1;
|
||||
}
|
||||
NodeKind::Internal { children } => {
|
||||
let children = children.as_slice();
|
||||
assert!(
|
||||
(2..=MAX_CHILDREN).contains(&children.len()),
|
||||
"{label}: node {node_index} has {} children",
|
||||
children.len()
|
||||
);
|
||||
let mut children_max_key = None;
|
||||
for &child_index in children {
|
||||
let child = &tree.nodes[child_index];
|
||||
assert!(
|
||||
covers(&node.bounds, &child.bounds),
|
||||
"{label}: node {node_index} {:?} does not cover child {child_index} {:?}",
|
||||
node.bounds,
|
||||
child.bounds
|
||||
);
|
||||
children_max_key = cmp::max(children_max_key, Some(child.max_key));
|
||||
stack.push(child_index);
|
||||
}
|
||||
assert_eq!(
|
||||
Some(node.max_key),
|
||||
children_max_key,
|
||||
"{label}: node {node_index} max key"
|
||||
);
|
||||
if let Some(&last_child) = children.last() {
|
||||
assert_eq!(
|
||||
tree.nodes[last_child].max_key, node.max_key,
|
||||
"{label}: node {node_index} last child doesn't hold the max key"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert!(
|
||||
visited.iter().all(|visited| *visited),
|
||||
"{label}: unreachable node"
|
||||
);
|
||||
assert_eq!(leaf_count, inserted_count, "{label}");
|
||||
let Some(max_leaf) = tree.max_leaf else {
|
||||
panic!("{label}: missing max leaf");
|
||||
};
|
||||
assert!(
|
||||
matches!(tree.nodes[max_leaf].kind, NodeKind::Leaf { .. }),
|
||||
"{label}"
|
||||
);
|
||||
assert_eq!(
|
||||
tree.nodes[max_leaf].max_key, tree.nodes[root].max_key,
|
||||
"{label}: max leaf"
|
||||
);
|
||||
}
|
||||
|
||||
fn log_uniform(rng: &mut impl Rng, min: f32, max: f32) -> f32 {
|
||||
rng.random_range(min.ln()..max.ln()).exp()
|
||||
}
|
||||
|
||||
fn overlap_scenario(rng: &mut impl Rng) -> Vec<Bounds<f32>> {
|
||||
(0..ITEM_COUNT)
|
||||
.map(|_| {
|
||||
bounds(
|
||||
rng.random_range(0.0..200.0),
|
||||
rng.random_range(0.0..200.0),
|
||||
rng.random_range(10.0..100.0),
|
||||
rng.random_range(10.0..100.0),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn nested_shrinking_scenario() -> Vec<Bounds<f32>> {
|
||||
(0..ITEM_COUNT)
|
||||
.map(|item_index| {
|
||||
let inset = item_index as f32 * 0.2;
|
||||
bounds(inset, inset, 1_200.0 - inset * 2.0, 1_000.0 - inset * 2.0)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn nested_growing_scenario() -> Vec<Bounds<f32>> {
|
||||
let mut items = nested_shrinking_scenario();
|
||||
items.reverse();
|
||||
items
|
||||
}
|
||||
|
||||
fn clusters_scenario(rng: &mut impl Rng) -> Vec<Bounds<f32>> {
|
||||
let centers: Vec<(f32, f32)> = (0..24)
|
||||
.map(|_| {
|
||||
(
|
||||
rng.random_range(0.0..4_000.0),
|
||||
rng.random_range(0.0..3_000.0),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
(0..ITEM_COUNT)
|
||||
.map(|_| {
|
||||
let (center_x, center_y) = centers[rng.random_range(0..centers.len())];
|
||||
bounds(
|
||||
center_x + rng.random_range(-60.0..60.0),
|
||||
center_y + rng.random_range(-60.0..60.0),
|
||||
rng.random_range(1.0..40.0),
|
||||
rng.random_range(1.0..40.0),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn grid_scenario(rng: &mut impl Rng) -> Vec<Bounds<f32>> {
|
||||
let columns = 50;
|
||||
(0..ITEM_COUNT)
|
||||
.map(|item_index| {
|
||||
let column = (item_index % columns) as f32;
|
||||
let row = (item_index / columns) as f32;
|
||||
if rng.random_ratio(1, 10) {
|
||||
bounds(
|
||||
column * 20.0 - 5.0,
|
||||
row * 20.0 - 5.0,
|
||||
rng.random_range(20.0..90.0),
|
||||
rng.random_range(20.0..90.0),
|
||||
)
|
||||
} else {
|
||||
bounds(column * 20.0, row * 20.0, 20.0, 20.0)
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn stripes_scenario(rng: &mut impl Rng) -> Vec<Bounds<f32>> {
|
||||
(0..ITEM_COUNT)
|
||||
.map(|item_index| {
|
||||
let offset = (item_index % 200) as f32 * 18.0;
|
||||
if item_index % 2 == 0 {
|
||||
bounds(0.0, offset, 3_600.0, rng.random_range(4.0..24.0))
|
||||
} else {
|
||||
bounds(offset, 0.0, rng.random_range(4.0..24.0), 3_600.0)
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn overlay_scenario(rng: &mut impl Rng) -> Vec<Bounds<f32>> {
|
||||
(0..ITEM_COUNT)
|
||||
.map(|item_index| {
|
||||
if item_index % 250 == 249 {
|
||||
bounds(
|
||||
rng.random_range(-50.0..50.0),
|
||||
rng.random_range(-50.0..50.0),
|
||||
rng.random_range(800.0..1_200.0),
|
||||
rng.random_range(600.0..900.0),
|
||||
)
|
||||
} else {
|
||||
bounds(
|
||||
rng.random_range(0.0..1_000.0),
|
||||
rng.random_range(0.0..800.0),
|
||||
rng.random_range(2.0..60.0),
|
||||
rng.random_range(2.0..20.0),
|
||||
)
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn random_scenario(rng: &mut impl Rng) -> Vec<Bounds<f32>> {
|
||||
(0..ITEM_COUNT)
|
||||
.map(|_| {
|
||||
bounds(
|
||||
rng.random_range(-500.0..3_000.0),
|
||||
rng.random_range(-500.0..2_000.0),
|
||||
log_uniform(rng, 0.5, 2_000.0),
|
||||
log_uniform(rng, 0.5, 2_000.0),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn with_kinds<U: Clone>(
|
||||
items: &[Bounds<U>],
|
||||
mode: KindMode,
|
||||
rng: &mut impl Rng,
|
||||
) -> Vec<(Bounds<U>, PrimitiveKind, PrimitiveKind)>
|
||||
where
|
||||
U: Debug + Default + PartialEq,
|
||||
{
|
||||
items
|
||||
.iter()
|
||||
.map(|item| {
|
||||
let (kind, max_shared_kind) = mode.kinds(rng);
|
||||
(item.clone(), kind, max_shared_kind)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn assert_scenario_matches_oracle(name: &str, items: &[Bounds<f32>], seed: u64) {
|
||||
for mode in KIND_MODES {
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
|
||||
let label = format!("{name} {mode:?} seed {seed}");
|
||||
let items = with_kinds(items, mode, &mut rng);
|
||||
assert_matches_oracle(&mut BoundsTree::<f32>::default(), &items, &label);
|
||||
|
||||
let scaled_items: Vec<_> = items
|
||||
.iter()
|
||||
.map(|(item, kind, max_shared_kind)| {
|
||||
(item.map(ScaledPixels), *kind, *max_shared_kind)
|
||||
})
|
||||
.collect();
|
||||
assert_matches_oracle(
|
||||
&mut BoundsTree::<ScaledPixels>::default(),
|
||||
&scaled_items,
|
||||
&format!("{label} scaled"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oracle_overlap() {
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(1);
|
||||
assert_scenario_matches_oracle("overlap", &overlap_scenario(&mut rng), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oracle_nested_shrinking() {
|
||||
assert_scenario_matches_oracle("nested shrinking", &nested_shrinking_scenario(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oracle_nested_growing() {
|
||||
assert_scenario_matches_oracle("nested growing", &nested_growing_scenario(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oracle_clusters() {
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(4);
|
||||
assert_scenario_matches_oracle("clusters", &clusters_scenario(&mut rng), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oracle_grid() {
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(5);
|
||||
assert_scenario_matches_oracle("grid", &grid_scenario(&mut rng), 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oracle_stripes() {
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(6);
|
||||
assert_scenario_matches_oracle("stripes", &stripes_scenario(&mut rng), 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oracle_overlay() {
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(7);
|
||||
assert_scenario_matches_oracle("overlay", &overlay_scenario(&mut rng), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_oracle_random() {
|
||||
for seed in [8, 9, 10] {
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
|
||||
assert_scenario_matches_oracle("random", &random_scenario(&mut rng), seed);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_union_keeps_exact_corners() {
|
||||
let mut tree = BoundsTree::<f32>::default();
|
||||
let (quad, path, shadow) = (
|
||||
PrimitiveKind::Quad,
|
||||
PrimitiveKind::Path,
|
||||
PrimitiveKind::Shadow,
|
||||
);
|
||||
assert_eq!(tree.insert(bounds(0.0, 0.0, 0.1, 10.0), quad, shadow), 1);
|
||||
// Stored as an origin and size, the union of these would put its right
|
||||
// edge at `-1000.0 + 1000.1`, which rounds to just below `0.1`.
|
||||
assert_eq!(
|
||||
tree.insert(bounds(-1000.0, 0.0, 1.0, 10.0), path, shadow),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
tree.insert(bounds(0.09999, 0.0, 1.0, 10.0), quad, shadow),
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_touching_edges() {
|
||||
let mut tree = BoundsTree::<f32>::default();
|
||||
let (quad, shadow) = (PrimitiveKind::Quad, PrimitiveKind::Shadow);
|
||||
assert_eq!(tree.insert(bounds(0.0, 0.0, 10.0, 10.0), quad, shadow), 1);
|
||||
assert_eq!(tree.insert(bounds(10.0, 0.0, 10.0, 10.0), quad, shadow), 1);
|
||||
assert_eq!(tree.insert(bounds(0.0, 10.0, 10.0, 10.0), quad, shadow), 1);
|
||||
assert_eq!(tree.insert(bounds(10.0, 10.0, 10.0, 10.0), quad, shadow), 1);
|
||||
assert_eq!(
|
||||
tree.insert(bounds(-10.0, -10.0, 10.0, 10.0), quad, shadow),
|
||||
1
|
||||
);
|
||||
assert_eq!(tree.insert(bounds(9.0, 9.0, 2.0, 2.0), quad, shadow), 2);
|
||||
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(11);
|
||||
let items: Vec<_> = (0..ITEM_COUNT)
|
||||
.map(|_| {
|
||||
let (kind, max_shared_kind) = KindMode::AllKinds.kinds(&mut rng);
|
||||
let item = bounds(
|
||||
rng.random_range(0..40) as f32 * 5.0,
|
||||
rng.random_range(0..40) as f32 * 5.0,
|
||||
rng.random_range(0..6) as f32 * 5.0,
|
||||
rng.random_range(0..6) as f32 * 5.0,
|
||||
);
|
||||
(item, kind, max_shared_kind)
|
||||
})
|
||||
.collect();
|
||||
assert_matches_oracle(&mut BoundsTree::default(), &items, "touching");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zero_and_negative_sizes() {
|
||||
let mut tree = BoundsTree::<f32>::default();
|
||||
let (quad, shadow) = (PrimitiveKind::Quad, PrimitiveKind::Shadow);
|
||||
assert_eq!(tree.insert(bounds(0.0, 0.0, 100.0, 100.0), quad, shadow), 1);
|
||||
assert_eq!(tree.insert(bounds(10.0, 10.0, 0.0, 0.0), quad, shadow), 2);
|
||||
assert_eq!(tree.insert(bounds(10.0, 10.0, 0.0, 10.0), quad, shadow), 2);
|
||||
assert_eq!(
|
||||
tree.insert(bounds(50.0, 50.0, -10.0, -10.0), quad, shadow),
|
||||
2
|
||||
);
|
||||
assert_eq!(tree.insert(bounds(50.0, 50.0, 10.0, 10.0), quad, shadow), 2);
|
||||
assert_eq!(tree.insert(bounds(200.0, 200.0, 0.0, 0.0), quad, shadow), 1);
|
||||
assert_eq!(tree.insert(bounds(200.0, 200.0, 0.0, 0.0), quad, shadow), 1);
|
||||
assert_eq!(
|
||||
tree.insert(bounds(300.0, 300.0, -10.0, -10.0), quad, shadow),
|
||||
1
|
||||
);
|
||||
assert_eq!(
|
||||
tree.insert(bounds(300.0, 300.0, -10.0, -10.0), quad, shadow),
|
||||
1
|
||||
);
|
||||
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(12);
|
||||
for mode in KIND_MODES {
|
||||
let items: Vec<_> = (0..ITEM_COUNT)
|
||||
.map(|_| {
|
||||
let (kind, max_shared_kind) = mode.kinds(&mut rng);
|
||||
let item = bounds(
|
||||
rng.random_range(-300.0..300.0),
|
||||
rng.random_range(-300.0..300.0),
|
||||
rng.random_range(-80.0..80.0),
|
||||
rng.random_range(-80.0..80.0),
|
||||
);
|
||||
(item, kind, max_shared_kind)
|
||||
})
|
||||
.collect();
|
||||
assert_matches_oracle(
|
||||
&mut BoundsTree::default(),
|
||||
&items,
|
||||
&format!("negative sizes {mode:?}"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_negative_coordinates() {
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(13);
|
||||
let items: Vec<_> = (0..ITEM_COUNT)
|
||||
.map(|_| {
|
||||
let (kind, max_shared_kind) = KindMode::AllKinds.kinds(&mut rng);
|
||||
let item = bounds(
|
||||
rng.random_range(-5_000.0..-1_000.0),
|
||||
rng.random_range(-5_000.0..-1_000.0),
|
||||
log_uniform(&mut rng, 1.0, 1_500.0),
|
||||
log_uniform(&mut rng, 1.0, 1_500.0),
|
||||
);
|
||||
(item, kind, max_shared_kind)
|
||||
})
|
||||
.collect();
|
||||
assert_matches_oracle(&mut BoundsTree::default(), &items, "negative coordinates");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_i32() {
|
||||
for mode in KIND_MODES {
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(14);
|
||||
let items: Vec<_> = (0..ITEM_COUNT)
|
||||
.map(|_| {
|
||||
let (kind, max_shared_kind) = mode.kinds(&mut rng);
|
||||
let item = bounds(
|
||||
rng.random_range(-1_000..1_000),
|
||||
rng.random_range(-1_000..1_000),
|
||||
rng.random_range(-20..200),
|
||||
rng.random_range(-20..200),
|
||||
);
|
||||
(item, kind, max_shared_kind)
|
||||
})
|
||||
.collect();
|
||||
assert_matches_oracle(
|
||||
&mut BoundsTree::<i32>::default(),
|
||||
&items,
|
||||
&format!("i32 {mode:?}"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_clear_reuse() {
|
||||
let mut tree = BoundsTree::<f32>::default();
|
||||
for seed in 15..18 {
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(seed);
|
||||
let items = with_kinds(&random_scenario(&mut rng), KindMode::AllKinds, &mut rng);
|
||||
tree.clear();
|
||||
assert_invariants(&tree, 0, "cleared");
|
||||
assert_matches_oracle(&mut tree, &items, &format!("clear reuse seed {seed}"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_f32_special_values() {
|
||||
let special_values = [
|
||||
f32::NAN,
|
||||
-f32::NAN,
|
||||
f32::INFINITY,
|
||||
f32::NEG_INFINITY,
|
||||
f32::MAX,
|
||||
f32::MIN,
|
||||
0.0,
|
||||
-0.0,
|
||||
];
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(19);
|
||||
let value = |rng: &mut rand::rngs::StdRng, min: f32, max: f32| {
|
||||
if rng.random_ratio(1, 8) {
|
||||
special_values[rng.random_range(0..special_values.len())]
|
||||
} else {
|
||||
rng.random_range(min..max)
|
||||
}
|
||||
};
|
||||
for mode in KIND_MODES {
|
||||
let items: Vec<_> = (0..ITEM_COUNT)
|
||||
.map(|_| {
|
||||
let (kind, max_shared_kind) = mode.kinds(&mut rng);
|
||||
let item = bounds(
|
||||
value(&mut rng, -200.0, 200.0),
|
||||
value(&mut rng, -200.0, 200.0),
|
||||
value(&mut rng, -20.0, 100.0),
|
||||
value(&mut rng, -20.0, 100.0),
|
||||
);
|
||||
(item, kind, max_shared_kind)
|
||||
})
|
||||
.collect();
|
||||
assert_matches_oracle(
|
||||
&mut BoundsTree::<f32>::default(),
|
||||
&items,
|
||||
&format!("f32 special values {mode:?}"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_scaled_pixels_edge_values() {
|
||||
let edge_values = [
|
||||
-0.0,
|
||||
0.0,
|
||||
f32::INFINITY,
|
||||
f32::NEG_INFINITY,
|
||||
f32::MAX,
|
||||
f32::MIN,
|
||||
f32::from_bits(0x7fc0_0001),
|
||||
f32::from_bits(0xffc0_0001),
|
||||
];
|
||||
let mut rng = rand::rngs::StdRng::seed_from_u64(20);
|
||||
let value = |rng: &mut rand::rngs::StdRng, min: f32, max: f32| {
|
||||
ScaledPixels(if rng.random_ratio(1, 6) {
|
||||
edge_values[rng.random_range(0..edge_values.len())]
|
||||
} else {
|
||||
rng.random_range(min..max)
|
||||
})
|
||||
};
|
||||
for mode in KIND_MODES {
|
||||
let items: Vec<_> = (0..ITEM_COUNT)
|
||||
.map(|_| {
|
||||
let (kind, max_shared_kind) = mode.kinds(&mut rng);
|
||||
let item = bounds(
|
||||
value(&mut rng, -200.0, 200.0),
|
||||
value(&mut rng, -200.0, 200.0),
|
||||
value(&mut rng, -20.0, 100.0),
|
||||
value(&mut rng, -20.0, 100.0),
|
||||
);
|
||||
(item, kind, max_shared_kind)
|
||||
})
|
||||
.collect();
|
||||
assert_matches_oracle(
|
||||
&mut BoundsTree::<ScaledPixels>::default(),
|
||||
&items,
|
||||
&format!("scaled pixels edge values {mode:?}"),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user