this_algorithm/tests/common/mod.rs

75 lines
2.5 KiB
Rust
Raw Normal View History

2023-02-25 16:44:22 -05:00
#![allow(dead_code)]
use geoutils::Location;
2023-02-24 00:31:32 -05:00
use std::fs::File;
use csv::ReaderBuilder;
use serde::{Deserialize, Serialize};
2023-02-28 11:53:39 -05:00
/// 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
///
/// With the encoding I selected, the max difference of my sample data actually fits in under 1 meter!
pub const ALLOWED_DISTANCE_ERROR_M: f64 = 1.0_f64;
2023-02-28 11:53:39 -05:00
// 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;
2023-02-22 21:55:40 -05:00
#[macro_export]
macro_rules! assert_eq_u64 {
($a:expr, $b:expr) => {{
let are_equal = $a == $b;
let diff = $a ^ $b;
eprintln!("Comparing u64:");
let a_txt = format!("{:0>64b}", $a).replace('0', " ");
let b_txt = format!("{:0>64b}", $b).replace('0', " ");
let diff_txt = if diff == 0 {
String::from("NONE")
} else {
format!("{:0>64b}", diff)
.replace('0', " ")
.replace('1', "~")
};
2023-02-24 00:31:32 -05:00
eprintln!("\t.{a_txt}.");
eprintln!("\t.{b_txt}.");
eprintln!("Diff:\t.{diff_txt}.");
2023-03-11 14:56:51 -05:00
eprintln!("\t | | | | | | | |");
eprintln!("\t 63 52 48 40 32 28 16 0");
2023-02-15 21:16:43 -05:00
2023-02-24 00:31:32 -05:00
assert!(are_equal);
2023-02-22 21:55:40 -05:00
}};
}
2023-02-24 00:31:32 -05:00
/// Get a Vec of test entries
pub fn test_events() -> Vec<TestEntry> {
ReaderBuilder::new()
.from_reader(File::open("./test-data/01-sample-latlon-s2cpp-cellid.csv").unwrap())
.deserialize()
.collect::<Result<Vec<_>, _>>()
.unwrap()
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct TestEntry {
pub lat: f64,
pub lon: f64,
2023-02-28 09:36:36 -05:00
pub cellid: u64,
2023-02-24 00:31:32 -05:00
}
2023-02-25 11:14:15 -05:00
/// Gets the approximate geodetic difference in meters between two latlons in meters
2023-02-28 11:53:39 -05:00
pub fn approx_geodetic_difference_m((lat1, lon1): (f64, f64), (lat2, lon2): (f64, f64)) -> f64 {
Location::new(lat1, lon1)
.distance_to(&Location::new(lat2, lon2))
.unwrap()
.meters()
2023-02-28 11:53:39 -05:00
}