mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
isqrt: add more tests
This commit is contained in:
parent
68f0b475c7
commit
d49da0fe54
@ -299,6 +299,23 @@ macro_rules! int_module {
|
||||
assert_eq!((2 as $T).isqrt(), 1 as $T);
|
||||
assert_eq!((99 as $T).isqrt(), 9 as $T);
|
||||
assert_eq!((100 as $T).isqrt(), 10 as $T);
|
||||
|
||||
let n_max: $T = (1024 * 1024).min($T::MAX as u128) as $T;
|
||||
for n in 0..=n_max {
|
||||
let isqrt: $T = n.isqrt();
|
||||
|
||||
assert!(isqrt.pow(2) <= n);
|
||||
let (square, overflow) = (isqrt + 1).overflowing_pow(2);
|
||||
assert!(overflow || square > n);
|
||||
}
|
||||
|
||||
for n in ($T::MAX - 127)..=$T::MAX {
|
||||
let isqrt: $T = n.isqrt();
|
||||
|
||||
assert!(isqrt.pow(2) <= n);
|
||||
let (square, overflow) = (isqrt + 1).overflowing_pow(2);
|
||||
assert!(overflow || square > n);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -214,6 +214,21 @@ macro_rules! uint_module {
|
||||
assert_eq!((99 as $T).isqrt(), 9 as $T);
|
||||
assert_eq!((100 as $T).isqrt(), 10 as $T);
|
||||
assert_eq!($T::MAX.isqrt(), (1 << ($T::BITS / 2)) - 1);
|
||||
|
||||
let n_max: $T = (1024 * 1024).min($T::MAX as u128) as $T;
|
||||
for n in 0..=n_max {
|
||||
let isqrt: $T = n.isqrt();
|
||||
|
||||
assert!(isqrt.pow(2) <= n);
|
||||
assert!(isqrt + 1 == (1 as $T) << ($T::BITS / 2) || (isqrt + 1).pow(2) > n);
|
||||
}
|
||||
|
||||
for n in ($T::MAX - 255)..=$T::MAX {
|
||||
let isqrt: $T = n.isqrt();
|
||||
|
||||
assert!(isqrt.pow(2) <= n);
|
||||
assert!(isqrt + 1 == (1 as $T) << ($T::BITS / 2) || (isqrt + 1).pow(2) > n);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user