From 44405e3e4eb8c619f014d45bd3872ccf9bf9a558 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 21:09:09 -0800 Subject: [PATCH] Tests: cover geom arc, straightness, quad length and anchor table checks Adds tests that fail when these checks break: - circular_arc returns None for a 60 degree cubic whose ends are at radii 100 and 105 from where their normals cross. - The near-straight limit in osculating grows with the segment's speed: a 300 pt segment that bends by 1e-9 pt is straight, and one that bends by 1e-3 pt is curved. - has_length is false for a quadratic whose three points coincide and true when its middle point lies off its ends. - Anchors::table_bytes counts both the chunk table and the running totals, for a short list and for a list split unevenly by an insert. Removes the unused From<&[Anchor]> impl for Anchors. Nothing in the workspace converts a slice to Anchors. --- crates/geom/src/anchors.rs | 25 +++++++++++++++++++------ crates/geom/src/bez.rs | 11 +++++++++++ crates/geom/src/curvature.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/crates/geom/src/anchors.rs b/crates/geom/src/anchors.rs index 3b9f4aa9bd..78cfe5509a 100644 --- a/crates/geom/src/anchors.rs +++ b/crates/geom/src/anchors.rs @@ -554,12 +554,6 @@ impl From> for Anchors { } } -impl From<&[Anchor]> for Anchors { - fn from(s: &[Anchor]) -> Self { - Self::from(s.to_vec()) - } -} - impl From<[Anchor; N]> for Anchors { fn from(s: [Anchor; N]) -> Self { Self::from(Vec::from(s)) @@ -892,4 +886,23 @@ mod tests { moved.remove(0); assert_eq!(before.pairs_not_shared(&moved).count(), 10_000); } + + /// The table bytes take in both tables: at least what the chunk table and the running totals + /// hold, and at most what they have reserved. A short list has no running totals; a list split + /// unevenly by an insert has both. + #[test] + fn table_bytes_count_the_chunk_table_and_the_running_totals() { + let (chunk, total) = (size_of::>>(), size_of::()); + let within = |a: &Anchors| { + let held = a.chunks.len() * chunk + a.ends.len() * total; + let reserved = a.chunks.capacity() * chunk + a.ends.capacity() * total; + (held..=reserved).contains(&a.table_bytes()) + }; + let short = Anchors::from(line(5)); + assert!(short.ends.is_empty() && within(&short), "{}", short.table_bytes()); + let mut uneven = Anchors::from(line(CHUNK)); + uneven.insert(10, Anchor::corner(Point::ZERO)); + assert!(uneven.chunks.len() == 2 && uneven.ends.len() == 2, "{:?}", uneven.ends); + assert!(within(&uneven), "{}", uneven.table_bytes()); + } } diff --git a/crates/geom/src/bez.rs b/crates/geom/src/bez.rs index 8f6b3c4a0a..6565c7d68e 100644 --- a/crates/geom/src/bez.rs +++ b/crates/geom/src/bez.rs @@ -106,4 +106,15 @@ mod tests { assert_eq!(tangent(&c[0], 1.0), Vec2::new(0.0, 1.0)); assert_eq!(unit(Vec2::ZERO), Vec2::new(1.0, 0.0)); } + + /// A quadratic whose three points coincide has no length; one whose middle point lies off its + /// coinciding ends has length. + #[test] + fn a_quad_has_length_unless_its_points_coincide() { + use kurbo::{Point, QuadBez}; + let (p, q) = (Point::new(3.0, 4.0), Point::new(9.0, -2.0)); + assert!(!has_length(&PathSeg::Quad(QuadBez::new(p, p, p)))); + assert!(has_length(&PathSeg::Quad(QuadBez::new(p, q, p)))); + assert!(has_length(&PathSeg::Quad(QuadBez::new(p, p, q)))); + } } diff --git a/crates/geom/src/curvature.rs b/crates/geom/src/curvature.rs index aea7afde1d..0a56d6101c 100644 --- a/crates/geom/src/curvature.rs +++ b/crates/geom/src/curvature.rs @@ -316,6 +316,33 @@ mod tests { assert!(curvature_circle(s, 0.25, false).is_ok()); } + /// A segment that bends by a billionth of a point over 300 points is straight: the limit grows + /// with the segment's speed, so a fast segment needs a larger bend to count as curved. A bend + /// a million times larger is curved. + #[test] + fn a_fast_segment_that_barely_bends_is_straight() { + let nearly = |dy: f64| CubicBez::new(Point::new(0.0, 0.0), Point::new(100.0, dy), Point::new(200.0, 0.0), Point::new(300.0, 0.0)); + assert_eq!(curvature_circle_with(nearly(1e-9), 0.5, false, false), Err(NoCircle::Straight)); + assert_eq!(osculating(nearly(1e-9), 0.5), Err(NoCircle::Straight)); + assert!(curvature_circle_with(nearly(1e-3), 0.5, false, false).is_ok()); + } + + /// The ends of a 60° cubic at radii 100 and 105 from where their normals cross are not the + /// cubic of a circular arc, even with handles of the right length for the mean radius; the + /// same cubic with both ends at radius 100 is one. + #[test] + fn ends_at_different_radii_are_not_a_circular_arc() { + let turn = 60f64.to_radians(); + let arc = |r3: f64, mean: f64| { + let handle = 4.0 / 3.0 * (turn / 4.0).tan() * mean; + let (p0, p3) = (Point::new(100.0, 0.0), Point::new(r3 * turn.cos(), r3 * turn.sin())); + CubicBez::new(p0, p0 + Vec2::new(0.0, handle), p3 - Vec2::new(-turn.sin(), turn.cos()) * handle, p3) + }; + assert_eq!(circular_arc(arc(105.0, 102.5)), None); + let (center, radius) = circular_arc(arc(100.0, 100.0)).unwrap(); + assert!(center.distance(Point::ORIGIN) < 1e-9 && (radius - 100.0).abs() < 1e-9, "{center:?} {radius}"); + } + /// A cusp, a handle drawn back onto its anchor at that end, a segment with no length, points /// that are not finite and a `t` outside 0 to 1 have no curvature circle. #[test]