From 6ec81bd17e18eed3de705a07a86bd1bf239e6ba3 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 26 Aug 2025 21:49:03 +0200 Subject: add first tests --- Cargo.lock | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ hittekaart/Cargo.toml | 1 + hittekaart/src/gpx.rs | 33 +++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 0594ef5..d67ed63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -558,6 +558,12 @@ version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +[[package]] +name = "glob" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280" + [[package]] name = "half" version = "2.6.0" @@ -615,6 +621,7 @@ dependencies = [ "num-traits", "rayon", "roxmltree", + "rstest", "rusqlite", "thiserror", ] @@ -1310,6 +1317,12 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "relative-path" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2" + [[package]] name = "roxmltree" version = "0.18.1" @@ -1319,6 +1332,32 @@ dependencies = [ "xmlparser", ] +[[package]] +name = "rstest" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5a3193c063baaa2a95a33f03035c8a72b83d97a54916055ba22d35ed3839d49" +dependencies = [ + "rstest_macros", +] + +[[package]] +name = "rstest_macros" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c845311f0ff7951c5506121a9ad75aec44d083c31583b2ea5a30bcb0b0abba0" +dependencies = [ + "cfg-if", + "glob", + "proc-macro2", + "quote", + "regex", + "relative-path", + "rustc_version", + "syn", + "unicode-ident", +] + [[package]] name = "rusqlite" version = "0.29.0" @@ -1339,6 +1378,15 @@ version = "0.1.25" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "989e6739f80c4ad5b13e0fd7fe89531180375b18520cc8c82080e4dc4035b84f" +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + [[package]] name = "rusttype" version = "0.9.3" @@ -1379,6 +1427,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "semver" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0" + [[package]] name = "serde" version = "1.0.219" diff --git a/hittekaart/Cargo.toml b/hittekaart/Cargo.toml index 62ec6e9..651aec4 100644 --- a/hittekaart/Cargo.toml +++ b/hittekaart/Cargo.toml @@ -27,3 +27,4 @@ thiserror = "2.0.12" [dev-dependencies] criterion = "0.5.0" +rstest = { version = "0.26.1", default-features = false } diff --git a/hittekaart/src/gpx.rs b/hittekaart/src/gpx.rs index 86cb88b..8bd076d 100644 --- a/hittekaart/src/gpx.rs +++ b/hittekaart/src/gpx.rs @@ -60,6 +60,8 @@ impl Coordinates { let ymax = 2u64.pow(zoom) as f64 * HEIGHT; let lambda = self.longitude.to_radians().clamp(-max_lon, max_lon); + // The coordinate +180° is the same as -180°, so we want to map them to the same point + let lambda = if lambda == PI { -PI } else { lambda }; let phi = self.latitude.to_radians().clamp(-max_lat, max_lat); let phi = (phi.tan() + (1.0 / phi.cos())).ln(); let x = xmax / (2.0 * PI) * (lambda + PI); @@ -171,3 +173,34 @@ pub fn extract_from_file>( }; extract_from_str(&content) } + +#[cfg(test)] +mod test { + use super::*; + use rstest::*; + + #[rstest] + #[case((0.0, 0.0), 0, (128, 128))] + #[case((-180.0, 0.0), 0, (0, 128))] + #[case((180.0, 0.0), 0, (0, 128))] + #[case((179.99, 0.0), 0, (255, 128))] + #[case((0.0, 90.0), 0, (128, 0))] + #[case((0.0, -90.0), 0, (128, 255))] + #[case((0.0, 0.0), 4, (2048, 2048))] + #[case((-180.0, 0.0), 4, (0, 2048))] + #[case((180.0, 0.0), 4, (0, 2048))] + #[case((179.99, 0.0), 4, (4095, 2048))] + #[case((0.0, 90.0), 4, (2048, 0))] + #[case((0.0, -90.0), 4, (2048, 4095))] + #[case((-90.0, 0.0), 0, (64, 128))] + #[case((90.0, 0.0), 0, (192, 128))] + // Example taken from + // https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Example:_Convert_a_GPS_coordinate_to_a_pixel_position_in_a_Web_Mercator_tile + #[case((139.7006793, 35.6590699), 18, (232798 * 256 + 238, 103246 * 256 + 105))] + fn web_mercator(#[case] input: (f64, f64), #[case] zoom: u32, #[case] expected: (u64, u64)) { + assert_eq!( + Coordinates::new(input.0, input.1).web_mercator(zoom), + expected + ); + } +} -- cgit v1.2.3