letmut number = not_nan(5.0f32);
number += not_nan(4.0f32);
assert_eq!(*number, 9.0f32);
number -= not_nan(4.0f32);
assert_eq!(*number, 5.0f32);
number *= not_nan(4.0f32);
assert_eq!(*number, 20.0f32);
number /= not_nan(4.0f32);
assert_eq!(*number, 5.0f32);
number %= not_nan(4.0f32);
assert_eq!(*number, 1.0f32);
number = not_nan(5.0f32);
number += 4.0f32;
assert_eq!(*number, 9.0f32);
number -= 4.0f32;
assert_eq!(*number, 5.0f32);
number *= 4.0f32;
assert_eq!(*number, 20.0f32);
number /= 4.0f32;
assert_eq!(*number, 5.0f32);
number %= 4.0f32;
assert_eq!(*number, 1.0f32);
letmut number = not_nan(5.0f64);
number += not_nan(4.0f64);
assert_eq!(*number, 9.0f64);
number -= not_nan(4.0f64);
assert_eq!(*number, 5.0f64);
number *= not_nan(4.0f64);
assert_eq!(*number, 20.0f64);
number /= not_nan(4.0f64);
assert_eq!(*number, 5.0f64);
number %= not_nan(4.0f64);
assert_eq!(*number, 1.0f64);
number = not_nan(5.0f64);
number += 4.0f64;
assert_eq!(*number, 9.0f64);
number -= 4.0f64;
assert_eq!(*number, 5.0f64);
number *= 4.0f64;
assert_eq!(*number, 20.0f64);
number /= 4.0f64;
assert_eq!(*number, 5.0f64);
number %= 4.0f64;
assert_eq!(*number, 1.0f64);
#[test] fn hash_is_good_for_whole_numbers() { let state = RandomState::new(); let limit = 10000;
letmut set = ::std::collections::HashSet::with_capacity(limit); for i in0..limit { letmut h = state.build_hasher();
OrderedFloat::from(i as f64).hash(&mut h);
set.insert(h.finish());
}
// This allows 100 collisions, which is far too // many, but should guard against transient issues // that will result from using RandomState let pct_unique = set.len() as f64 / limit as f64;
assert!(0.99f64 < pct_unique, "percent-unique={}", pct_unique);
}
#[test] fn hash_is_good_for_fractional_numbers() { let state = RandomState::new(); let limit = 10000;
letmut set = ::std::collections::HashSet::with_capacity(limit); for i in0..limit { letmut h = state.build_hasher();
OrderedFloat::from(i as f64 * (1f64 / limit as f64)).hash(&mut h);
set.insert(h.finish());
}
// This allows 100 collisions, which is far too // many, but should guard against transient issues // that will result from using RandomState let pct_unique = set.len() as f64 / limit as f64;
assert!(0.99f64 < pct_unique, "percent-unique={}", pct_unique);
}
#[test] #[should_panic] fn test_add_fails_on_nan() { let a = not_nan(std::f32::INFINITY); let b = not_nan(std::f32::NEG_INFINITY); let _c = a + b;
}
#[test] #[should_panic] fn test_add_fails_on_nan_ref() { let a = not_nan(std::f32::INFINITY); let b = not_nan(std::f32::NEG_INFINITY); let _c = a + &b;
}
#[test] #[should_panic] fn test_add_fails_on_nan_ref_ref() { let a = not_nan(std::f32::INFINITY); let b = not_nan(std::f32::NEG_INFINITY); let _c = &a + &b;
}
#[test] #[should_panic] fn test_add_fails_on_nan_t_ref() { let a = not_nan(std::f32::INFINITY); let b = std::f32::NEG_INFINITY; let _c = a + &b;
}
#[test] #[should_panic] fn test_add_fails_on_nan_ref_t_ref() { let a = not_nan(std::f32::INFINITY); let b = std::f32::NEG_INFINITY; let _c = &a + &b;
}
#[test] #[should_panic] fn test_add_fails_on_nan_ref_t() { let a = not_nan(std::f32::INFINITY); let b = std::f32::NEG_INFINITY; let _c = &a + b;
}
#[test] #[should_panic] fn test_add_assign_fails_on_nan_ref() { letmut a = not_nan(std::f32::INFINITY); let b = not_nan(std::f32::NEG_INFINITY);
a += &b;
}
#[test] #[should_panic] fn test_add_assign_fails_on_nan_t_ref() { letmut a = not_nan(std::f32::INFINITY); let b = std::f32::NEG_INFINITY;
a += &b;
}
#[test] #[should_panic] fn test_add_assign_fails_on_nan_t() { letmut a = not_nan(std::f32::INFINITY); let b = std::f32::NEG_INFINITY;
a += b;
}
#[test] #[should_panic] fn test_sum_fails_on_nan() { let a = not_nan(std::f32::INFINITY); let b = not_nan(std::f32::NEG_INFINITY); let _c: NotNan<_> = [a, b].iter().sum();
}
#[test] #[should_panic] fn test_product_fails_on_nan() { let a = not_nan(std::f32::INFINITY); let b = not_nan(0f32); let _c: NotNan<_> = [a, b].iter().product();
}
#[test] fn not_nan64_sum_product() { let a = not_nan(2138.1237); let b = not_nan(132f64); let c = not_nan(5.1);
assert_eq!(
std::iter::empty::<NotNan<f64>>().sum::<NotNan<_>>(),
NotNan::new(0f64).unwrap()
);
assert_eq!([a].iter().sum::<NotNan<_>>(), a);
assert_eq!([a, b].iter().sum::<NotNan<_>>(), a + b);
assert_eq!([a, b, c].iter().sum::<NotNan<_>>(), a + b + c);
assert_eq!(
std::iter::empty::<NotNan<f64>>().product::<NotNan<_>>(),
NotNan::new(1f64).unwrap()
);
assert_eq!([a].iter().product::<NotNan<_>>(), a);
assert_eq!([a, b].iter().product::<NotNan<_>>(), a * b);
assert_eq!([a, b, c].iter().product::<NotNan<_>>(), a * b * c);
}
#[cfg(feature = "arbitrary")] mod arbitrary_test { usesuper::{NotNan, OrderedFloat}; use arbitrary::{Arbitrary, Unstructured};
#[test] fn exhaustive() { // Exhaustively search all patterns of sign and exponent bits plus a few mantissa bits. for high_bytes in0..=u16::MAX { let [h1, h2] = high_bytes.to_be_bytes();
// Each of these should not // * panic, // * return an error, or // * need more bytes than given. let n32: NotNan<f32> = Unstructured::new(&[h1, h2, h1, h2])
.arbitrary()
.expect("NotNan<f32> failure"); let n64: NotNan<f64> = Unstructured::new(&[h1, h2, h1, h2, h1, h2, h1, h2])
.arbitrary()
.expect("NotNan<f64> failure"); let _: OrderedFloat<f32> = Unstructured::new(&[h1, h2, h1, h2])
.arbitrary()
.expect("OrderedFloat<f32> failure"); let _: OrderedFloat<f64> = Unstructured::new(&[h1, h2, h1, h2, h1, h2, h1, h2])
.arbitrary()
.expect("OrderedFloat<f64> failure");
// Check for violation of NotNan's property of never containing a NaN.
assert!(!n32.into_inner().is_nan());
assert!(!n64.into_inner().is_nan());
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.