More tests and cleanup

This commit is contained in:
Austen Adler 2023-02-28 11:53:39 -05:00
parent 8b6e3b5d1a
commit 9c99cf46d6
5 changed files with 86 additions and 13 deletions

Binary file not shown.

View File

@ -1,9 +1,13 @@
build: fmt build-rust build-wasm build-js 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/ rsync -ha ./web-frontend/build/ build/
du -shc build/* | sort -h du -shc build/* | sort -h
test-rust:
cargo test --all
cargo fmt --all --check
build-rust: build-rust:
cargo build --all cargo build --all
@ -28,10 +32,13 @@ watch-js:
fmt: fmt:
# just --fmt --unstable # just --fmt --unstable
cargo fmt cargo fmt --all
yarn --cwd ./web-frontend/ format yarn --cwd ./web-frontend/ format
init: serve:
http-server --verbose ./web-frontend/build/
init-verbose:
yarn --cwd ./web-frontend/ yarn --cwd ./web-frontend/
cargo fetch cargo fetch
cargo install wasm-pack cargo install wasm-pack

View File

@ -200,7 +200,6 @@ impl Address<'_> {
} }
/// Get the address as a latitude and longitude `f64` value /// Get the address as a latitude and longitude `f64` value
// TODO: Test this
#[must_use] #[must_use]
pub fn as_lat_lon(&self) -> (f64, f64) { pub fn as_lat_lon(&self) -> (f64, f64) {
let lat_lng = LatLng::from(self.as_cellid()); let lat_lng = LatLng::from(self.as_cellid());

View File

@ -1,9 +1,12 @@
use common::test_events; use common::test_events;
use s2::cellid::CellID; use s2::cellid::CellID;
use std::cmp;
use this_algorithm::Error; use this_algorithm::Error;
use this_algorithm::{Address, CELLID_LEVEL}; use this_algorithm::{Address, CELLID_LEVEL};
#[macro_use] #[macro_use]
mod common; 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_BITMASK;
use common::CELLID_LEVEL_23_END_BIT; 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] #[test]
fn test_cellid_translation() { fn test_cellid_translation() {
for (idx, entry) in test_events().iter().enumerate() { 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 = Address::from_lat_lon(entry.lat, entry.lon).unwrap();
let addr_cellid = addr.as_cellid(); let addr_cellid = addr.as_cellid();
eprintln!("Testing word {idx}");
eprintln!( eprintln!(
"Entry: ({},{}) => {:0>64b}", "Entry: ({},{}) => {:0>64b}",
entry.lat, entry.lon, entry.cellid entry.lat, entry.lon, entry.cellid

View File

@ -4,6 +4,23 @@ use std::fs::File;
use csv::ReaderBuilder; use csv::ReaderBuilder;
use serde::{Deserialize, Serialize}; 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_export]
macro_rules! assert_eq_u64 { macro_rules! assert_eq_u64 {
($a:expr, $b:expr) => {{ ($a:expr, $b:expr) => {{
@ -43,10 +60,7 @@ pub struct TestEntry {
pub cellid: u64, pub cellid: u64,
} }
/// Bitmask to preserve all data bits at level 23 /// Gets the approximate geodetic difference in meters between two latlons
pub const CELLID_LEVEL_23_BITMASK: u64 = pub fn approx_geodetic_difference_m((lat1, lon1): (f64, f64), (lat2, lon2): (f64, f64)) -> f64 {
0b11111111_11111111_11111111_11111111_11111111_11111111_10000000_00000000; 1000.0_f64
}
/// 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;