Skip to content

Commit 7fe5eaf

Browse files
Test integer exponentiation in a const context
1 parent 269bf89 commit 7fe5eaf

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
11
// run-pass
22

3+
#![feature(const_int_pow)]
4+
#![feature(wrapping_next_power_of_two)]
5+
36
const IS_POWER_OF_TWO_A: bool = 0u32.is_power_of_two();
47
const IS_POWER_OF_TWO_B: bool = 32u32.is_power_of_two();
58
const IS_POWER_OF_TWO_C: bool = 33u32.is_power_of_two();
69

10+
const POW: u8 = 3u8.pow(5);
11+
12+
const CHECKED_POW_OK: Option<u8> = 3u8.checked_pow(5);
13+
const CHECKED_POW_OVERFLOW: Option<u8> = 3u8.checked_pow(6);
14+
15+
const WRAPPING_POW: u8 = 3u8.wrapping_pow(6);
16+
const OVERFLOWING_POW: (u8, bool) = 3u8.overflowing_pow(6);
17+
const SATURATING_POW: u8 = 3u8.saturating_pow(6);
18+
19+
const NEXT_POWER_OF_TWO: u32 = 3u32.next_power_of_two();
20+
21+
const CHECKED_NEXT_POWER_OF_TWO_OK: Option<u32> = 3u32.checked_next_power_of_two();
22+
const CHECKED_NEXT_POWER_OF_TWO_OVERFLOW: Option<u32> =
23+
u32::max_value().checked_next_power_of_two();
24+
25+
const WRAPPING_NEXT_POWER_OF_TWO: u32 =
26+
u32::max_value().wrapping_next_power_of_two();
27+
728
fn main() {
829
assert!(!IS_POWER_OF_TWO_A);
930
assert!(IS_POWER_OF_TWO_B);
1031
assert!(!IS_POWER_OF_TWO_C);
32+
33+
assert_eq!(POW, 243);
34+
35+
assert_eq!(CHECKED_POW_OK, Some(243));
36+
assert_eq!(CHECKED_POW_OVERFLOW, None);
37+
38+
assert_eq!(WRAPPING_POW, 217);
39+
assert_eq!(OVERFLOWING_POW, (217, true));
40+
assert_eq!(SATURATING_POW, u8::max_value());
41+
42+
assert_eq!(NEXT_POWER_OF_TWO, 4);
43+
44+
assert_eq!(CHECKED_NEXT_POWER_OF_TWO_OK, Some(4));
45+
assert_eq!(CHECKED_NEXT_POWER_OF_TWO_OVERFLOW, None);
46+
47+
assert_eq!(WRAPPING_NEXT_POWER_OF_TWO, 0);
1148
}

0 commit comments

Comments
 (0)