Debian Codegraph @

debian/rust-vendor/bigdecimal/src/lib.tests.property-tests.rs

raw
1 // Property tests to be included by lib.rs (if enabled)
2
3
4 mod arithmetic {
5 use super::*;
6 use stdlib::num::NonZeroI128;
7
8
9 macro_rules! impl_test {
10 ($t:ty) => {
11 paste! { proptest! {
12 #[test]
13 fn [< add_ref $t >](n: $t, m: i128, e: i8) {
14 let d = BigDecimal::new(m.into(), e as i64);
15 let sum = n + &d;
16
17 let s1 = &d + n;
18 let s2 = d.clone() + n;
19
20 prop_assert_eq!(&sum, &s1);
21 prop_assert_eq!(&sum, &s2);
22
23 let mut s = d;
24 s += n;
25 prop_assert_eq!(sum, s);
26 }
27
28 #[test]
29 fn [< sub_ $t >](n: $t, m: i128, e: i8) {
30 let d = BigDecimal::new(m.into(), e as i64);
31 let diff_n_d = n - &d;
32 let diff_d_n = d.clone() - n;
33 prop_assert_eq!(&diff_n_d, &diff_d_n.neg());
34
35 let mut a = d.clone();
36 a -= n;
37 prop_assert_eq!(&a, &diff_n_d.neg());
38 }
39
40 #[test]
41 fn [< mul_ $t >](n: $t, m: i128, e: i8) {
42 let d = BigDecimal::new(m.into(), e as i64);
43 let prod_n_d = n * &d;
44 let prod_d_n = d.clone() * n;
45 prop_assert_eq!(&prod_n_d, &prod_d_n);
46
47 let mut r = d.clone();
48 r *= n;
49 prop_assert_eq!(&prod_n_d, &r);
50
51 let r = d.neg() * n;
52 prop_assert_eq!(prod_n_d.neg(), r);
53 }
54
55 #[test]
56 fn [< div_ $t >](n: $t, m: NonZeroI128, e: i8) {
57 let d = BigDecimal::new(m.get().into(), e as i64);
58 let quotient_n_ref_d = n / &d;
59 let quotient_n_d = n / d.clone();
60 prop_assert_eq!(&quotient_n_ref_d, &quotient_n_d);
61
62 let prod = quotient_n_d * &d;
63 let diff = n - &prod;
64 prop_assert!(diff.abs() < BigDecimal::new(1.into(), 60));
65 }
66 } }
67 };
68 (float-div $t:ty) => {
69 paste! { proptest! {
70 #[test]
71 fn [< div_ $t >](n: $t, m: NonZeroI128, e: i8) {
72 let d = BigDecimal::new(m.get().into(), e as i64);
73 let quotient_n_ref_d = n / &d;
74 let quotient_n_d = n / d.clone();
75 prop_assert_eq!(&quotient_n_ref_d, &quotient_n_d);
76
77 let quotient_ref_d_n = &d / n;
78 let quotient_d_n = d.clone() / n;
79 prop_assert_eq!(&quotient_ref_d_n, &quotient_d_n);
80
81 let mut q = d.clone();
82 q /= n;
83 prop_assert_eq!(&q, &quotient_d_n);
84 }
85 } }
86 };
87 }
88
89 impl_test!(u8);
90 impl_test!(u16);
91 impl_test!(u32);
92 impl_test!(u64);
93 impl_test!(u128);
94
95 impl_test!(i8);
96 impl_test!(i16);
97 impl_test!(i32);
98 impl_test!(i64);
99 impl_test!(i128);
100
101 impl_test!(float-div f32);
102 impl_test!(float-div f64);
103
104 proptest! {
105 #[test]
106 fn square(f: f32) {
107 // ignore non-normal numbers
108 prop_assume!(f.is_normal());
109
110 let n: BigDecimal = BigDecimal::from_f32(f).unwrap();
111 let n_times_n = &n * &n;
112
113 prop_assert_eq!(n_times_n, n.square())
114 }
115 }
116 }