| 1 |
|
| 2 |
mod impl_div { |
| 3 |
use super::*; |
| 4 |
|
| 5 |
macro_rules! impl_case { |
| 6 |
($name:ident: $numerator:literal / $denominator:literal => $quotient:literal) => { |
| 7 |
#[test] |
| 8 |
fn $name() { |
| 9 |
let a: BigDecimal = $numerator.parse().unwrap(); |
| 10 |
let b: BigDecimal = $denominator.parse().unwrap(); |
| 11 |
let c: BigDecimal = $quotient.parse().unwrap(); |
| 12 |
|
| 13 |
let q = a.clone() / b.clone(); |
| 14 |
assert_eq!(q, c); |
| 15 |
assert_eq!(q.scale, c.scale); |
| 16 |
assert_eq!(a.clone() / &b, c); |
| 17 |
assert_eq!(&a / b.clone(), c); |
| 18 |
assert_eq!(&a / &b, c); |
| 19 |
} |
| 20 |
}; |
| 21 |
} |
| 22 |
|
| 23 |
impl_case!(zero_over_one: "0" / "1" => "0"); |
| 24 |
impl_case!(zero_over_ten: "0" / "10" => "0"); |
| 25 |
impl_case!(two_over_one: "2" / "1" => "2"); |
| 26 |
impl_case!(div_2e1_one: "2e1" / "1" => "2e1"); |
| 27 |
impl_case!(div_10_10: "10" / "10" => "1"); |
| 28 |
impl_case!(div_100_10d0: "100" / "10.0" => "1e1"); |
| 29 |
|
| 30 |
impl_case!(div_20d0_200: "20.0" / "200" => "0.1"); |
| 31 |
impl_case!(div_4_2: "4" / "2" => "2"); |
| 32 |
impl_case!(div_15_3: "15" / "3" => "5"); |
| 33 |
impl_case!(div_1_2: "1" / "2" => "0.5"); |
| 34 |
impl_case!(div_1_2en2: "1" / "2e-2" => "5e1"); |
| 35 |
impl_case!(div_1_0d2: "1" / "0.2" => "5"); |
| 36 |
impl_case!(div_1d0_0d02: "1.0" / "0.02" => "5e1"); |
| 37 |
impl_case!(div_1_0d020: "1" / "0.020" => "5e1"); |
| 38 |
impl_case!(div_1d0_0d020: "1.0" / "0.020" => "5e1"); |
| 39 |
impl_case!(div_5d0_4d00: "5.0" / "4.00" => "1.25"); |
| 40 |
impl_case!(div_5d0_4d000: "5.0" / "4.000" => "1.25"); |
| 41 |
impl_case!(div_5_4d000: "5" / "4.000" => "1.25"); |
| 42 |
impl_case!(div_5_4: "5" / "4" => "125e-2"); |
| 43 |
impl_case!(div_100_5: "100" / "5" => "20"); |
| 44 |
impl_case!(div_n50_5: "-50" / "5" => "-10"); |
| 45 |
impl_case!(div_200_n5: "200" / "-5" => "-40."); |
| 46 |
impl_case!(div_1_3: "1" / "3" => ".3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333"); |
| 47 |
impl_case!(div_n2_n3: "-2" / "-3" => ".6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667"); |
| 48 |
impl_case!(div_n12d34_1d233: "-12.34" / "1.233" => "-10.00811030008110300081103000811030008110300081103000811030008110300081103000811030008110300081103001"); |
| 49 |
impl_case!(div_125348_352d2283: "125348" / "352.2283" => "355.8714617763535752237966114591019517738921035021887792661748076460636467881768727839301952739175132"); |
| 50 |
impl_case!(div_22_7: "22" / "7" => "3.142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857142857143"); |
| 51 |
|
| 52 |
#[test] |
| 53 |
#[should_panic(expected = "Division by zero")] |
| 54 |
fn test_division_by_zero_panics() { |
| 55 |
let x = BigDecimal::from_str("3.14").unwrap(); |
| 56 |
let _r = x / 0; |
| 57 |
} |
| 58 |
|
| 59 |
#[test] |
| 60 |
#[should_panic(expected = "Division by zero")] |
| 61 |
fn test_division_by_zero_panics_v2() { |
| 62 |
let x = BigDecimal::from_str("3.14").unwrap(); |
| 63 |
let _r = x / BigDecimal::zero(); |
| 64 |
} |
| 65 |
|
| 66 |
#[test] |
| 67 |
fn test_division_by_large_number() { |
| 68 |
let n = 1u8; |
| 69 |
let d: BigDecimal = "79437738588056219546528239237352667078".parse().unwrap(); |
| 70 |
|
| 71 |
let quotient_n_ref_d = n / &d; |
| 72 |
let quotient_n_d = n / d.clone(); |
| 73 |
assert_eq!(quotient_n_ref_d, quotient_n_d); |
| 74 |
|
| 75 |
let expected: BigDecimal = "1.258847517281104957975270408416632052090243053529147458917576143852500316808428812104171430669001064E-38".parse().unwrap(); |
| 76 |
assert_eq!(quotient_n_ref_d, expected); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn divide_by_f32_infinity() { |
| 82 |
let num: BigDecimal = "123".parse().unwrap(); |
| 83 |
let quotient = num / f32::INFINITY; |
| 84 |
assert!(quotient.is_zero()); |
| 85 |
} |
| 86 |
|
| 87 |
#[test] |
| 88 |
fn divide_by_f32_nan() { |
| 89 |
let num: BigDecimal = "123".parse().unwrap(); |
| 90 |
let quotient = num / f32::NAN; |
| 91 |
assert!(quotient.is_zero()); |
| 92 |
} |
| 93 |
|
| 94 |
#[test] |
| 95 |
fn divide_by_f64_infinity() { |
| 96 |
let num: BigDecimal = "123".parse().unwrap(); |
| 97 |
let quotient = num / f64::INFINITY; |
| 98 |
assert!(quotient.is_zero()); |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn divide_by_f64_nan() { |
| 103 |
let num: BigDecimal = "123".parse().unwrap(); |
| 104 |
let quotient = num / f64::NAN; |
| 105 |
assert!(quotient.is_zero()); |
| 106 |
} |
| 107 |
|
| 108 |
macro_rules! impl_case { |
| 109 |
($name:ident: num / $denom:literal => $expected:literal) => { |
| 110 |
#[test] |
| 111 |
fn $name() { |
| 112 |
let num = numerator(); |
| 113 |
let den = $denom; |
| 114 |
|
| 115 |
let expected: BigDecimal = $expected.parse().unwrap(); |
| 116 |
|
| 117 |
{ |
| 118 |
let quotient = &num / den; |
| 119 |
assert_eq!("ient, &expected); |
| 120 |
} |
| 121 |
|
| 122 |
let quotient = num / den; |
| 123 |
assert_eq!(quotient, expected); |
| 124 |
} |
| 125 |
}; |
| 126 |
($name:ident: $numer:literal / den => $expected:literal) => { |
| 127 |
#[test] |
| 128 |
fn $name() { |
| 129 |
let num = $numer; |
| 130 |
let den = denominator(); |
| 131 |
|
| 132 |
let expected: BigDecimal = $expected.parse().unwrap(); |
| 133 |
|
| 134 |
{ |
| 135 |
let quotient = num / &den; |
| 136 |
assert_eq!("ient, &expected); |
| 137 |
} |
| 138 |
|
| 139 |
let quotient = num / den; |
| 140 |
assert_eq!(quotient, expected); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
mod dec123_over_float { |
| 146 |
use super::*; |
| 147 |
|
| 148 |
fn numerator() -> BigDecimal { |
| 149 |
"123".parse().unwrap() |
| 150 |
} |
| 151 |
|
| 152 |
impl_case!(divide_by_one: num / 1.0 => "123"); |
| 153 |
impl_case!(divide_by_neg_one: num / -1.0 => "-123"); |
| 154 |
impl_case!(divide_by_two: num / 2.0 => "61.5"); |
| 155 |
impl_case!(divide_by_neg_two: num / -2.0 => "-61.5"); |
| 156 |
impl_case!(divide_by_half: num / 0.25 => "492"); |
| 157 |
|
| 158 |
impl_case!(divide_by_0d3: num / 0.3 => "410.0000000000000151730480032104732806385410274753293198491224660103204812897069364682009036747721645"); |
| 159 |
impl_case!(divide_by_n10d01: num / -10.01 => "-12.28771228771228797395438676755509642184314950109407611037730443717479254091338404424812715057130341"); |
| 160 |
} |
| 161 |
|
| 162 |
mod num_float_over_dec123 { |
| 163 |
use super::*; |
| 164 |
|
| 165 |
fn denominator() -> BigDecimal { |
| 166 |
"123".parse().unwrap() |
| 167 |
} |
| 168 |
|
| 169 |
impl_case!(num_one: 1.0 / den => "0.008130081300813008130081300813008130081300813008130081300813008130081300813008130081300813008130081301"); |
| 170 |
impl_case!(num_123: 123.0 / den => "1"); |
| 171 |
impl_case!(num_676d5: 676.5 / den => "5.5"); |
| 172 |
} |
| 173 |
|