about summary refs log tree commit diff
path: root/library/compiler-builtins/libm/src/math/fabs.rs
diff options
context:
space:
mode:
Diffstat (limited to 'library/compiler-builtins/libm/src/math/fabs.rs')
-rw-r--r--library/compiler-builtins/libm/src/math/fabs.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/library/compiler-builtins/libm/src/math/fabs.rs b/library/compiler-builtins/libm/src/math/fabs.rs
index 5a7f795f6a3..b2255ad32cf 100644
--- a/library/compiler-builtins/libm/src/math/fabs.rs
+++ b/library/compiler-builtins/libm/src/math/fabs.rs
@@ -15,3 +15,27 @@ pub fn fabs(x: f64) -> f64 {
     }
     f64::from_bits(x.to_bits() & (u64::MAX / 2))
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use core::f64::*;
+
+    #[test]
+    fn sanity_check() {
+        assert_eq!(fabs(-1.0), 1.0);
+        assert_eq!(fabs(2.8), 2.8);
+    }
+
+    /// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs
+    #[test]
+    fn spec_tests() {
+        assert!(fabs(NAN).is_nan());
+        for f in [0.0, -0.0].iter().copied() {
+            assert_eq!(fabs(f), 0.0);
+        }
+        for f in [INFINITY, NEG_INFINITY].iter().copied() {
+            assert_eq!(fabs(f), INFINITY);
+        }
+    }
+}