Add tests to Q7 type.
authorGreg Burri <greg.burri@gmail.com>
Sat, 10 Sep 2022 10:34:07 +0000 (12:34 +0200)
committerGreg Burri <greg.burri@gmail.com>
Sat, 10 Sep 2022 10:34:07 +0000 (12:34 +0200)
ch5-data-in-depth/src/q7.rs

index 63ad0dc..38c2ae7 100644 (file)
@@ -40,4 +40,37 @@ mod tests {
         let a = 0.45;
         assert_eq!(a, (f64::from(Q7::from(a)) * 100.0).round() / 100.0);
     }
+
+    #[test]
+    fn out_of_bounds() {
+        assert_eq!(Q7::from(10.), Q7::from(1.));
+        assert_eq!(Q7::from(-10.), Q7::from(-1.));
+    }
+
+    #[test]
+    fn f32_to_q7() {
+        let n1 = 0.7f32;
+        let q1 = Q7::from(n1);
+
+        let n2 = -0.4;
+        let q2 = Q7::from(n2);
+
+        let n3 = 123.0;
+        let q3 = Q7::from(n3);
+
+        assert_eq!(q1, Q7(89));
+        assert_eq!(q2, Q7(-51));
+        assert_eq!(q3, Q7(127));
+    }
+
+    #[test]
+    fn q7_to_f32() {
+        let q1 = Q7::from(0.7);
+        let n1 = f32::from(q1);
+        assert_eq!(n1, 0.6953125);
+
+        let q2 = Q7::from(n1);
+        let n2 = f32::from(q2);
+        assert_eq!(n1, n2);
+    }
 }
\ No newline at end of file