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.
This commit is contained in:
dotCooCoo committed 2026-12-02 21:09:09 -08:00
1 parent 4002b2b66c
commit 44405e3e4e
3 files changed
+57 -6

No files matched your search

+19 -6
View File
@@ -554,12 +554,6 @@ impl From<Vec<Anchor>> for Anchors {
}
}
impl From<&[Anchor]> for Anchors {
fn from(s: &[Anchor]) -> Self {
Self::from(s.to_vec())
}
}
impl<const N: usize> 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::<Arc<Vec<Anchor>>>(), size_of::<usize>());
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());
}
}
+11
View File
@@ -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))));
}
}
+27
View File
@@ -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]