diff options
author | Daniel Schadt <kingdread@gmx.de> | 2025-08-26 21:49:03 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2025-08-26 21:49:03 +0200 |
commit | 6ec81bd17e18eed3de705a07a86bd1bf239e6ba3 (patch) | |
tree | a3c6e70ee6cd6533321e235a6c13279d6f676aed | |
parent | 5a4e23797e74c2989cdea86128350de53c0b15e4 (diff) | |
download | hittekaart-tests.tar.gz hittekaart-tests.tar.bz2 hittekaart-tests.zip |
add first teststests
-rw-r--r-- | Cargo.lock | 54 | ||||
-rw-r--r-- | hittekaart/Cargo.toml | 1 | ||||
-rw-r--r-- | hittekaart/src/gpx.rs | 33 |
3 files changed, 88 insertions, 0 deletions
@@ -559,6 +559,12 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -615,6 +621,7 @@ dependencies = [ "num-traits", "rayon", "roxmltree", + "rstest", "rusqlite", "thiserror", ] @@ -1311,6 +1318,12 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1320,6 +1333,32 @@ dependencies = [ ] [[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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1340,6 +1379,15 @@ 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" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1380,6 +1428,12 @@ dependencies = [ ] [[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" source = "registry+https://github.com/rust-lang/crates.io-index" 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<P: AsRef<Path>>( }; 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 + ); + } +} |