| 1 |
|
| 2 |
#[allow(non_snake_case)] |
| 3 |
mod test_round_pair { |
| 4 |
use paste::paste; |
| 5 |
use super::*; |
| 6 |
|
| 7 |
macro_rules! impl_test { |
| 8 |
( $($mode:ident),+ => $expected:literal) => { |
| 9 |
$( |
| 10 |
paste! { |
| 11 |
#[test] |
| 12 |
fn [< mode_ $mode >]() { |
| 13 |
let (pair, sign, trailing_zeros) = test_input(); |
| 14 |
let mode = self::RoundingMode::$mode; |
| 15 |
let result = mode.round_pair(sign, pair, trailing_zeros); |
| 16 |
assert_eq!(result, $expected); |
| 17 |
} |
| 18 |
} |
| 19 |
)* |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
macro_rules! define_test_input { |
| 24 |
( - $lhs:literal . $rhs:literal $($t:tt)* ) => { |
| 25 |
define_test_input!(sign=Sign::Minus, pair=($lhs, $rhs), $($t)*); |
| 26 |
}; |
| 27 |
( $lhs:literal . $rhs:literal $($t:tt)*) => { |
| 28 |
define_test_input!(sign=Sign::Plus, pair=($lhs, $rhs), $($t)*); |
| 29 |
}; |
| 30 |
( sign=$sign:expr, pair=$pair:expr, ) => { |
| 31 |
define_test_input!(sign=$sign, pair=$pair, trailing_zeros=true); |
| 32 |
}; |
| 33 |
( sign=$sign:expr, pair=$pair:expr, 000x ) => { |
| 34 |
define_test_input!(sign=$sign, pair=$pair, trailing_zeros=false); |
| 35 |
}; |
| 36 |
( sign=$sign:expr, pair=$pair:expr, trailing_zeros=$trailing_zeros:literal ) => { |
| 37 |
fn test_input() -> ((u8, u8), Sign, bool) { ($pair, $sign, $trailing_zeros) } |
| 38 |
}; |
| 39 |
} |
| 40 |
|
| 41 |
mod case_0_1 { |
| 42 |
use super::*; |
| 43 |
|
| 44 |
define_test_input!(0 . 1); |
| 45 |
|
| 46 |
impl_test!(Up, Ceiling => 1); |
| 47 |
impl_test!(Down, Floor, HalfUp, HalfDown, HalfEven => 0); |
| 48 |
} |
| 49 |
|
| 50 |
mod case_neg_0_1 { |
| 51 |
use super::*; |
| 52 |
|
| 53 |
define_test_input!(-0 . 1); |
| 54 |
|
| 55 |
impl_test!(Up, Floor => 1); |
| 56 |
impl_test!(Down, Ceiling, HalfUp, HalfDown, HalfEven => 0); |
| 57 |
} |
| 58 |
|
| 59 |
mod case_0_5 { |
| 60 |
use super::*; |
| 61 |
|
| 62 |
define_test_input!( 0 . 5 ); |
| 63 |
|
| 64 |
impl_test!(Up, Ceiling, HalfUp => 1); |
| 65 |
impl_test!(Down, Floor, HalfDown, HalfEven => 0); |
| 66 |
} |
| 67 |
|
| 68 |
mod case_neg_0_5 { |
| 69 |
use super::*; |
| 70 |
|
| 71 |
define_test_input!(-0 . 5); |
| 72 |
|
| 73 |
impl_test!(Up, Floor, HalfUp => 1); |
| 74 |
impl_test!(Down, Ceiling, HalfDown, HalfEven => 0); |
| 75 |
} |
| 76 |
|
| 77 |
mod case_0_5_000x { |
| 78 |
use super::*; |
| 79 |
|
| 80 |
// ...000x indicates a non-zero trailing digit; affects behavior of rounding N.0 and N.5 |
| 81 |
define_test_input!(0 . 5 000x); |
| 82 |
|
| 83 |
impl_test!(Up, Ceiling, HalfUp, HalfDown, HalfEven => 1); |
| 84 |
impl_test!(Down, Floor => 0); |
| 85 |
} |
| 86 |
|
| 87 |
mod case_neg_0_5_000x { |
| 88 |
use super::*; |
| 89 |
|
| 90 |
define_test_input!(-0 . 5 000x); |
| 91 |
|
| 92 |
impl_test!(Up, Floor, HalfUp, HalfDown, HalfEven => 1); |
| 93 |
impl_test!(Down, Ceiling => 0); |
| 94 |
} |
| 95 |
|
| 96 |
mod case_0_7 { |
| 97 |
use super::*; |
| 98 |
|
| 99 |
define_test_input!(0 . 7); |
| 100 |
|
| 101 |
impl_test!(Up, Ceiling, HalfUp, HalfDown, HalfEven => 1); |
| 102 |
impl_test!(Down, Floor => 0); |
| 103 |
} |
| 104 |
|
| 105 |
mod case_neg_0_7 { |
| 106 |
use super::*; |
| 107 |
|
| 108 |
define_test_input!(-0 . 7); |
| 109 |
|
| 110 |
impl_test!(Up, Floor, HalfUp, HalfDown, HalfEven => 1); |
| 111 |
impl_test!(Down, Ceiling => 0); |
| 112 |
} |
| 113 |
|
| 114 |
mod case_neg_4_3_000x { |
| 115 |
use super::*; |
| 116 |
|
| 117 |
define_test_input!(-4 . 3 000x); |
| 118 |
|
| 119 |
impl_test!(Up, Floor => 5); |
| 120 |
impl_test!(Down, Ceiling, HalfUp, HalfDown, HalfEven => 4); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
mod case_9_5_000x { |
| 125 |
use super::*; |
| 126 |
|
| 127 |
define_test_input!(9 . 5 000x); |
| 128 |
|
| 129 |
impl_test!(Up, Ceiling, HalfDown, HalfUp, HalfEven => 10); |
| 130 |
impl_test!(Down, Floor => 9); |
| 131 |
} |
| 132 |
|
| 133 |
mod case_9_5 { |
| 134 |
use super::*; |
| 135 |
|
| 136 |
define_test_input!(9 . 5); |
| 137 |
|
| 138 |
impl_test!(Up, Ceiling, HalfUp, HalfEven => 10); |
| 139 |
impl_test!(Down, Floor, HalfDown => 9); |
| 140 |
} |
| 141 |
|
| 142 |
mod case_8_5 { |
| 143 |
use super::*; |
| 144 |
|
| 145 |
define_test_input!(8 . 5); |
| 146 |
|
| 147 |
impl_test!(Up, Ceiling, HalfUp => 9); |
| 148 |
impl_test!(Down, Floor, HalfDown, HalfEven => 8); |
| 149 |
} |
| 150 |
|
| 151 |
mod case_neg_6_5 { |
| 152 |
use super::*; |
| 153 |
|
| 154 |
define_test_input!(-6 . 5); |
| 155 |
|
| 156 |
impl_test!(Up, Floor, HalfUp => 7); |
| 157 |
impl_test!(Down, Ceiling, HalfDown, HalfEven => 6); |
| 158 |
} |
| 159 |
|
| 160 |
mod case_neg_6_5_000x { |
| 161 |
use super::*; |
| 162 |
|
| 163 |
define_test_input!(-6 . 5 000x); |
| 164 |
|
| 165 |
impl_test!(Up, Floor, HalfUp, HalfDown, HalfEven => 7); |
| 166 |
impl_test!(Down, Ceiling => 6); |
| 167 |
} |
| 168 |
|
| 169 |
mod case_3_0 { |
| 170 |
use super::*; |
| 171 |
|
| 172 |
define_test_input!(3 . 0); |
| 173 |
|
| 174 |
impl_test!(Up, Down, Ceiling, Floor, HalfUp, HalfDown, HalfEven => 3); |
| 175 |
} |
| 176 |
|
| 177 |
mod case_3_0_000x { |
| 178 |
use super::*; |
| 179 |
|
| 180 |
define_test_input!(3 . 0 000x); |
| 181 |
|
| 182 |
impl_test!(Up, Ceiling => 4); |
| 183 |
impl_test!(Down, Floor, HalfUp, HalfDown, HalfEven => 3); |
| 184 |
} |
| 185 |
|
| 186 |
mod case_neg_2_0 { |
| 187 |
use super::*; |
| 188 |
|
| 189 |
define_test_input!(-2 . 0); |
| 190 |
|
| 191 |
impl_test!(Up, Down, Ceiling, Floor, HalfUp, HalfDown, HalfEven => 2); |
| 192 |
} |
| 193 |
|
| 194 |
mod case_neg_2_0_000x { |
| 195 |
use super::*; |
| 196 |
|
| 197 |
define_test_input!(-2 . 0 000x); |
| 198 |
|
| 199 |
impl_test!(Up, Floor => 3); |
| 200 |
impl_test!(Down, Ceiling, HalfUp, HalfDown, HalfEven => 2); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
#[cfg(test)] |
| 206 |
#[allow(non_snake_case)] |
| 207 |
mod test_round_u32 { |
| 208 |
use paste::paste; |
| 209 |
use super::*; |
| 210 |
|
| 211 |
macro_rules! impl_test { |
| 212 |
( $pos:literal :: $($mode:ident),+ => $expected:literal) => { |
| 213 |
$( |
| 214 |
paste! { |
| 215 |
#[test] |
| 216 |
fn [< digit_ $pos _mode_ $mode >]() { |
| 217 |
let (value, sign, trailing_zeros) = test_input(); |
| 218 |
let mode = self::RoundingMode::$mode; |
| 219 |
let pos = stdlib::num::NonZeroU8::new($pos as u8).unwrap(); |
| 220 |
let result = mode.round_u32(pos, sign, value, trailing_zeros); |
| 221 |
assert_eq!(result, $expected); |
| 222 |
} |
| 223 |
} |
| 224 |
)* |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
macro_rules! define_test_input { |
| 229 |
( - $value:literal $($t:tt)* ) => { |
| 230 |
define_test_input!(sign=Sign::Minus, value=$value $($t)*); |
| 231 |
}; |
| 232 |
( $value:literal $($t:tt)* ) => { |
| 233 |
define_test_input!(sign=Sign::Plus, value=$value $($t)*); |
| 234 |
}; |
| 235 |
( sign=$sign:expr, value=$value:literal ...000x ) => { |
| 236 |
define_test_input!(sign=$sign, value=$value, trailing_zeros=false); |
| 237 |
}; |
| 238 |
( sign=$sign:expr, value=$value:literal ) => { |
| 239 |
define_test_input!(sign=$sign, value=$value, trailing_zeros=true); |
| 240 |
}; |
| 241 |
( sign=$sign:expr, value=$value:expr, trailing_zeros=$trailing_zeros:literal ) => { |
| 242 |
fn test_input() -> (u32, Sign, bool) { ($value, $sign, $trailing_zeros) } |
| 243 |
}; |
| 244 |
} |
| 245 |
|
| 246 |
mod case_13950000 { |
| 247 |
use super::*; |
| 248 |
|
| 249 |
define_test_input!(13950000); |
| 250 |
|
| 251 |
impl_test!(3 :: Up => 13950000); |
| 252 |
impl_test!(5 :: Up, Ceiling, HalfUp, HalfEven => 14000000); |
| 253 |
impl_test!(5 :: Down, HalfDown => 13900000); |
| 254 |
} |
| 255 |
|
| 256 |
mod case_neg_35488622_000x { |
| 257 |
use super::*; |
| 258 |
|
| 259 |
// ...000x indicates non-zero trailing digit |
| 260 |
define_test_input!(-35488622 ...000x); |
| 261 |
|
| 262 |
impl_test!(1 :: Up => 35488630); |
| 263 |
impl_test!(1 :: Down => 35488620); |
| 264 |
impl_test!(2 :: Up => 35488700); |
| 265 |
impl_test!(2 :: Down => 35488600); |
| 266 |
impl_test!(7 :: Up, Floor => 40000000); |
| 267 |
impl_test!(7 :: Down, Ceiling => 30000000); |
| 268 |
impl_test!(8 :: Up => 100000000); |
| 269 |
impl_test!(8 :: Down => 0); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
#[allow(non_snake_case)] |
| 275 |
mod test_round_biguint_inplace { |
| 276 |
use super::*; |
| 277 |
use paste::paste; |
| 278 |
|
| 279 |
macro_rules! impl_test { |
| 280 |
(IMPL; $prec:literal, $mode:ident => $expected:literal, $exp:literal) => { |
| 281 |
paste! { |
| 282 |
#[test] |
| 283 |
fn [< prec $prec _mode_ $mode >]() { |
| 284 |
let mut n = test_input(); |
| 285 |
let rounder = NonDigitRoundingData { |
| 286 |
mode: self::RoundingMode::$mode, |
| 287 |
sign: Sign::Plus, |
| 288 |
}; |
| 289 |
let prec = NonZeroU64::new($prec).unwrap(); |
| 290 |
let result = round_biguint_inplace(&mut n, prec, rounder); |
| 291 |
let expected: BigUint = $expected.parse().unwrap(); |
| 292 |
assert_eq!(&expected, &n); |
| 293 |
assert_eq!($exp, result); |
| 294 |
} |
| 295 |
} |
| 296 |
}; |
| 297 |
($prec:literal: $($mode:ident),+ => $expected:literal E $exp:literal) => { |
| 298 |
$( impl_test!(IMPL; $prec, $mode => $expected, $exp); )* |
| 299 |
}; |
| 300 |
($prec:literal: $($($mode:ident),+ => $expected:literal E $exp:literal),+ ) => { |
| 301 |
$( impl_test!($prec: $($mode),* => $expected E $exp); )* |
| 302 |
}; |
| 303 |
} |
| 304 |
|
| 305 |
mod case_14414008876e200 { |
| 306 |
use super::*; |
| 307 |
|
| 308 |
fn test_input() -> BigUint { |
| 309 |
"1441400887642029388383856645559897315584165538408309505171135258930500598075662511441702498467332463786833300335785089981027498174604649257434184055851638492438374046355824930138046686225938550901559011829587689".parse().unwrap() |
| 310 |
} |
| 311 |
|
| 312 |
impl_test!(15: Up => "144140088764203" E 196, |
| 313 |
Down => "144140088764202" E 196); |
| 314 |
|
| 315 |
impl_test!(11: Down, |
| 316 |
HalfEven => "14414008876" E 200, |
| 317 |
Up => "14414008877" E 200); |
| 318 |
|
| 319 |
impl_test!(1: Down, |
| 320 |
HalfEven => "1" E 210, |
| 321 |
Up => "2" E 210); |
| 322 |
} |
| 323 |
|
| 324 |
mod case_99191529355902971933631238546419824884699 { |
| 325 |
use super::*; |
| 326 |
|
| 327 |
fn test_input() -> BigUint { |
| 328 |
"99191529355902971933631238546419824884699".parse().unwrap() |
| 329 |
} |
| 330 |
|
| 331 |
impl_test!(42: Down, Up => "99191529355902971933631238546419824884699" E 0); |
| 332 |
|
| 333 |
impl_test!(41: Down, |
| 334 |
Up, |
| 335 |
HalfEven => "99191529355902971933631238546419824884699" E 0); |
| 336 |
|
| 337 |
impl_test!(40: Up => "9919152935590297193363123854641982488470" E 1, |
| 338 |
Down => "9919152935590297193363123854641982488469" E 1); |
| 339 |
|
| 340 |
impl_test!(13: Up => "9919152935591" E 28, |
| 341 |
Down => "9919152935590" E 28); |
| 342 |
|
| 343 |
impl_test!(4: Up => "9920" E 37, |
| 344 |
Down => "9919" E 37); |
| 345 |
impl_test!(3: Up => "992" E 38, |
| 346 |
Down => "991" E 38); |
| 347 |
impl_test!(2: Up => "10" E 40, |
| 348 |
Down => "99" E 39); |
| 349 |
impl_test!(1: Up => "1" E 41, |
| 350 |
Down => "9" E 40); |
| 351 |
} |
| 352 |
|
| 353 |
mod case_99 { |
| 354 |
use super::*; |
| 355 |
|
| 356 |
fn test_input() -> BigUint { |
| 357 |
"99".parse().unwrap() |
| 358 |
} |
| 359 |
|
| 360 |
impl_test!(2: Up => "99" E 0, |
| 361 |
Down => "99" E 0); |
| 362 |
impl_test!(1: Up => "1" E 2, |
| 363 |
Down => "9" E 1); |
| 364 |
} |
| 365 |
} |