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