]> git.proxmox.com Git - rustc.git/blame - src/libcore/tests/num/uint_macros.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libcore / tests / num / uint_macros.rs
CommitLineData
60c5eb7d
XL
1macro_rules! uint_module {
2 ($T:ident, $T_i:ident) => {
3 #[cfg(test)]
4 mod tests {
5 use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
6 use core::$T_i::*;
7 use std::mem;
8 use std::str::FromStr;
9
10 use crate::num;
11
12 #[test]
13 fn test_overflows() {
14 assert!(MAX > 0);
15 assert!(MIN <= 0);
16 assert!((MIN + MAX).wrapping_add(1) == 0);
17 }
18
19 #[test]
20 fn test_num() {
21 num::test_num(10 as $T, 2 as $T);
22 }
23
24 #[test]
25 fn test_bitwise_operators() {
26 assert!(0b1110 as $T == (0b1100 as $T).bitor(0b1010 as $T));
27 assert!(0b1000 as $T == (0b1100 as $T).bitand(0b1010 as $T));
28 assert!(0b0110 as $T == (0b1100 as $T).bitxor(0b1010 as $T));
29 assert!(0b1110 as $T == (0b0111 as $T).shl(1));
30 assert!(0b0111 as $T == (0b1110 as $T).shr(1));
31 assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
32 }
33
34 const A: $T = 0b0101100;
35 const B: $T = 0b0100001;
36 const C: $T = 0b1111001;
37
38 const _0: $T = 0;
39 const _1: $T = !0;
40
41 #[test]
42 fn test_count_ones() {
43 assert!(A.count_ones() == 3);
44 assert!(B.count_ones() == 2);
45 assert!(C.count_ones() == 5);
46 }
47
48 #[test]
49 fn test_count_zeros() {
50 let bits = mem::size_of::<$T>() * 8;
51 assert!(A.count_zeros() == bits as u32 - 3);
52 assert!(B.count_zeros() == bits as u32 - 2);
53 assert!(C.count_zeros() == bits as u32 - 5);
54 }
55
56 #[test]
57 fn test_rotate() {
58 assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
59 assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
60 assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
61
62 // Rotating these should make no difference
63 //
64 // We test using 124 bits because to ensure that overlong bit shifts do
65 // not cause undefined behaviour. See #10183.
66 assert_eq!(_0.rotate_left(124), _0);
67 assert_eq!(_1.rotate_left(124), _1);
68 assert_eq!(_0.rotate_right(124), _0);
69 assert_eq!(_1.rotate_right(124), _1);
70
71 // Rotating by 0 should have no effect
72 assert_eq!(A.rotate_left(0), A);
73 assert_eq!(B.rotate_left(0), B);
74 assert_eq!(C.rotate_left(0), C);
75 // Rotating by a multiple of word size should also have no effect
76 assert_eq!(A.rotate_left(64), A);
77 assert_eq!(B.rotate_left(64), B);
78 assert_eq!(C.rotate_left(64), C);
79 }
80
81 #[test]
82 fn test_swap_bytes() {
83 assert_eq!(A.swap_bytes().swap_bytes(), A);
84 assert_eq!(B.swap_bytes().swap_bytes(), B);
85 assert_eq!(C.swap_bytes().swap_bytes(), C);
86
87 // Swapping these should make no difference
88 assert_eq!(_0.swap_bytes(), _0);
89 assert_eq!(_1.swap_bytes(), _1);
90 }
91
92 #[test]
93 fn test_reverse_bits() {
94 assert_eq!(A.reverse_bits().reverse_bits(), A);
95 assert_eq!(B.reverse_bits().reverse_bits(), B);
96 assert_eq!(C.reverse_bits().reverse_bits(), C);
97
98 // Swapping these should make no difference
99 assert_eq!(_0.reverse_bits(), _0);
100 assert_eq!(_1.reverse_bits(), _1);
101 }
102
103 #[test]
104 fn test_le() {
105 assert_eq!($T::from_le(A.to_le()), A);
106 assert_eq!($T::from_le(B.to_le()), B);
107 assert_eq!($T::from_le(C.to_le()), C);
108 assert_eq!($T::from_le(_0), _0);
109 assert_eq!($T::from_le(_1), _1);
110 assert_eq!(_0.to_le(), _0);
111 assert_eq!(_1.to_le(), _1);
112 }
113
114 #[test]
115 fn test_be() {
116 assert_eq!($T::from_be(A.to_be()), A);
117 assert_eq!($T::from_be(B.to_be()), B);
118 assert_eq!($T::from_be(C.to_be()), C);
119 assert_eq!($T::from_be(_0), _0);
120 assert_eq!($T::from_be(_1), _1);
121 assert_eq!(_0.to_be(), _0);
122 assert_eq!(_1.to_be(), _1);
123 }
124
125 #[test]
126 fn test_unsigned_checked_div() {
127 assert!((10 as $T).checked_div(2) == Some(5));
128 assert!((5 as $T).checked_div(0) == None);
129 }
130
131 fn from_str<T: FromStr>(t: &str) -> Option<T> {
132 FromStr::from_str(t).ok()
133 }
134
135 #[test]
136 pub fn test_from_str() {
137 assert_eq!(from_str::<$T>("0"), Some(0 as $T));
138 assert_eq!(from_str::<$T>("3"), Some(3 as $T));
139 assert_eq!(from_str::<$T>("10"), Some(10 as $T));
140 assert_eq!(from_str::<u32>("123456789"), Some(123456789 as u32));
141 assert_eq!(from_str::<$T>("00100"), Some(100 as $T));
142
143 assert_eq!(from_str::<$T>(""), None);
144 assert_eq!(from_str::<$T>(" "), None);
145 assert_eq!(from_str::<$T>("x"), None);
146 }
147
148 #[test]
149 pub fn test_parse_bytes() {
150 assert_eq!($T::from_str_radix("123", 10), Ok(123 as $T));
151 assert_eq!($T::from_str_radix("1001", 2), Ok(9 as $T));
152 assert_eq!($T::from_str_radix("123", 8), Ok(83 as $T));
153 assert_eq!(u16::from_str_radix("123", 16), Ok(291 as u16));
154 assert_eq!(u16::from_str_radix("ffff", 16), Ok(65535 as u16));
155 assert_eq!($T::from_str_radix("z", 36), Ok(35 as $T));
156
157 assert_eq!($T::from_str_radix("Z", 10).ok(), None::<$T>);
158 assert_eq!($T::from_str_radix("_", 2).ok(), None::<$T>);
159 }
160 }
161 };
e9174d1e 162}