Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The root of most evil #1063

Merged
merged 3 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions geom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ pub fn trim_f64(x: f64) -> f64 {
/// Serializes a trimmed `f64` as an `i32` to save space.
fn serialize_f64<S: Serializer>(x: &f64, s: S) -> Result<S::Ok, S::Error> {
// So a trimmed f64's range becomes 2**31 / 10,000 =~ 214,000, which is plenty
// We don't need to round() here; trim_f64 already handles that.
let int = (x * 10_000.0) as i32;
// We MUST round here, the same as trim_f64. The unit test demonstrates why.
let int = (x * 10_000.0).round() as i32;
int.serialize(s)
}

Expand Down Expand Up @@ -180,28 +180,39 @@ mod tests {

#[test]
fn f64_trimming() {
// Roundtrip a bunch of random f64's
let mut rng = rand_xorshift::XorShiftRng::seed_from_u64(42);
for _ in 0..1_000 {
let input = rng.gen_range(-214_000.00..214_000.0);
let trimmed = trim_f64(input);
let json_roundtrip: f64 =
serde_json::from_slice(serde_json::to_string(&trimmed).unwrap().as_bytes())
.unwrap();
let bincode_roundtrip: f64 =
bincode::deserialize(&bincode::serialize(&trimmed).unwrap()).unwrap();
assert_eq!(json_roundtrip, trimmed);
assert_eq!(bincode_roundtrip, trimmed);
// Generate a random point
let input = Pt2D::new(
rng.gen_range(-214_000.00..214_000.0),
rng.gen_range(-214_000.00..214_000.0),
);
// Round-trip to JSON and bincode
let json_roundtrip: Pt2D =
serde_json::from_slice(serde_json::to_string(&input).unwrap().as_bytes()).unwrap();
let bincode_roundtrip: Pt2D =
bincode::deserialize(&bincode::serialize(&input).unwrap()).unwrap();

// Make sure everything is EXACTLY equal
if !exactly_eq(input, json_roundtrip) || !exactly_eq(input, bincode_roundtrip) {
panic!("Round-tripping mismatch!\ninput= {:?}\njson_roundtrip= {:?}\nbincode_roundtrip={:?}", input,json_roundtrip, bincode_roundtrip);
}
}

// Hardcode a particular case, where we can hand-verify that it trims to 4 decimal places
let input = 1.2345678;
let trimmed = trim_f64(input);
let json_roundtrip: f64 =
serde_json::from_slice(serde_json::to_string(&trimmed).unwrap().as_bytes()).unwrap();
let bincode_roundtrip: f64 =
bincode::deserialize(&bincode::serialize(&trimmed).unwrap()).unwrap();
assert_eq!(json_roundtrip, 1.2346);
assert_eq!(bincode_roundtrip, 1.2346);
let input = Pt2D::new(1.2345678, 9.876543);
let json_roundtrip: Pt2D =
serde_json::from_slice(serde_json::to_string(&input).unwrap().as_bytes()).unwrap();
let bincode_roundtrip: Pt2D =
bincode::deserialize(&bincode::serialize(&input).unwrap()).unwrap();
assert_eq!(input.x(), 1.2346);
assert_eq!(input.y(), 9.8765);
assert!(exactly_eq(input, json_roundtrip));
assert!(exactly_eq(input, bincode_roundtrip));
}

// Don't use the PartialEq implementation, which does an epsilon check
fn exactly_eq(pt1: Pt2D, pt2: Pt2D) -> bool {
pt1.x() == pt2.x() && pt1.y() == pt2.y()
}
}
31 changes: 30 additions & 1 deletion tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rand::seq::SliceRandom;

use abstio::{CityName, MapName};
use abstutil::Timer;
use geom::{Duration, Time};
use geom::{Distance, Duration, Time};
use map_model::{IntersectionID, LaneType, Map, Perimeter, RoadID};
use sim::{AlertHandler, PrebakeSummary, Sim, SimFlags, SimOptions};
use synthpop::{IndividTrip, PersonSpec, Scenario, TripEndpoint, TripMode, TripPurpose};
Expand All @@ -31,6 +31,9 @@ fn main() -> Result<()> {
if false {
smoke_test()?;
}
if true {
geometry_test()?;
}
Ok(())
}

Expand Down Expand Up @@ -389,3 +392,29 @@ fn bus_test() -> Result<()> {
}
Ok(())
}

/// Check serialized geometry on every map.
fn geometry_test() -> Result<()> {
let mut timer = Timer::new("check geometry for all maps");
for name in MapName::list_all_maps_locally() {
let map = map_model::Map::load_synchronously(name.path(), &mut timer);

for l in map.all_lanes() {
let mut sum = Distance::ZERO;
// This'll crash if duplicate adjacent points snuck in. The sum check is mostly just to
// make sure the lines actually get iterated. But also, if it fails, we're discovering
// the same sort of serialization problem!
for line in l.lane_center_pts.lines() {
sum += line.length();
}
assert_eq!(sum, l.lane_center_pts.length());
}

for i in map.all_intersections() {
for pair in i.polygon.get_outer_ring().points().windows(2) {
assert_ne!(pair[0], pair[1]);
}
}
}
Ok(())
}