Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2d tests #39

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl WKBType {
3 => WKBDimension::Xyz,
_ => {
return Err(WKBError::General(format!(
"WKB type value out of range. Got: {}",
"WKB dimension value out of range. Got: {}",
value
)))
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
mod common;
pub mod error;
pub mod reader;
#[cfg(test)]
mod test;
pub mod writer;

pub use common::{Endianness, WKBType};
249 changes: 249 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
use geo_traits::to_geo::ToGeoGeometry;
use geo_types::{
line_string, point, polygon, Geometry, GeometryCollection, LineString, MultiLineString,
MultiPoint, MultiPolygon, Point, Polygon,
};

use crate::reader::read_wkb;
use crate::writer::{
write_geometry_collection, write_line_string, write_multi_line_string, write_multi_point,
write_multi_polygon, write_point, write_polygon,
};
use crate::Endianness;

fn point_2d() -> Point {
point!(
x: 0., y: 1.
)
}

fn linestring_2d() -> LineString {
line_string![
(x: 0., y: 1.),
(x: 1., y: 2.)
]
}

fn polygon_2d() -> Polygon {
polygon![
(x: -111., y: 45.),
(x: -111., y: 41.),
(x: -104., y: 41.),
(x: -104., y: 45.),
]
}

fn polygon_2d_with_interior() -> Polygon {
polygon!(
exterior: [
(x: -111., y: 45.),
(x: -111., y: 41.),
(x: -104., y: 41.),
(x: -104., y: 45.),
],
interiors: [
[
(x: -110., y: 44.),
(x: -110., y: 42.),
(x: -105., y: 42.),
(x: -105., y: 44.),
],
],
)
}

fn multi_point_2d() -> MultiPoint {
MultiPoint::new(vec![
point!(
x: 0., y: 1.
),
point!(
x: 1., y: 2.
),
])
}

fn multi_line_string_2d() -> MultiLineString {
MultiLineString::new(vec![
line_string![
(x: -111., y: 45.),
(x: -111., y: 41.),
(x: -104., y: 41.),
(x: -104., y: 45.),
],
line_string![
(x: -110., y: 44.),
(x: -110., y: 42.),
(x: -105., y: 42.),
(x: -105., y: 44.),
],
])
}

fn multi_polygon_2d() -> MultiPolygon {
MultiPolygon::new(vec![
polygon![
(x: -111., y: 45.),
(x: -111., y: 41.),
(x: -104., y: 41.),
(x: -104., y: 45.),
],
polygon!(
exterior: [
(x: -111., y: 45.),
(x: -111., y: 41.),
(x: -104., y: 41.),
(x: -104., y: 45.),
],
interiors: [
[
(x: -110., y: 44.),
(x: -110., y: 42.),
(x: -105., y: 42.),
(x: -105., y: 44.),
],
],
),
])
}

#[test]
fn round_trip_point() {
let orig = point_2d();
let mut buf = Vec::new();
write_point(&mut buf, &orig, Endianness::LittleEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::Point(orig), retour.to_geometry());

// Big endian
let mut buf = Vec::new();
write_point(&mut buf, &orig, Endianness::BigEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::Point(orig), retour.to_geometry());
}

#[test]
fn round_trip_line_string() {
let orig = linestring_2d();

let mut buf = Vec::new();
write_line_string(&mut buf, &orig, Endianness::LittleEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::LineString(orig.clone()), retour.to_geometry());

// Big endian
let mut buf = Vec::new();
write_line_string(&mut buf, &orig, Endianness::BigEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::LineString(orig), retour.to_geometry());
}

#[test]
fn round_trip_polygon() {
let orig = polygon_2d();

let mut buf = Vec::new();
write_polygon(&mut buf, &orig, Endianness::LittleEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::Polygon(orig.clone()), retour.to_geometry());

// Big endian
let mut buf = Vec::new();
write_polygon(&mut buf, &orig, Endianness::BigEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::Polygon(orig), retour.to_geometry());
}

#[test]
fn round_trip_polygon_with_interior() {
let orig = polygon_2d_with_interior();

let mut buf = Vec::new();
write_polygon(&mut buf, &orig, Endianness::LittleEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::Polygon(orig.clone()), retour.to_geometry());

// Big endian
let mut buf = Vec::new();
write_polygon(&mut buf, &orig, Endianness::BigEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::Polygon(orig), retour.to_geometry());
}

#[test]
fn round_trip_multi_point() {
let orig = multi_point_2d();

let mut buf = Vec::new();
write_multi_point(&mut buf, &orig, Endianness::LittleEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::MultiPoint(orig.clone()), retour.to_geometry());

// Big endian
let mut buf = Vec::new();
write_multi_point(&mut buf, &orig, Endianness::BigEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::MultiPoint(orig), retour.to_geometry());
}

#[test]
fn round_trip_multi_line_string() {
let orig = multi_line_string_2d();

let mut buf = Vec::new();
write_multi_line_string(&mut buf, &orig, Endianness::LittleEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(
Geometry::MultiLineString(orig.clone()),
retour.to_geometry()
);

// Big endian
let mut buf = Vec::new();
write_multi_line_string(&mut buf, &orig, Endianness::BigEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::MultiLineString(orig), retour.to_geometry());
}

#[test]
fn round_trip_multi_polygon() {
let orig = multi_polygon_2d();

let mut buf = Vec::new();
write_multi_polygon(&mut buf, &orig, Endianness::LittleEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::MultiPolygon(orig.clone()), retour.to_geometry());

// Big endian
let mut buf = Vec::new();
write_multi_polygon(&mut buf, &orig, Endianness::BigEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::MultiPolygon(orig), retour.to_geometry());
}

#[test]
fn round_trip_geometry_collection() {
let orig = GeometryCollection::new_from(vec![
Geometry::Point(point_2d()),
Geometry::LineString(linestring_2d()),
Geometry::Polygon(polygon_2d()),
Geometry::Polygon(polygon_2d_with_interior()),
Geometry::MultiPoint(multi_point_2d()),
Geometry::MultiLineString(multi_line_string_2d()),
Geometry::MultiPolygon(multi_polygon_2d()),
]);

let mut buf = Vec::new();
write_geometry_collection(&mut buf, &orig, Endianness::LittleEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(
Geometry::GeometryCollection(orig.clone()),
retour.to_geometry()
);

// Big endian
let mut buf = Vec::new();
write_geometry_collection(&mut buf, &orig, Endianness::BigEndian).unwrap();
let retour = read_wkb(&buf).unwrap();
assert_eq!(Geometry::GeometryCollection(orig), retour.to_geometry());
}
2 changes: 1 addition & 1 deletion src/writer/geometrycollection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn write_geometry_collection(
endianness: Endianness,
) -> WKBResult<()> {
// Byte order
writer.write_u8(Endianness::LittleEndian.into())?;
writer.write_u8(endianness.into())?;

// Content
match endianness {
Expand Down
2 changes: 1 addition & 1 deletion src/writer/linestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn write_line_string_content<B: ByteOrder>(
geom: &impl LineStringTrait<T = f64>,
) -> WKBResult<()> {
let wkb_type = WKBType::LineString(geom.dim().try_into()?);
writer.write_u32::<LittleEndian>(wkb_type.into())?;
writer.write_u32::<B>(wkb_type.into())?;

// numPoints
writer
Expand Down
2 changes: 1 addition & 1 deletion src/writer/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn write_point_content<B: ByteOrder>(
geom: &impl PointTrait<T = f64>,
) -> WKBResult<()> {
let wkb_type = WKBType::Point(geom.dim().try_into()?);
writer.write_u32::<LittleEndian>(wkb_type.into())?;
writer.write_u32::<B>(wkb_type.into())?;

if let Some(coord) = geom.coord() {
write_coord::<B>(writer, &coord)?;
Expand Down
2 changes: 1 addition & 1 deletion src/writer/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn write_polygon_content<B: ByteOrder>(
geom: &impl PolygonTrait<T = f64>,
) -> WKBResult<()> {
let wkb_type = WKBType::Polygon(geom.dim().try_into()?);
writer.write_u32::<LittleEndian>(wkb_type.into())?;
writer.write_u32::<B>(wkb_type.into())?;

// numRings
let num_rings = if geom.exterior().is_some() {
Expand Down
Loading