// 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::arithmetic::traits::UnsignedAbs;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::signeds::PrimitiveSigned;
use malachite_base::num::basic::unsigneds::PrimitiveUnsigned;
use malachite_base::num::conversion::traits::ConvertibleFrom;
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, signed_pair_gen_var_5, signed_rounding_mode_pair_gen,
signed_signed_rounding_mode_triple_gen_var_2, unsigned_pair_gen_var_13,
unsigned_pair_gen_var_27, unsigned_rounding_mode_pair_gen,
unsigned_unsigned_rounding_mode_triple_gen_var_2,
};
use std::cmp::Ordering::{self, *};
use std::panic::catch_unwind;
#[test]
fn test_round_to_multiple_unsigned() {
fn test(n: T, d: T, rm: RoundingMode, q: T, o: Ordering) {
assert_eq!(n.round_to_multiple(d, rm), (q, o));
let mut mut_n = n;
assert_eq!(mut_n.round_to_multiple_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, 122, Less);
test::(123, 2, Floor, 122, Less);
test::(123, 2, Up, 124, Greater);
test::(123, 2, Ceiling, 124, Greater);
test::(123, 2, Nearest, 124, Greater);
test::(125, 2, Down, 124, Less);
test::(125, 2, Floor, 124, Less);
test::(125, 2, Up, 126, Greater);
test::(125, 2, Ceiling, 126, Greater);
test::(125, 2, Nearest, 124, Less);
test::(123, 123, Down, 123, Equal);
test::(123, 123, Floor, 123, Equal);
test::(123, 123, Up, 123, Equal);
test::(123, 123, Ceiling, 123, Equal);
test::(123, 123, Nearest, 123, Equal);
test::(123, 123, Exact, 123, Equal);
test::(123, 456, Down, 0, Less);
test::(123, 456, Floor, 0, Less);
test::(123, 456, Up, 456, Greater);
test::(123, 456, Ceiling, 456, 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, 999999999999, Less);
test::(1000000000000, 3, Floor, 999999999999, Less);
test::(1000000000000, 3, Up, 1000000000002, Greater);
test::(1000000000000, 3, Ceiling, 1000000000002, Greater);
test::(1000000000000, 3, Nearest, 999999999999, Less);
test::(999999999999, 2, Down, 999999999998, Less);
test::(999999999999, 2, Floor, 999999999998, Less);
test::(999999999999, 2, Up, 1000000000000, Greater);
test::(999999999999, 2, Ceiling, 1000000000000, Greater);
test::(999999999999, 2, Nearest, 1000000000000, Greater);
test::(1000000000001, 2, Down, 1000000000000, Less);
test::(1000000000001, 2, Floor, 1000000000000, Less);
test::(1000000000001, 2, Up, 1000000000002, Greater);
test::(1000000000001, 2, Ceiling, 1000000000002, Greater);
test::(1000000000001, 2, Nearest, 1000000000000, Less);
test::(
1000000000000000000000000,
0xffffffff,
Down,
999999999999996832276305,
Less,
);
test::(
1000000000000000000000000,
0xffffffff,
Floor,
999999999999996832276305,
Less,
);
test::(
1000000000000000000000000,
0xffffffff,
Up,
1000000000000001127243600,
Greater,
);
test::(
1000000000000000000000000,
0xffffffff,
Ceiling,
1000000000000001127243600,
Greater,
);
test::(
1000000000000000000000000,
0xffffffff,
Nearest,
1000000000000001127243600,
Greater,
);
test::(
1000000000000000000000000,
1000000000000,
Down,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Floor,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Up,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Ceiling,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Nearest,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Exact,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000001,
Down,
999999999999999999999999,
Less,
);
test::(
1000000000000000000000000,
1000000000001,
Floor,
999999999999999999999999,
Less,
);
test::(
1000000000000000000000000,
1000000000001,
Up,
1000000000001000000000000,
Greater,
);
test::(
1000000000000000000000000,
1000000000001,
Ceiling,
1000000000001000000000000,
Greater,
);
test::(
1000000000000000000000000,
1000000000001,
Nearest,
999999999999999999999999,
Less,
);
test::(
2999999999999999999999999,
2000000000000000000000000,
Nearest,
2000000000000000000000000,
Less,
);
test::(
3000000000000000000000000,
2000000000000000000000000,
Nearest,
4000000000000000000000000,
Greater,
);
test::(
3000000000000000000000001,
2000000000000000000000000,
Nearest,
4000000000000000000000000,
Greater,
);
test::(0, 0, Floor, 0, Equal);
test::(0, 0, Ceiling, 0, Equal);
test::(0, 0, Down, 0, Equal);
test::(0, 0, Up, 0, Equal);
test::(0, 0, Nearest, 0, Equal);
test::(0, 0, Exact, 0, Equal);
test::(2, 0, Floor, 0, Less);
test::(2, 0, Down, 0, Less);
test::(2, 0, Nearest, 0, Less);
}
#[test]
fn test_round_to_multiple_signed() {
fn test(n: T, d: T, rm: RoundingMode, q: T, o: Ordering) {
assert_eq!(n.round_to_multiple(d, rm), (q, o));
let mut mut_n = n;
assert_eq!(mut_n.round_to_multiple_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, 122, Less);
test::(123, 2, Floor, 122, Less);
test::(123, 2, Up, 124, Greater);
test::(123, 2, Ceiling, 124, Greater);
test::(123, 2, Nearest, 124, Greater);
test::(125, 2, Down, 124, Less);
test::(125, 2, Floor, 124, Less);
test::(125, 2, Up, 126, Greater);
test::(125, 2, Ceiling, 126, Greater);
test::(125, 2, Nearest, 124, Less);
test::(123, 123, Down, 123, Equal);
test::(123, 123, Floor, 123, Equal);
test::(123, 123, Up, 123, Equal);
test::(123, 123, Ceiling, 123, Equal);
test::(123, 123, Nearest, 123, Equal);
test::(123, 123, Exact, 123, Equal);
test::(123, 456, Down, 0, Less);
test::(123, 456, Floor, 0, Less);
test::(123, 456, Up, 456, Greater);
test::(123, 456, Ceiling, 456, 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, 999999999999, Less);
test::(1000000000000, 3, Floor, 999999999999, Less);
test::(1000000000000, 3, Up, 1000000000002, Greater);
test::(1000000000000, 3, Ceiling, 1000000000002, Greater);
test::(1000000000000, 3, Nearest, 999999999999, Less);
test::(999999999999, 2, Down, 999999999998, Less);
test::(999999999999, 2, Floor, 999999999998, Less);
test::(999999999999, 2, Up, 1000000000000, Greater);
test::(999999999999, 2, Ceiling, 1000000000000, Greater);
test::(999999999999, 2, Nearest, 1000000000000, Greater);
test::(1000000000001, 2, Down, 1000000000000, Less);
test::(1000000000001, 2, Floor, 1000000000000, Less);
test::(1000000000001, 2, Up, 1000000000002, Greater);
test::(1000000000001, 2, Ceiling, 1000000000002, Greater);
test::(1000000000001, 2, Nearest, 1000000000000, Less);
test::(
1000000000000000000000000,
0xffffffff,
Down,
999999999999996832276305,
Less,
);
test::(
1000000000000000000000000,
0xffffffff,
Floor,
999999999999996832276305,
Less,
);
test::(
1000000000000000000000000,
0xffffffff,
Up,
1000000000000001127243600,
Greater,
);
test::(
1000000000000000000000000,
0xffffffff,
Ceiling,
1000000000000001127243600,
Greater,
);
test::(
1000000000000000000000000,
0xffffffff,
Nearest,
1000000000000001127243600,
Greater,
);
test::(
1000000000000000000000000,
1000000000000,
Down,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Floor,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Up,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Ceiling,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Nearest,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000000,
Exact,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
1000000000001,
Down,
999999999999999999999999,
Less,
);
test::(
1000000000000000000000000,
1000000000001,
Floor,
999999999999999999999999,
Less,
);
test::(
1000000000000000000000000,
1000000000001,
Up,
1000000000001000000000000,
Greater,
);
test::(
1000000000000000000000000,
1000000000001,
Ceiling,
1000000000001000000000000,
Greater,
);
test::(
1000000000000000000000000,
1000000000001,
Nearest,
999999999999999999999999,
Less,
);
test::(
2999999999999999999999999,
2000000000000000000000000,
Nearest,
2000000000000000000000000,
Less,
);
test::(
3000000000000000000000000,
2000000000000000000000000,
Nearest,
4000000000000000000000000,
Greater,
);
test::(
3000000000000000000000001,
2000000000000000000000000,
Nearest,
4000000000000000000000000,
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, 122, Less);
test::(123, -2, Floor, 122, Less);
test::(123, -2, Up, 124, Greater);
test::(123, -2, Ceiling, 124, Greater);
test::(123, -2, Nearest, 124, Greater);
test::(125, -2, Down, 124, Less);
test::(125, -2, Floor, 124, Less);
test::(125, -2, Up, 126, Greater);
test::(125, -2, Ceiling, 126, Greater);
test::(125, -2, Nearest, 124, Less);
test::(123, -123, Down, 123, Equal);
test::(123, -123, Floor, 123, Equal);
test::(123, -123, Up, 123, Equal);
test::(123, -123, Ceiling, 123, Equal);
test::(123, -123, Nearest, 123, Equal);
test::(123, -123, Exact, 123, Equal);
test::(123, -456, Down, 0, Less);
test::(123, -456, Floor, 0, Less);
test::(123, -456, Up, 456, Greater);
test::(123, -456, Ceiling, 456, 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, 999999999999, Less);
test::(1000000000000, -3, Floor, 999999999999, Less);
test::(1000000000000, -3, Up, 1000000000002, Greater);
test::(1000000000000, -3, Ceiling, 1000000000002, Greater);
test::(1000000000000, -3, Nearest, 999999999999, Less);
test::(999999999999, -2, Down, 999999999998, Less);
test::(999999999999, -2, Floor, 999999999998, Less);
test::(999999999999, -2, Up, 1000000000000, Greater);
test::(999999999999, -2, Ceiling, 1000000000000, Greater);
test::(999999999999, -2, Nearest, 1000000000000, Greater);
test::(1000000000001, -2, Down, 1000000000000, Less);
test::(1000000000001, -2, Floor, 1000000000000, Less);
test::(1000000000001, -2, Up, 1000000000002, Greater);
test::(1000000000001, -2, Ceiling, 1000000000002, Greater);
test::(1000000000001, -2, Nearest, 1000000000000, Less);
test::(
1000000000000000000000000,
-0xffffffff,
Down,
999999999999996832276305,
Less,
);
test::(
1000000000000000000000000,
-0xffffffff,
Floor,
999999999999996832276305,
Less,
);
test::(
1000000000000000000000000,
-0xffffffff,
Up,
1000000000000001127243600,
Greater,
);
test::(
1000000000000000000000000,
-0xffffffff,
Ceiling,
1000000000000001127243600,
Greater,
);
test::(
1000000000000000000000000,
-0xffffffff,
Nearest,
1000000000000001127243600,
Greater,
);
test::(
1000000000000000000000000,
1000000000000,
Down,
1000000000000000000000000,
Equal,
);
test::(
1000000000000000000000000,
-1000000000000,
Floor,
1000000000000000000000000,
Equal,
);
test::