Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit acaf7e2

Browse files
committedOct 14, 2024
Run most core::num tests in const context too
1 parent ecb3830 commit acaf7e2

File tree

3 files changed

+539
-525
lines changed

3 files changed

+539
-525
lines changed
 

‎core/tests/lib.rs

+35
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
#![feature(clone_to_uninit)]
1717
#![feature(const_align_of_val_raw)]
1818
#![feature(const_align_offset)]
19+
#![feature(const_bigint_helper_methods)]
1920
#![feature(const_black_box)]
21+
#![feature(const_eval_select)]
2022
#![feature(const_hash)]
2123
#![feature(const_heap)]
2224
#![feature(const_likely)]
2325
#![feature(const_nonnull_new)]
26+
#![feature(const_num_midpoint)]
2427
#![feature(const_option_ext)]
2528
#![feature(const_pin)]
2629
#![feature(const_pointer_is_aligned)]
@@ -45,6 +48,7 @@
4548
#![feature(get_many_mut)]
4649
#![feature(hasher_prefixfree_extras)]
4750
#![feature(hashmap_internals)]
51+
#![feature(inline_const_pat)]
4852
#![feature(int_roundings)]
4953
#![feature(ip)]
5054
#![feature(ip_from)]
@@ -104,6 +108,37 @@
104108
#![deny(fuzzy_provenance_casts)]
105109
#![deny(unsafe_op_in_unsafe_fn)]
106110

111+
/// Version of `assert_matches` that ignores fancy runtime printing in const context and uses structural equality.
112+
macro_rules! assert_eq_const_safe {
113+
($left:expr, $right:expr$(, $($arg:tt)+)?) => {
114+
{
115+
fn runtime() {
116+
assert_eq!($left, $right, $($arg)*);
117+
}
118+
const fn compiletime() {
119+
assert!(matches!($left, const { $right }));
120+
}
121+
core::intrinsics::const_eval_select((), compiletime, runtime)
122+
}
123+
};
124+
}
125+
126+
/// Creates a test for runtime and a test for constant-time.
127+
macro_rules! test_runtime_and_compiletime {
128+
($(
129+
$(#[$attr:meta])*
130+
fn $test:ident() $block:block
131+
)*) => {
132+
$(
133+
$(#[$attr])*
134+
#[test]
135+
fn $test() $block
136+
$(#[$attr])*
137+
const _: () = $block;
138+
)*
139+
}
140+
}
141+
107142
mod alloc;
108143
mod any;
109144
mod array;

‎core/tests/num/int_macros.rs

+298-308
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,6 @@ macro_rules! int_module {
1717
num::test_num(10 as $T, 2 as $T);
1818
}
1919

20-
#[test]
21-
fn test_rem_euclid() {
22-
assert_eq!((-1 as $T).rem_euclid(MIN), MAX);
23-
}
24-
25-
#[test]
26-
pub fn test_abs() {
27-
assert_eq!((1 as $T).abs(), 1 as $T);
28-
assert_eq!((0 as $T).abs(), 0 as $T);
29-
assert_eq!((-1 as $T).abs(), 1 as $T);
30-
}
31-
32-
#[test]
33-
fn test_signum() {
34-
assert_eq!((1 as $T).signum(), 1 as $T);
35-
assert_eq!((0 as $T).signum(), 0 as $T);
36-
assert_eq!((-0 as $T).signum(), 0 as $T);
37-
assert_eq!((-1 as $T).signum(), -1 as $T);
38-
}
39-
40-
#[test]
41-
fn test_is_positive() {
42-
assert!((1 as $T).is_positive());
43-
assert!(!(0 as $T).is_positive());
44-
assert!(!(-0 as $T).is_positive());
45-
assert!(!(-1 as $T).is_positive());
46-
}
47-
48-
#[test]
49-
fn test_is_negative() {
50-
assert!(!(1 as $T).is_negative());
51-
assert!(!(0 as $T).is_negative());
52-
assert!(!(-0 as $T).is_negative());
53-
assert!((-1 as $T).is_negative());
54-
}
55-
5620
#[test]
5721
fn test_bitwise_operators() {
5822
assert_eq!(0b1110 as $T, (0b1100 as $T).bitor(0b1010 as $T));
@@ -63,141 +27,167 @@ macro_rules! int_module {
6327
assert_eq!(-(0b11 as $T) - (1 as $T), (0b11 as $T).not());
6428
}
6529

30+
test_runtime_and_compiletime! {
31+
32+
fn test_rem_euclid() {
33+
assert_eq_const_safe!((-1 as $T).rem_euclid(MIN), MAX);
34+
}
35+
36+
fn test_abs() {
37+
assert_eq_const_safe!((1 as $T).abs(), 1 as $T);
38+
assert_eq_const_safe!((0 as $T).abs(), 0 as $T);
39+
assert_eq_const_safe!((-1 as $T).abs(), 1 as $T);
40+
}
41+
42+
fn test_signum() {
43+
assert_eq_const_safe!((1 as $T).signum(), 1 as $T);
44+
assert_eq_const_safe!((0 as $T).signum(), 0 as $T);
45+
assert_eq_const_safe!((-0 as $T).signum(), 0 as $T);
46+
assert_eq_const_safe!((-1 as $T).signum(), -1 as $T);
47+
}
48+
49+
fn test_is_positive() {
50+
assert!((1 as $T).is_positive());
51+
assert!(!(0 as $T).is_positive());
52+
assert!(!(-0 as $T).is_positive());
53+
assert!(!(-1 as $T).is_positive());
54+
}
55+
56+
fn test_is_negative() {
57+
assert!(!(1 as $T).is_negative());
58+
assert!(!(0 as $T).is_negative());
59+
assert!(!(-0 as $T).is_negative());
60+
assert!((-1 as $T).is_negative());
61+
}
62+
}
63+
6664
const A: $T = 0b0101100;
6765
const B: $T = 0b0100001;
6866
const C: $T = 0b1111001;
6967

7068
const _0: $T = 0;
7169
const _1: $T = !0;
7270

73-
#[test]
74-
fn test_count_ones() {
75-
assert_eq!(A.count_ones(), 3);
76-
assert_eq!(B.count_ones(), 2);
77-
assert_eq!(C.count_ones(), 5);
78-
}
71+
test_runtime_and_compiletime! {
72+
fn test_count_ones() {
73+
assert_eq_const_safe!(A.count_ones(), 3);
74+
assert_eq_const_safe!(B.count_ones(), 2);
75+
assert_eq_const_safe!(C.count_ones(), 5);
76+
}
7977

80-
#[test]
81-
fn test_count_zeros() {
82-
assert_eq!(A.count_zeros(), $T::BITS - 3);
83-
assert_eq!(B.count_zeros(), $T::BITS - 2);
84-
assert_eq!(C.count_zeros(), $T::BITS - 5);
85-
}
78+
fn test_count_zeros() {
79+
assert_eq_const_safe!(A.count_zeros(), $T::BITS - 3);
80+
assert_eq_const_safe!(B.count_zeros(), $T::BITS - 2);
81+
assert_eq_const_safe!(C.count_zeros(), $T::BITS - 5);
82+
}
8683

87-
#[test]
88-
fn test_leading_trailing_ones() {
89-
let a: $T = 0b0101_1111;
90-
assert_eq!(a.trailing_ones(), 5);
91-
assert_eq!((!a).leading_ones(), $T::BITS - 7);
84+
fn test_leading_trailing_ones() {
85+
const A: $T = 0b0101_1111;
86+
assert_eq_const_safe!(A.trailing_ones(), 5);
87+
assert_eq_const_safe!((!A).leading_ones(), $T::BITS - 7);
9288

93-
assert_eq!(a.reverse_bits().leading_ones(), 5);
89+
assert_eq_const_safe!(A.reverse_bits().leading_ones(), 5);
9490

95-
assert_eq!(_1.leading_ones(), $T::BITS);
96-
assert_eq!(_1.trailing_ones(), $T::BITS);
91+
assert_eq_const_safe!(_1.leading_ones(), $T::BITS);
92+
assert_eq_const_safe!(_1.trailing_ones(), $T::BITS);
9793

98-
assert_eq!((_1 << 1).trailing_ones(), 0);
99-
assert_eq!(MAX.leading_ones(), 0);
94+
assert_eq_const_safe!((_1 << 1).trailing_ones(), 0);
95+
assert_eq_const_safe!(MAX.leading_ones(), 0);
10096

101-
assert_eq!((_1 << 1).leading_ones(), $T::BITS - 1);
102-
assert_eq!(MAX.trailing_ones(), $T::BITS - 1);
97+
assert_eq_const_safe!((_1 << 1).leading_ones(), $T::BITS - 1);
98+
assert_eq_const_safe!(MAX.trailing_ones(), $T::BITS - 1);
10399

104-
assert_eq!(_0.leading_ones(), 0);
105-
assert_eq!(_0.trailing_ones(), 0);
100+
assert_eq_const_safe!(_0.leading_ones(), 0);
101+
assert_eq_const_safe!(_0.trailing_ones(), 0);
106102

107-
let x: $T = 0b0010_1100;
108-
assert_eq!(x.leading_ones(), 0);
109-
assert_eq!(x.trailing_ones(), 0);
110-
}
103+
const X: $T = 0b0010_1100;
104+
assert_eq_const_safe!(X.leading_ones(), 0);
105+
assert_eq_const_safe!(X.trailing_ones(), 0);
106+
}
111107

112-
#[test]
113-
fn test_rotate() {
114-
assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
115-
assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
116-
assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
117-
118-
// Rotating these should make no difference
119-
//
120-
// We test using 124 bits because to ensure that overlong bit shifts do
121-
// not cause undefined behaviour. See #10183.
122-
assert_eq!(_0.rotate_left(124), _0);
123-
assert_eq!(_1.rotate_left(124), _1);
124-
assert_eq!(_0.rotate_right(124), _0);
125-
assert_eq!(_1.rotate_right(124), _1);
126-
127-
// Rotating by 0 should have no effect
128-
assert_eq!(A.rotate_left(0), A);
129-
assert_eq!(B.rotate_left(0), B);
130-
assert_eq!(C.rotate_left(0), C);
131-
// Rotating by a multiple of word size should also have no effect
132-
assert_eq!(A.rotate_left(128), A);
133-
assert_eq!(B.rotate_left(128), B);
134-
assert_eq!(C.rotate_left(128), C);
135-
}
108+
fn test_rotate() {
109+
assert_eq_const_safe!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
110+
assert_eq_const_safe!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
111+
assert_eq_const_safe!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
112+
113+
// Rotating these should make no difference
114+
//
115+
// We test using 124 bits because to ensure that overlong bit shifts do
116+
// not cause undefined behaviour. See #10183.
117+
assert_eq_const_safe!(_0.rotate_left(124), _0);
118+
assert_eq_const_safe!(_1.rotate_left(124), _1);
119+
assert_eq_const_safe!(_0.rotate_right(124), _0);
120+
assert_eq_const_safe!(_1.rotate_right(124), _1);
121+
122+
// Rotating by 0 should have no effect
123+
assert_eq_const_safe!(A.rotate_left(0), A);
124+
assert_eq_const_safe!(B.rotate_left(0), B);
125+
assert_eq_const_safe!(C.rotate_left(0), C);
126+
// Rotating by a multiple of word size should also have no effect
127+
assert_eq_const_safe!(A.rotate_left(128), A);
128+
assert_eq_const_safe!(B.rotate_left(128), B);
129+
assert_eq_const_safe!(C.rotate_left(128), C);
130+
}
136131

137-
#[test]
138-
fn test_swap_bytes() {
139-
assert_eq!(A.swap_bytes().swap_bytes(), A);
140-
assert_eq!(B.swap_bytes().swap_bytes(), B);
141-
assert_eq!(C.swap_bytes().swap_bytes(), C);
142-
143-
// Swapping these should make no difference
144-
assert_eq!(_0.swap_bytes(), _0);
145-
assert_eq!(_1.swap_bytes(), _1);
146-
}
132+
fn test_swap_bytes() {
133+
assert_eq_const_safe!(A.swap_bytes().swap_bytes(), A);
134+
assert_eq_const_safe!(B.swap_bytes().swap_bytes(), B);
135+
assert_eq_const_safe!(C.swap_bytes().swap_bytes(), C);
147136

148-
#[test]
149-
fn test_le() {
150-
assert_eq!($T::from_le(A.to_le()), A);
151-
assert_eq!($T::from_le(B.to_le()), B);
152-
assert_eq!($T::from_le(C.to_le()), C);
153-
assert_eq!($T::from_le(_0), _0);
154-
assert_eq!($T::from_le(_1), _1);
155-
assert_eq!(_0.to_le(), _0);
156-
assert_eq!(_1.to_le(), _1);
157-
}
137+
// Swapping these should make no difference
138+
assert_eq_const_safe!(_0.swap_bytes(), _0);
139+
assert_eq_const_safe!(_1.swap_bytes(), _1);
140+
}
158141

159-
#[test]
160-
fn test_be() {
161-
assert_eq!($T::from_be(A.to_be()), A);
162-
assert_eq!($T::from_be(B.to_be()), B);
163-
assert_eq!($T::from_be(C.to_be()), C);
164-
assert_eq!($T::from_be(_0), _0);
165-
assert_eq!($T::from_be(_1), _1);
166-
assert_eq!(_0.to_be(), _0);
167-
assert_eq!(_1.to_be(), _1);
168-
}
142+
fn test_le() {
143+
assert_eq_const_safe!($T::from_le(A.to_le()), A);
144+
assert_eq_const_safe!($T::from_le(B.to_le()), B);
145+
assert_eq_const_safe!($T::from_le(C.to_le()), C);
146+
assert_eq_const_safe!($T::from_le(_0), _0);
147+
assert_eq_const_safe!($T::from_le(_1), _1);
148+
assert_eq_const_safe!(_0.to_le(), _0);
149+
assert_eq_const_safe!(_1.to_le(), _1);
150+
}
169151

170-
#[test]
171-
fn test_signed_checked_div() {
172-
assert_eq!((10 as $T).checked_div(2), Some(5));
173-
assert_eq!((5 as $T).checked_div(0), None);
174-
assert_eq!(isize::MIN.checked_div(-1), None);
175-
}
152+
fn test_be() {
153+
assert_eq_const_safe!($T::from_be(A.to_be()), A);
154+
assert_eq_const_safe!($T::from_be(B.to_be()), B);
155+
assert_eq_const_safe!($T::from_be(C.to_be()), C);
156+
assert_eq_const_safe!($T::from_be(_0), _0);
157+
assert_eq_const_safe!($T::from_be(_1), _1);
158+
assert_eq_const_safe!(_0.to_be(), _0);
159+
assert_eq_const_safe!(_1.to_be(), _1);
160+
}
176161

177-
#[test]
178-
fn test_saturating_abs() {
179-
assert_eq!((0 as $T).saturating_abs(), 0);
180-
assert_eq!((123 as $T).saturating_abs(), 123);
181-
assert_eq!((-123 as $T).saturating_abs(), 123);
182-
assert_eq!((MAX - 2).saturating_abs(), MAX - 2);
183-
assert_eq!((MAX - 1).saturating_abs(), MAX - 1);
184-
assert_eq!(MAX.saturating_abs(), MAX);
185-
assert_eq!((MIN + 2).saturating_abs(), MAX - 1);
186-
assert_eq!((MIN + 1).saturating_abs(), MAX);
187-
assert_eq!(MIN.saturating_abs(), MAX);
188-
}
162+
fn test_signed_checked_div() {
163+
assert_eq_const_safe!((10 as $T).checked_div(2), Some(5));
164+
assert_eq_const_safe!((5 as $T).checked_div(0), None);
165+
assert_eq_const_safe!(isize::MIN.checked_div(-1), None);
166+
}
189167

190-
#[test]
191-
fn test_saturating_neg() {
192-
assert_eq!((0 as $T).saturating_neg(), 0);
193-
assert_eq!((123 as $T).saturating_neg(), -123);
194-
assert_eq!((-123 as $T).saturating_neg(), 123);
195-
assert_eq!((MAX - 2).saturating_neg(), MIN + 3);
196-
assert_eq!((MAX - 1).saturating_neg(), MIN + 2);
197-
assert_eq!(MAX.saturating_neg(), MIN + 1);
198-
assert_eq!((MIN + 2).saturating_neg(), MAX - 1);
199-
assert_eq!((MIN + 1).saturating_neg(), MAX);
200-
assert_eq!(MIN.saturating_neg(), MAX);
168+
fn test_saturating_abs() {
169+
assert_eq_const_safe!((0 as $T).saturating_abs(), 0);
170+
assert_eq_const_safe!((123 as $T).saturating_abs(), 123);
171+
assert_eq_const_safe!((-123 as $T).saturating_abs(), 123);
172+
assert_eq_const_safe!((MAX - 2).saturating_abs(), MAX - 2);
173+
assert_eq_const_safe!((MAX - 1).saturating_abs(), MAX - 1);
174+
assert_eq_const_safe!(MAX.saturating_abs(), MAX);
175+
assert_eq_const_safe!((MIN + 2).saturating_abs(), MAX - 1);
176+
assert_eq_const_safe!((MIN + 1).saturating_abs(), MAX);
177+
assert_eq_const_safe!(MIN.saturating_abs(), MAX);
178+
}
179+
180+
fn test_saturating_neg() {
181+
assert_eq_const_safe!((0 as $T).saturating_neg(), 0);
182+
assert_eq_const_safe!((123 as $T).saturating_neg(), -123);
183+
assert_eq_const_safe!((-123 as $T).saturating_neg(), 123);
184+
assert_eq_const_safe!((MAX - 2).saturating_neg(), MIN + 3);
185+
assert_eq_const_safe!((MAX - 1).saturating_neg(), MIN + 2);
186+
assert_eq_const_safe!(MAX.saturating_neg(), MIN + 1);
187+
assert_eq_const_safe!((MIN + 2).saturating_neg(), MAX - 1);
188+
assert_eq_const_safe!((MIN + 1).saturating_neg(), MAX);
189+
assert_eq_const_safe!(MIN.saturating_neg(), MAX);
190+
}
201191
}
202192

203193
#[test]
@@ -222,173 +212,173 @@ macro_rules! int_module {
222212
assert_eq!(from_str::<$T>("x"), None);
223213
}
224214

225-
#[test]
226-
fn test_from_str_radix() {
227-
assert_eq!($T::from_str_radix("123", 10), Ok(123 as $T));
228-
assert_eq!($T::from_str_radix("1001", 2), Ok(9 as $T));
229-
assert_eq!($T::from_str_radix("123", 8), Ok(83 as $T));
230-
assert_eq!(i32::from_str_radix("123", 16), Ok(291 as i32));
231-
assert_eq!(i32::from_str_radix("ffff", 16), Ok(65535 as i32));
232-
assert_eq!(i32::from_str_radix("FFFF", 16), Ok(65535 as i32));
233-
assert_eq!($T::from_str_radix("z", 36), Ok(35 as $T));
234-
assert_eq!($T::from_str_radix("Z", 36), Ok(35 as $T));
235-
236-
assert_eq!($T::from_str_radix("-123", 10), Ok(-123 as $T));
237-
assert_eq!($T::from_str_radix("-1001", 2), Ok(-9 as $T));
238-
assert_eq!($T::from_str_radix("-123", 8), Ok(-83 as $T));
239-
assert_eq!(i32::from_str_radix("-123", 16), Ok(-291 as i32));
240-
assert_eq!(i32::from_str_radix("-ffff", 16), Ok(-65535 as i32));
241-
assert_eq!(i32::from_str_radix("-FFFF", 16), Ok(-65535 as i32));
242-
assert_eq!($T::from_str_radix("-z", 36), Ok(-35 as $T));
243-
assert_eq!($T::from_str_radix("-Z", 36), Ok(-35 as $T));
244-
245-
assert_eq!($T::from_str_radix("Z", 35).ok(), None::<$T>);
246-
assert_eq!($T::from_str_radix("-9", 2).ok(), None::<$T>);
247-
assert_eq!($T::from_str_radix("10_0", 10).ok(), None::<$T>);
248-
assert_eq!(u32::from_str_radix("-9", 10).ok(), None::<u32>);
249-
}
215+
test_runtime_and_compiletime! {
216+
fn test_from_str_radix() {
217+
assert_eq_const_safe!($T::from_str_radix("123", 10), Ok(123 as $T));
218+
assert_eq_const_safe!($T::from_str_radix("1001", 2), Ok(9 as $T));
219+
assert_eq_const_safe!($T::from_str_radix("123", 8), Ok(83 as $T));
220+
assert_eq_const_safe!(i32::from_str_radix("123", 16), Ok(291 as i32));
221+
assert_eq_const_safe!(i32::from_str_radix("ffff", 16), Ok(65535 as i32));
222+
assert_eq_const_safe!(i32::from_str_radix("FFFF", 16), Ok(65535 as i32));
223+
assert_eq_const_safe!($T::from_str_radix("z", 36), Ok(35 as $T));
224+
assert_eq_const_safe!($T::from_str_radix("Z", 36), Ok(35 as $T));
225+
226+
assert_eq_const_safe!($T::from_str_radix("-123", 10), Ok(-123 as $T));
227+
assert_eq_const_safe!($T::from_str_radix("-1001", 2), Ok(-9 as $T));
228+
assert_eq_const_safe!($T::from_str_radix("-123", 8), Ok(-83 as $T));
229+
assert_eq_const_safe!(i32::from_str_radix("-123", 16), Ok(-291 as i32));
230+
assert_eq_const_safe!(i32::from_str_radix("-ffff", 16), Ok(-65535 as i32));
231+
assert_eq_const_safe!(i32::from_str_radix("-FFFF", 16), Ok(-65535 as i32));
232+
assert_eq_const_safe!($T::from_str_radix("-z", 36), Ok(-35 as $T));
233+
assert_eq_const_safe!($T::from_str_radix("-Z", 36), Ok(-35 as $T));
234+
235+
assert!($T::from_str_radix("Z", 35).is_err());
236+
assert!($T::from_str_radix("-9", 2).is_err());
237+
assert!($T::from_str_radix("10_0", 10).is_err());
238+
assert!(u32::from_str_radix("-9", 10).is_err());
239+
}
250240

251-
#[test]
252-
fn test_pow() {
253-
let mut r = 2 as $T;
254-
assert_eq!(r.pow(2), 4 as $T);
255-
assert_eq!(r.pow(0), 1 as $T);
256-
assert_eq!(r.wrapping_pow(2), 4 as $T);
257-
assert_eq!(r.wrapping_pow(0), 1 as $T);
258-
assert_eq!(r.checked_pow(2), Some(4 as $T));
259-
assert_eq!(r.checked_pow(0), Some(1 as $T));
260-
assert_eq!(r.overflowing_pow(2), (4 as $T, false));
261-
assert_eq!(r.overflowing_pow(0), (1 as $T, false));
262-
assert_eq!(r.saturating_pow(2), 4 as $T);
263-
assert_eq!(r.saturating_pow(0), 1 as $T);
264-
265-
r = MAX;
266-
// use `^` to represent .pow() with no overflow.
267-
// if itest::MAX == 2^j-1, then itest is a `j` bit int,
268-
// so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
269-
// thussaturating_pow the overflowing result is exactly 1.
270-
assert_eq!(r.wrapping_pow(2), 1 as $T);
271-
assert_eq!(r.checked_pow(2), None);
272-
assert_eq!(r.overflowing_pow(2), (1 as $T, true));
273-
assert_eq!(r.saturating_pow(2), MAX);
274-
//test for negative exponent.
275-
r = -2 as $T;
276-
assert_eq!(r.pow(2), 4 as $T);
277-
assert_eq!(r.pow(3), -8 as $T);
278-
assert_eq!(r.pow(0), 1 as $T);
279-
assert_eq!(r.wrapping_pow(2), 4 as $T);
280-
assert_eq!(r.wrapping_pow(3), -8 as $T);
281-
assert_eq!(r.wrapping_pow(0), 1 as $T);
282-
assert_eq!(r.checked_pow(2), Some(4 as $T));
283-
assert_eq!(r.checked_pow(3), Some(-8 as $T));
284-
assert_eq!(r.checked_pow(0), Some(1 as $T));
285-
assert_eq!(r.overflowing_pow(2), (4 as $T, false));
286-
assert_eq!(r.overflowing_pow(3), (-8 as $T, false));
287-
assert_eq!(r.overflowing_pow(0), (1 as $T, false));
288-
assert_eq!(r.saturating_pow(2), 4 as $T);
289-
assert_eq!(r.saturating_pow(3), -8 as $T);
290-
assert_eq!(r.saturating_pow(0), 1 as $T);
291-
}
241+
fn test_pow() {
242+
{
243+
const R: $T = 2;
244+
assert_eq_const_safe!(R.pow(2), 4 as $T);
245+
assert_eq_const_safe!(R.pow(0), 1 as $T);
246+
assert_eq_const_safe!(R.wrapping_pow(2), 4 as $T);
247+
assert_eq_const_safe!(R.wrapping_pow(0), 1 as $T);
248+
assert_eq_const_safe!(R.checked_pow(2), Some(4 as $T));
249+
assert_eq_const_safe!(R.checked_pow(0), Some(1 as $T));
250+
assert_eq_const_safe!(R.overflowing_pow(2), (4 as $T, false));
251+
assert_eq_const_safe!(R.overflowing_pow(0), (1 as $T, false));
252+
assert_eq_const_safe!(R.saturating_pow(2), 4 as $T);
253+
assert_eq_const_safe!(R.saturating_pow(0), 1 as $T);
254+
}
255+
256+
{
257+
const R: $T = MAX;
258+
// use `^` to represent .pow() with no overflow.
259+
// if itest::MAX == 2^j-1, then itest is a `j` bit int,
260+
// so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
261+
// thussaturating_pow the overflowing result is exactly 1.
262+
assert_eq_const_safe!(R.wrapping_pow(2), 1 as $T);
263+
assert_eq_const_safe!(R.checked_pow(2), None);
264+
assert_eq_const_safe!(R.overflowing_pow(2), (1 as $T, true));
265+
assert_eq_const_safe!(R.saturating_pow(2), MAX);
266+
}
267+
268+
{
269+
// test for negative exponent.
270+
const R: $T = -2;
271+
assert_eq_const_safe!(R.pow(2), 4 as $T);
272+
assert_eq_const_safe!(R.pow(3), -8 as $T);
273+
assert_eq_const_safe!(R.pow(0), 1 as $T);
274+
assert_eq_const_safe!(R.wrapping_pow(2), 4 as $T);
275+
assert_eq_const_safe!(R.wrapping_pow(3), -8 as $T);
276+
assert_eq_const_safe!(R.wrapping_pow(0), 1 as $T);
277+
assert_eq_const_safe!(R.checked_pow(2), Some(4 as $T));
278+
assert_eq_const_safe!(R.checked_pow(3), Some(-8 as $T));
279+
assert_eq_const_safe!(R.checked_pow(0), Some(1 as $T));
280+
assert_eq_const_safe!(R.overflowing_pow(2), (4 as $T, false));
281+
assert_eq_const_safe!(R.overflowing_pow(3), (-8 as $T, false));
282+
assert_eq_const_safe!(R.overflowing_pow(0), (1 as $T, false));
283+
assert_eq_const_safe!(R.saturating_pow(2), 4 as $T);
284+
assert_eq_const_safe!(R.saturating_pow(3), -8 as $T);
285+
assert_eq_const_safe!(R.saturating_pow(0), 1 as $T);
286+
}
287+
}
292288

293-
#[test]
294-
fn test_div_floor() {
295-
let a: $T = 8;
296-
let b = 3;
297-
assert_eq!(a.div_floor(b), 2);
298-
assert_eq!(a.div_floor(-b), -3);
299-
assert_eq!((-a).div_floor(b), -3);
300-
assert_eq!((-a).div_floor(-b), 2);
301-
}
289+
fn test_div_floor() {
290+
const A: $T = 8;
291+
const B: $T = 3;
292+
assert_eq_const_safe!(A.div_floor(B), 2);
293+
assert_eq_const_safe!(A.div_floor(-B), -3);
294+
assert_eq_const_safe!((-A).div_floor(B), -3);
295+
assert_eq_const_safe!((-A).div_floor(-B), 2);
296+
}
302297

303-
#[test]
304-
fn test_div_ceil() {
305-
let a: $T = 8;
306-
let b = 3;
307-
assert_eq!(a.div_ceil(b), 3);
308-
assert_eq!(a.div_ceil(-b), -2);
309-
assert_eq!((-a).div_ceil(b), -2);
310-
assert_eq!((-a).div_ceil(-b), 3);
311-
}
298+
fn test_div_ceil() {
299+
const A: $T = 8;
300+
const B: $T = 3;
301+
assert_eq_const_safe!(A.div_ceil(B), 3);
302+
assert_eq_const_safe!(A.div_ceil(-B), -2);
303+
assert_eq_const_safe!((-A).div_ceil(B), -2);
304+
assert_eq_const_safe!((-A).div_ceil(-B), 3);
305+
}
312306

313-
#[test]
314-
fn test_next_multiple_of() {
315-
assert_eq!((16 as $T).next_multiple_of(8), 16);
316-
assert_eq!((23 as $T).next_multiple_of(8), 24);
317-
assert_eq!((16 as $T).next_multiple_of(-8), 16);
318-
assert_eq!((23 as $T).next_multiple_of(-8), 16);
319-
assert_eq!((-16 as $T).next_multiple_of(8), -16);
320-
assert_eq!((-23 as $T).next_multiple_of(8), -16);
321-
assert_eq!((-16 as $T).next_multiple_of(-8), -16);
322-
assert_eq!((-23 as $T).next_multiple_of(-8), -24);
323-
assert_eq!(MIN.next_multiple_of(-1), MIN);
324-
}
307+
fn test_next_multiple_of() {
308+
assert_eq_const_safe!((16 as $T).next_multiple_of(8), 16);
309+
assert_eq_const_safe!((23 as $T).next_multiple_of(8), 24);
310+
assert_eq_const_safe!((16 as $T).next_multiple_of(-8), 16);
311+
assert_eq_const_safe!((23 as $T).next_multiple_of(-8), 16);
312+
assert_eq_const_safe!((-16 as $T).next_multiple_of(8), -16);
313+
assert_eq_const_safe!((-23 as $T).next_multiple_of(8), -16);
314+
assert_eq_const_safe!((-16 as $T).next_multiple_of(-8), -16);
315+
assert_eq_const_safe!((-23 as $T).next_multiple_of(-8), -24);
316+
assert_eq_const_safe!(MIN.next_multiple_of(-1), MIN);
317+
}
325318

326-
#[test]
327-
fn test_checked_next_multiple_of() {
328-
assert_eq!((16 as $T).checked_next_multiple_of(8), Some(16));
329-
assert_eq!((23 as $T).checked_next_multiple_of(8), Some(24));
330-
assert_eq!((16 as $T).checked_next_multiple_of(-8), Some(16));
331-
assert_eq!((23 as $T).checked_next_multiple_of(-8), Some(16));
332-
assert_eq!((-16 as $T).checked_next_multiple_of(8), Some(-16));
333-
assert_eq!((-23 as $T).checked_next_multiple_of(8), Some(-16));
334-
assert_eq!((-16 as $T).checked_next_multiple_of(-8), Some(-16));
335-
assert_eq!((-23 as $T).checked_next_multiple_of(-8), Some(-24));
336-
assert_eq!((1 as $T).checked_next_multiple_of(0), None);
337-
assert_eq!(MAX.checked_next_multiple_of(2), None);
338-
assert_eq!(MIN.checked_next_multiple_of(-3), None);
339-
assert_eq!(MIN.checked_next_multiple_of(-1), Some(MIN));
340-
}
319+
fn test_checked_next_multiple_of() {
320+
assert_eq_const_safe!((16 as $T).checked_next_multiple_of(8), Some(16));
321+
assert_eq_const_safe!((23 as $T).checked_next_multiple_of(8), Some(24));
322+
assert_eq_const_safe!((16 as $T).checked_next_multiple_of(-8), Some(16));
323+
assert_eq_const_safe!((23 as $T).checked_next_multiple_of(-8), Some(16));
324+
assert_eq_const_safe!((-16 as $T).checked_next_multiple_of(8), Some(-16));
325+
assert_eq_const_safe!((-23 as $T).checked_next_multiple_of(8), Some(-16));
326+
assert_eq_const_safe!((-16 as $T).checked_next_multiple_of(-8), Some(-16));
327+
assert_eq_const_safe!((-23 as $T).checked_next_multiple_of(-8), Some(-24));
328+
assert_eq_const_safe!((1 as $T).checked_next_multiple_of(0), None);
329+
assert_eq_const_safe!(MAX.checked_next_multiple_of(2), None);
330+
assert_eq_const_safe!(MIN.checked_next_multiple_of(-3), None);
331+
assert_eq_const_safe!(MIN.checked_next_multiple_of(-1), Some(MIN));
332+
}
341333

342-
#[test]
343-
fn test_carrying_add() {
344-
assert_eq!($T::MAX.carrying_add(1, false), ($T::MIN, true));
345-
assert_eq!($T::MAX.carrying_add(0, true), ($T::MIN, true));
346-
assert_eq!($T::MAX.carrying_add(1, true), ($T::MIN + 1, true));
347-
assert_eq!($T::MAX.carrying_add(-1, false), ($T::MAX - 1, false));
348-
assert_eq!($T::MAX.carrying_add(-1, true), ($T::MAX, false)); // no intermediate overflow
349-
assert_eq!($T::MIN.carrying_add(-1, false), ($T::MAX, true));
350-
assert_eq!($T::MIN.carrying_add(-1, true), ($T::MIN, false)); // no intermediate overflow
351-
assert_eq!((0 as $T).carrying_add($T::MAX, true), ($T::MIN, true));
352-
assert_eq!((0 as $T).carrying_add($T::MIN, true), ($T::MIN + 1, false));
353-
}
334+
fn test_carrying_add() {
335+
assert_eq_const_safe!(MAX.carrying_add(1, false), (MIN, true));
336+
assert_eq_const_safe!(MAX.carrying_add(0, true), (MIN, true));
337+
assert_eq_const_safe!(MAX.carrying_add(1, true), (MIN + 1, true));
338+
assert_eq_const_safe!(MAX.carrying_add(-1, false), (MAX - 1, false));
339+
assert_eq_const_safe!(MAX.carrying_add(-1, true), (MAX, false)); // no intermediate overflow
340+
assert_eq_const_safe!(MIN.carrying_add(-1, false), (MAX, true));
341+
assert_eq_const_safe!(MIN.carrying_add(-1, true), (MIN, false)); // no intermediate overflow
342+
assert_eq_const_safe!((0 as $T).carrying_add(MAX, true), (MIN, true));
343+
assert_eq_const_safe!((0 as $T).carrying_add(MIN, true), (MIN + 1, false));
344+
}
354345

355-
#[test]
356-
fn test_borrowing_sub() {
357-
assert_eq!($T::MIN.borrowing_sub(1, false), ($T::MAX, true));
358-
assert_eq!($T::MIN.borrowing_sub(0, true), ($T::MAX, true));
359-
assert_eq!($T::MIN.borrowing_sub(1, true), ($T::MAX - 1, true));
360-
assert_eq!($T::MIN.borrowing_sub(-1, false), ($T::MIN + 1, false));
361-
assert_eq!($T::MIN.borrowing_sub(-1, true), ($T::MIN, false)); // no intermediate overflow
362-
assert_eq!($T::MAX.borrowing_sub(-1, false), ($T::MIN, true));
363-
assert_eq!($T::MAX.borrowing_sub(-1, true), ($T::MAX, false)); // no intermediate overflow
364-
assert_eq!((0 as $T).borrowing_sub($T::MIN, false), ($T::MIN, true));
365-
assert_eq!((0 as $T).borrowing_sub($T::MIN, true), ($T::MAX, false));
366-
}
346+
fn test_borrowing_sub() {
347+
assert_eq_const_safe!(MIN.borrowing_sub(1, false), (MAX, true));
348+
assert_eq_const_safe!(MIN.borrowing_sub(0, true), (MAX, true));
349+
assert_eq_const_safe!(MIN.borrowing_sub(1, true), (MAX - 1, true));
350+
assert_eq_const_safe!(MIN.borrowing_sub(-1, false), (MIN + 1, false));
351+
assert_eq_const_safe!(MIN.borrowing_sub(-1, true), (MIN, false)); // no intermediate overflow
352+
assert_eq_const_safe!(MAX.borrowing_sub(-1, false), (MIN, true));
353+
assert_eq_const_safe!(MAX.borrowing_sub(-1, true), (MAX, false)); // no intermediate overflow
354+
assert_eq_const_safe!((0 as $T).borrowing_sub(MIN, false), (MIN, true));
355+
assert_eq_const_safe!((0 as $T).borrowing_sub(MIN, true), (MAX, false));
356+
}
367357

368-
#[test]
369-
fn test_midpoint() {
370-
assert_eq!(<$T>::midpoint(1, 3), 2);
371-
assert_eq!(<$T>::midpoint(3, 1), 2);
372-
373-
assert_eq!(<$T>::midpoint(0, 0), 0);
374-
assert_eq!(<$T>::midpoint(0, 2), 1);
375-
assert_eq!(<$T>::midpoint(2, 0), 1);
376-
assert_eq!(<$T>::midpoint(2, 2), 2);
377-
378-
assert_eq!(<$T>::midpoint(1, 4), 2);
379-
assert_eq!(<$T>::midpoint(4, 1), 2);
380-
assert_eq!(<$T>::midpoint(3, 4), 3);
381-
assert_eq!(<$T>::midpoint(4, 3), 3);
382-
383-
assert_eq!(<$T>::midpoint(<$T>::MIN, <$T>::MAX), -1);
384-
assert_eq!(<$T>::midpoint(<$T>::MAX, <$T>::MIN), -1);
385-
assert_eq!(<$T>::midpoint(<$T>::MIN, <$T>::MIN), <$T>::MIN);
386-
assert_eq!(<$T>::midpoint(<$T>::MAX, <$T>::MAX), <$T>::MAX);
387-
388-
assert_eq!(<$T>::midpoint(<$T>::MIN, 6), <$T>::MIN / 2 + 3);
389-
assert_eq!(<$T>::midpoint(6, <$T>::MIN), <$T>::MIN / 2 + 3);
390-
assert_eq!(<$T>::midpoint(<$T>::MAX, 6), <$T>::MAX / 2 + 3);
391-
assert_eq!(<$T>::midpoint(6, <$T>::MAX), <$T>::MAX / 2 + 3);
358+
fn test_midpoint() {
359+
assert_eq_const_safe!(<$T>::midpoint(1, 3), 2);
360+
assert_eq_const_safe!(<$T>::midpoint(3, 1), 2);
361+
362+
assert_eq_const_safe!(<$T>::midpoint(0, 0), 0);
363+
assert_eq_const_safe!(<$T>::midpoint(0, 2), 1);
364+
assert_eq_const_safe!(<$T>::midpoint(2, 0), 1);
365+
assert_eq_const_safe!(<$T>::midpoint(2, 2), 2);
366+
367+
assert_eq_const_safe!(<$T>::midpoint(1, 4), 2);
368+
assert_eq_const_safe!(<$T>::midpoint(4, 1), 2);
369+
assert_eq_const_safe!(<$T>::midpoint(3, 4), 3);
370+
assert_eq_const_safe!(<$T>::midpoint(4, 3), 3);
371+
372+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, <$T>::MAX), -1);
373+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, <$T>::MIN), -1);
374+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, <$T>::MIN), <$T>::MIN);
375+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, <$T>::MAX), <$T>::MAX);
376+
377+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, 6), <$T>::MIN / 2 + 3);
378+
assert_eq_const_safe!(<$T>::midpoint(6, <$T>::MIN), <$T>::MIN / 2 + 3);
379+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, 6), <$T>::MAX / 2 + 3);
380+
assert_eq_const_safe!(<$T>::midpoint(6, <$T>::MAX), <$T>::MAX / 2 + 3);
381+
}
392382
}
393383
};
394384
}

‎core/tests/num/uint_macros.rs

+206-217
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ macro_rules! uint_module {
22
($T:ident) => {
33
use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
44
use core::$T::*;
5-
use std::str::FromStr;
65

76
use crate::num;
87

@@ -35,122 +34,115 @@ macro_rules! uint_module {
3534
const _0: $T = 0;
3635
const _1: $T = !0;
3736

38-
#[test]
39-
fn test_count_ones() {
40-
assert!(A.count_ones() == 3);
41-
assert!(B.count_ones() == 2);
42-
assert!(C.count_ones() == 5);
43-
}
37+
test_runtime_and_compiletime! {
38+
fn test_count_ones() {
39+
assert!(A.count_ones() == 3);
40+
assert!(B.count_ones() == 2);
41+
assert!(C.count_ones() == 5);
42+
}
4443

45-
#[test]
46-
fn test_count_zeros() {
47-
assert!(A.count_zeros() == $T::BITS - 3);
48-
assert!(B.count_zeros() == $T::BITS - 2);
49-
assert!(C.count_zeros() == $T::BITS - 5);
50-
}
44+
fn test_count_zeros() {
45+
assert!(A.count_zeros() == $T::BITS - 3);
46+
assert!(B.count_zeros() == $T::BITS - 2);
47+
assert!(C.count_zeros() == $T::BITS - 5);
48+
}
5149

52-
#[test]
53-
fn test_leading_trailing_ones() {
54-
let a: $T = 0b0101_1111;
55-
assert_eq!(a.trailing_ones(), 5);
56-
assert_eq!((!a).leading_ones(), $T::BITS - 7);
50+
fn test_leading_trailing_ones() {
51+
const A: $T = 0b0101_1111;
52+
assert_eq_const_safe!(A.trailing_ones(), 5);
53+
assert_eq_const_safe!((!A).leading_ones(), $T::BITS - 7);
5754

58-
assert_eq!(a.reverse_bits().leading_ones(), 5);
55+
assert_eq_const_safe!(A.reverse_bits().leading_ones(), 5);
5956

60-
assert_eq!(_1.leading_ones(), $T::BITS);
61-
assert_eq!(_1.trailing_ones(), $T::BITS);
57+
assert_eq_const_safe!(_1.leading_ones(), $T::BITS);
58+
assert_eq_const_safe!(_1.trailing_ones(), $T::BITS);
6259

63-
assert_eq!((_1 << 1).trailing_ones(), 0);
64-
assert_eq!((_1 >> 1).leading_ones(), 0);
60+
assert_eq_const_safe!((_1 << 1).trailing_ones(), 0);
61+
assert_eq_const_safe!((_1 >> 1).leading_ones(), 0);
6562

66-
assert_eq!((_1 << 1).leading_ones(), $T::BITS - 1);
67-
assert_eq!((_1 >> 1).trailing_ones(), $T::BITS - 1);
63+
assert_eq_const_safe!((_1 << 1).leading_ones(), $T::BITS - 1);
64+
assert_eq_const_safe!((_1 >> 1).trailing_ones(), $T::BITS - 1);
6865

69-
assert_eq!(_0.leading_ones(), 0);
70-
assert_eq!(_0.trailing_ones(), 0);
66+
assert_eq_const_safe!(_0.leading_ones(), 0);
67+
assert_eq_const_safe!(_0.trailing_ones(), 0);
7168

72-
let x: $T = 0b0010_1100;
73-
assert_eq!(x.leading_ones(), 0);
74-
assert_eq!(x.trailing_ones(), 0);
75-
}
69+
const X: $T = 0b0010_1100;
70+
assert_eq_const_safe!(X.leading_ones(), 0);
71+
assert_eq_const_safe!(X.trailing_ones(), 0);
72+
}
7673

77-
#[test]
78-
fn test_rotate() {
79-
assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
80-
assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
81-
assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
82-
83-
// Rotating these should make no difference
84-
//
85-
// We test using 124 bits because to ensure that overlong bit shifts do
86-
// not cause undefined behaviour. See #10183.
87-
assert_eq!(_0.rotate_left(124), _0);
88-
assert_eq!(_1.rotate_left(124), _1);
89-
assert_eq!(_0.rotate_right(124), _0);
90-
assert_eq!(_1.rotate_right(124), _1);
91-
92-
// Rotating by 0 should have no effect
93-
assert_eq!(A.rotate_left(0), A);
94-
assert_eq!(B.rotate_left(0), B);
95-
assert_eq!(C.rotate_left(0), C);
96-
// Rotating by a multiple of word size should also have no effect
97-
assert_eq!(A.rotate_left(128), A);
98-
assert_eq!(B.rotate_left(128), B);
99-
assert_eq!(C.rotate_left(128), C);
100-
}
74+
fn test_rotate() {
75+
assert_eq_const_safe!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
76+
assert_eq_const_safe!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
77+
assert_eq_const_safe!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
78+
79+
// Rotating these should make no difference
80+
//
81+
// We test using 124 bits because to ensure that overlong bit shifts do
82+
// not cause undefined behaviour. See #10183.
83+
assert_eq_const_safe!(_0.rotate_left(124), _0);
84+
assert_eq_const_safe!(_1.rotate_left(124), _1);
85+
assert_eq_const_safe!(_0.rotate_right(124), _0);
86+
assert_eq_const_safe!(_1.rotate_right(124), _1);
87+
88+
// Rotating by 0 should have no effect
89+
assert_eq_const_safe!(A.rotate_left(0), A);
90+
assert_eq_const_safe!(B.rotate_left(0), B);
91+
assert_eq_const_safe!(C.rotate_left(0), C);
92+
// Rotating by a multiple of word size should also have no effect
93+
assert_eq_const_safe!(A.rotate_left(128), A);
94+
assert_eq_const_safe!(B.rotate_left(128), B);
95+
assert_eq_const_safe!(C.rotate_left(128), C);
96+
}
10197

102-
#[test]
103-
fn test_swap_bytes() {
104-
assert_eq!(A.swap_bytes().swap_bytes(), A);
105-
assert_eq!(B.swap_bytes().swap_bytes(), B);
106-
assert_eq!(C.swap_bytes().swap_bytes(), C);
107-
108-
// Swapping these should make no difference
109-
assert_eq!(_0.swap_bytes(), _0);
110-
assert_eq!(_1.swap_bytes(), _1);
111-
}
98+
fn test_swap_bytes() {
99+
assert_eq_const_safe!(A.swap_bytes().swap_bytes(), A);
100+
assert_eq_const_safe!(B.swap_bytes().swap_bytes(), B);
101+
assert_eq_const_safe!(C.swap_bytes().swap_bytes(), C);
112102

113-
#[test]
114-
fn test_reverse_bits() {
115-
assert_eq!(A.reverse_bits().reverse_bits(), A);
116-
assert_eq!(B.reverse_bits().reverse_bits(), B);
117-
assert_eq!(C.reverse_bits().reverse_bits(), C);
118-
119-
// Swapping these should make no difference
120-
assert_eq!(_0.reverse_bits(), _0);
121-
assert_eq!(_1.reverse_bits(), _1);
122-
}
103+
// Swapping these should make no difference
104+
assert_eq_const_safe!(_0.swap_bytes(), _0);
105+
assert_eq_const_safe!(_1.swap_bytes(), _1);
106+
}
123107

124-
#[test]
125-
fn test_le() {
126-
assert_eq!($T::from_le(A.to_le()), A);
127-
assert_eq!($T::from_le(B.to_le()), B);
128-
assert_eq!($T::from_le(C.to_le()), C);
129-
assert_eq!($T::from_le(_0), _0);
130-
assert_eq!($T::from_le(_1), _1);
131-
assert_eq!(_0.to_le(), _0);
132-
assert_eq!(_1.to_le(), _1);
133-
}
108+
fn test_reverse_bits() {
109+
assert_eq_const_safe!(A.reverse_bits().reverse_bits(), A);
110+
assert_eq_const_safe!(B.reverse_bits().reverse_bits(), B);
111+
assert_eq_const_safe!(C.reverse_bits().reverse_bits(), C);
134112

135-
#[test]
136-
fn test_be() {
137-
assert_eq!($T::from_be(A.to_be()), A);
138-
assert_eq!($T::from_be(B.to_be()), B);
139-
assert_eq!($T::from_be(C.to_be()), C);
140-
assert_eq!($T::from_be(_0), _0);
141-
assert_eq!($T::from_be(_1), _1);
142-
assert_eq!(_0.to_be(), _0);
143-
assert_eq!(_1.to_be(), _1);
144-
}
113+
// Swapping these should make no difference
114+
assert_eq_const_safe!(_0.reverse_bits(), _0);
115+
assert_eq_const_safe!(_1.reverse_bits(), _1);
116+
}
145117

146-
#[test]
147-
fn test_unsigned_checked_div() {
148-
assert!((10 as $T).checked_div(2) == Some(5));
149-
assert!((5 as $T).checked_div(0) == None);
118+
fn test_le() {
119+
assert_eq_const_safe!($T::from_le(A.to_le()), A);
120+
assert_eq_const_safe!($T::from_le(B.to_le()), B);
121+
assert_eq_const_safe!($T::from_le(C.to_le()), C);
122+
assert_eq_const_safe!($T::from_le(_0), _0);
123+
assert_eq_const_safe!($T::from_le(_1), _1);
124+
assert_eq_const_safe!(_0.to_le(), _0);
125+
assert_eq_const_safe!(_1.to_le(), _1);
126+
}
127+
128+
fn test_be() {
129+
assert_eq_const_safe!($T::from_be(A.to_be()), A);
130+
assert_eq_const_safe!($T::from_be(B.to_be()), B);
131+
assert_eq_const_safe!($T::from_be(C.to_be()), C);
132+
assert_eq_const_safe!($T::from_be(_0), _0);
133+
assert_eq_const_safe!($T::from_be(_1), _1);
134+
assert_eq_const_safe!(_0.to_be(), _0);
135+
assert_eq_const_safe!(_1.to_be(), _1);
136+
}
137+
138+
fn test_unsigned_checked_div() {
139+
assert_eq_const_safe!((10 as $T).checked_div(2), Some(5));
140+
assert_eq_const_safe!((5 as $T).checked_div(0), None);
141+
}
150142
}
151143

152-
fn from_str<T: FromStr>(t: &str) -> Option<T> {
153-
FromStr::from_str(t).ok()
144+
fn from_str<T: core::str::FromStr>(t: &str) -> Option<T> {
145+
core::str::FromStr::from_str(t).ok()
154146
}
155147

156148
#[test]
@@ -166,52 +158,55 @@ macro_rules! uint_module {
166158
assert_eq!(from_str::<$T>("x"), None);
167159
}
168160

169-
#[test]
170-
pub fn test_parse_bytes() {
171-
assert_eq!($T::from_str_radix("123", 10), Ok(123 as $T));
172-
assert_eq!($T::from_str_radix("1001", 2), Ok(9 as $T));
173-
assert_eq!($T::from_str_radix("123", 8), Ok(83 as $T));
174-
assert_eq!(u16::from_str_radix("123", 16), Ok(291 as u16));
175-
assert_eq!(u16::from_str_radix("ffff", 16), Ok(65535 as u16));
176-
assert_eq!($T::from_str_radix("z", 36), Ok(35 as $T));
177-
178-
assert_eq!($T::from_str_radix("Z", 10).ok(), None::<$T>);
179-
assert_eq!($T::from_str_radix("_", 2).ok(), None::<$T>);
180-
}
161+
test_runtime_and_compiletime! {
162+
fn test_parse_bytes() {
163+
assert_eq_const_safe!($T::from_str_radix("123", 10), Ok(123 as $T));
164+
assert_eq_const_safe!($T::from_str_radix("1001", 2), Ok(9 as $T));
165+
assert_eq_const_safe!($T::from_str_radix("123", 8), Ok(83 as $T));
166+
assert_eq_const_safe!(u16::from_str_radix("123", 16), Ok(291 as u16));
167+
assert_eq_const_safe!(u16::from_str_radix("ffff", 16), Ok(65535 as u16));
168+
assert_eq_const_safe!($T::from_str_radix("z", 36), Ok(35 as $T));
169+
170+
assert!($T::from_str_radix("Z", 10).is_err());
171+
assert!($T::from_str_radix("_", 2).is_err());
172+
}
181173

182-
#[test]
183-
fn test_pow() {
184-
let mut r = 2 as $T;
185-
assert_eq!(r.pow(2), 4 as $T);
186-
assert_eq!(r.pow(0), 1 as $T);
187-
assert_eq!(r.wrapping_pow(2), 4 as $T);
188-
assert_eq!(r.wrapping_pow(0), 1 as $T);
189-
assert_eq!(r.checked_pow(2), Some(4 as $T));
190-
assert_eq!(r.checked_pow(0), Some(1 as $T));
191-
assert_eq!(r.overflowing_pow(2), (4 as $T, false));
192-
assert_eq!(r.overflowing_pow(0), (1 as $T, false));
193-
assert_eq!(r.saturating_pow(2), 4 as $T);
194-
assert_eq!(r.saturating_pow(0), 1 as $T);
195-
196-
r = MAX;
197-
// use `^` to represent .pow() with no overflow.
198-
// if itest::MAX == 2^j-1, then itest is a `j` bit int,
199-
// so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
200-
// thussaturating_pow the overflowing result is exactly 1.
201-
assert_eq!(r.wrapping_pow(2), 1 as $T);
202-
assert_eq!(r.checked_pow(2), None);
203-
assert_eq!(r.overflowing_pow(2), (1 as $T, true));
204-
assert_eq!(r.saturating_pow(2), MAX);
205-
}
174+
fn test_pow() {
175+
{
176+
const R: $T = 2;
177+
assert_eq_const_safe!(R.pow(2), 4 as $T);
178+
assert_eq_const_safe!(R.pow(0), 1 as $T);
179+
assert_eq_const_safe!(R.wrapping_pow(2), 4 as $T);
180+
assert_eq_const_safe!(R.wrapping_pow(0), 1 as $T);
181+
assert_eq_const_safe!(R.checked_pow(2), Some(4 as $T));
182+
assert_eq_const_safe!(R.checked_pow(0), Some(1 as $T));
183+
assert_eq_const_safe!(R.overflowing_pow(2), (4 as $T, false));
184+
assert_eq_const_safe!(R.overflowing_pow(0), (1 as $T, false));
185+
assert_eq_const_safe!(R.saturating_pow(2), 4 as $T);
186+
assert_eq_const_safe!(R.saturating_pow(0), 1 as $T);
187+
}
188+
189+
{
190+
const R: $T = $T::MAX;
191+
// use `^` to represent .pow() with no overflow.
192+
// if itest::MAX == 2^j-1, then itest is a `j` bit int,
193+
// so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
194+
// thussaturating_pow the overflowing result is exactly 1.
195+
assert_eq_const_safe!(R.wrapping_pow(2), 1 as $T);
196+
assert_eq_const_safe!(R.checked_pow(2), None);
197+
assert_eq_const_safe!(R.overflowing_pow(2), (1 as $T, true));
198+
assert_eq_const_safe!(R.saturating_pow(2), MAX);
199+
}
200+
}
206201

207-
#[test]
208-
fn test_isqrt() {
209-
assert_eq!((0 as $T).isqrt(), 0 as $T);
210-
assert_eq!((1 as $T).isqrt(), 1 as $T);
211-
assert_eq!((2 as $T).isqrt(), 1 as $T);
212-
assert_eq!((99 as $T).isqrt(), 9 as $T);
213-
assert_eq!((100 as $T).isqrt(), 10 as $T);
214-
assert_eq!($T::MAX.isqrt(), (1 << ($T::BITS / 2)) - 1);
202+
fn test_isqrt() {
203+
assert_eq_const_safe!((0 as $T).isqrt(), 0 as $T);
204+
assert_eq_const_safe!((1 as $T).isqrt(), 1 as $T);
205+
assert_eq_const_safe!((2 as $T).isqrt(), 1 as $T);
206+
assert_eq_const_safe!((99 as $T).isqrt(), 9 as $T);
207+
assert_eq_const_safe!((100 as $T).isqrt(), 10 as $T);
208+
assert_eq_const_safe!($T::MAX.isqrt(), (1 << ($T::BITS / 2)) - 1);
209+
}
215210
}
216211

217212
#[cfg(not(miri))] // Miri is too slow
@@ -233,85 +228,79 @@ macro_rules! uint_module {
233228
}
234229
}
235230

236-
#[test]
237-
fn test_div_floor() {
238-
assert_eq!((8 as $T).div_floor(3), 2);
239-
}
231+
test_runtime_and_compiletime! {
232+
fn test_div_floor() {
233+
assert_eq_const_safe!((8 as $T).div_floor(3), 2);
234+
}
240235

241-
#[test]
242-
fn test_div_ceil() {
243-
assert_eq!((8 as $T).div_ceil(3), 3);
244-
}
236+
fn test_div_ceil() {
237+
assert_eq_const_safe!((8 as $T).div_ceil(3), 3);
238+
}
245239

246-
#[test]
247-
fn test_next_multiple_of() {
248-
assert_eq!((16 as $T).next_multiple_of(8), 16);
249-
assert_eq!((23 as $T).next_multiple_of(8), 24);
250-
assert_eq!(MAX.next_multiple_of(1), MAX);
251-
}
240+
fn test_next_multiple_of() {
241+
assert_eq_const_safe!((16 as $T).next_multiple_of(8), 16);
242+
assert_eq_const_safe!((23 as $T).next_multiple_of(8), 24);
243+
assert_eq_const_safe!(MAX.next_multiple_of(1), MAX);
244+
}
252245

253-
#[test]
254-
fn test_checked_next_multiple_of() {
255-
assert_eq!((16 as $T).checked_next_multiple_of(8), Some(16));
256-
assert_eq!((23 as $T).checked_next_multiple_of(8), Some(24));
257-
assert_eq!((1 as $T).checked_next_multiple_of(0), None);
258-
assert_eq!(MAX.checked_next_multiple_of(2), None);
259-
}
246+
fn test_checked_next_multiple_of() {
247+
assert_eq_const_safe!((16 as $T).checked_next_multiple_of(8), Some(16));
248+
assert_eq_const_safe!((23 as $T).checked_next_multiple_of(8), Some(24));
249+
assert_eq_const_safe!((1 as $T).checked_next_multiple_of(0), None);
250+
assert_eq_const_safe!(MAX.checked_next_multiple_of(2), None);
251+
}
260252

261-
#[test]
262-
fn test_is_next_multiple_of() {
263-
assert!((12 as $T).is_multiple_of(4));
264-
assert!(!(12 as $T).is_multiple_of(5));
265-
assert!((0 as $T).is_multiple_of(0));
266-
assert!(!(12 as $T).is_multiple_of(0));
267-
}
253+
fn test_is_next_multiple_of() {
254+
assert!((12 as $T).is_multiple_of(4));
255+
assert!(!(12 as $T).is_multiple_of(5));
256+
assert!((0 as $T).is_multiple_of(0));
257+
assert!(!(12 as $T).is_multiple_of(0));
258+
}
268259

269-
#[test]
270-
fn test_carrying_add() {
271-
assert_eq!($T::MAX.carrying_add(1, false), (0, true));
272-
assert_eq!($T::MAX.carrying_add(0, true), (0, true));
273-
assert_eq!($T::MAX.carrying_add(1, true), (1, true));
274-
275-
assert_eq!($T::MIN.carrying_add($T::MAX, false), ($T::MAX, false));
276-
assert_eq!($T::MIN.carrying_add(0, true), (1, false));
277-
assert_eq!($T::MIN.carrying_add($T::MAX, true), (0, true));
278-
}
260+
fn test_carrying_add() {
261+
assert_eq_const_safe!($T::MAX.carrying_add(1, false), (0, true));
262+
assert_eq_const_safe!($T::MAX.carrying_add(0, true), (0, true));
263+
assert_eq_const_safe!($T::MAX.carrying_add(1, true), (1, true));
279264

280-
#[test]
281-
fn test_borrowing_sub() {
282-
assert_eq!($T::MIN.borrowing_sub(1, false), ($T::MAX, true));
283-
assert_eq!($T::MIN.borrowing_sub(0, true), ($T::MAX, true));
284-
assert_eq!($T::MIN.borrowing_sub(1, true), ($T::MAX - 1, true));
285-
286-
assert_eq!($T::MAX.borrowing_sub($T::MAX, false), (0, false));
287-
assert_eq!($T::MAX.borrowing_sub(0, true), ($T::MAX - 1, false));
288-
assert_eq!($T::MAX.borrowing_sub($T::MAX, true), ($T::MAX, true));
289-
}
265+
assert_eq_const_safe!($T::MIN.carrying_add($T::MAX, false), ($T::MAX, false));
266+
assert_eq_const_safe!($T::MIN.carrying_add(0, true), (1, false));
267+
assert_eq_const_safe!($T::MIN.carrying_add($T::MAX, true), (0, true));
268+
}
290269

291-
#[test]
292-
fn test_midpoint() {
293-
assert_eq!(<$T>::midpoint(1, 3), 2);
294-
assert_eq!(<$T>::midpoint(3, 1), 2);
295-
296-
assert_eq!(<$T>::midpoint(0, 0), 0);
297-
assert_eq!(<$T>::midpoint(0, 2), 1);
298-
assert_eq!(<$T>::midpoint(2, 0), 1);
299-
assert_eq!(<$T>::midpoint(2, 2), 2);
300-
301-
assert_eq!(<$T>::midpoint(1, 4), 2);
302-
assert_eq!(<$T>::midpoint(4, 1), 2);
303-
assert_eq!(<$T>::midpoint(3, 4), 3);
304-
assert_eq!(<$T>::midpoint(4, 3), 3);
305-
306-
assert_eq!(<$T>::midpoint(<$T>::MIN, <$T>::MAX), (<$T>::MAX - <$T>::MIN) / 2);
307-
assert_eq!(<$T>::midpoint(<$T>::MAX, <$T>::MIN), (<$T>::MAX - <$T>::MIN) / 2);
308-
assert_eq!(<$T>::midpoint(<$T>::MIN, <$T>::MIN), <$T>::MIN);
309-
assert_eq!(<$T>::midpoint(<$T>::MAX, <$T>::MAX), <$T>::MAX);
310-
311-
assert_eq!(<$T>::midpoint(<$T>::MIN, 6), <$T>::MIN / 2 + 3);
312-
assert_eq!(<$T>::midpoint(6, <$T>::MIN), <$T>::MIN / 2 + 3);
313-
assert_eq!(<$T>::midpoint(<$T>::MAX, 6), (<$T>::MAX - <$T>::MIN) / 2 + 3);
314-
assert_eq!(<$T>::midpoint(6, <$T>::MAX), (<$T>::MAX - <$T>::MIN) / 2 + 3);
270+
fn test_borrowing_sub() {
271+
assert_eq_const_safe!($T::MIN.borrowing_sub(1, false), ($T::MAX, true));
272+
assert_eq_const_safe!($T::MIN.borrowing_sub(0, true), ($T::MAX, true));
273+
assert_eq_const_safe!($T::MIN.borrowing_sub(1, true), ($T::MAX - 1, true));
274+
275+
assert_eq_const_safe!($T::MAX.borrowing_sub($T::MAX, false), (0, false));
276+
assert_eq_const_safe!($T::MAX.borrowing_sub(0, true), ($T::MAX - 1, false));
277+
assert_eq_const_safe!($T::MAX.borrowing_sub($T::MAX, true), ($T::MAX, true));
278+
}
279+
280+
fn test_midpoint() {
281+
assert_eq_const_safe!(<$T>::midpoint(1, 3), 2);
282+
assert_eq_const_safe!(<$T>::midpoint(3, 1), 2);
283+
284+
assert_eq_const_safe!(<$T>::midpoint(0, 0), 0);
285+
assert_eq_const_safe!(<$T>::midpoint(0, 2), 1);
286+
assert_eq_const_safe!(<$T>::midpoint(2, 0), 1);
287+
assert_eq_const_safe!(<$T>::midpoint(2, 2), 2);
288+
289+
assert_eq_const_safe!(<$T>::midpoint(1, 4), 2);
290+
assert_eq_const_safe!(<$T>::midpoint(4, 1), 2);
291+
assert_eq_const_safe!(<$T>::midpoint(3, 4), 3);
292+
assert_eq_const_safe!(<$T>::midpoint(4, 3), 3);
293+
294+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, <$T>::MAX), (<$T>::MAX - <$T>::MIN) / 2);
295+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, <$T>::MIN), (<$T>::MAX - <$T>::MIN) / 2);
296+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, <$T>::MIN), <$T>::MIN);
297+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, <$T>::MAX), <$T>::MAX);
298+
299+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MIN, 6), <$T>::MIN / 2 + 3);
300+
assert_eq_const_safe!(<$T>::midpoint(6, <$T>::MIN), <$T>::MIN / 2 + 3);
301+
assert_eq_const_safe!(<$T>::midpoint(<$T>::MAX, 6), (<$T>::MAX - <$T>::MIN) / 2 + 3);
302+
assert_eq_const_safe!(<$T>::midpoint(6, <$T>::MAX), (<$T>::MAX - <$T>::MIN) / 2 + 3);
303+
}
315304
}
316305
};
317306
}

0 commit comments

Comments
 (0)
Please sign in to comment.