diff --git a/docs/annotated_words.ods b/docs/annotated_words.ods index 483f47d..5dfa053 100644 Binary files a/docs/annotated_words.ods and b/docs/annotated_words.ods differ diff --git a/justfile b/justfile index 768550b..e6e599d 100644 --- a/justfile +++ b/justfile @@ -1,9 +1,13 @@ build: fmt build-rust build-wasm build-js -all: clean build # build-docs +all: clean fmt test-rust build # build-docs rsync -ha ./web-frontend/build/ build/ du -shc build/* | sort -h +test-rust: + cargo test --all + cargo fmt --all --check + build-rust: cargo build --all @@ -28,10 +32,13 @@ watch-js: fmt: # just --fmt --unstable - cargo fmt + cargo fmt --all yarn --cwd ./web-frontend/ format -init: +serve: + http-server --verbose ./web-frontend/build/ + +init-verbose: yarn --cwd ./web-frontend/ cargo fetch cargo install wasm-pack diff --git a/src/lib.rs b/src/lib.rs index 13bdc6c..9e44503 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -200,7 +200,6 @@ impl Address<'_> { } /// Get the address as a latitude and longitude `f64` value - // TODO: Test this #[must_use] pub fn as_lat_lon(&self) -> (f64, f64) { let lat_lng = LatLng::from(self.as_cellid()); diff --git a/tests/algorithm.rs b/tests/algorithm.rs index b0785ca..a49eabd 100644 --- a/tests/algorithm.rs +++ b/tests/algorithm.rs @@ -1,9 +1,12 @@ use common::test_events; use s2::cellid::CellID; +use std::cmp; use this_algorithm::Error; use this_algorithm::{Address, CELLID_LEVEL}; #[macro_use] mod common; +use common::approx_geodetic_difference_m; +use common::ALLOWED_DISTANCE_ERROR_M; use common::CELLID_LEVEL_23_BITMASK; use common::CELLID_LEVEL_23_END_BIT; @@ -19,14 +22,64 @@ fn test_invalid_lat_lon() { ); } +#[test] +fn test_decoding_lat_lon() { + let mut max = f64::MIN; + let mut min = f64::MAX; + for (idx, entry) in test_events().iter().enumerate() { + eprintln!("Testing row {idx}"); + + let addr = Address::from_lat_lon(entry.lat, entry.lon).unwrap(); + let addr_latlon = addr.as_lat_lon(); + + let (lat_diff, lon_diff) = ( + (entry.lat - addr_latlon.0).abs(), + (entry.lon - addr_latlon.1).abs(), + ); + + min = if lat_diff < min { + lat_diff + } else if lon_diff < min { + lon_diff + } else { + min + }; + + max = if lat_diff > max && lat_diff < 1_f64 { + lat_diff + } else if lon_diff > max && lon_diff < 1_f64 { + lon_diff + } else { + max + }; + + if lat_diff > 0.01_f64 || lon_diff > 0.01_f64 { + eprintln!( + "Uh oh: {lat_diff}, {lon_diff}, ({}, {}) => ({:?}) => ({:?})", + entry.lat, entry.lon, addr, addr_latlon + ); + } + + // Ensure the distance is not more than the allowed distance + assert!( + approx_geodetic_difference_m(addr_latlon, (entry.lat, entry.lon)) + < ALLOWED_DISTANCE_ERROR_M + ); + + // assert_eq!((entry.lat, entry.lon), (latlon.0, latlon.1)); + } + eprintln!("Got max: {max} and min: {min}"); + assert!(false); +} + #[test] fn test_cellid_translation() { for (idx, entry) in test_events().iter().enumerate() { + eprintln!("Testing row {idx}"); + let addr = Address::from_lat_lon(entry.lat, entry.lon).unwrap(); let addr_cellid = addr.as_cellid(); - eprintln!("Testing word {idx}"); - eprintln!( "Entry: ({},{}) => {:0>64b}", entry.lat, entry.lon, entry.cellid diff --git a/tests/common/mod.rs b/tests/common/mod.rs index ab7bdf2..c65a57a 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -4,6 +4,23 @@ use std::fs::File; use csv::ReaderBuilder; use serde::{Deserialize, Serialize}; +/// The allowed error for encoding and decoding points, in meters +/// +/// Due to the selected S2 level being 23, our error is expected to be under 2 meters +pub const ALLOWED_DISTANCE_ERROR_M: f64 = 0.0_f64; +// The earth's approximate latitude circumfrence +pub const EARTH_CIRCUMFERENCE_LAT: f64 = 0.0_f64; +// The earth's approximate longitude circumfrence +pub const EARTH_CIRCUMFERENCE_LON: f64 = 0.0_f64; + +/// Bitmask to preserve all data bits at level 23 +pub const CELLID_LEVEL_23_BITMASK: u64 = + 0b11111111_11111111_11111111_11111111_11111111_11111111_10000000_00000000; + +/// The single 1 bit at the end of level 23 +pub const CELLID_LEVEL_23_END_BIT: u64 = + 0b00000000_00000000_00000000_00000000_00000000_00000000_01000000_00000000; + #[macro_export] macro_rules! assert_eq_u64 { ($a:expr, $b:expr) => {{ @@ -43,10 +60,7 @@ pub struct TestEntry { pub cellid: u64, } -/// Bitmask to preserve all data bits at level 23 -pub const CELLID_LEVEL_23_BITMASK: u64 = - 0b11111111_11111111_11111111_11111111_11111111_11111111_10000000_00000000; - -/// The single 1 bit at the end of level 23 -pub const CELLID_LEVEL_23_END_BIT: u64 = - 0b00000000_00000000_00000000_00000000_00000000_00000000_01000000_00000000; +/// Gets the approximate geodetic difference in meters between two latlons +pub fn approx_geodetic_difference_m((lat1, lon1): (f64, f64), (lat2, lon2): (f64, f64)) -> f64 { + 1000.0_f64 +}