diff options
| author | bors <bors@rust-lang.org> | 2014-12-20 01:12:19 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-12-20 01:12:19 +0000 |
| commit | 1c2df5cc3cfc0c9e80adf9fa6504d55056741c5a (patch) | |
| tree | d9d1841b7f7a7f562a1d3288b98ceba78915a39b /src/libstd/num | |
| parent | cbe9fb45bc705a89f23b434c686544d490923596 (diff) | |
| parent | f6328b60da4c506f0f15dc0194f9b9a89aa61a79 (diff) | |
| download | rust-1c2df5cc3cfc0c9e80adf9fa6504d55056741c5a.tar.gz rust-1c2df5cc3cfc0c9e80adf9fa6504d55056741c5a.zip | |
auto merge of #19640 : aliblong/rust/power_of_two_reform, r=Gankro
The `is_power_of_two()` method of the `UnsignedInt` trait currently returns `true` for `self == 0`. Zero is not a power of two, assuming an integral exponent `k >= 0`. I've therefore moved this functionality to the new method `is_power_of_two_or_zero()` and reformed `is_power_of_two()` to return false for `self == 0`. To illustrate the usefulness of the existence of both functions, consider `HashMap`. Its capacity must be zero or a power of two; conversely, it also requires a (non-zero) power of two for key and val alignment. Also, added a small amount of documentation regarding #18604.
Diffstat (limited to 'src/libstd/num')
| -rw-r--r-- | src/libstd/num/mod.rs | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index a568aafe1ed..fdece4fbc0d 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -664,11 +664,32 @@ mod tests { assert_eq!(third.checked_mul(4), None); } + macro_rules! test_is_power_of_two { + ($test_name:ident, $T:ident) => ( + fn $test_name() { + #![test] + assert_eq!((0 as $T).is_power_of_two(), false); + assert_eq!((1 as $T).is_power_of_two(), true); + assert_eq!((2 as $T).is_power_of_two(), true); + assert_eq!((3 as $T).is_power_of_two(), false); + assert_eq!((4 as $T).is_power_of_two(), true); + assert_eq!((5 as $T).is_power_of_two(), false); + assert!(($T::MAX / 2 + 1).is_power_of_two(), true); + } + ) + } + + test_is_power_of_two!{ test_is_power_of_two_u8, u8 } + test_is_power_of_two!{ test_is_power_of_two_u16, u16 } + test_is_power_of_two!{ test_is_power_of_two_u32, u32 } + test_is_power_of_two!{ test_is_power_of_two_u64, u64 } + test_is_power_of_two!{ test_is_power_of_two_uint, uint } + macro_rules! test_next_power_of_two { ($test_name:ident, $T:ident) => ( fn $test_name() { #![test] - assert_eq!((0 as $T).next_power_of_two(), 0); + assert_eq!((0 as $T).next_power_of_two(), 1); let mut next_power = 1; for i in range::<$T>(1, 40) { assert_eq!(i.next_power_of_two(), next_power); @@ -688,15 +709,15 @@ mod tests { ($test_name:ident, $T:ident) => ( fn $test_name() { #![test] - assert_eq!((0 as $T).checked_next_power_of_two(), None); + assert_eq!((0 as $T).checked_next_power_of_two(), Some(1)); + assert!(($T::MAX / 2).checked_next_power_of_two().is_some()); + assert_eq!(($T::MAX - 1).checked_next_power_of_two(), None); + assert_eq!($T::MAX.checked_next_power_of_two(), None); let mut next_power = 1; for i in range::<$T>(1, 40) { assert_eq!(i.checked_next_power_of_two(), Some(next_power)); if i == next_power { next_power *= 2 } } - assert!(($T::MAX / 2).checked_next_power_of_two().is_some()); - assert_eq!(($T::MAX - 1).checked_next_power_of_two(), None); - assert_eq!($T::MAX.checked_next_power_of_two(), None); } ) } |
