]> git.lizzy.rs Git - rust.git/blob - library/core/tests/num/uint_macros.rs
Rollup merge of #95214 - tbu-:pr_vec_append_doc, r=Mark-Simulacrum
[rust.git] / library / core / tests / num / uint_macros.rs
1 macro_rules! uint_module {
2     ($T:ident) => {
3         #[cfg(test)]
4         mod tests {
5             use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
6             use core::$T::*;
7             use std::str::FromStr;
8
9             use crate::num;
10
11             #[test]
12             fn test_overflows() {
13                 assert!(MAX > 0);
14                 assert!(MIN <= 0);
15                 assert!((MIN + MAX).wrapping_add(1) == 0);
16             }
17
18             #[test]
19             fn test_num() {
20                 num::test_num(10 as $T, 2 as $T);
21             }
22
23             #[test]
24             fn test_bitwise_operators() {
25                 assert!(0b1110 as $T == (0b1100 as $T).bitor(0b1010 as $T));
26                 assert!(0b1000 as $T == (0b1100 as $T).bitand(0b1010 as $T));
27                 assert!(0b0110 as $T == (0b1100 as $T).bitxor(0b1010 as $T));
28                 assert!(0b1110 as $T == (0b0111 as $T).shl(1));
29                 assert!(0b0111 as $T == (0b1110 as $T).shr(1));
30                 assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
31             }
32
33             const A: $T = 0b0101100;
34             const B: $T = 0b0100001;
35             const C: $T = 0b1111001;
36
37             const _0: $T = 0;
38             const _1: $T = !0;
39
40             #[test]
41             fn test_count_ones() {
42                 assert!(A.count_ones() == 3);
43                 assert!(B.count_ones() == 2);
44                 assert!(C.count_ones() == 5);
45             }
46
47             #[test]
48             fn test_count_zeros() {
49                 assert!(A.count_zeros() == $T::BITS - 3);
50                 assert!(B.count_zeros() == $T::BITS - 2);
51                 assert!(C.count_zeros() == $T::BITS - 5);
52             }
53
54             #[test]
55             fn test_leading_trailing_ones() {
56                 let a: $T = 0b0101_1111;
57                 assert_eq!(a.trailing_ones(), 5);
58                 assert_eq!((!a).leading_ones(), $T::BITS - 7);
59
60                 assert_eq!(a.reverse_bits().leading_ones(), 5);
61
62                 assert_eq!(_1.leading_ones(), $T::BITS);
63                 assert_eq!(_1.trailing_ones(), $T::BITS);
64
65                 assert_eq!((_1 << 1).trailing_ones(), 0);
66                 assert_eq!((_1 >> 1).leading_ones(), 0);
67
68                 assert_eq!((_1 << 1).leading_ones(), $T::BITS - 1);
69                 assert_eq!((_1 >> 1).trailing_ones(), $T::BITS - 1);
70
71                 assert_eq!(_0.leading_ones(), 0);
72                 assert_eq!(_0.trailing_ones(), 0);
73
74                 let x: $T = 0b0010_1100;
75                 assert_eq!(x.leading_ones(), 0);
76                 assert_eq!(x.trailing_ones(), 0);
77             }
78
79             #[test]
80             fn test_rotate() {
81                 assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
82                 assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
83                 assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
84
85                 // Rotating these should make no difference
86                 //
87                 // We test using 124 bits because to ensure that overlong bit shifts do
88                 // not cause undefined behaviour. See #10183.
89                 assert_eq!(_0.rotate_left(124), _0);
90                 assert_eq!(_1.rotate_left(124), _1);
91                 assert_eq!(_0.rotate_right(124), _0);
92                 assert_eq!(_1.rotate_right(124), _1);
93
94                 // Rotating by 0 should have no effect
95                 assert_eq!(A.rotate_left(0), A);
96                 assert_eq!(B.rotate_left(0), B);
97                 assert_eq!(C.rotate_left(0), C);
98                 // Rotating by a multiple of word size should also have no effect
99                 assert_eq!(A.rotate_left(128), A);
100                 assert_eq!(B.rotate_left(128), B);
101                 assert_eq!(C.rotate_left(128), C);
102             }
103
104             #[test]
105             fn test_swap_bytes() {
106                 assert_eq!(A.swap_bytes().swap_bytes(), A);
107                 assert_eq!(B.swap_bytes().swap_bytes(), B);
108                 assert_eq!(C.swap_bytes().swap_bytes(), C);
109
110                 // Swapping these should make no difference
111                 assert_eq!(_0.swap_bytes(), _0);
112                 assert_eq!(_1.swap_bytes(), _1);
113             }
114
115             #[test]
116             fn test_reverse_bits() {
117                 assert_eq!(A.reverse_bits().reverse_bits(), A);
118                 assert_eq!(B.reverse_bits().reverse_bits(), B);
119                 assert_eq!(C.reverse_bits().reverse_bits(), C);
120
121                 // Swapping these should make no difference
122                 assert_eq!(_0.reverse_bits(), _0);
123                 assert_eq!(_1.reverse_bits(), _1);
124             }
125
126             #[test]
127             fn test_le() {
128                 assert_eq!($T::from_le(A.to_le()), A);
129                 assert_eq!($T::from_le(B.to_le()), B);
130                 assert_eq!($T::from_le(C.to_le()), C);
131                 assert_eq!($T::from_le(_0), _0);
132                 assert_eq!($T::from_le(_1), _1);
133                 assert_eq!(_0.to_le(), _0);
134                 assert_eq!(_1.to_le(), _1);
135             }
136
137             #[test]
138             fn test_be() {
139                 assert_eq!($T::from_be(A.to_be()), A);
140                 assert_eq!($T::from_be(B.to_be()), B);
141                 assert_eq!($T::from_be(C.to_be()), C);
142                 assert_eq!($T::from_be(_0), _0);
143                 assert_eq!($T::from_be(_1), _1);
144                 assert_eq!(_0.to_be(), _0);
145                 assert_eq!(_1.to_be(), _1);
146             }
147
148             #[test]
149             fn test_unsigned_checked_div() {
150                 assert!((10 as $T).checked_div(2) == Some(5));
151                 assert!((5 as $T).checked_div(0) == None);
152             }
153
154             fn from_str<T: FromStr>(t: &str) -> Option<T> {
155                 FromStr::from_str(t).ok()
156             }
157
158             #[test]
159             pub fn test_from_str() {
160                 assert_eq!(from_str::<$T>("0"), Some(0 as $T));
161                 assert_eq!(from_str::<$T>("3"), Some(3 as $T));
162                 assert_eq!(from_str::<$T>("10"), Some(10 as $T));
163                 assert_eq!(from_str::<u32>("123456789"), Some(123456789 as u32));
164                 assert_eq!(from_str::<$T>("00100"), Some(100 as $T));
165
166                 assert_eq!(from_str::<$T>(""), None);
167                 assert_eq!(from_str::<$T>(" "), None);
168                 assert_eq!(from_str::<$T>("x"), None);
169             }
170
171             #[test]
172             pub fn test_parse_bytes() {
173                 assert_eq!($T::from_str_radix("123", 10), Ok(123 as $T));
174                 assert_eq!($T::from_str_radix("1001", 2), Ok(9 as $T));
175                 assert_eq!($T::from_str_radix("123", 8), Ok(83 as $T));
176                 assert_eq!(u16::from_str_radix("123", 16), Ok(291 as u16));
177                 assert_eq!(u16::from_str_radix("ffff", 16), Ok(65535 as u16));
178                 assert_eq!($T::from_str_radix("z", 36), Ok(35 as $T));
179
180                 assert_eq!($T::from_str_radix("Z", 10).ok(), None::<$T>);
181                 assert_eq!($T::from_str_radix("_", 2).ok(), None::<$T>);
182             }
183
184             #[test]
185             fn test_pow() {
186                 let mut r = 2 as $T;
187                 assert_eq!(r.pow(2), 4 as $T);
188                 assert_eq!(r.pow(0), 1 as $T);
189                 assert_eq!(r.wrapping_pow(2), 4 as $T);
190                 assert_eq!(r.wrapping_pow(0), 1 as $T);
191                 assert_eq!(r.checked_pow(2), Some(4 as $T));
192                 assert_eq!(r.checked_pow(0), Some(1 as $T));
193                 assert_eq!(r.overflowing_pow(2), (4 as $T, false));
194                 assert_eq!(r.overflowing_pow(0), (1 as $T, false));
195                 assert_eq!(r.saturating_pow(2), 4 as $T);
196                 assert_eq!(r.saturating_pow(0), 1 as $T);
197
198                 r = MAX;
199                 // use `^` to represent .pow() with no overflow.
200                 // if itest::MAX == 2^j-1, then itest is a `j` bit int,
201                 // so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
202                 // thussaturating_pow the overflowing result is exactly 1.
203                 assert_eq!(r.wrapping_pow(2), 1 as $T);
204                 assert_eq!(r.checked_pow(2), None);
205                 assert_eq!(r.overflowing_pow(2), (1 as $T, true));
206                 assert_eq!(r.saturating_pow(2), MAX);
207             }
208
209             #[test]
210             fn test_div_floor() {
211                 assert_eq!((8 as $T).div_floor(3), 2);
212             }
213
214             #[test]
215             fn test_div_ceil() {
216                 assert_eq!((8 as $T).div_ceil(3), 3);
217             }
218
219             #[test]
220             fn test_next_multiple_of() {
221                 assert_eq!((16 as $T).next_multiple_of(8), 16);
222                 assert_eq!((23 as $T).next_multiple_of(8), 24);
223                 assert_eq!(MAX.next_multiple_of(1), MAX);
224             }
225
226             #[test]
227             fn test_checked_next_multiple_of() {
228                 assert_eq!((16 as $T).checked_next_multiple_of(8), Some(16));
229                 assert_eq!((23 as $T).checked_next_multiple_of(8), Some(24));
230                 assert_eq!((1 as $T).checked_next_multiple_of(0), None);
231                 assert_eq!(MAX.checked_next_multiple_of(2), None);
232             }
233         }
234     };
235 }