Put all #[test] in a mod tests (#2728)

workspace: put all `#[test]` in a `mod tests`
This commit is contained in:
Firestar99
2025-06-19 20:25:58 -07:00
committed by Keavon Chambers
parent 579bedd9ff
commit 006209c5d0
12 changed files with 739 additions and 687 deletions
@@ -91,18 +91,21 @@ pub fn get_current_normalized_pivot(inputs: &[NodeInput]) -> DVec2 {
if let Some(&TaggedValue::DVec2(pivot)) = inputs[5].as_value() { pivot } else { DVec2::splat(0.5) } if let Some(&TaggedValue::DVec2(pivot)) = inputs[5].as_value() { pivot } else { DVec2::splat(0.5) }
} }
/// ![](https://files.keavon.com/-/OptimisticSpotlessTinamou/capture.png) #[cfg(test)]
/// mod tests {
/// Source: use super::*;
/// ```tex /// ![](https://files.keavon.com/-/OptimisticSpotlessTinamou/capture.png)
/// \begin{bmatrix} ///
/// S_{x}\cos(\theta)-S_{y}\sin(\theta)H_{y} & S_{x}\cos(\theta)H_{x}-S_{y}\sin(\theta) & T_{x}\\ /// Source:
/// S_{x}\sin(\theta)+S_{y}\cos(\theta)H_{y} & S_{x}\sin(\theta)H_{x}+S_{y}\cos(\theta) & T_{y}\\ /// ```tex
/// 0 & 0 & 1 /// \begin{bmatrix}
/// \end{bmatrix} /// S_{x}\cos(\theta)-S_{y}\sin(\theta)H_{y} & S_{x}\cos(\theta)H_{x}-S_{y}\sin(\theta) & T_{x}\\
/// ``` /// S_{x}\sin(\theta)+S_{y}\cos(\theta)H_{y} & S_{x}\sin(\theta)H_{x}+S_{y}\cos(\theta) & T_{y}\\
#[test] /// 0 & 0 & 1
fn derive_transform() { /// \end{bmatrix}
/// ```
#[test]
fn derive_transform() {
for shear_x in -10..=10 { for shear_x in -10..=10 {
let shear_x = (shear_x as f64) / 2.; let shear_x = (shear_x as f64) / 2.;
for angle in (0..=360).step_by(15) { for angle in (0..=360).step_by(15) {
@@ -133,6 +136,7 @@ fn derive_transform() {
} }
} }
} }
}
} }
/// Expand a bounds to avoid div zero errors /// Expand a bounds to avoid div zero errors
@@ -522,8 +522,11 @@ pub struct NodeRelations {
// Helper functions // Helper functions
// ================ // ================
#[test] #[cfg(test)]
fn test_tree() { mod tests {
use super::*;
#[test]
fn test_tree() {
let mut metadata = DocumentMetadata::default(); let mut metadata = DocumentMetadata::default();
let root = LayerNodeIdentifier::ROOT_PARENT; let root = LayerNodeIdentifier::ROOT_PARENT;
let metadata = &mut metadata; let metadata = &mut metadata;
@@ -567,4 +570,5 @@ fn test_tree() {
root.descendants(metadata).map(LayerNodeIdentifier::to_node).rev().collect::<Vec<_>>(), root.descendants(metadata).map(LayerNodeIdentifier::to_node).rev().collect::<Vec<_>>(),
vec![NodeId(10), NodeId(9), NodeId(5), NodeId(4), NodeId(3), NodeId(2)] vec![NodeId(10), NodeId(9), NodeId(5), NodeId(4), NodeId(3), NodeId(2)]
); );
}
} }
@@ -382,8 +382,11 @@ impl DistributionSnapper {
} }
} }
#[test] #[cfg(test)]
fn merge_intersecting_test() { mod tests {
use super::*;
#[test]
fn merge_intersecting_test() {
let mut rectangles = vec![Rect::from_square(DVec2::ZERO, 2.), Rect::from_square(DVec2::new(10., 0.), 2.)]; let mut rectangles = vec![Rect::from_square(DVec2::ZERO, 2.), Rect::from_square(DVec2::new(10., 0.), 2.)];
DistributionSnapper::merge_intersecting(&mut rectangles); DistributionSnapper::merge_intersecting(&mut rectangles);
assert_eq!(rectangles.len(), 2); assert_eq!(rectangles.len(), 2);
@@ -398,53 +401,53 @@ fn merge_intersecting_test() {
assert_eq!(rectangles.len(), 6); assert_eq!(rectangles.len(), 6);
assert_eq!(rectangles[0], Rect::from_box([DVec2::new(-2., -2.), DVec2::new(3., 2.)])); assert_eq!(rectangles[0], Rect::from_box([DVec2::new(-2., -2.), DVec2::new(3., 2.)]));
assert_eq!(rectangles[3], Rect::from_box([DVec2::new(8., -2.), DVec2::new(13., 2.)])); assert_eq!(rectangles[3], Rect::from_box([DVec2::new(8., -2.), DVec2::new(13., 2.)]));
} }
#[test] #[test]
fn dist_simple_2() { fn dist_simple_2() {
let rectangles = [10., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)); let rectangles = [10., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.); let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right); let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right);
assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. })); assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. }));
assert_eq!(rectangles.len(), 2); assert_eq!(rectangles.len(), 2);
} }
#[test] #[test]
fn dist_simple_3() { fn dist_simple_3() {
let rectangles = [10., 20., 30.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)); let rectangles = [10., 20., 30.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.); let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right); let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right);
assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. })); assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. }));
assert_eq!(rectangles.len(), 3); assert_eq!(rectangles.len(), 3);
} }
#[test] #[test]
fn dist_out_of_tolerance() { fn dist_out_of_tolerance() {
let rectangles = [10., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)); let rectangles = [10., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.); let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 0.4, dist_right); let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 0.4, dist_right);
assert_eq!(offset, None); assert_eq!(offset, None);
assert_eq!(rectangles.len(), 1); assert_eq!(rectangles.len(), 1);
} }
#[test] #[test]
fn dist_with_nonsense() { fn dist_with_nonsense() {
let source = Rect::from_square(DVec2::new(0.5, 0.), 2.); let source = Rect::from_square(DVec2::new(0.5, 0.), 2.);
let rectangles = [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)); let rectangles = [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.));
let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right); let (offset, rectangles) = DistributionSnapper::top_level_matches(source, &rectangles, 1., dist_right);
assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. })); assert_eq!(offset, Some(DistributionMatch { first: 5.5, equal: 6. }));
assert_eq!(rectangles.len(), 2); assert_eq!(rectangles.len(), 2);
} }
#[cfg(test)] #[cfg(test)]
fn assert_boxes_in_order(rectangles: &VecDeque<Rect>, index: usize) { fn assert_boxes_in_order(rectangles: &VecDeque<Rect>, index: usize) {
for (&first, &second) in rectangles.iter().zip(rectangles.iter().skip(1)) { for (&first, &second) in rectangles.iter().zip(rectangles.iter().skip(1)) {
assert!(first.max()[index] < second.min()[index], "{first:?} {second:?} {index}") assert!(first.max()[index] < second.min()[index], "{first:?} {second:?} {index}")
} }
} }
#[test] #[test]
fn dist_snap_point_right() { fn dist_snap_point_right() {
let dist_snapper = DistributionSnapper { let dist_snapper = DistributionSnapper {
right: [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(), right: [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
left: [-2.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(), left: [-2.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
@@ -463,10 +466,10 @@ fn dist_snap_point_right() {
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3); assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3);
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[0], expected_box); assert_eq!(snap_results.points[0].distribution_boxes_horizontal[0], expected_box);
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
} }
#[test] #[test]
fn dist_snap_point_right_left() { fn dist_snap_point_right_left() {
let dist_snapper = DistributionSnapper { let dist_snapper = DistributionSnapper {
right: [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(), right: [2., 10., 15., 20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(), left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
@@ -494,10 +497,10 @@ fn dist_snap_point_right_left() {
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[1], expected_left1); assert_eq!(snap_results.points[0].distribution_boxes_horizontal[1], expected_left1);
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], expected_center); assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], expected_center);
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
} }
#[test] #[test]
fn dist_snap_point_left() { fn dist_snap_point_left() {
let dist_snapper = DistributionSnapper { let dist_snapper = DistributionSnapper {
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(), left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
..Default::default() ..Default::default()
@@ -511,10 +514,10 @@ fn dist_snap_point_left() {
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3); assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3);
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], Rect::from_square(DVec2::new(0., 0.), 2.)); assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], Rect::from_square(DVec2::new(0., 0.), 2.));
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
} }
#[test] #[test]
fn dist_snap_point_left_right() { fn dist_snap_point_left_right() {
let dist_snapper = DistributionSnapper { let dist_snapper = DistributionSnapper {
left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(), left: [-2., -10., -15., -20.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
right: [2., 10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(), right: [2., 10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
@@ -529,10 +532,10 @@ fn dist_snap_point_left_right() {
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 4); assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 4);
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], Rect::from_square(DVec2::new(0., 0.), 2.)); assert_eq!(snap_results.points[0].distribution_boxes_horizontal[2], Rect::from_square(DVec2::new(0., 0.), 2.));
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
} }
#[test] #[test]
fn dist_snap_point_center_x() { fn dist_snap_point_center_x() {
let dist_snapper = DistributionSnapper { let dist_snapper = DistributionSnapper {
left: [-10., -15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(), left: [-10., -15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
right: [10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(), right: [10., 15.].map(|x| Rect::from_square(DVec2::new(x, 0.), 2.)).to_vec(),
@@ -552,12 +555,12 @@ fn dist_snap_point_center_x() {
assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3); assert_eq!(snap_results.points[0].distribution_boxes_horizontal.len(), 3);
assert_eq!(snap_results.points[0].distribution_boxes_horizontal[1], expected_box); assert_eq!(snap_results.points[0].distribution_boxes_horizontal[1], expected_box);
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
} }
// ---------------------------------- // ----------------------------------
#[test] #[test]
fn dist_snap_point_down() { fn dist_snap_point_down() {
let dist_snapper = DistributionSnapper { let dist_snapper = DistributionSnapper {
down: [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(), down: [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
up: [-2.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(), up: [-2.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
@@ -577,10 +580,10 @@ fn dist_snap_point_down() {
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3); assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3);
assert_eq!(snap_results.points[0].distribution_boxes_vertical[0], expected_box); assert_eq!(snap_results.points[0].distribution_boxes_vertical[0], expected_box);
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
} }
#[test] #[test]
fn dist_snap_point_down_up() { fn dist_snap_point_down_up() {
let dist_snapper = DistributionSnapper { let dist_snapper = DistributionSnapper {
down: [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(), down: [2., 10., 15., 20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(), up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
@@ -606,10 +609,10 @@ fn dist_snap_point_down_up() {
assert_eq!(snap_results.points[0].distribution_boxes_vertical[1], expected_up); assert_eq!(snap_results.points[0].distribution_boxes_vertical[1], expected_up);
assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], expected_center); assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], expected_center);
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
} }
#[test] #[test]
fn dist_snap_point_up() { fn dist_snap_point_up() {
let dist_snapper = DistributionSnapper { let dist_snapper = DistributionSnapper {
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(), up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
..Default::default() ..Default::default()
@@ -623,10 +626,10 @@ fn dist_snap_point_up() {
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3); assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 3);
assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], Rect::from_square(DVec2::new(0., 0.), 2.)); assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], Rect::from_square(DVec2::new(0., 0.), 2.));
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
} }
#[test] #[test]
fn dist_snap_point_up_down() { fn dist_snap_point_up_down() {
let dist_snapper = DistributionSnapper { let dist_snapper = DistributionSnapper {
up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(), up: [-2., -10., -15., -20.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
down: [2., 10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(), down: [2., 10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
@@ -641,10 +644,10 @@ fn dist_snap_point_up_down() {
assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 4); assert_eq!(snap_results.points[0].distribution_boxes_vertical.len(), 4);
assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], Rect::from_square(DVec2::new(0., 0.), 2.)); assert_eq!(snap_results.points[0].distribution_boxes_vertical[2], Rect::from_square(DVec2::new(0., 0.), 2.));
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
} }
#[test] #[test]
fn dist_snap_point_center_y() { fn dist_snap_point_center_y() {
let dist_snapper = DistributionSnapper { let dist_snapper = DistributionSnapper {
up: [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(), up: [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
down: [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(), down: [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
@@ -665,10 +668,10 @@ fn dist_snap_point_center_y() {
assert_eq!(snap_results.points[0].distribution_boxes_vertical[1], expected_box); assert_eq!(snap_results.points[0].distribution_boxes_vertical[1], expected_box);
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
} }
#[test] #[test]
fn dist_snap_point_center_xy() { fn dist_snap_point_center_xy() {
let dist_snapper = DistributionSnapper { let dist_snapper = DistributionSnapper {
up: [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(), up: [-10., -15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
down: [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(), down: [10., 15.].map(|y| Rect::from_square(DVec2::new(0., y), 2.)).to_vec(),
@@ -695,4 +698,5 @@ fn dist_snap_point_center_xy() {
assert_eq!(Rect::from_box(snap_results.points[0].source_bounds.unwrap().bounding_box()), Rect::from_square(DVec2::new(0., 0.), 2.)); assert_eq!(Rect::from_box(snap_results.points[0].source_bounds.unwrap().bounding_box()), Rect::from_square(DVec2::new(0., 0.), 2.));
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_horizontal, 0);
assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1); assert_boxes_in_order(&snap_results.points[0].distribution_boxes_vertical, 1);
}
} }
@@ -800,8 +800,12 @@ impl BoundingBoxManager {
} }
} }
#[test] #[cfg(test)]
fn skew_transform_singular() { mod tests {
use super::*;
#[test]
fn skew_transform_singular() {
for edge in [ for edge in [
SelectedEdges::new(true, false, false, false, [DVec2::NEG_ONE, DVec2::ONE]), SelectedEdges::new(true, false, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
SelectedEdges::new(false, true, false, false, [DVec2::NEG_ONE, DVec2::ONE]), SelectedEdges::new(false, true, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
@@ -813,10 +817,10 @@ fn skew_transform_singular() {
// This shouldn't panic. We don't really care about the behavior in this test. // This shouldn't panic. We don't really care about the behavior in this test.
let _ = edge.skew_transform(DVec2::new(1.5, 1.5), transform, false); let _ = edge.skew_transform(DVec2::new(1.5, 1.5), transform, false);
} }
} }
#[test] #[test]
fn skew_transform_correct() { fn skew_transform_correct() {
for edge in [ for edge in [
SelectedEdges::new(true, false, false, false, [DVec2::NEG_ONE, DVec2::ONE]), SelectedEdges::new(true, false, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
SelectedEdges::new(false, true, false, false, [DVec2::NEG_ONE, DVec2::ONE]), SelectedEdges::new(false, true, false, false, [DVec2::NEG_ONE, DVec2::ONE]),
@@ -855,4 +859,5 @@ fn skew_transform_correct() {
assert_eq!(constructed_transform, final_transform); assert_eq!(constructed_transform, final_transform);
} }
}
} }
+7 -2
View File
@@ -570,8 +570,12 @@ pub fn solve_spline_first_handle_closed(points: &[DVec2]) -> Vec<DVec2> {
x x
} }
#[test] #[cfg(test)]
fn closed_spline() { mod tests {
use super::*;
#[test]
fn closed_spline() {
// These points are just chosen arbitrary // These points are just chosen arbitrary
let points = [DVec2::new(0., 0.), DVec2::new(0., 0.), DVec2::new(6., 5.), DVec2::new(7., 9.), DVec2::new(2., 3.)]; let points = [DVec2::new(0., 0.), DVec2::new(0., 0.), DVec2::new(6., 5.), DVec2::new(7., 9.), DVec2::new(2., 3.)];
@@ -594,4 +598,5 @@ fn closed_spline() {
"second derivative at the end of a {derivative2_end_a} is equal to the second derivative at the start of b {derivative2_start_b}" "second derivative at the end of a {derivative2_end_a} is equal to the second derivative at the start of b {derivative2_start_b}"
); );
} }
}
} }
@@ -150,8 +150,12 @@ impl core::ops::Mul<Quad> for DAffine2 {
Quad(rhs.0.map(|point| self.transform_point2(point))) Quad(rhs.0.map(|point| self.transform_point2(point)))
} }
} }
#[test]
fn offset_quad() { #[cfg(test)]
mod tests {
use super::*;
#[test]
fn offset_quad() {
fn eq(a: Quad, b: Quad) -> bool { fn eq(a: Quad, b: Quad) -> bool {
a.0.iter().zip(b.0).all(|(a, b)| a.abs_diff_eq(b, 0.0001)) a.0.iter().zip(b.0).all(|(a, b)| a.abs_diff_eq(b, 0.0001))
} }
@@ -162,9 +166,9 @@ fn offset_quad() {
(DAffine2::from_scale(DVec2::new(-1., 1.)) * Quad::from_box([DVec2::ZERO, DVec2::ONE])).inflate(0.5), (DAffine2::from_scale(DVec2::new(-1., 1.)) * Quad::from_box([DVec2::ZERO, DVec2::ONE])).inflate(0.5),
DAffine2::from_scale(DVec2::new(-1., 1.)) * Quad::from_box([DVec2::splat(-0.5), DVec2::splat(1.5)]) DAffine2::from_scale(DVec2::new(-1., 1.)) * Quad::from_box([DVec2::splat(-0.5), DVec2::splat(1.5)])
)); ));
} }
#[test] #[test]
fn quad_contains() { fn quad_contains() {
assert!(Quad::from_box([DVec2::ZERO, DVec2::ONE]).contains(DVec2::splat(0.5))); assert!(Quad::from_box([DVec2::ZERO, DVec2::ONE]).contains(DVec2::splat(0.5)));
assert!(Quad::from_box([DVec2::ONE, DVec2::ZERO]).contains(DVec2::splat(0.5))); assert!(Quad::from_box([DVec2::ONE, DVec2::ZERO]).contains(DVec2::splat(0.5)));
assert!(Quad::from_box([DVec2::splat(300.), DVec2::splat(500.)]).contains(DVec2::splat(350.))); assert!(Quad::from_box([DVec2::splat(300.), DVec2::splat(500.)]).contains(DVec2::splat(350.)));
@@ -173,20 +177,21 @@ fn quad_contains() {
assert!(!Quad::from_box([DVec2::ZERO, DVec2::ONE]).contains(DVec2::new(1., 1.1))); assert!(!Quad::from_box([DVec2::ZERO, DVec2::ONE]).contains(DVec2::new(1., 1.1)));
assert!(!Quad::from_box([DVec2::ONE, DVec2::ZERO]).contains(DVec2::new(0.5, -0.01))); assert!(!Quad::from_box([DVec2::ONE, DVec2::ZERO]).contains(DVec2::new(0.5, -0.01)));
assert!(!(DAffine2::from_scale(DVec2::new(-1., 1.)) * Quad::from_box([DVec2::ZERO, DVec2::ONE])).contains(DVec2::splat(0.5))); assert!(!(DAffine2::from_scale(DVec2::new(-1., 1.)) * Quad::from_box([DVec2::ZERO, DVec2::ONE])).contains(DVec2::splat(0.5)));
} }
#[test] #[test]
fn intersect_lines() { fn intersect_lines() {
assert_eq!( assert_eq!(
Quad::intersect_lines(DVec2::new(-5., 5.), DVec2::new(5., 5.), DVec2::new(2., 7.), DVec2::new(2., 3.)), Quad::intersect_lines(DVec2::new(-5., 5.), DVec2::new(5., 5.), DVec2::new(2., 7.), DVec2::new(2., 3.)),
Some(DVec2::new(2., 5.)) Some(DVec2::new(2., 5.))
); );
assert_eq!(Quad::intersect_lines(DVec2::new(4., 6.), DVec2::new(4., 5.), DVec2::new(2., 7.), DVec2::new(2., 3.)), None); assert_eq!(Quad::intersect_lines(DVec2::new(4., 6.), DVec2::new(4., 5.), DVec2::new(2., 7.), DVec2::new(2., 3.)), None);
assert_eq!(Quad::intersect_lines(DVec2::new(-5., 5.), DVec2::new(5., 5.), DVec2::new(2., 7.), DVec2::new(2., 9.)), None); assert_eq!(Quad::intersect_lines(DVec2::new(-5., 5.), DVec2::new(5., 5.), DVec2::new(2., 7.), DVec2::new(2., 9.)), None);
} }
#[test] #[test]
fn intersect_quad() { fn intersect_quad() {
assert!(Quad::from_box([DVec2::ZERO, DVec2::splat(5.)]).intersects(Quad::from_box([DVec2::splat(4.), DVec2::splat(7.)]))); assert!(Quad::from_box([DVec2::ZERO, DVec2::splat(5.)]).intersects(Quad::from_box([DVec2::splat(4.), DVec2::splat(7.)])));
assert!(Quad::from_box([DVec2::ZERO, DVec2::splat(5.)]).intersects(Quad::from_box([DVec2::splat(4.), DVec2::splat(4.2)]))); assert!(Quad::from_box([DVec2::ZERO, DVec2::splat(5.)]).intersects(Quad::from_box([DVec2::splat(4.), DVec2::splat(4.2)])));
assert!(!Quad::from_box([DVec2::ZERO, DVec2::splat(3.)]).intersects(Quad::from_box([DVec2::splat(4.), DVec2::splat(4.2)]))); assert!(!Quad::from_box([DVec2::ZERO, DVec2::splat(3.)]).intersects(Quad::from_box([DVec2::splat(4.), DVec2::splat(4.2)])));
}
} }
+6 -2
View File
@@ -1058,8 +1058,11 @@ impl Color {
} }
} }
#[test] #[cfg(test)]
fn hsl_roundtrip() { mod tests {
use super::*;
#[test]
fn hsl_roundtrip() {
for (red, green, blue) in [ for (red, green, blue) in [
(24, 98, 118), (24, 98, 118),
(69, 11, 89), (69, 11, 89),
@@ -1091,4 +1094,5 @@ fn hsl_roundtrip() {
assert!((col.b() - result.b()) < f32::EPSILON * 100.); assert!((col.b() - result.b()) < f32::EPSILON * 100.);
assert!((col.a() - result.a()) < f32::EPSILON * 100.); assert!((col.a() - result.a()) < f32::EPSILON * 100.);
} }
}
} }
+6 -2
View File
@@ -239,11 +239,15 @@ impl<'a> Iterator for SplitWordsIncludingSpaces<'a> {
} }
} }
#[test] #[cfg(test)]
fn split_words_including_spaces() { mod tests {
use super::*;
#[test]
fn split_words_including_spaces() {
let mut split_words = SplitWordsIncludingSpaces::new("hello world ."); let mut split_words = SplitWordsIncludingSpaces::new("hello world .");
assert_eq!(split_words.next(), Some("hello ")); assert_eq!(split_words.next(), Some("hello "));
assert_eq!(split_words.next(), Some("world ")); assert_eq!(split_words.next(), Some("world "));
assert_eq!(split_words.next(), Some(".")); assert_eq!(split_words.next(), Some("."));
assert_eq!(split_words.next(), None); assert_eq!(split_words.next(), None);
}
} }
@@ -136,8 +136,11 @@ pub fn solve_spline_first_handle_closed(points: &[DVec2]) -> Vec<DVec2> {
x x
} }
#[test] #[cfg(test)]
fn closed_spline() { mod tests {
use super::*;
#[test]
fn closed_spline() {
use crate::vector::misc::{dvec2_to_point, point_to_dvec2}; use crate::vector::misc::{dvec2_to_point, point_to_dvec2};
use kurbo::{BezPath, ParamCurve, ParamCurveDeriv}; use kurbo::{BezPath, ParamCurve, ParamCurveDeriv};
@@ -175,4 +178,5 @@ fn closed_spline() {
"second derivative at the end of a {derivative2_end_a} is equal to the second derivative at the start of b {derivative2_start_b}" "second derivative at the end of a {derivative2_end_a} is equal to the second derivative at the start of b {derivative2_start_b}"
); );
} }
}
} }
@@ -245,8 +245,11 @@ fn grid<T: GridSpacing>(
VectorDataTable::new(vector_data) VectorDataTable::new(vector_data)
} }
#[test] #[cfg(test)]
fn isometric_grid_test() { mod tests {
use super::*;
#[test]
fn isometric_grid_test() {
// Doesn't crash with weird angles // Doesn't crash with weird angles
grid((), (), GridType::Isometric, 0., (0., 0.).into(), 5, 5); grid((), (), GridType::Isometric, 0., (0., 0.).into(), 5, 5);
grid((), (), GridType::Isometric, 90., (90., 90.).into(), 5, 5); grid((), (), GridType::Isometric, 90., (90., 90.).into(), 5, 5);
@@ -263,10 +266,10 @@ fn isometric_grid_test() {
(bezier.start - bezier.end).length() (bezier.start - bezier.end).length()
); );
} }
} }
#[test] #[test]
fn skew_isometric_grid_test() { fn skew_isometric_grid_test() {
let grid = grid((), (), GridType::Isometric, 10., (40., 30.).into(), 5, 5); let grid = grid((), (), GridType::Isometric, 10., (40., 30.).into(), 5, 5);
assert_eq!(grid.instance_ref_iter().next().unwrap().instance.point_domain.ids().len(), 5 * 5); assert_eq!(grid.instance_ref_iter().next().unwrap().instance.point_domain.ids().len(), 5 * 5);
assert_eq!(grid.instance_ref_iter().next().unwrap().instance.segment_bezier_iter().count(), 4 * 5 + 4 * 9); assert_eq!(grid.instance_ref_iter().next().unwrap().instance.segment_bezier_iter().count(), 4 * 5 + 4 * 9);
@@ -276,4 +279,5 @@ fn skew_isometric_grid_test() {
let angle = (vector.angle_to(DVec2::X).to_degrees() + 180.) % 180.; let angle = (vector.angle_to(DVec2::X).to_degrees() + 180.) % 180.;
assert!([90., 150., 40.].into_iter().any(|target| (target - angle).abs() < 1e-10), "unexpected angle of {}", angle) assert!([90., 150., 40.].into_iter().any(|target| (target - angle).abs() < 1e-10), "unexpected angle of {}", angle)
} }
}
} }
+12 -8
View File
@@ -676,8 +676,11 @@ fn assert_subpath_eq(generated: &[bezier_rs::Subpath<PointId>], expected: &[bezi
} }
} }
#[test] #[cfg(test)]
fn construct_closed_subpath() { mod tests {
use super::*;
#[test]
fn construct_closed_subpath() {
let circle = bezier_rs::Subpath::new_ellipse(DVec2::NEG_ONE, DVec2::ONE); let circle = bezier_rs::Subpath::new_ellipse(DVec2::NEG_ONE, DVec2::ONE);
let vector_data = VectorData::from_subpath(&circle); let vector_data = VectorData::from_subpath(&circle);
assert_eq!(vector_data.point_domain.ids().len(), 4); assert_eq!(vector_data.point_domain.ids().len(), 4);
@@ -687,10 +690,10 @@ fn construct_closed_subpath() {
let generated = vector_data.stroke_bezier_paths().collect::<Vec<_>>(); let generated = vector_data.stroke_bezier_paths().collect::<Vec<_>>();
assert_subpath_eq(&generated, &[circle]); assert_subpath_eq(&generated, &[circle]);
} }
#[test] #[test]
fn construct_open_subpath() { fn construct_open_subpath() {
let bezier = bezier_rs::Bezier::from_cubic_dvec2(DVec2::ZERO, DVec2::NEG_ONE, DVec2::ONE, DVec2::X); let bezier = bezier_rs::Bezier::from_cubic_dvec2(DVec2::ZERO, DVec2::NEG_ONE, DVec2::ONE, DVec2::X);
let subpath = bezier_rs::Subpath::from_bezier(&bezier); let subpath = bezier_rs::Subpath::from_bezier(&bezier);
let vector_data = VectorData::from_subpath(&subpath); let vector_data = VectorData::from_subpath(&subpath);
@@ -700,10 +703,10 @@ fn construct_open_subpath() {
let generated = vector_data.stroke_bezier_paths().collect::<Vec<_>>(); let generated = vector_data.stroke_bezier_paths().collect::<Vec<_>>();
assert_subpath_eq(&generated, &[subpath]); assert_subpath_eq(&generated, &[subpath]);
} }
#[test] #[test]
fn construct_many_subpath() { fn construct_many_subpath() {
let curve = bezier_rs::Bezier::from_cubic_dvec2(DVec2::ZERO, DVec2::NEG_ONE, DVec2::ONE, DVec2::X); let curve = bezier_rs::Bezier::from_cubic_dvec2(DVec2::ZERO, DVec2::NEG_ONE, DVec2::ONE, DVec2::X);
let curve = bezier_rs::Subpath::from_bezier(&curve); let curve = bezier_rs::Subpath::from_bezier(&curve);
let circle = bezier_rs::Subpath::new_ellipse(DVec2::NEG_ONE, DVec2::ONE); let circle = bezier_rs::Subpath::new_ellipse(DVec2::NEG_ONE, DVec2::ONE);
@@ -717,4 +720,5 @@ fn construct_many_subpath() {
let generated = vector_data.stroke_bezier_paths().collect::<Vec<_>>(); let generated = vector_data.stroke_bezier_paths().collect::<Vec<_>>();
assert_subpath_eq(&generated, &[curve, circle]); assert_subpath_eq(&generated, &[curve, circle]);
}
} }
@@ -436,64 +436,6 @@ async fn path_modify(_ctx: impl Ctx, mut vector_data: VectorDataTable, modificat
vector_data vector_data
} }
#[test]
fn modify_new() {
let vector_data = VectorData::from_subpaths(
[bezier_rs::Subpath::new_ellipse(DVec2::ZERO, DVec2::ONE), bezier_rs::Subpath::new_rect(DVec2::NEG_ONE, DVec2::ZERO)],
false,
);
let modify = VectorModification::create_from_vector(&vector_data);
let mut new = VectorData::default();
modify.apply(&mut new);
assert_eq!(vector_data, new);
}
#[test]
fn modify_existing() {
use bezier_rs::{Bezier, Subpath};
let subpaths = [
Subpath::new_ellipse(DVec2::ZERO, DVec2::ONE),
Subpath::new_rect(DVec2::NEG_ONE, DVec2::ZERO),
Subpath::from_beziers(
&[
Bezier::from_quadratic_dvec2(DVec2::new(0., 0.), DVec2::new(5., 10.), DVec2::new(10., 0.)),
Bezier::from_quadratic_dvec2(DVec2::new(10., 0.), DVec2::new(15., 10.), DVec2::new(20., 0.)),
],
false,
),
];
let mut vector_data = VectorData::from_subpaths(subpaths, false);
let mut modify_new = VectorModification::create_from_vector(&vector_data);
let mut modify_original = VectorModification::default();
for modification in [&mut modify_new, &mut modify_original] {
let point = vector_data.point_domain.ids()[0];
modification.modify(&VectorModificationType::ApplyPointDelta { point, delta: DVec2::X * 0.5 });
let point = vector_data.point_domain.ids()[9];
modification.modify(&VectorModificationType::ApplyPointDelta { point, delta: DVec2::X });
}
let mut new = VectorData::default();
modify_new.apply(&mut new);
modify_original.apply(&mut vector_data);
assert_eq!(vector_data, new);
assert_eq!(vector_data.point_domain.positions()[0], DVec2::X);
assert_eq!(vector_data.point_domain.positions()[9], DVec2::new(11., 0.));
assert_eq!(
vector_data.segment_bezier_iter().nth(8).unwrap().1,
Bezier::from_quadratic_dvec2(DVec2::new(0., 0.), DVec2::new(5., 10.), DVec2::new(11., 0.))
);
assert_eq!(
vector_data.segment_bezier_iter().nth(9).unwrap().1,
Bezier::from_quadratic_dvec2(DVec2::new(11., 0.), DVec2::new(16., 10.), DVec2::new(20., 0.))
);
}
// Do we want to enforce that all serialized/deserialized hashmaps are a vec of tuples? // Do we want to enforce that all serialized/deserialized hashmaps are a vec of tuples?
// TODO: Eventually remove this document upgrade code // TODO: Eventually remove this document upgrade code
use serde::de::{SeqAccess, Visitor}; use serde::de::{SeqAccess, Visitor};
@@ -686,3 +628,66 @@ impl<'a> AppendBezpath<'a> {
} }
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn modify_new() {
let vector_data = VectorData::from_subpaths(
[bezier_rs::Subpath::new_ellipse(DVec2::ZERO, DVec2::ONE), bezier_rs::Subpath::new_rect(DVec2::NEG_ONE, DVec2::ZERO)],
false,
);
let modify = VectorModification::create_from_vector(&vector_data);
let mut new = VectorData::default();
modify.apply(&mut new);
assert_eq!(vector_data, new);
}
#[test]
fn modify_existing() {
use bezier_rs::{Bezier, Subpath};
let subpaths = [
Subpath::new_ellipse(DVec2::ZERO, DVec2::ONE),
Subpath::new_rect(DVec2::NEG_ONE, DVec2::ZERO),
Subpath::from_beziers(
&[
Bezier::from_quadratic_dvec2(DVec2::new(0., 0.), DVec2::new(5., 10.), DVec2::new(10., 0.)),
Bezier::from_quadratic_dvec2(DVec2::new(10., 0.), DVec2::new(15., 10.), DVec2::new(20., 0.)),
],
false,
),
];
let mut vector_data = VectorData::from_subpaths(subpaths, false);
let mut modify_new = VectorModification::create_from_vector(&vector_data);
let mut modify_original = VectorModification::default();
for modification in [&mut modify_new, &mut modify_original] {
let point = vector_data.point_domain.ids()[0];
modification.modify(&VectorModificationType::ApplyPointDelta { point, delta: DVec2::X * 0.5 });
let point = vector_data.point_domain.ids()[9];
modification.modify(&VectorModificationType::ApplyPointDelta { point, delta: DVec2::X });
}
let mut new = VectorData::default();
modify_new.apply(&mut new);
modify_original.apply(&mut vector_data);
assert_eq!(vector_data, new);
assert_eq!(vector_data.point_domain.positions()[0], DVec2::X);
assert_eq!(vector_data.point_domain.positions()[9], DVec2::new(11., 0.));
assert_eq!(
vector_data.segment_bezier_iter().nth(8).unwrap().1,
Bezier::from_quadratic_dvec2(DVec2::new(0., 0.), DVec2::new(5., 10.), DVec2::new(11., 0.))
);
assert_eq!(
vector_data.segment_bezier_iter().nth(9).unwrap().1,
Bezier::from_quadratic_dvec2(DVec2::new(11., 0.), DVec2::new(16., 10.), DVec2::new(20., 0.))
);
}
}