|
1 | 1 | // run-pass
|
| 2 | +// #![feature(const_int_pow)] |
| 3 | +#![feature(wrapping_next_power_of_two)] |
2 | 4 |
|
3 | 5 | const IS_POWER_OF_TWO_A: bool = 0u32.is_power_of_two();
|
4 | 6 | const IS_POWER_OF_TWO_B: bool = 32u32.is_power_of_two();
|
5 | 7 | const IS_POWER_OF_TWO_C: bool = 33u32.is_power_of_two();
|
6 | 8 |
|
| 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 | + |
7 | 26 | fn main() {
|
8 | 27 | assert!(!IS_POWER_OF_TWO_A);
|
9 | 28 | assert!(IS_POWER_OF_TWO_B);
|
10 | 29 | 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); |
11 | 47 | }
|
0 commit comments