Skip to content

Commit 9aa0a1f

Browse files
committed
Power-of-two int tests
Most of these have to be deleted; the various power_of_two functions use `intrinsics::ctlz_nonzero` internally, so they can't be made const before rust-lang#66275 is merged.
1 parent 09994af commit 9aa0a1f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
11
// run-pass
2+
// #![feature(const_int_pow)]
3+
#![feature(wrapping_next_power_of_two)]
24

35
const IS_POWER_OF_TWO_A: bool = 0u32.is_power_of_two();
46
const IS_POWER_OF_TWO_B: bool = 32u32.is_power_of_two();
57
const IS_POWER_OF_TWO_C: bool = 33u32.is_power_of_two();
68

9+
const NEXT_POWER_OF_TWO_A: u32 = 0u32.next_power_of_two();
10+
const NEXT_POWER_OF_TWO_B: u8 = 2u8.next_power_of_two();
11+
const NEXT_POWER_OF_TWO_C: u8 = 3u8.next_power_of_two();
12+
const NEXT_POWER_OF_TWO_D: u8 = 127u8.next_power_of_two();
13+
14+
const CHECKED_NEXT_POWER_OF_TWO_A: Option<u32> = 0u32.checked_next_power_of_two();
15+
const CHECKED_NEXT_POWER_OF_TWO_B: Option<u8> = 2u8.checked_next_power_of_two();
16+
const CHECKED_NEXT_POWER_OF_TWO_C: Option<u8> = 3u8.checked_next_power_of_two();
17+
const CHECKED_NEXT_POWER_OF_TWO_D: Option<u8> = 127u8.checked_next_power_of_two();
18+
const CHECKED_NEXT_POWER_OF_TWO_E: Option<u8> = 129u8.checked_next_power_of_two();
19+
20+
const WRAPPING_NEXT_POWER_OF_TWO_A: u32 = 0u32.wrapping_next_power_of_two();
21+
const WRAPPING_NEXT_POWER_OF_TWO_B: u8 = 2u8.wrapping_next_power_of_two();
22+
const WRAPPING_NEXT_POWER_OF_TWO_C: u8 = 3u8.wrapping_next_power_of_two();
23+
const WRAPPING_NEXT_POWER_OF_TWO_D: u8 = 127u8.wrapping_next_power_of_two();
24+
const WRAPPING_NEXT_POWER_OF_TWO_E: u8 = u8::max_value().wrapping_next_power_of_two();
25+
726
fn main() {
827
assert!(!IS_POWER_OF_TWO_A);
928
assert!(IS_POWER_OF_TWO_B);
1029
assert!(!IS_POWER_OF_TWO_C);
30+
31+
assert_eq!(NEXT_POWER_OF_TWO_A, 2);
32+
assert_eq!(NEXT_POWER_OF_TWO_B, 2);
33+
assert_eq!(NEXT_POWER_OF_TWO_C, 4);
34+
assert_eq!(NEXT_POWER_OF_TWO_D, 128);
35+
36+
assert_eq!(CHECKED_NEXT_POWER_OF_TWO_A, Some(2));
37+
assert_eq!(CHECKED_NEXT_POWER_OF_TWO_B, Some(2));
38+
assert_eq!(CHECKED_NEXT_POWER_OF_TWO_C, Some(4));
39+
assert_eq!(CHECKED_NEXT_POWER_OF_TWO_D, Some(128));
40+
assert_eq!(CHECKED_NEXT_POWER_OF_TWO_E, None);
41+
42+
assert_eq!(WRAPPING_NEXT_POWER_OF_TWO_A, 2);
43+
assert_eq!(WRAPPING_NEXT_POWER_OF_TWO_B, 2);
44+
assert_eq!(WRAPPING_NEXT_POWER_OF_TWO_C, 4);
45+
assert_eq!(WRAPPING_NEXT_POWER_OF_TWO_D, 128);
46+
assert_eq!(WRAPPING_NEXT_POWER_OF_TWO_E, 0);
1147
}

0 commit comments

Comments
 (0)