| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use core::ops::RangeInclusive; |
| 6 |
use fixed_decimal::Decimal; |
| 7 |
use fixed_decimal::Sign; |
| 8 |
use fixed_decimal::SignedRoundingMode as SRM; |
| 9 |
use fixed_decimal::UnsignedRoundingMode as URM; |
| 10 |
use writeable::Writeable; |
| 11 |
|
| 12 |
#[test] |
| 13 |
pub fn test_ecma402_table() { |
| 14 |
// Source: <https://tc39.es/ecma402/#table-intl-rounding-modes> |
| 15 |
let cases: [(_, _, _, _, _, _, _); 9] = [ |
| 16 |
("ceil", SRM::Ceil, -1, 1, 1, 1, 2), |
| 17 |
("floor", SRM::Floor, -2, 0, 0, 0, 1), |
| 18 |
("expand", SRM::Unsigned(URM::Expand), -2, 1, 1, 1, 2), |
| 19 |
("trunc", SRM::Unsigned(URM::Trunc), -1, 0, 0, 0, 1), |
| 20 |
("half_ceil", SRM::HalfCeil, -1, 0, 1, 1, 2), |
| 21 |
("half_floor", SRM::HalfFloor, -2, 0, 0, 1, 1), |
| 22 |
( |
| 23 |
"half_expand", |
| 24 |
SRM::Unsigned(URM::HalfExpand), |
| 25 |
-2, |
| 26 |
0, |
| 27 |
1, |
| 28 |
1, |
| 29 |
2, |
| 30 |
), |
| 31 |
("half_trunc", SRM::Unsigned(URM::HalfTrunc), -1, 0, 0, 1, 1), |
| 32 |
("half_even", SRM::Unsigned(URM::HalfEven), -2, 0, 0, 1, 2), |
| 33 |
]; |
| 34 |
for (name, mode, e1, e2, e3, e4, e5) in cases { |
| 35 |
let mut fd1: Decimal = "-1.5".parse().unwrap(); |
| 36 |
let mut fd2: Decimal = "0.4".parse().unwrap(); |
| 37 |
let mut fd3: Decimal = "0.5".parse().unwrap(); |
| 38 |
let mut fd4: Decimal = "0.6".parse().unwrap(); |
| 39 |
let mut fd5: Decimal = "1.5".parse().unwrap(); |
| 40 |
fd1.round_with_mode(0, mode); |
| 41 |
fd2.round_with_mode(0, mode); |
| 42 |
fd3.round_with_mode(0, mode); |
| 43 |
fd4.round_with_mode(0, mode); |
| 44 |
fd5.round_with_mode(0, mode); |
| 45 |
assert_eq!( |
| 46 |
fd1.write_to_string(), |
| 47 |
e1.write_to_string(), |
| 48 |
"-1.5 failed for {name}" |
| 49 |
); |
| 50 |
assert_eq!( |
| 51 |
fd2.write_to_string(), |
| 52 |
e2.write_to_string(), |
| 53 |
"0.4 failed for {name}" |
| 54 |
); |
| 55 |
assert_eq!( |
| 56 |
fd3.write_to_string(), |
| 57 |
e3.write_to_string(), |
| 58 |
"0.5 failed for {name}" |
| 59 |
); |
| 60 |
assert_eq!( |
| 61 |
fd4.write_to_string(), |
| 62 |
e4.write_to_string(), |
| 63 |
"0.6 failed for {name}" |
| 64 |
); |
| 65 |
assert_eq!( |
| 66 |
fd5.write_to_string(), |
| 67 |
e5.write_to_string(), |
| 68 |
"1.5 failed for {name}" |
| 69 |
); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
pub fn test_within_ranges() { |
| 75 |
struct TestCase { |
| 76 |
rounding_mode_name: &'static str, |
| 77 |
rounding_mode: SRM, |
| 78 |
range_n2000: RangeInclusive<i32>, |
| 79 |
range_n1000: RangeInclusive<i32>, |
| 80 |
range_0: RangeInclusive<i32>, |
| 81 |
range_1000: RangeInclusive<i32>, |
| 82 |
range_2000: RangeInclusive<i32>, |
| 83 |
} |
| 84 |
let cases: [TestCase; 9] = [ |
| 85 |
TestCase { |
| 86 |
rounding_mode_name: "ceil", |
| 87 |
rounding_mode: SRM::Ceil, |
| 88 |
range_n2000: -2999..=-2000, |
| 89 |
range_n1000: -1999..=-1000, |
| 90 |
range_0: -999..=0, |
| 91 |
range_1000: 1..=1000, |
| 92 |
range_2000: 1001..=2000, |
| 93 |
}, |
| 94 |
TestCase { |
| 95 |
rounding_mode_name: "floor", |
| 96 |
rounding_mode: SRM::Floor, |
| 97 |
range_n2000: -2000..=-1001, |
| 98 |
range_n1000: -1000..=-1, |
| 99 |
range_0: 0..=999, |
| 100 |
range_1000: 1000..=1999, |
| 101 |
range_2000: 2000..=2999, |
| 102 |
}, |
| 103 |
TestCase { |
| 104 |
rounding_mode_name: "expand", |
| 105 |
rounding_mode: SRM::Unsigned(URM::Expand), |
| 106 |
range_n2000: -2000..=-1001, |
| 107 |
range_n1000: -1000..=-1, |
| 108 |
range_0: 0..=0, |
| 109 |
range_1000: 1..=1000, |
| 110 |
range_2000: 1001..=2000, |
| 111 |
}, |
| 112 |
TestCase { |
| 113 |
rounding_mode_name: "trunc", |
| 114 |
rounding_mode: SRM::Unsigned(URM::Trunc), |
| 115 |
range_n2000: -2999..=-2000, |
| 116 |
range_n1000: -1999..=-1000, |
| 117 |
range_0: -999..=999, |
| 118 |
range_1000: 1000..=1999, |
| 119 |
range_2000: 2000..=2999, |
| 120 |
}, |
| 121 |
TestCase { |
| 122 |
rounding_mode_name: "half_ceil", |
| 123 |
rounding_mode: SRM::HalfCeil, |
| 124 |
range_n2000: -2500..=-1501, |
| 125 |
range_n1000: -1500..=-501, |
| 126 |
range_0: -500..=449, |
| 127 |
range_1000: 500..=1449, |
| 128 |
range_2000: 1500..=2449, |
| 129 |
}, |
| 130 |
TestCase { |
| 131 |
rounding_mode_name: "half_floor", |
| 132 |
rounding_mode: SRM::HalfFloor, |
| 133 |
range_n2000: -2449..=-1500, |
| 134 |
range_n1000: -1449..=-500, |
| 135 |
range_0: -449..=500, |
| 136 |
range_1000: 501..=1500, |
| 137 |
range_2000: 1501..=2500, |
| 138 |
}, |
| 139 |
TestCase { |
| 140 |
rounding_mode_name: "half_expand", |
| 141 |
rounding_mode: SRM::Unsigned(URM::HalfExpand), |
| 142 |
range_n2000: -2449..=-1500, |
| 143 |
range_n1000: -1449..=-500, |
| 144 |
range_0: -449..=449, |
| 145 |
range_1000: 500..=1449, |
| 146 |
range_2000: 1500..=2449, |
| 147 |
}, |
| 148 |
TestCase { |
| 149 |
rounding_mode_name: "half_trunc", |
| 150 |
rounding_mode: SRM::Unsigned(URM::HalfTrunc), |
| 151 |
range_n2000: -2500..=-1501, |
| 152 |
range_n1000: -1500..=-501, |
| 153 |
range_0: -500..=500, |
| 154 |
range_1000: 501..=1500, |
| 155 |
range_2000: 1501..=2500, |
| 156 |
}, |
| 157 |
TestCase { |
| 158 |
rounding_mode_name: "half_even", |
| 159 |
rounding_mode: SRM::Unsigned(URM::HalfEven), |
| 160 |
range_n2000: -2500..=-1500, |
| 161 |
range_n1000: -1449..=-501, |
| 162 |
range_0: -500..=500, |
| 163 |
range_1000: 501..=1449, |
| 164 |
range_2000: 1500..=2500, |
| 165 |
}, |
| 166 |
]; |
| 167 |
for TestCase { |
| 168 |
rounding_mode_name, |
| 169 |
rounding_mode, |
| 170 |
range_n2000, |
| 171 |
range_n1000, |
| 172 |
range_0, |
| 173 |
range_1000, |
| 174 |
range_2000, |
| 175 |
} in cases |
| 176 |
{ |
| 177 |
for n in range_n2000 { |
| 178 |
let mut fd = Decimal::from(n); |
| 179 |
fd.round_with_mode(3, rounding_mode); |
| 180 |
assert_eq!(fd.write_to_string(), "-2000", "{rounding_mode_name}: {n}"); |
| 181 |
let mut fd = Decimal::from(n - 1000000); |
| 182 |
fd.multiply_pow10(-5); |
| 183 |
fd.round_with_mode(-2, rounding_mode); |
| 184 |
assert_eq!( |
| 185 |
fd.write_to_string(), |
| 186 |
"-10.02", |
| 187 |
"{rounding_mode_name}: {n} ÷ 10^5 ± 10" |
| 188 |
); |
| 189 |
} |
| 190 |
for n in range_n1000 { |
| 191 |
let mut fd = Decimal::from(n); |
| 192 |
fd.round_with_mode(3, rounding_mode); |
| 193 |
assert_eq!(fd.write_to_string(), "-1000", "{rounding_mode_name}: {n}"); |
| 194 |
let mut fd = Decimal::from(n - 1000000); |
| 195 |
fd.multiply_pow10(-5); |
| 196 |
fd.round_with_mode(-2, rounding_mode); |
| 197 |
assert_eq!( |
| 198 |
fd.write_to_string(), |
| 199 |
"-10.01", |
| 200 |
"{rounding_mode_name}: {n} ÷ 10^5 ± 10" |
| 201 |
); |
| 202 |
} |
| 203 |
for n in range_0 { |
| 204 |
let mut fd = Decimal::from(n); |
| 205 |
fd.round_with_mode(3, rounding_mode); |
| 206 |
fd.set_sign(Sign::None); // get rid of -0 |
| 207 |
assert_eq!(fd.write_to_string(), "000", "{rounding_mode_name}: {n}"); |
| 208 |
|
| 209 |
let (mut fd, expected) = if n < 0 { |
| 210 |
( |
| 211 |
{ |
| 212 |
let mut fd = Decimal::from(n - 1000000); |
| 213 |
fd.multiply_pow10(-5); |
| 214 |
fd |
| 215 |
}, |
| 216 |
"-10.00", |
| 217 |
) |
| 218 |
} else { |
| 219 |
( |
| 220 |
{ |
| 221 |
let mut fd = Decimal::from(n + 1000000); |
| 222 |
fd.multiply_pow10(-5); |
| 223 |
fd |
| 224 |
}, |
| 225 |
"10.00", |
| 226 |
) |
| 227 |
}; |
| 228 |
fd.round_with_mode(-2, rounding_mode); |
| 229 |
assert_eq!( |
| 230 |
fd.write_to_string(), |
| 231 |
expected, |
| 232 |
"{rounding_mode_name}: {n} ÷ 10^5 ± 10" |
| 233 |
); |
| 234 |
} |
| 235 |
for n in range_1000 { |
| 236 |
let mut fd = Decimal::from(n); |
| 237 |
fd.round_with_mode(3, rounding_mode); |
| 238 |
assert_eq!(fd.write_to_string(), "1000", "{rounding_mode_name}: {n}"); |
| 239 |
let mut fd = Decimal::from(n + 1000000); |
| 240 |
fd.multiply_pow10(-5); |
| 241 |
fd.round_with_mode(-2, rounding_mode); |
| 242 |
assert_eq!( |
| 243 |
fd.write_to_string(), |
| 244 |
"10.01", |
| 245 |
"{rounding_mode_name}: {n} ÷ 10^5 ± 10" |
| 246 |
); |
| 247 |
} |
| 248 |
for n in range_2000 { |
| 249 |
let mut fd = Decimal::from(n); |
| 250 |
fd.round_with_mode(3, rounding_mode); |
| 251 |
assert_eq!(fd.write_to_string(), "2000", "{rounding_mode_name}: {n}"); |
| 252 |
let mut fd = Decimal::from(n + 1000000); |
| 253 |
fd.multiply_pow10(-5); |
| 254 |
fd.round_with_mode(-2, rounding_mode); |
| 255 |
assert_eq!( |
| 256 |
fd.write_to_string(), |
| 257 |
"10.02", |
| 258 |
"{rounding_mode_name}: {n} ÷ 10^5 ± 10" |
| 259 |
); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
#[test] |
| 265 |
pub fn extra_rounding_mode_cases() { |
| 266 |
struct TestCase { |
| 267 |
input: &'static str, |
| 268 |
position: i16, |
| 269 |
// ceil, floor, expand, trunc, half_ceil, half_floor, half_expand, half_trunc, half_even |
| 270 |
all_expected: [&'static str; 9], |
| 271 |
} |
| 272 |
let cases: [TestCase; 8] = [ |
| 273 |
TestCase { |
| 274 |
input: "505.050", |
| 275 |
position: -3, |
| 276 |
all_expected: [ |
| 277 |
"505.050", "505.050", "505.050", "505.050", "505.050", "505.050", "505.050", |
| 278 |
"505.050", "505.050", |
| 279 |
], |
| 280 |
}, |
| 281 |
TestCase { |
| 282 |
input: "505.050", |
| 283 |
position: -2, |
| 284 |
all_expected: [ |
| 285 |
"505.05", "505.05", "505.05", "505.05", "505.05", "505.05", "505.05", "505.05", |
| 286 |
"505.05", |
| 287 |
], |
| 288 |
}, |
| 289 |
TestCase { |
| 290 |
input: "505.050", |
| 291 |
position: -1, |
| 292 |
all_expected: [ |
| 293 |
"505.1", "505.0", "505.1", "505.0", "505.1", "505.0", "505.1", "505.0", "505.0", |
| 294 |
], |
| 295 |
}, |
| 296 |
TestCase { |
| 297 |
input: "505.050", |
| 298 |
position: 0, |
| 299 |
all_expected: [ |
| 300 |
"506", "505", "506", "505", "505", "505", "505", "505", "505", |
| 301 |
], |
| 302 |
}, |
| 303 |
TestCase { |
| 304 |
input: "505.050", |
| 305 |
position: 1, |
| 306 |
all_expected: [ |
| 307 |
"510", "500", "510", "500", "510", "510", "510", "510", "510", |
| 308 |
], |
| 309 |
}, |
| 310 |
TestCase { |
| 311 |
input: "505.050", |
| 312 |
position: 2, |
| 313 |
all_expected: [ |
| 314 |
"600", "500", "600", "500", "500", "500", "500", "500", "500", |
| 315 |
], |
| 316 |
}, |
| 317 |
TestCase { |
| 318 |
input: "505.050", |
| 319 |
position: 3, |
| 320 |
all_expected: [ |
| 321 |
"1000", "000", "1000", "000", "1000", "1000", "1000", "1000", "1000", |
| 322 |
], |
| 323 |
}, |
| 324 |
TestCase { |
| 325 |
input: "505.050", |
| 326 |
position: 4, |
| 327 |
all_expected: [ |
| 328 |
"10000", "0000", "10000", "0000", "0000", "0000", "0000", "0000", "0000", |
| 329 |
], |
| 330 |
}, |
| 331 |
]; |
| 332 |
let rounding_modes: [(&'static str, SRM); 9] = [ |
| 333 |
("ceil", SRM::Ceil), |
| 334 |
("floor", SRM::Floor), |
| 335 |
("expand", SRM::Unsigned(URM::Expand)), |
| 336 |
("trunc", SRM::Unsigned(URM::Trunc)), |
| 337 |
("half_ceil", SRM::HalfCeil), |
| 338 |
("half_floor", SRM::HalfFloor), |
| 339 |
("half_expand", SRM::Unsigned(URM::HalfExpand)), |
| 340 |
("half_trunc", SRM::Unsigned(URM::HalfTrunc)), |
| 341 |
("half_even", SRM::Unsigned(URM::HalfEven)), |
| 342 |
]; |
| 343 |
for TestCase { |
| 344 |
input, |
| 345 |
position, |
| 346 |
all_expected, |
| 347 |
} in cases |
| 348 |
{ |
| 349 |
for ((rounding_mode_name, rounding_mode), expected) in |
| 350 |
rounding_modes.iter().zip(all_expected.iter()) |
| 351 |
{ |
| 352 |
let mut fd: Decimal = input.parse().unwrap(); |
| 353 |
fd.round_with_mode(position, *rounding_mode); |
| 354 |
assert_eq!( |
| 355 |
&*fd.write_to_string(), |
| 356 |
*expected, |
| 357 |
"{input}: {rounding_mode_name} @ {position}" |
| 358 |
) |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
#[test] |
| 364 |
pub fn test_ecma402_table_with_increments() { |
| 365 |
use fixed_decimal::RoundingIncrement; |
| 366 |
|
| 367 |
#[rustfmt::skip] // Don't split everything on its own line. Makes it look a lot nicer. |
| 368 |
let cases: [(_, _, [(_, _, _, _, _, _, _); 9]); 3] = [ |
| 369 |
("two", RoundingIncrement::MultiplesOf2, [ |
| 370 |
("ceil", SRM::Ceil, "-1.4", "0.4", "0.6", "0.6", "1.6"), |
| 371 |
("floor", SRM::Floor, "-1.6", "0.4", "0.4", "0.6", "1.4"), |
| 372 |
("expand", SRM::Unsigned(URM::Expand), "-1.6", "0.4", "0.6", "0.6", "1.6"), |
| 373 |
("trunc", SRM::Unsigned(URM::Trunc), "-1.4", "0.4", "0.4", "0.6", "1.4"), |
| 374 |
("half_ceil", SRM::HalfCeil, "-1.4", "0.4", "0.6", "0.6", "1.6"), |
| 375 |
("half_floor", SRM::HalfFloor, "-1.6", "0.4", "0.4", "0.6", "1.4"), |
| 376 |
("half_expand", SRM::Unsigned(URM::HalfExpand), "-1.6", "0.4", "0.6", "0.6", "1.6"), |
| 377 |
("half_trunc", SRM::Unsigned(URM::HalfTrunc), "-1.4", "0.4", "0.4", "0.6", "1.4"), |
| 378 |
("half_even", SRM::Unsigned(URM::HalfEven), "-1.6", "0.4", "0.4", "0.6", "1.6"), |
| 379 |
]), |
| 380 |
("five", RoundingIncrement::MultiplesOf5, [ |
| 381 |
("ceil", SRM::Ceil, "-1.5", "0.5", "0.5", "1.0", "1.5"), |
| 382 |
("floor", SRM::Floor, "-1.5", "0.0", "0.5", "0.5", "1.5"), |
| 383 |
("expand", SRM::Unsigned(URM::Expand), "-1.5", "0.5", "0.5", "1.0", "1.5"), |
| 384 |
("trunc", SRM::Unsigned(URM::Trunc), "-1.5", "0.0", "0.5", "0.5", "1.5"), |
| 385 |
("half_ceil", SRM::HalfCeil, "-1.5", "0.5", "0.5", "0.5", "1.5"), |
| 386 |
("half_floor", SRM::HalfFloor, "-1.5", "0.5", "0.5", "0.5", "1.5"), |
| 387 |
("half_expand", SRM::Unsigned(URM::HalfExpand), "-1.5", "0.5", "0.5", "0.5", "1.5"), |
| 388 |
("half_trunc", SRM::Unsigned(URM::HalfTrunc), "-1.5", "0.5", "0.5", "0.5", "1.5"), |
| 389 |
("half_even", SRM::Unsigned(URM::HalfEven), "-1.5", "0.5", "0.5", "0.5", "1.5"), |
| 390 |
]), |
| 391 |
("twenty-five", RoundingIncrement::MultiplesOf25, [ |
| 392 |
("ceil", SRM::Ceil, "-0.0", "2.5", "2.5", "2.5", "2.5"), |
| 393 |
("floor", SRM::Floor, "-2.5", "0.0", "0.0", "0.0", "0.0"), |
| 394 |
("expand", SRM::Unsigned(URM::Expand), "-2.5", "2.5", "2.5", "2.5", "2.5"), |
| 395 |
("trunc", SRM::Unsigned(URM::Trunc), "-0.0", "0.0", "0.0", "0.0", "0.0"), |
| 396 |
("half_ceil", SRM::HalfCeil, "-2.5", "0.0", "0.0", "0.0", "2.5"), |
| 397 |
("half_floor", SRM::HalfFloor, "-2.5", "0.0", "0.0", "0.0", "2.5"), |
| 398 |
("half_expand", SRM::Unsigned(URM::HalfExpand), "-2.5", "0.0", "0.0", "0.0", "2.5"), |
| 399 |
("half_trunc", SRM::Unsigned(URM::HalfTrunc), "-2.5", "0.0", "0.0", "0.0", "2.5"), |
| 400 |
("half_even", SRM::Unsigned(URM::HalfEven), "-2.5", "0.0", "0.0", "0.0", "2.5"), |
| 401 |
]), |
| 402 |
]; |
| 403 |
|
| 404 |
for (increment_str, increment, cases) in cases { |
| 405 |
for (rounding_mode_name, rounding_mode, e1, e2, e3, e4, e5) in cases { |
| 406 |
let mut fd1: Decimal = "-1.5".parse().unwrap(); |
| 407 |
let mut fd2: Decimal = "0.4".parse().unwrap(); |
| 408 |
let mut fd3: Decimal = "0.5".parse().unwrap(); |
| 409 |
let mut fd4: Decimal = "0.6".parse().unwrap(); |
| 410 |
let mut fd5: Decimal = "1.5".parse().unwrap(); |
| 411 |
// The original ECMA-402 table tests rounding at magnitude 0. |
| 412 |
// However, testing rounding at magnitude -1 gives more |
| 413 |
// interesting test cases for increments. |
| 414 |
fd1.round_with_mode_and_increment(-1, rounding_mode, increment); |
| 415 |
fd2.round_with_mode_and_increment(-1, rounding_mode, increment); |
| 416 |
fd3.round_with_mode_and_increment(-1, rounding_mode, increment); |
| 417 |
fd4.round_with_mode_and_increment(-1, rounding_mode, increment); |
| 418 |
fd5.round_with_mode_and_increment(-1, rounding_mode, increment); |
| 419 |
assert_eq!( |
| 420 |
fd1.write_to_string(), |
| 421 |
e1, |
| 422 |
"-1.5 failed for {rounding_mode_name} with increments of {increment_str}" |
| 423 |
); |
| 424 |
assert_eq!( |
| 425 |
fd2.write_to_string(), |
| 426 |
e2, |
| 427 |
"0.4 failed for {rounding_mode_name} with increments of {increment_str}" |
| 428 |
); |
| 429 |
assert_eq!( |
| 430 |
fd3.write_to_string(), |
| 431 |
e3, |
| 432 |
"0.5 failed for {rounding_mode_name} with increments of {increment_str}" |
| 433 |
); |
| 434 |
assert_eq!( |
| 435 |
fd4.write_to_string(), |
| 436 |
e4, |
| 437 |
"0.6 failed for {rounding_mode_name} with increments of {increment_str}" |
| 438 |
); |
| 439 |
assert_eq!( |
| 440 |
fd5.write_to_string(), |
| 441 |
e5, |
| 442 |
"1.5 failed for {rounding_mode_name} with increments of {increment_str}" |
| 443 |
); |
| 444 |
} |
| 445 |
} |
| 446 |
} |