From b5a0780bdc4ecf070d4c5f3dfc2ca729125e4695 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Tue, 19 Sep 2023 18:42:21 -0400 Subject: [PATCH] A couple examples using the wkt macro in tests --- geo-types/src/geometry/multi_line_string.rs | 30 +++----- geo-types/src/geometry/multi_point.rs | 39 +++++----- geo/src/algorithm/affine_ops.rs | 6 +- geo/src/algorithm/area.rs | 12 +--- geo/src/algorithm/centroid.rs | 79 +++++++-------------- 5 files changed, 59 insertions(+), 107 deletions(-) diff --git a/geo-types/src/geometry/multi_line_string.rs b/geo-types/src/geometry/multi_line_string.rs index 0e05673af9..11b0c8ff88 100644 --- a/geo-types/src/geometry/multi_line_string.rs +++ b/geo-types/src/geometry/multi_line_string.rs @@ -197,29 +197,21 @@ where #[cfg(test)] mod test { use super::*; - use crate::line_string; + use crate::{line_string, wkt}; #[test] fn test_iter() { - let multi: Vec> = 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 = MultiLineString::new(multi); + let multi: MultiLineString = 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) }); } } @@ -227,16 +219,10 @@ mod test { 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) }); } } } diff --git a/geo-types/src/geometry/multi_point.rs b/geo-types/src/geometry/multi_point.rs index d28e59d8ea..1391da78ed 100644 --- a/geo-types/src/geometry/multi_point.rs +++ b/geo-types/src/geometry/multi_point.rs @@ -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 { @@ -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; @@ -235,26 +235,25 @@ 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.)); } @@ -262,26 +261,24 @@ mod 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.)); } } diff --git a/geo/src/algorithm/affine_ops.rs b/geo/src/algorithm/affine_ops.rs index 978ff3b272..b534595ccd 100644 --- a/geo/src/algorithm/affine_ops.rs +++ b/geo/src/algorithm/affine_ops.rs @@ -390,7 +390,7 @@ impl AffineTransform { #[cfg(test)] mod tests { use super::*; - use crate::{polygon, Point}; + use crate::{wkt, Point}; // given a matrix with the shape // [[a, b, xoff], @@ -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); } } diff --git a/geo/src/algorithm/area.rs b/geo/src/algorithm/area.rs index 744112f135..6aec9d21af 100644 --- a/geo/src/algorithm/area.rs +++ b/geo/src/algorithm/area.rs @@ -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] @@ -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] diff --git a/geo/src/algorithm/centroid.rs b/geo/src/algorithm/centroid.rs index a7e2d0c3f8..8718295f55 100644 --- a/geo/src/algorithm/centroid.rs +++ b/geo/src/algorithm/centroid.rs @@ -692,7 +692,7 @@ impl WeightedCentroid { #[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(x: T, y: T) -> Coord { @@ -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] @@ -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); } @@ -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]