]> git.proxmox.com Git - rustc.git/blob - src/libcore/tests/num/int_macros.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / src / libcore / tests / num / int_macros.rs
1 macro_rules! int_module {
2 ($T:ident, $T_i:ident) => {
3 #[cfg(test)]
4 mod tests {
5 use core::mem;
6 use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
7 use core::$T_i::*;
8
9 use crate::num;
10
11 #[test]
12 fn test_overflows() {
13 assert!(MAX > 0);
14 assert!(MIN <= 0);
15 assert_eq!(MIN + MAX + 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_rem_euclid() {
25 assert_eq!((-1 as $T).rem_euclid(MIN), MAX);
26 }
27
28 #[test]
29 pub fn test_abs() {
30 assert_eq!((1 as $T).abs(), 1 as $T);
31 assert_eq!((0 as $T).abs(), 0 as $T);
32 assert_eq!((-1 as $T).abs(), 1 as $T);
33 }
34
35 #[test]
36 fn test_signum() {
37 assert_eq!((1 as $T).signum(), 1 as $T);
38 assert_eq!((0 as $T).signum(), 0 as $T);
39 assert_eq!((-0 as $T).signum(), 0 as $T);
40 assert_eq!((-1 as $T).signum(), -1 as $T);
41 }
42
43 #[test]
44 fn test_is_positive() {
45 assert!((1 as $T).is_positive());
46 assert!(!(0 as $T).is_positive());
47 assert!(!(-0 as $T).is_positive());
48 assert!(!(-1 as $T).is_positive());
49 }
50
51 #[test]
52 fn test_is_negative() {
53 assert!(!(1 as $T).is_negative());
54 assert!(!(0 as $T).is_negative());
55 assert!(!(-0 as $T).is_negative());
56 assert!((-1 as $T).is_negative());
57 }
58
59 #[test]
60 fn test_bitwise_operators() {
61 assert_eq!(0b1110 as $T, (0b1100 as $T).bitor(0b1010 as $T));
62 assert_eq!(0b1000 as $T, (0b1100 as $T).bitand(0b1010 as $T));
63 assert_eq!(0b0110 as $T, (0b1100 as $T).bitxor(0b1010 as $T));
64 assert_eq!(0b1110 as $T, (0b0111 as $T).shl(1));
65 assert_eq!(0b0111 as $T, (0b1110 as $T).shr(1));
66 assert_eq!(-(0b11 as $T) - (1 as $T), (0b11 as $T).not());
67 }
68
69 const A: $T = 0b0101100;
70 const B: $T = 0b0100001;
71 const C: $T = 0b1111001;
72
73 const _0: $T = 0;
74 const _1: $T = !0;
75
76 #[test]
77 fn test_count_ones() {
78 assert_eq!(A.count_ones(), 3);
79 assert_eq!(B.count_ones(), 2);
80 assert_eq!(C.count_ones(), 5);
81 }
82
83 #[test]
84 fn test_count_zeros() {
85 let bits = mem::size_of::<$T>() * 8;
86 assert_eq!(A.count_zeros(), bits as u32 - 3);
87 assert_eq!(B.count_zeros(), bits as u32 - 2);
88 assert_eq!(C.count_zeros(), bits as u32 - 5);
89 }
90
91 #[test]
92 fn test_leading_trailing_ones() {
93 let bits = (mem::size_of::<$T>() * 8) as u32;
94
95 let a: $T = 0b0101_1111;
96 assert_eq!(a.trailing_ones(), 5);
97 assert_eq!((!a).leading_ones(), bits - 7);
98
99 assert_eq!(a.reverse_bits().leading_ones(), 5);
100
101 assert_eq!(_1.leading_ones(), bits);
102 assert_eq!(_1.trailing_ones(), bits);
103
104 assert_eq!((_1 << 1).trailing_ones(), 0);
105 assert_eq!(MAX.leading_ones(), 0);
106
107 assert_eq!((_1 << 1).leading_ones(), bits - 1);
108 assert_eq!(MAX.trailing_ones(), bits - 1);
109
110 assert_eq!(_0.leading_ones(), 0);
111 assert_eq!(_0.trailing_ones(), 0);
112
113 let x: $T = 0b0010_1100;
114 assert_eq!(x.leading_ones(), 0);
115 assert_eq!(x.trailing_ones(), 0);
116 }
117
118 #[test]
119 fn test_rotate() {
120 assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
121 assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
122 assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
123
124 // Rotating these should make no difference
125 //
126 // We test using 124 bits because to ensure that overlong bit shifts do
127 // not cause undefined behaviour. See #10183.
128 assert_eq!(_0.rotate_left(124), _0);
129 assert_eq!(_1.rotate_left(124), _1);
130 assert_eq!(_0.rotate_right(124), _0);
131 assert_eq!(_1.rotate_right(124), _1);
132
133 // Rotating by 0 should have no effect
134 assert_eq!(A.rotate_left(0), A);
135 assert_eq!(B.rotate_left(0), B);
136 assert_eq!(C.rotate_left(0), C);
137 // Rotating by a multiple of word size should also have no effect
138 assert_eq!(A.rotate_left(64), A);
139 assert_eq!(B.rotate_left(64), B);
140 assert_eq!(C.rotate_left(64), C);
141 }
142
143 #[test]
144 fn test_swap_bytes() {
145 assert_eq!(A.swap_bytes().swap_bytes(), A);
146 assert_eq!(B.swap_bytes().swap_bytes(), B);
147 assert_eq!(C.swap_bytes().swap_bytes(), C);
148
149 // Swapping these should make no difference
150 assert_eq!(_0.swap_bytes(), _0);
151 assert_eq!(_1.swap_bytes(), _1);
152 }
153
154 #[test]
155 fn test_le() {
156 assert_eq!($T::from_le(A.to_le()), A);
157 assert_eq!($T::from_le(B.to_le()), B);
158 assert_eq!($T::from_le(C.to_le()), C);
159 assert_eq!($T::from_le(_0), _0);
160 assert_eq!($T::from_le(_1), _1);
161 assert_eq!(_0.to_le(), _0);
162 assert_eq!(_1.to_le(), _1);
163 }
164
165 #[test]
166 fn test_be() {
167 assert_eq!($T::from_be(A.to_be()), A);
168 assert_eq!($T::from_be(B.to_be()), B);
169 assert_eq!($T::from_be(C.to_be()), C);
170 assert_eq!($T::from_be(_0), _0);
171 assert_eq!($T::from_be(_1), _1);
172 assert_eq!(_0.to_be(), _0);
173 assert_eq!(_1.to_be(), _1);
174 }
175
176 #[test]
177 fn test_signed_checked_div() {
178 assert_eq!((10 as $T).checked_div(2), Some(5));
179 assert_eq!((5 as $T).checked_div(0), None);
180 assert_eq!(isize::MIN.checked_div(-1), None);
181 }
182
183 #[test]
184 fn test_saturating_abs() {
185 assert_eq!((0 as $T).saturating_abs(), 0);
186 assert_eq!((123 as $T).saturating_abs(), 123);
187 assert_eq!((-123 as $T).saturating_abs(), 123);
188 assert_eq!((MAX - 2).saturating_abs(), MAX - 2);
189 assert_eq!((MAX - 1).saturating_abs(), MAX - 1);
190 assert_eq!(MAX.saturating_abs(), MAX);
191 assert_eq!((MIN + 2).saturating_abs(), MAX - 1);
192 assert_eq!((MIN + 1).saturating_abs(), MAX);
193 assert_eq!(MIN.saturating_abs(), MAX);
194 }
195
196 #[test]
197 fn test_saturating_neg() {
198 assert_eq!((0 as $T).saturating_neg(), 0);
199 assert_eq!((123 as $T).saturating_neg(), -123);
200 assert_eq!((-123 as $T).saturating_neg(), 123);
201 assert_eq!((MAX - 2).saturating_neg(), MIN + 3);
202 assert_eq!((MAX - 1).saturating_neg(), MIN + 2);
203 assert_eq!(MAX.saturating_neg(), MIN + 1);
204 assert_eq!((MIN + 2).saturating_neg(), MAX - 1);
205 assert_eq!((MIN + 1).saturating_neg(), MAX);
206 assert_eq!(MIN.saturating_neg(), MAX);
207 }
208
209 #[test]
210 fn test_from_str() {
211 fn from_str<T: ::std::str::FromStr>(t: &str) -> Option<T> {
212 ::std::str::FromStr::from_str(t).ok()
213 }
214 assert_eq!(from_str::<$T>("0"), Some(0 as $T));
215 assert_eq!(from_str::<$T>("3"), Some(3 as $T));
216 assert_eq!(from_str::<$T>("10"), Some(10 as $T));
217 assert_eq!(from_str::<i32>("123456789"), Some(123456789 as i32));
218 assert_eq!(from_str::<$T>("00100"), Some(100 as $T));
219
220 assert_eq!(from_str::<$T>("-1"), Some(-1 as $T));
221 assert_eq!(from_str::<$T>("-3"), Some(-3 as $T));
222 assert_eq!(from_str::<$T>("-10"), Some(-10 as $T));
223 assert_eq!(from_str::<i32>("-123456789"), Some(-123456789 as i32));
224 assert_eq!(from_str::<$T>("-00100"), Some(-100 as $T));
225
226 assert_eq!(from_str::<$T>(""), None);
227 assert_eq!(from_str::<$T>(" "), None);
228 assert_eq!(from_str::<$T>("x"), None);
229 }
230
231 #[test]
232 fn test_from_str_radix() {
233 assert_eq!($T::from_str_radix("123", 10), Ok(123 as $T));
234 assert_eq!($T::from_str_radix("1001", 2), Ok(9 as $T));
235 assert_eq!($T::from_str_radix("123", 8), Ok(83 as $T));
236 assert_eq!(i32::from_str_radix("123", 16), Ok(291 as i32));
237 assert_eq!(i32::from_str_radix("ffff", 16), Ok(65535 as i32));
238 assert_eq!(i32::from_str_radix("FFFF", 16), Ok(65535 as i32));
239 assert_eq!($T::from_str_radix("z", 36), Ok(35 as $T));
240 assert_eq!($T::from_str_radix("Z", 36), Ok(35 as $T));
241
242 assert_eq!($T::from_str_radix("-123", 10), Ok(-123 as $T));
243 assert_eq!($T::from_str_radix("-1001", 2), Ok(-9 as $T));
244 assert_eq!($T::from_str_radix("-123", 8), Ok(-83 as $T));
245 assert_eq!(i32::from_str_radix("-123", 16), Ok(-291 as i32));
246 assert_eq!(i32::from_str_radix("-ffff", 16), Ok(-65535 as i32));
247 assert_eq!(i32::from_str_radix("-FFFF", 16), Ok(-65535 as i32));
248 assert_eq!($T::from_str_radix("-z", 36), Ok(-35 as $T));
249 assert_eq!($T::from_str_radix("-Z", 36), Ok(-35 as $T));
250
251 assert_eq!($T::from_str_radix("Z", 35).ok(), None::<$T>);
252 assert_eq!($T::from_str_radix("-9", 2).ok(), None::<$T>);
253 }
254
255 #[test]
256 fn test_pow() {
257 let mut r = 2 as $T;
258
259 assert_eq!(r.pow(2), 4 as $T);
260 assert_eq!(r.pow(0), 1 as $T);
261 r = -2 as $T;
262 assert_eq!(r.pow(2), 4 as $T);
263 assert_eq!(r.pow(3), -8 as $T);
264 }
265 }
266 };
267 }