From: Greg Burri Date: Sat, 10 Sep 2022 10:34:07 +0000 (+0200) Subject: Add tests to Q7 type. X-Git-Url: http://git.euphorik.ch/?a=commitdiff_plain;h=7e189ee78a25b4ed2e19875979b9e1e0deee7927;p=rust_in_action.git Add tests to Q7 type. --- diff --git a/ch5-data-in-depth/src/q7.rs b/ch5-data-in-depth/src/q7.rs index 63ad0dc..38c2ae7 100644 --- a/ch5-data-in-depth/src/q7.rs +++ b/ch5-data-in-depth/src/q7.rs @@ -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