Skip to content

Commit b1c02ac

Browse files
committed
Move rem tests to impl_ops_rem.rs
1 parent 135912c commit b1c02ac

File tree

2 files changed

+78
-49
lines changed

2 files changed

+78
-49
lines changed

src/impl_ops_rem.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl<'a> Rem<&'a BigDecimal> for BigDecimal {
3333
BigDecimal::new(result, scale)
3434
}
3535
}
36+
3637
impl<'a> Rem<BigDecimal> for &'a BigDecimal {
3738
type Output = BigDecimal;
3839

@@ -76,3 +77,80 @@ impl<'a, 'b> Rem<&'b BigDecimal> for &'a BigDecimal {
7677
BigDecimal::new(result, scale)
7778
}
7879
}
80+
81+
82+
#[cfg(test)]
83+
mod test {
84+
use super::*;
85+
use paste::paste;
86+
87+
macro_rules! impl_case {
88+
($a:literal % $b:literal => $c:literal ) => {
89+
paste! {
90+
impl_case!([< case_ $a _ $b >]: $a % $b => $c);
91+
}
92+
};
93+
($name:ident: $a:literal % $b:literal => $c:literal ) => {
94+
#[test]
95+
fn $name() {
96+
let mut a: BigDecimal = $a.parse().unwrap();
97+
let b: BigDecimal = $b.parse().unwrap();
98+
let c: BigDecimal = $c.parse().unwrap();
99+
100+
assert_eq!(a.clone() % b.clone(), c);
101+
102+
assert_eq!(a.clone() % &b, c);
103+
assert_eq!(&a % b.clone(), c);
104+
assert_eq!(&a % &b, c);
105+
}
106+
};
107+
}
108+
109+
impl_case!("100" % "5" => "0");
110+
impl_case!("2e1" % "1" => "0");
111+
impl_case!("2" % "1" => "0");
112+
impl_case!("1" % "3" => "1");
113+
impl_case!("1" % "5e-1" => "0");
114+
impl_case!("15e-1" % "1" => "0.5");
115+
impl_case!("1" % "3e-2" => "1e-2");
116+
impl_case!("10" % "3e-3" => "0.001");
117+
impl_case!("3" % "2" => "1");
118+
impl_case!("1234e-2" % "1233e-3" => "0.01");
119+
120+
impl_case!(case_neg3_2: "-3" % "2" => "-1");
121+
impl_case!(case_3_neg2: "3" % "-2" => "1");
122+
impl_case!(case_neg3_neg2: "3" % "-2" => "1");
123+
124+
impl_case!(case_neg95eneg1_515eneg2: "-9.5" % "5.15" => "-4.35");
125+
126+
127+
#[cfg(property_tests)]
128+
mod prop {
129+
use super::*;
130+
use proptest::*;
131+
use num_traits::FromPrimitive;
132+
133+
proptest! {
134+
#[test]
135+
fn quotient_and_remainder(f: f32, g: f32) {
136+
// ignore non-normal numbers
137+
prop_assume!(f.is_normal());
138+
prop_assume!(g.is_normal());
139+
prop_assume!(!g.is_zero());
140+
141+
let (f, g) = if f.abs() > g.abs() {
142+
(f, g)
143+
} else {
144+
(g, f)
145+
};
146+
147+
let a = BigDecimal::from_f32(f).unwrap();
148+
let b = BigDecimal::from_f32(g).unwrap();
149+
150+
let r = &a % &b;
151+
let q = (&a / &b).with_scale(0);
152+
assert_eq!(a, q * b + r);
153+
}
154+
}
155+
}
156+
}

src/lib.rs

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,55 +1763,6 @@ mod bigdecimal_tests {
17631763
assert!(BigDecimal::try_from(f64::NAN).is_err());
17641764
}
17651765

1766-
#[test]
1767-
fn test_rem() {
1768-
let vals = vec![
1769-
("100", "5", "0"),
1770-
("2e1", "1", "0"),
1771-
("2", "1", "0"),
1772-
("1", "3", "1"),
1773-
("1", "0.5", "0"),
1774-
("1.5", "1", "0.5"),
1775-
("1", "3e-2", "1e-2"),
1776-
("10", "0.003", "0.001"),
1777-
("3", "2", "1"),
1778-
("-3", "2", "-1"),
1779-
("3", "-2", "1"),
1780-
("-3", "-2", "-1"),
1781-
("12.34", "1.233", "0.01"),
1782-
];
1783-
for &(x, y, z) in vals.iter() {
1784-
let a = BigDecimal::from_str(x).unwrap();
1785-
let b = BigDecimal::from_str(y).unwrap();
1786-
let c = BigDecimal::from_str(z).unwrap();
1787-
1788-
let rem = &a % &b;
1789-
assert_eq!(rem, c, "{} [&{} % &{}] == {}", rem, a, b, c);
1790-
1791-
let rem = a.clone() % &b;
1792-
assert_eq!(rem, c, "{} [{} % &{}] == {}", rem, a, b, c);
1793-
1794-
let rem = &a % b.clone();
1795-
assert_eq!(rem, c, "{} [&{} % {}] == {}", rem, a, b, c);
1796-
1797-
let rem = a.clone() % b.clone();
1798-
assert_eq!(rem, c, "{} [{} % {}] == {}", rem, a, b, c);
1799-
}
1800-
let vals = vec![
1801-
(("100", -2), ("50", -1), "0"),
1802-
(("100", 0), ("50", -1), "0"),
1803-
(("100", -2), ("30", 0), "10"),
1804-
(("100", 0), ("30", -1), "10"),
1805-
];
1806-
for &((x, xs), (y, ys), z) in vals.iter() {
1807-
let a = BigDecimal::from_str(x).unwrap().with_scale(xs);
1808-
let b = BigDecimal::from_str(y).unwrap().with_scale(ys);
1809-
let c = BigDecimal::from_str(z).unwrap();
1810-
let rem = &a % &b;
1811-
assert_eq!(rem, c, "{} [{} % {}] == {}", rem, a, b, c);
1812-
}
1813-
}
1814-
18151766
#[test]
18161767
fn test_equal() {
18171768
let vals = vec![

0 commit comments

Comments
 (0)