this_algorithm/tests/algorithm.rs
2023-02-22 21:55:40 -05:00

50 lines
1.4 KiB
Rust

use csv::ReaderBuilder;
use s2::cellid::CellID;
use serde::{Deserialize, Deserializer, Serialize};
use std::{fs::File, str::FromStr};
use this_algorithm::{Address, CELLID_LEVEL};
#[macro_use]
mod common;
// use common::assert_eq_u64;
// const CELLID_LEVEL_23_BITMASK: u64 =
// 0b11111111_11111111_11111111_11111111_11111111_11111111_10000000_00000000;
#[test]
fn test_cellid_translation() {
let entries: Vec<TestEntry> = ReaderBuilder::new()
.from_reader(File::open("./test-data/01-sample-latlon-s2cpp-cellid.csv").unwrap())
.deserialize()
.collect::<Result<Vec<_>, _>>()
.unwrap();
for entry in &entries {
let addr = Address::from_lat_lon(entry.lat, entry.lon);
let addr_cellid = addr.as_cell_id().unwrap();
// Make sure the address is at the right level
assert_eq_u64!(addr_cellid.level(), CELLID_LEVEL as u64);
// Next, check if creating an address from a cellid matches itself
assert_eq!(addr, Address::from_cellid_u64(entry.cell_id));
// Now check if the actual cell id matches
assert_eq_u64!(
addr_cellid.0,
CellID(entry.cell_id).parent(CELLID_LEVEL as u64).0
);
}
}
#[test]
fn test_encoding() {}
#[test]
fn test_decoding() {}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct TestEntry {
lat: f64,
lon: f64,
cell_id: u64,
}