Skip to content

Commit

Permalink
✅ Test points inside are within shape
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Apr 19, 2024
1 parent 18e8202 commit c86ba18
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ getset = "^0.1.2"
criterion = { version = "^0.5", features = ["html_reports"] }
nalgebra = { version = "^0.32.3", features = ["proptest"] }
proptest = "1"
proptest-derive = "^0.4.0"
test-case = "3"
test-strategy = "^0.3.1"

Expand Down
13 changes: 1 addition & 12 deletions src/shapes/ellipse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,4 @@ impl Ellipse<u8, u8> {
}

#[cfg(test)]
mod tests {
use super::*;

use crate::traits::*;

use test_strategy::proptest;

#[proptest]
fn ellipse_points_inside_u8_fuzz(ellipse: Ellipse<u8, u8>) {
let _out = ellipse.points_inside();
}
}
mod tests {}
23 changes: 23 additions & 0 deletions src/shapes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,26 @@ where
Self::Collection(collection)
}
}

#[cfg(test)]
mod tests {
use test_strategy::proptest;

use super::*;
use crate::testing::*;
use crate::traits::*;

#[proptest]
fn test_shape_u8_points_inside_are_within_fuzz(shape_view: ShapeView<u8, u8>) {
let shape = Shape::from(shape_view);
let points = shape.points_inside();
for point in points {
assert!(
shape.within(&point),
"point {:?} is not within shape {:?}",
point,
shape
);
}
}
}
73 changes: 56 additions & 17 deletions src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ macro_rules! assert_vec_eq {
}

type Mapped<I, O> = Map<StrategyFor<I>, fn(_: I) -> O>;
type FilterMapped<I, O> = FilterMap<StrategyFor<I>, fn(_: I) -> Option<O>>;

///////////////////////////////////////////////////////////////////////////////
// Shrinkable point
Expand Down Expand Up @@ -137,8 +138,8 @@ where

impl<T> Arbitrary for PointView<T, 2>
where
T::Strategy: Clone,
T::Parameters: Clone,
<T as Arbitrary>::Strategy: Clone,
<T as Arbitrary>::Parameters: Clone,
T: Arbitrary + Scalar + Clone,
{
type Parameters = T::Parameters;
Expand All @@ -153,11 +154,11 @@ where

impl<T, R> Arbitrary for Circle<T, R>
where
T::Strategy: Clone,
T::Parameters: Clone,
<T as Arbitrary>::Strategy: Clone,
<T as Arbitrary>::Parameters: Clone,
T: Arbitrary + Scalar + Clone,
R::Strategy: Clone,
R::Parameters: Clone,
<R as Arbitrary>::Strategy: Clone,
<R as Arbitrary>::Parameters: Clone,
R: Arbitrary + Scalar + Unsigned + Clone,
{
type Parameters = <(T, R) as Arbitrary>::Parameters;
Expand All @@ -177,11 +178,11 @@ where

impl<T, R> Arbitrary for Ellipse<T, R>
where
T::Strategy: Clone,
T::Parameters: Clone,
<T as Arbitrary>::Strategy: Clone,
<T as Arbitrary>::Parameters: Clone,
T: Arbitrary + Scalar + Clone,
R::Strategy: Clone,
R::Parameters: Clone,
<R as Arbitrary>::Strategy: Clone,
<R as Arbitrary>::Parameters: Clone,
R: Arbitrary + Scalar + Unsigned + Clone,
{
type Parameters = <(T, R) as Arbitrary>::Parameters;
Expand All @@ -200,8 +201,8 @@ where

impl<T> Arbitrary for Line<T>
where
T::Strategy: Clone,
T::Parameters: Clone,
<T as Arbitrary>::Strategy: Clone,
<T as Arbitrary>::Parameters: Clone,
T: Arbitrary + Scalar + Clone + PartialOrd,
{
type Parameters = <(PointView<T, 2>, PointView<T, 2>) as Arbitrary>::Parameters;
Expand All @@ -217,8 +218,8 @@ where

impl<T> Arbitrary for Rectangle<T>
where
T::Strategy: Clone,
T::Parameters: Clone,
<T as Arbitrary>::Strategy: Clone,
<T as Arbitrary>::Parameters: Clone,
T: Arbitrary + Scalar + Copy + Ord,
{
type Parameters = <(PointView<T, 2>, PointView<T, 2>) as Arbitrary>::Parameters;
Expand All @@ -230,12 +231,12 @@ where
}

///////////////////////////////////////////////////////////////////////////////
// Arbitrary Rectangle
// Arbitrary Triangle

impl<T> Arbitrary for Triangle<T>
where
T::Strategy: Clone,
T::Parameters: Clone,
<T as Arbitrary>::Strategy: Clone,
<T as Arbitrary>::Parameters: Clone,
T: Arbitrary + Scalar + Copy + Ord,
{
type Parameters = <(PointView<T, 2>, PointView<T, 2>, PointView<T, 2>) as Arbitrary>::Parameters;
Expand All @@ -245,3 +246,41 @@ where
}
type Strategy = Mapped<(PointView<T, 2>, PointView<T, 2>, PointView<T, 2>), Triangle<T>>;
}

///////////////////////////////////////////////////////////////////////////////
// Arbitrary Shape

#[derive(proptest_derive::Arbitrary, Debug)]
pub enum ShapeView<T, R>
where
<T as Arbitrary>::Strategy: Clone,
<T as Arbitrary>::Parameters: Clone,
T: Arbitrary + Scalar + Copy + Ord,
<R as Arbitrary>::Strategy: Clone,
<R as Arbitrary>::Parameters: Clone,
R: Arbitrary + Scalar + Unsigned + Copy + Ord,
{
Rectangle(Rectangle<T>),
Circle(Circle<T, R>),
Ellipse(Ellipse<T, R>),
Triangle(Triangle<T>),
}

impl<T, R> From<ShapeView<T, R>> for Shape<T, R>
where
<T as Arbitrary>::Strategy: Clone,
<T as Arbitrary>::Parameters: Clone,
T: Arbitrary + Scalar + Copy + Ord,
<R as Arbitrary>::Strategy: Clone,
<R as Arbitrary>::Parameters: Clone,
R: Arbitrary + Scalar + Unsigned + Copy + Ord,
{
fn from(val: ShapeView<T, R>) -> Self {
match val {
ShapeView::Rectangle(rectangle) => Shape::Rectangle(rectangle),
ShapeView::Circle(circle) => Shape::Circle(circle),
ShapeView::Ellipse(ellipse) => Shape::Ellipse(ellipse),
ShapeView::Triangle(triangle) => Shape::Triangle(triangle),
}
}
}

0 comments on commit c86ba18

Please sign in to comment.