use core::hash::{Hash, Hasher};
use super::*;
#[test]
fn the_interval_is_closed_at_both_ends_and_nowhere_wider() {
assert_eq!(NormCoord::new(0.0).map(NormCoord::get), Some(0.0));
assert_eq!(NormCoord::new(1.0).map(NormCoord::get), Some(1.0));
assert_eq!(NormCoord::new(0.5).map(NormCoord::get), Some(0.5));
assert!(
NormCoord::new(f32::from_bits(1)).is_some(),
"the smallest positive subnormal is inside the interval, and nothing here rounds it away"
);
assert_eq!(
NormCoord::new(-f32::from_bits(1)),
None,
"the smallest negative subnormal is below zero"
);
assert_eq!(NormCoord::new(1.000_000_1), None);
assert_eq!(NormCoord::new(-0.000_000_1), None);
assert_eq!(NormCoord::new(2.0), None);
assert_eq!(NormCoord::new(-1.0), None);
}
#[test]
fn a_value_that_is_not_a_number_is_refused() {
assert_eq!(NormCoord::new(f32::NAN), None);
assert_eq!(NormCoord::new(f32::INFINITY), None);
assert_eq!(NormCoord::new(f32::NEG_INFINITY), None);
assert_eq!(NormCoord::new(-f32::NAN), None);
}
#[test]
fn a_negative_zero_is_admitted_and_arrives_as_the_positive_one() {
let signed = NormCoord::new(-0.0).expect("negative zero is in the interval");
assert_eq!(signed, NormCoord::ZERO);
assert!(signed.get().is_sign_positive());
assert_eq!(signed.get().to_bits(), 0.0_f32.to_bits());
}
#[test]
fn two_equal_coordinates_hash_alike() {
fn hashed(coord: NormCoord) -> u64 {
let mut hasher = std::collections::hash_map::DefaultHasher::new();
coord.hash(&mut hasher);
hasher.finish()
}
let signed = NormCoord::new(-0.0).expect("negative zero is in the interval");
assert_eq!(signed, NormCoord::ZERO);
assert_eq!(hashed(signed), hashed(NormCoord::ZERO));
}
#[test]
fn admitted_coordinates_are_totally_ordered() {
let mut frame = [
NormCoord::new(0.75).unwrap(),
NormCoord::ZERO,
NormCoord::ONE,
NormCoord::new(0.25).unwrap(),
];
frame.sort();
assert_eq!(
frame.map(NormCoord::get),
[0.0, 0.25, 0.75, 1.0],
"sorting a coordinate never reaches `cmp`'s panic arm"
);
}
#[test]
fn a_coordinate_round_trips_through_a_cursor_and_a_forged_one_is_refused() {
let mut bytes = Vec::new();
let written = NormCoord::new(0.625).unwrap();
crate::CursorValue::write_cursor(&written, &mut bytes);
assert_eq!(
<NormCoord as crate::CursorValue>::read_cursor(&bytes),
Some(written)
);
let mut forged = Vec::new();
crate::CursorValue::write_cursor(&2.0_f32, &mut forged);
assert_eq!(
<NormCoord as crate::CursorValue>::read_cursor(&forged),
None,
"a forged cursor meets the constructor's own refusal"
);
}
#[test]
fn a_coordinate_widens_to_both_float_widths_and_neither_narrows_back() {
let coord = NormCoord::new(0.25).unwrap();
assert_eq!(f32::from(coord), 0.25_f32);
assert_eq!(f64::from(coord), 0.25_f64);
}