]> git.lizzy.rs Git - rust.git/blobdiff - library/std/src/f64/tests.rs
More lerp tests, altering lerp docs
[rust.git] / library / std / src / f64 / tests.rs
index 3efb2e9d3236cace9a271b56ba4198aefa1911b4..04cb0109261a48068d54261a7c3cb381ed458220 100644 (file)
@@ -756,8 +756,11 @@ fn s_nan() -> f64 {
 
 #[test]
 fn test_lerp_exact() {
+    // simple values
     assert_eq!(f64::lerp(0.0, 2.0, 4.0), 2.0);
     assert_eq!(f64::lerp(1.0, 2.0, 4.0), 4.0);
+
+    // boundary values
     assert_eq!(f64::lerp(0.0, f64::MIN, f64::MAX), f64::MIN);
     assert_eq!(f64::lerp(1.0, f64::MIN, f64::MAX), f64::MAX);
 }
@@ -766,11 +769,42 @@ fn test_lerp_exact() {
 fn test_lerp_consistent() {
     assert_eq!(f64::lerp(f64::MAX, f64::MIN, f64::MIN), f64::MIN);
     assert_eq!(f64::lerp(f64::MIN, f64::MAX, f64::MAX), f64::MAX);
+
+    // as long as t is finite, a/b can be infinite
+    assert_eq!(f64::lerp(f64::MAX, f64::NEG_INFINITY, f64::NEG_INFINITY), f64::NEG_INFINITY);
+    assert_eq!(f64::lerp(f64::MIN, f64::INFINITY, f64::INFINITY), f64::INFINITY);
+}
+
+#[test]
+fn test_lerp_nan_infinite() {
+    // non-finite t is not NaN if a/b different
+    assert!(!f64::lerp(f64::INFINITY, f64::MIN, f64::MAX).is_nan());
+    assert!(!f64::lerp(f64::NEG_INFINITY, f64::MIN, f64::MAX).is_nan());
 }
 
 #[test]
 fn test_lerp_values() {
+    // just a few basic values
     assert_eq!(f64::lerp(0.25, 1.0, 2.0), 1.25);
     assert_eq!(f64::lerp(0.50, 1.0, 2.0), 1.50);
     assert_eq!(f64::lerp(0.75, 1.0, 2.0), 1.75);
 }
+
+#[test]
+fn test_lerp_monotonic() {
+    // near 0
+    let below_zero = f64::lerp(-f64::EPSILON, f64::MIN, f64::MAX);
+    let zero = f64::lerp(0.0, f64::MIN, f64::MAX);
+    let above_zero = f64::lerp(f64::EPSILON, f64::MIN, f64::MAX);
+    assert!(below_zero <= zero);
+    assert!(zero <= above_zero);
+    assert!(below_zero <= above_zero);
+
+    // near 1
+    let below_one = f64::lerp(1.0 - f64::EPSILON, f64::MIN, f64::MAX);
+    let one = f64::lerp(1.0, f64::MIN, f64::MAX);
+    let above_one = f64::lerp(1.0 + f64::EPSILON, f64::MIN, f64::MAX);
+    assert!(below_one <= one);
+    assert!(one <= above_one);
+    assert!(below_one <= above_one);
+}