// Copyright © 2024 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See .
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::signeds::PrimitiveSigned;
use malachite_base::num::basic::unsigneds::PrimitiveUnsigned;
use malachite_base::rounding_modes::exhaustive::exhaustive_rounding_modes;
use malachite_base::rounding_modes::RoundingMode::{self, *};
use malachite_base::test_util::generators::{
signed_pair_gen_var_3, signed_pair_gen_var_5, signed_rounding_mode_pair_gen,
signed_rounding_mode_pair_gen_var_1, signed_rounding_mode_pair_gen_var_2,
signed_rounding_mode_pair_gen_var_3, signed_signed_rounding_mode_triple_gen_var_1,
unsigned_pair_gen_var_11, unsigned_pair_gen_var_12, unsigned_pair_gen_var_13,
unsigned_rounding_mode_pair_gen, unsigned_rounding_mode_pair_gen_var_1,
unsigned_unsigned_rounding_mode_triple_gen_var_1,
};
use std::cmp::Ordering::{self, *};
use std::panic::catch_unwind;
#[test]
fn test_div_round_unsigned() {
fn test(n: T, d: T, rm: RoundingMode, q: T, o: Ordering) {
assert_eq!(n.div_round(d, rm), (q, o));
let mut mut_n = n;
assert_eq!(mut_n.div_round_assign(d, rm), o);
assert_eq!(mut_n, q);
}
test::(0, 1, Down, 0, Equal);
test::(0, 1, Floor, 0, Equal);
test::(0, 1, Up, 0, Equal);
test::(0, 1, Ceiling, 0, Equal);
test::(0, 1, Nearest, 0, Equal);
test::(0, 1, Exact, 0, Equal);
test::(0, 123, Down, 0, Equal);
test::(0, 123, Floor, 0, Equal);
test::(0, 123, Up, 0, Equal);
test::(0, 123, Ceiling, 0, Equal);
test::(0, 123, Nearest, 0, Equal);
test::(0, 123, Exact, 0, Equal);
test::(1, 1, Down, 1, Equal);
test::(1, 1, Floor, 1, Equal);
test::(1, 1, Up, 1, Equal);
test::(1, 1, Ceiling, 1, Equal);
test::(1, 1, Nearest, 1, Equal);
test::(1, 1, Exact, 1, Equal);
test::(123, 1, Down, 123, Equal);
test::(123, 1, Floor, 123, Equal);
test::(123, 1, Up, 123, Equal);
test::(123, 1, Ceiling, 123, Equal);
test::(123, 1, Nearest, 123, Equal);
test::(123, 1, Exact, 123, Equal);
test::(123, 2, Down, 61, Less);
test::(123, 2, Floor, 61, Less);
test::(123, 2, Up, 62, Greater);
test::(123, 2, Ceiling, 62, Greater);
test::(123, 2, Nearest, 62, Greater);
test::(125, 2, Down, 62, Less);
test::(125, 2, Floor, 62, Less);
test::(125, 2, Up, 63, Greater);
test::(125, 2, Ceiling, 63, Greater);
test::(125, 2, Nearest, 62, Less);
test::(123, 123, Down, 1, Equal);
test::(123, 123, Floor, 1, Equal);
test::(123, 123, Up, 1, Equal);
test::(123, 123, Ceiling, 1, Equal);
test::(123, 123, Nearest, 1, Equal);
test::(123, 123, Exact, 1, Equal);
test::(123, 456, Down, 0, Less);
test::(123, 456, Floor, 0, Less);
test::(123, 456, Up, 1, Greater);
test::(123, 456, Ceiling, 1, Greater);
test::(123, 456, Nearest, 0, Less);
test::(1000000000000, 1, Down, 1000000000000, Equal);
test::(1000000000000, 1, Floor, 1000000000000, Equal);
test::(1000000000000, 1, Up, 1000000000000, Equal);
test::(1000000000000, 1, Ceiling, 1000000000000, Equal);
test::(1000000000000, 1, Nearest, 1000000000000, Equal);
test::(1000000000000, 1, Exact, 1000000000000, Equal);
test::(1000000000000, 3, Down, 333333333333, Less);
test::(1000000000000, 3, Floor, 333333333333, Less);
test::(1000000000000, 3, Up, 333333333334, Greater);
test::(1000000000000, 3, Ceiling, 333333333334, Greater);
test::(1000000000000, 3, Nearest, 333333333333, Less);
test::(999999999999, 2, Down, 499999999999, Less);
test::(999999999999, 2, Floor, 499999999999, Less);
test::(999999999999, 2, Up, 500000000000, Greater);
test::(999999999999, 2, Ceiling, 500000000000, Greater);
test::(999999999999, 2, Nearest, 500000000000, Greater);
test::(1000000000001, 2, Down, 500000000000, Less);
test::(1000000000001, 2, Floor, 500000000000, Less);
test::(1000000000001, 2, Up, 500000000001, Greater);
test::(1000000000001, 2, Ceiling, 500000000001, Greater);
test::(1000000000001, 2, Nearest, 500000000000, Less);
test::(
1000000000000000000000000,
0xffffffff,
Down,
232830643708079,
Less,
);
test::(
1000000000000000000000000,
0xffffffff,
Floor,
232830643708079,
Less,
);
test::(
1000000000000000000000000,
0xffffffff,
Up,
232830643708080,
Greater,
);
test::(
1000000000000000000000000,
0xffffffff,
Ceiling,
232830643708080,
Greater,
);
test::(
1000000000000000000000000,
0xffffffff,
Nearest,
232830643708080,
Greater,
);
test::(
1000000000000000000000000,
1000000000000,
Down,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Floor,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Up,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Ceiling,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Nearest,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Exact,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000001,
Down,
999999999999,
Less,
);
test::(
1000000000000000000000000,
1000000000001,
Floor,
999999999999,
Less,
);
test::(
1000000000000000000000000,
1000000000001,
Up,
1000000000000,
Greater,
);
test::(
1000000000000000000000000,
1000000000001,
Ceiling,
1000000000000,
Greater,
);
test::(
1000000000000000000000000,
1000000000001,
Nearest,
999999999999,
Less,
);
test::(
2999999999999999999999999,
2000000000000000000000000,
Nearest,
1,
Less,
);
test::(
3000000000000000000000000,
2000000000000000000000000,
Nearest,
2,
Greater,
);
test::(
3000000000000000000000001,
2000000000000000000000000,
Nearest,
2,
Greater,
);
}
#[test]
fn test_div_round_signed() {
fn test(n: T, d: T, rm: RoundingMode, q: T, o: Ordering) {
assert_eq!(n.div_round(d, rm), (q, o));
let mut mut_n = n;
assert_eq!(mut_n.div_round_assign(d, rm), o);
assert_eq!(mut_n, q);
}
test::(0, 1, Down, 0, Equal);
test::(0, 1, Floor, 0, Equal);
test::(0, 1, Up, 0, Equal);
test::(0, 1, Ceiling, 0, Equal);
test::(0, 1, Nearest, 0, Equal);
test::(0, 1, Exact, 0, Equal);
test::(0, 123, Down, 0, Equal);
test::(0, 123, Floor, 0, Equal);
test::(0, 123, Up, 0, Equal);
test::(0, 123, Ceiling, 0, Equal);
test::(0, 123, Nearest, 0, Equal);
test::(0, 123, Exact, 0, Equal);
test::(1, 1, Down, 1, Equal);
test::(1, 1, Floor, 1, Equal);
test::(1, 1, Up, 1, Equal);
test::(1, 1, Ceiling, 1, Equal);
test::(1, 1, Nearest, 1, Equal);
test::(1, 1, Exact, 1, Equal);
test::(123, 1, Down, 123, Equal);
test::(123, 1, Floor, 123, Equal);
test::(123, 1, Up, 123, Equal);
test::(123, 1, Ceiling, 123, Equal);
test::(123, 1, Nearest, 123, Equal);
test::(123, 1, Exact, 123, Equal);
test::(123, 2, Down, 61, Less);
test::(123, 2, Floor, 61, Less);
test::(123, 2, Up, 62, Greater);
test::(123, 2, Ceiling, 62, Greater);
test::(123, 2, Nearest, 62, Greater);
test::(125, 2, Down, 62, Less);
test::(125, 2, Floor, 62, Less);
test::(125, 2, Up, 63, Greater);
test::(125, 2, Ceiling, 63, Greater);
test::(125, 2, Nearest, 62, Less);
test::(123, 123, Down, 1, Equal);
test::(123, 123, Floor, 1, Equal);
test::(123, 123, Up, 1, Equal);
test::(123, 123, Ceiling, 1, Equal);
test::(123, 123, Nearest, 1, Equal);
test::(123, 123, Exact, 1, Equal);
test::(123, 456, Down, 0, Less);
test::(123, 456, Floor, 0, Less);
test::(123, 456, Up, 1, Greater);
test::(123, 456, Ceiling, 1, Greater);
test::(123, 456, Nearest, 0, Less);
test::(1000000000000, 1, Down, 1000000000000, Equal);
test::(1000000000000, 1, Floor, 1000000000000, Equal);
test::(1000000000000, 1, Up, 1000000000000, Equal);
test::(1000000000000, 1, Ceiling, 1000000000000, Equal);
test::(1000000000000, 1, Nearest, 1000000000000, Equal);
test::(1000000000000, 1, Exact, 1000000000000, Equal);
test::(1000000000000, 3, Down, 333333333333, Less);
test::(1000000000000, 3, Floor, 333333333333, Less);
test::(1000000000000, 3, Up, 333333333334, Greater);
test::(1000000000000, 3, Ceiling, 333333333334, Greater);
test::(1000000000000, 3, Nearest, 333333333333, Less);
test::(999999999999, 2, Down, 499999999999, Less);
test::(999999999999, 2, Floor, 499999999999, Less);
test::(999999999999, 2, Up, 500000000000, Greater);
test::(999999999999, 2, Ceiling, 500000000000, Greater);
test::(999999999999, 2, Nearest, 500000000000, Greater);
test::(1000000000001, 2, Down, 500000000000, Less);
test::(1000000000001, 2, Floor, 500000000000, Less);
test::(1000000000001, 2, Up, 500000000001, Greater);
test::(1000000000001, 2, Ceiling, 500000000001, Greater);
test::(1000000000001, 2, Nearest, 500000000000, Less);
test::(
1000000000000000000000000,
0xffffffff,
Down,
232830643708079,
Less,
);
test::(
1000000000000000000000000,
0xffffffff,
Floor,
232830643708079,
Less,
);
test::(
1000000000000000000000000,
0xffffffff,
Up,
232830643708080,
Greater,
);
test::(
1000000000000000000000000,
0xffffffff,
Ceiling,
232830643708080,
Greater,
);
test::(
1000000000000000000000000,
0xffffffff,
Nearest,
232830643708080,
Greater,
);
test::(
1000000000000000000000000,
1000000000000,
Down,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Floor,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Up,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Ceiling,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Nearest,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Exact,
1000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000001,
Down,
999999999999,
Less,
);
test::(
1000000000000000000000000,
1000000000001,
Floor,
999999999999,
Less,
);
test::(
1000000000000000000000000,
1000000000001,
Up,
1000000000000,
Greater,
);
test::(
1000000000000000000000000,
1000000000001,
Ceiling,
1000000000000,
Greater,
);
test::(
1000000000000000000000000,
1000000000001,
Nearest,
999999999999,
Less,
);
test::(
2999999999999999999999999,
2000000000000000000000000,
Nearest,
1,
Less,
);
test::(
3000000000000000000000000,
2000000000000000000000000,
Nearest,
2,
Greater,
);
test::(
3000000000000000000000001,
2000000000000000000000000,
Nearest,
2,
Greater,
);
test::(0, -1, Down, 0, Equal);
test::(0, -1, Floor, 0, Equal);
test::(0, -1, Up, 0, Equal);
test::(0, -1, Ceiling, 0, Equal);
test::(0, -1, Nearest, 0, Equal);
test::(0, -1, Exact, 0, Equal);
test::(0, -123, Down, 0, Equal);
test::(0, -123, Floor, 0, Equal);
test::(0, -123, Up, 0, Equal);
test::(0, -123, Ceiling, 0, Equal);
test::(0, -123, Nearest, 0, Equal);
test::(0, -123, Exact, 0, Equal);
test::(1, -1, Down, -1, Equal);
test::(1, -1, Floor, -1, Equal);
test::(1, -1, Up, -1, Equal);
test::(1, -1, Ceiling, -1, Equal);
test::(1, -1, Nearest, -1, Equal);
test::(1, -1, Exact, -1, Equal);
test::(123, -1, Down, -123, Equal);
test::(123, -1, Floor, -123, Equal);
test::(123, -1, Up, -123, Equal);
test::(123, -1, Ceiling, -123, Equal);
test::(123, -1, Nearest, -123, Equal);
test::(123, -1, Exact, -123, Equal);
test::(123, -2, Down, -61, Greater);
test::(123, -2, Floor, -62, Less);
test::(123, -2, Up, -62, Less);
test::(123, -2, Ceiling, -61, Greater);
test::(123, -2, Nearest, -62, Less);
test::(125, -2, Down, -62, Greater);
test::(125, -2, Floor, -63, Less);
test::(125, -2, Up, -63, Less);
test::(125, -2, Ceiling, -62, Greater);
test::(125, -2, Nearest, -62, Greater);
test::(123, -123, Down, -1, Equal);
test::(123, -123, Floor, -1, Equal);
test::(123, -123, Up, -1, Equal);
test::(123, -123, Ceiling, -1, Equal);
test::(123, -123, Nearest, -1, Equal);
test::(123, -123, Exact, -1, Equal);
test::(123, -456, Down, 0, Greater);
test::(123, -456, Floor, -1, Less);
test::(123, -456, Up, -1, Less);
test::(123, -456, Ceiling, 0, Greater);
test::(123, -456, Nearest, 0, Greater);
test::(1000000000000, -1, Down, -1000000000000, Equal);
test::(1000000000000, -1, Floor, -1000000000000, Equal);
test::(1000000000000, -1, Up, -1000000000000, Equal);
test::(1000000000000, -1, Ceiling, -1000000000000, Equal);
test::(1000000000000, -1, Nearest, -1000000000000, Equal);
test::(1000000000000, -1, Exact, -1000000000000, Equal);
test::(1000000000000, -3, Down, -333333333333, Greater);
test::(1000000000000, -3, Floor, -333333333334, Less);
test::(1000000000000, -3, Up, -333333333334, Less);
test::(1000000000000, -3, Ceiling, -333333333333, Greater);
test::(1000000000000, -3, Nearest, -333333333333, Greater);
test::(999999999999, -2, Down, -499999999999, Greater);
test::(999999999999, -2, Floor, -500000000000, Less);
test::(999999999999, -2, Up, -500000000000, Less);
test::(999999999999, -2, Ceiling, -499999999999, Greater);
test::(999999999999, -2, Nearest, -500000000000, Less);
test::(1000000000001, -2, Down, -500000000000, Greater);
test::(1000000000001, -2, Floor, -500000000001, Less);
test::(1000000000001, -2, Up, -500000000001, Less);
test::(1000000000001, -2, Ceiling, -500000000000, Greater);
test::(1000000000001, -2, Nearest, -500000000000, Greater);
test::(
1000000000000000000000000,
-0xffffffff,
Down,
-232830643708079,
Greater,
);
test::(
1000000000000000000000000,
-0xffffffff,
Floor,
-232830643708080,
Less,
);
test::(
1000000000000000000000000,
-0xffffffff,
Up,
-232830643708080,
Less,
);
test::(
1000000000000000000000000,
-0xffffffff,
Ceiling,
-232830643708079,
Greater,
);
test::(
1000000000000000000000000,
-0xffffffff,
Nearest,
-232830643708080,
Less,
);
test::(
1000000000000000000000000,
-1000000000000,
Down,
-1000000000000,
Equal,
);
test::(
1000000000000000000000000,
-1000000000000,
Floor,
-1000000000000,
Equal,
);
test::(
1000000000000000000000000,
-1000000000000,
Up,
-1000000000000,
Equal,
);
test::(
1000000000000000000000000,
-1000000000000,
Ceiling,
-1000000000000,
Equal,
);
test::(
1000000000000000000000000,
-1000000000000,
Nearest,
-1000000000000,
Equal,
);
test::(
1000000000000000000000000,
-1000000000000,
Exact,
-1000000000000,
Equal,
);
test::(
1000000000000000000000000,
-1000000000001,
Down,
-999999999999,
Greater,
);
test::(
1000000000000000000000000,
-1000000000001,
Floor,
-1000000000000,
Less,
);
test::(
1000000000000000000000000,
-1000000000001,
Up,
-1000000000000,
Less,
);
test::(
1000000000000000000000000,
-1000000000001,
Ceiling,
-999999999999,
Greater,
);
test::(
1000000000000000000000000,
-1000000000001,
Nearest,
-999999999999,
Greater,
);
test::