Add u128 and i128.

This commit is contained in:
Sergey Pepyakin 2018-10-30 12:22:24 +01:00
parent 5bbfb9187d
commit 01bb91452f
1 changed files with 19 additions and 1 deletions

View File

@ -84,7 +84,7 @@ macro_rules! impl_isqrt {
}
impl_isqrt!(usize, u64, u32, u16, u8, isize, i64, i32, i16, i8);
impl_isqrt!(usize, u128, u64, u32, u16, u8, isize, i128, i64, i32, i16, i8);
#[cfg(test)]
@ -164,4 +164,22 @@ mod tests {
// check that the next number's square overflows
assert!((sqrt_max + 1).checked_mul(sqrt_max + 1).is_none());
}
#[test]
fn u128_sqrt() {
let sqrt_max: u128 = 18_446_744_073_709_551_615;
let tests = [
(0u128, 0u128),
(4, 2),
(7, 2),
(81, 9),
(80, 8),
(u128::max_value(), sqrt_max),
];
for &(in_, out) in tests.iter() {
assert_eq!(in_.integer_sqrt(), out, "in {}", in_);
}
assert!(sqrt_max * sqrt_max <= u128::max_value());
assert!((sqrt_max + 1).checked_mul(sqrt_max + 1).is_none());
}
}