Skip to content

Commit

Permalink
A couple examples using the wkt macro in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkirk committed Sep 20, 2023
1 parent cc63329 commit b5a0780
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 107 deletions.
30 changes: 8 additions & 22 deletions geo-types/src/geometry/multi_line_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,46 +197,32 @@ where
#[cfg(test)]
mod test {
use super::*;
use crate::line_string;
use crate::{line_string, wkt};

#[test]
fn test_iter() {
let multi: Vec<LineString<i32>> = vec![
line_string![(x: 0, y: 0), (x: 2, y: 0), (x: 1, y: 2), (x:0, y:0)],
line_string![(x: 10, y: 10), (x: 12, y: 10), (x: 11, y: 12), (x:10, y:10)],
];
let multi: MultiLineString<i32> = MultiLineString::new(multi);
let multi: MultiLineString<i32> = wkt! {
MULTILINESTRING((0 0,2 0,1 2,0 0), (10 10,12 10,11 12,10 10))
};

let mut first = true;
for p in &multi {
if first {
assert_eq!(
p,
&line_string![(x: 0, y: 0), (x: 2, y: 0), (x: 1, y: 2), (x:0, y:0)]
);
assert_eq!(p, &wkt! { LINESTRING(0 0,2 0,1 2,0 0) });
first = false;
} else {
assert_eq!(
p,
&line_string![(x: 10, y: 10), (x: 12, y: 10), (x: 11, y: 12), (x:10, y:10)]
);
assert_eq!(p, &wkt! { LINESTRING(10 10,12 10,11 12,10 10) });
}
}

// Do it again to prove that `multi` wasn't `moved`.
first = true;
for p in &multi {
if first {
assert_eq!(
p,
&line_string![(x: 0, y: 0), (x: 2, y: 0), (x: 1, y: 2), (x:0, y:0)]
);
assert_eq!(p, &wkt! { LINESTRING(0 0,2 0,1 2,0 0) });
first = false;
} else {
assert_eq!(
p,
&line_string![(x: 10, y: 10), (x: 12, y: 10), (x: 11, y: 12), (x:10, y:10)]
);
assert_eq!(p, &wkt! { LINESTRING(10 10,12 10,11 12,10 10) });
}
}
}
Expand Down
39 changes: 18 additions & 21 deletions geo-types/src/geometry/multi_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ where
#[cfg(test)]
mod test {
use super::*;
use crate::point;
use crate::{point, wkt};

#[test]
fn test_iter() {
let multi = MultiPoint::new(vec![point![x: 0, y: 0], point![x: 10, y: 10]]);
let multi = wkt! { MULTIPOINT(0 0,10 10) };

let mut first = true;
for p in &multi {
Expand All @@ -208,7 +208,7 @@ mod test {

#[test]
fn test_iter_mut() {
let mut multi = MultiPoint::new(vec![point![x: 0, y: 0], point![x: 10, y: 10]]);
let mut multi = wkt! { MULTIPOINT(0 0,10 10) };

for point in &mut multi {
point.0.x += 1;
Expand All @@ -235,53 +235,50 @@ mod test {
fn test_relative_eq() {
let delta = 1e-6;

let multi = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);
let multi = wkt! { MULTIPOINT(0. 0.,10. 10.) };

let mut multi_x = multi.clone();
*multi_x.0[0].x_mut() += delta;

let multi_x = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10.+delta, y: 10.]]);
assert!(multi.relative_eq(&multi_x, 1e-2, 1e-2));
assert!(multi.relative_ne(&multi_x, 1e-12, 1e-12));

let multi_y = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.+delta]]);
let mut multi_y = multi.clone();
*multi_y.0[0].y_mut() += delta;
assert!(multi.relative_eq(&multi_y, 1e-2, 1e-2));
assert!(multi.relative_ne(&multi_y, 1e-12, 1e-12));

// Under-sized but otherwise equal.
let multi_undersized = MultiPoint::new(vec![point![x: 0., y: 0.]]);
let multi_undersized = wkt! { MULTIPOINT(0. 0.) };
assert!(multi.relative_ne(&multi_undersized, 1., 1.));

// Over-sized but otherwise equal.
let multi_oversized = MultiPoint::new(vec![
point![x: 0., y: 0.],
point![x: 10., y: 10.],
point![x: 10., y:100.],
]);
let multi_oversized = wkt! { MULTIPOINT(0. 0.,10. 10.,10. 100.) };
assert!(multi.relative_ne(&multi_oversized, 1., 1.));
}

#[test]
fn test_abs_diff_eq() {
let delta = 1e-6;

let multi = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.]]);
let multi = wkt! { MULTIPOINT(0. 0.,10. 10.) };

let multi_x = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10.+delta, y: 10.]]);
let mut multi_x = multi.clone();
*multi_x.0[0].x_mut() += delta;
assert!(multi.abs_diff_eq(&multi_x, 1e-2));
assert!(multi.abs_diff_ne(&multi_x, 1e-12));

let multi_y = MultiPoint::new(vec![point![x: 0., y: 0.], point![x: 10., y: 10.+delta]]);
let mut multi_y = multi.clone();
*multi_y.0[0].y_mut() += delta;
assert!(multi.abs_diff_eq(&multi_y, 1e-2));
assert!(multi.abs_diff_ne(&multi_y, 1e-12));

// Under-sized but otherwise equal.
let multi_undersized = MultiPoint::new(vec![point![x: 0., y: 0.]]);
let multi_undersized = wkt! { MULTIPOINT(0. 0.) };
assert!(multi.abs_diff_ne(&multi_undersized, 1.));

// Over-sized but otherwise equal.
let multi_oversized = MultiPoint::new(vec![
point![x: 0., y: 0.],
point![x: 10., y: 10.],
point![x: 10., y:100.],
]);
let multi_oversized = wkt! { MULTIPOINT(0. 0.,10. 10.,10. 100.) };
assert!(multi.abs_diff_ne(&multi_oversized, 1.));
}
}
6 changes: 3 additions & 3 deletions geo/src/algorithm/affine_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ impl<U: CoordFloat> AffineTransform<U> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{polygon, Point};
use crate::{wkt, Point};

// given a matrix with the shape
// [[a, b, xoff],
Expand Down Expand Up @@ -430,10 +430,10 @@ mod tests {
#[test]
fn affine_transformed() {
let transform = AffineTransform::translate(1.0, 1.0).scaled(2.0, 2.0, (0.0, 0.0));
let mut poly = polygon![(x: 0.0, y: 0.0), (x: 0.0, y: 2.0), (x: 1.0, y: 2.0)];
let mut poly = wkt! { POLYGON((0.0 0.0,0.0 2.0,1.0 2.0)) };
poly.affine_transform_mut(&transform);

let expected = polygon![(x: 1.0, y: 1.0), (x: 1.0, y: 5.0), (x: 3.0, y: 5.0)];
let expected = wkt! { POLYGON((1.0 1.0,1.0 5.0,3.0 5.0)) };
assert_eq!(expected, poly);
}
}
12 changes: 3 additions & 9 deletions geo/src/algorithm/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ where
#[cfg(test)]
mod test {
use crate::Area;
use crate::{coord, polygon, Line, MultiPolygon, Polygon, Rect, Triangle};
use crate::{coord, polygon, wkt, Line, MultiPolygon, Polygon, Rect, Triangle};

// Area of the polygon
#[test]
Expand All @@ -273,18 +273,12 @@ mod test {

#[test]
fn area_one_point_polygon_test() {
let poly = polygon![(x: 1., y: 0.)];
let poly = wkt! { POLYGON((1. 0.)) };
assert_relative_eq!(poly.signed_area(), 0.);
}
#[test]
fn area_polygon_test() {
let polygon = polygon![
(x: 0., y: 0.),
(x: 5., y: 0.),
(x: 5., y: 6.),
(x: 0., y: 6.),
(x: 0., y: 0.)
];
let polygon = wkt! { POLYGON((0. 0.,5. 0.,5. 6.,0. 6.,0. 0.)) };
assert_relative_eq!(polygon.signed_area(), 30.);
}
#[test]
Expand Down
79 changes: 27 additions & 52 deletions geo/src/algorithm/centroid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ impl<T: GeoFloat> WeightedCentroid<T> {
#[cfg(test)]
mod test {
use super::*;
use crate::{coord, line_string, point, polygon};
use crate::{coord, line_string, point, polygon, wkt};

/// small helper to create a coordinate
fn c<T: GeoFloat>(x: T, y: T) -> Coord<T> {
Expand Down Expand Up @@ -783,10 +783,13 @@ mod test {
}
#[test]
fn multilinestring_test() {
let v1 = line_string![(x: 0.0, y: 0.0), (x: 1.0, y: 10.0)];
let v2 = line_string![(x: 1.0, y: 10.0), (x: 2.0, y: 0.0), (x: 3.0, y: 1.0)];
let v3 = line_string![(x: -12.0, y: -100.0), (x: 7.0, y: 8.0)];
let mls = MultiLineString::new(vec![v1, v2, v3]);
let mls = wkt! {
MULTILINESTRING(
(0.0 0.0,1.0 10.0),
(1.0 10.0,2.0 0.0,3.0 1.0),
(-12.0 -100.0,7.0 8.0)
)
};
assert_relative_eq!(
mls.centroid().unwrap(),
point![x: -1.9097834383655845, y: -37.683866439745714]
Expand All @@ -801,9 +804,7 @@ mod test {
#[test]
fn polygon_one_point_test() {
let p = point![ x: 2., y: 1. ];
let v = Vec::new();
let linestring = line_string![p.0];
let poly = Polygon::new(linestring, v);
let poly = polygon![p.0];
assert_relative_eq!(poly.centroid().unwrap(), p);
}

Expand Down Expand Up @@ -857,68 +858,42 @@ mod test {
#[test]
fn polygon_hole_test() {
// hexagon
let ls1 = LineString::from(vec![
(5.0, 1.0),
(4.0, 2.0),
(4.0, 3.0),
(5.0, 4.0),
(6.0, 4.0),
(7.0, 3.0),
(7.0, 2.0),
(6.0, 1.0),
(5.0, 1.0),
]);

let ls2 = LineString::from(vec![(5.0, 1.3), (5.5, 2.0), (6.0, 1.3), (5.0, 1.3)]);

let ls3 = LineString::from(vec![(5., 2.3), (5.5, 3.0), (6., 2.3), (5., 2.3)]);

let p1 = Polygon::new(ls1, vec![ls2, ls3]);
let p1 = wkt! { POLYGON(
(5.0 1.0,4.0 2.0,4.0 3.0,5.0 4.0,6.0 4.0,7.0 3.0,7.0 2.0,6.0 1.0,5.0 1.0),
(5.0 1.3,5.5 2.0,6.0 1.3,5.0 1.3),
(5.0 2.3,5.5 3.0,6.0 2.3,5.0 2.3)
) };
let centroid = p1.centroid().unwrap();
assert_relative_eq!(centroid, point!(x: 5.5, y: 2.5518518518518523));
}
#[test]
fn flat_polygon_test() {
let poly = Polygon::new(
LineString::from(vec![p(0., 1.), p(1., 1.), p(0., 1.)]),
vec![],
);
let poly = wkt! { POLYGON((0. 1.,1. 1.,0. 1.)) };
assert_eq!(poly.centroid(), Some(p(0.5, 1.)));
}
#[test]
fn multi_poly_with_flat_polygon_test() {
let poly = Polygon::new(
LineString::from(vec![p(0., 0.), p(1., 0.), p(0., 0.)]),
vec![],
);
let multipoly = MultiPolygon::new(vec![poly]);
let multipoly = wkt! { MULTIPOLYGON(((0. 0.,1. 0.,0. 0.))) };
assert_eq!(multipoly.centroid(), Some(p(0.5, 0.)));
}
#[test]
fn multi_poly_with_multiple_flat_polygon_test() {
let p1 = Polygon::new(
LineString::from(vec![p(1., 1.), p(1., 3.), p(1., 1.)]),
vec![],
);
let p2 = Polygon::new(
LineString::from(vec![p(2., 2.), p(6., 2.), p(2., 2.)]),
vec![],
);
let multipoly = MultiPolygon::new(vec![p1, p2]);
let multipoly = wkt! { MULTIPOLYGON(
((1. 1.,1. 3.,1. 1.)),
((2. 2.,6. 2.,2. 2.))
)};

assert_eq!(multipoly.centroid(), Some(p(3., 2.)));
}
#[test]
fn multi_poly_with_only_points_test() {
let p1 = Polygon::new(
LineString::from(vec![p(1., 1.), p(1., 1.), p(1., 1.)]),
vec![],
);
let p1 = wkt! { POLYGON((1. 1.,1. 1.,1. 1.)) };
assert_eq!(p1.centroid(), Some(p(1., 1.)));
let p2 = Polygon::new(
LineString::from(vec![p(2., 2.), p(2., 2.), p(2., 2.)]),
vec![],
);
let multipoly = MultiPolygon::new(vec![p1, p2]);

let multipoly = wkt! { MULTIPOLYGON(
((1. 1.,1. 1.,1. 1.)),
((2. 2., 2. 2.,2. 2.))
) };
assert_eq!(multipoly.centroid(), Some(p(1.5, 1.5)));
}
#[test]
Expand Down

0 comments on commit b5a0780

Please sign in to comment.