2023-02-24 00:31:32 -05:00
|
|
|
use common::test_events;
|
2023-02-22 21:55:40 -05:00
|
|
|
use s2::cellid::CellID;
|
2023-02-28 09:13:35 -05:00
|
|
|
use this_algorithm::Error;
|
2023-02-22 21:55:40 -05:00
|
|
|
use this_algorithm::{Address, CELLID_LEVEL};
|
|
|
|
#[macro_use]
|
|
|
|
mod common;
|
2023-02-24 00:31:32 -05:00
|
|
|
use common::CELLID_LEVEL_23_BITMASK;
|
|
|
|
use common::CELLID_LEVEL_23_END_BIT;
|
2023-02-22 21:55:40 -05:00
|
|
|
|
2023-02-28 09:13:35 -05:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_lat_lon() {
|
|
|
|
assert_eq!(
|
|
|
|
Address::from_lat_lon(1.0_f64, 400.0_f64),
|
|
|
|
Err(Error::InvalidLatLng)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
Address::from_lat_lon(1.0_f64, f64::NAN),
|
|
|
|
Err(Error::InvalidLatLng)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-22 21:55:40 -05:00
|
|
|
#[test]
|
|
|
|
fn test_cellid_translation() {
|
2023-02-24 00:31:32 -05:00
|
|
|
for (idx, entry) in test_events().iter().enumerate() {
|
|
|
|
let addr = Address::from_lat_lon(entry.lat, entry.lon).unwrap();
|
2023-02-28 09:20:06 -05:00
|
|
|
let addr_cellid = addr.as_cellid();
|
2023-02-22 21:55:40 -05:00
|
|
|
|
2023-02-24 00:31:32 -05:00
|
|
|
eprintln!("Testing word {idx}");
|
|
|
|
|
|
|
|
eprintln!(
|
|
|
|
"Entry: ({},{}) => {:0>64b}",
|
2023-02-28 09:20:30 -05:00
|
|
|
entry.lat, entry.lon, entry.cellid
|
2023-02-24 00:31:32 -05:00
|
|
|
);
|
|
|
|
eprintln!("\taddr: {addr}");
|
|
|
|
|
|
|
|
// Make sure the rust s2 implementation is accurate
|
2023-02-28 09:20:30 -05:00
|
|
|
assert_eq!(entry.cellid, CellID(entry.cellid).0);
|
2023-02-24 00:31:32 -05:00
|
|
|
assert_eq!(
|
2023-02-28 09:20:30 -05:00
|
|
|
(entry.cellid & CELLID_LEVEL_23_BITMASK) | CELLID_LEVEL_23_END_BIT,
|
|
|
|
CellID(entry.cellid).parent(CELLID_LEVEL).0
|
2023-02-24 00:31:32 -05:00
|
|
|
);
|
|
|
|
|
2023-02-22 21:55:40 -05:00
|
|
|
// Make sure the address is at the right level
|
2023-02-25 11:14:15 -05:00
|
|
|
assert_eq!(addr_cellid.level(), CELLID_LEVEL);
|
2023-02-22 21:55:40 -05:00
|
|
|
|
|
|
|
// Next, check if creating an address from a cellid matches itself
|
2023-02-28 09:20:30 -05:00
|
|
|
assert_eq!(addr, Address::from_cellid_u64(entry.cellid));
|
2023-02-22 21:55:40 -05:00
|
|
|
|
|
|
|
// Now check if the actual cell id matches
|
2023-02-28 09:20:30 -05:00
|
|
|
assert_eq_u64!(addr_cellid.0, CellID(entry.cellid).parent(CELLID_LEVEL).0);
|
2023-02-22 21:55:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-02-24 00:31:32 -05:00
|
|
|
fn test_encoding() {
|
|
|
|
for (_idx, entry) in test_events().iter().enumerate() {
|
|
|
|
let addr = Address::from_lat_lon(entry.lat, entry.lon).unwrap();
|
2023-02-25 11:14:15 -05:00
|
|
|
eprintln!("({}, {}) => {addr}", entry.lat, entry.lon);
|
2023-02-24 00:31:32 -05:00
|
|
|
}
|
2023-02-28 09:13:35 -05:00
|
|
|
// TODO:
|
|
|
|
// assert!(false);
|
2023-02-24 00:31:32 -05:00
|
|
|
}
|
2023-02-22 21:55:40 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_decoding() {}
|