Skip to content
This repository has been archived by the owner on Apr 2, 2022. It is now read-only.

JTS TestRunner which supports most Centroid and ConvexHull cases. #1

Merged
merged 2 commits into from
Feb 25, 2021
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
24 changes: 24 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "jts-test-runner"
version = "0.1.0"
authors = ["Michael Kirk <michael.code@endoftheworl.de>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
approx = "0.4.0"
geo = "*"
include_dir = { version = "0.6", features = ["search"] }
log = "0.4.14"
serde = { version = "*", features = ["derive"] }
serde-xml-rs = "*"
wkt = { version = "0.9", features = ["geo-types", "serde"] }

[dev-dependencies]
pretty_env_logger = "*"

[patch.crates-io]
geo = { git = "https://github.com/georust/geo", branch = "mkirk/centroid-fixups" }
geo-types = { git = "https://github.com/georust/geo", branch = "mkirk/centroid-fixups" }
wkt = { git = "https://github.com/georust/wkt" }
115 changes: 115 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use geo::{Point, Geometry};
use serde::Deserialize;

use super::Result;

/// Example of the XML that these structures represent
///
/// ```xml
/// <run>
/// <precisionModel scale="1.0" offsetx="0.0" offsety="0.0"/>
///
/// <case>
/// <desc>AA disjoint</desc>
/// <a>
/// POLYGON(
/// (0 0, 80 0, 80 80, 0 80, 0 0))
/// </a>
/// <b>
/// POLYGON(
/// (100 200, 100 140, 180 140, 180 200, 100 200))
/// </b>
/// <test><op name="relate" arg3="FF2FF1212" arg1="A" arg2="B"> true </op>
/// </test>
/// <test> <op name="intersects" arg1="A" arg2="B"> false </op></test>
/// <test> <op name="contains" arg1="A" arg2="B"> false </op></test>
/// </case>
/// </run>
/// ```
#[derive(Debug, Deserialize)]
pub(crate) struct Run {
#[serde(rename="case")]
pub cases: Vec<Case>,
}

#[derive(Debug, Deserialize)]
pub(crate) struct Case {
pub(crate) desc: String,
// `a` seems to always be a WKT geometry, but until we can handle `POINT EMPTY` this
// will error.
// see https://github.com/georust/wkt/issues/61
//
// I also spent some time trying to have serde "try" to deserialize, skipping any
// cases that were unparseable, without throwing away the whole thing but eventually ran out of time.
// See https://github.com/serde-rs/serde/issues/1583 for a related approach
//#[serde(deserialize_with = "wkt::deserialize_geometry")]
pub(crate) a: String,
#[serde(rename = "test")]
pub(crate) tests: Vec<Test>,
}

#[derive(Debug, Deserialize)]
pub(crate) struct Test {
#[serde(rename="op")]
pub(crate) operation_input: OperationInput,
}

#[derive(Debug, Deserialize)]
pub struct CentroidInput {
pub(crate) arg1: String,

#[serde(rename = "$value")]
#[serde(deserialize_with = "wkt::deserialize_point")]
pub(crate) expected: Option<geo::Point<f64>>,
}

#[derive(Debug, Deserialize)]
pub struct ConvexHullInput {
pub(crate) arg1: String,

#[serde(rename = "$value")]
#[serde(deserialize_with = "wkt::deserialize_geometry")]
pub(crate) expected: geo::Geometry<f64>,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "name")]
pub(crate) enum OperationInput {
#[serde(rename = "getCentroid")]
CentroidInput(CentroidInput),

#[serde(rename = "convexhull")]
ConvexHullInput(ConvexHullInput),

#[serde(other)]
Unsupported,
}

#[derive(Debug)]
pub(crate) enum Operation {
Centroid {
subject: Geometry<f64>,
expected: Option<Point<f64>>,
},
ConvexHull {
subject: Geometry<f64>,
expected: Geometry<f64>,
}
}

impl OperationInput {
pub(crate) fn into_operation(self, geometry: Geometry<f64>) -> Result<Operation>
{
match self {
Self::CentroidInput(centroid_input) => {
assert_eq!("A", centroid_input.arg1);
Ok(Operation::Centroid { subject: geometry.clone(), expected: centroid_input.expected })
}
Self::ConvexHullInput(convex_hull_input) => {
assert_eq!("A", convex_hull_input.arg1);
Ok(Operation::ConvexHull { subject: geometry.clone(), expected: convex_hull_input.expected })
}
Self::Unsupported => Err("This OperationInput not supported".into())
}
}
}
82 changes: 82 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#[macro_use]
extern crate log;

mod input;
use input::Operation;

mod runner;
pub use runner::TestRunner;

type Error = Box<dyn std::error::Error>;
type Result<T> = std::result::Result<T, Error>;

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

fn init_logging() {
use std::sync::Once;
static LOG_SETUP: Once = Once::new();
LOG_SETUP.call_once(|| {
pretty_env_logger::init();
});
}

#[test]
fn test_centroid() {
init_logging();
let mut runner = TestRunner::new().matching_filename_glob("*Centroid.xml");
runner.run().expect("test cases failed");

// sanity check that *something* was run
assert!(
runner.failures().len() + runner.successes().len() > 0,
"No tests were run."
);

if !runner.failures().is_empty() {
let failure_text = runner
.failures()
.iter()
.map(|failure| format!("{}", failure))
.collect::<Vec<String>>()
.join("\n");
panic!(
"{} failures / {} successes in JTS test suite:\n{}",
runner.failures().len(),
runner.successes().len(),
failure_text
);
}
}

#[test]
// several of the ConvexHull tests are currently failing
#[ignore]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run this ignored test to see the failing convex hull cases.

fn test_all_general() {
init_logging();
let mut runner = TestRunner::new();
runner.run().expect("test cases failed");

// sanity check that *something* was run
assert!(
runner.failures().len() + runner.successes().len() > 0,
"No tests were run."
);

if !runner.failures().is_empty() {
let failure_text = runner
.failures()
.iter()
.map(|failure| format!("{}", failure))
.collect::<Vec<String>>()
.join("\n");
panic!(
"{} failures / {} successes in JTS test suite:\n{}",
runner.failures().len(),
runner.successes().len(),
failure_text
);
}
}
}
Loading