]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/num/uint_macros.rs
39e41a4fad3b7d5092c834aa43c69a443b1018b7
[rust.git] / src / libcoretest / num / uint_macros.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 macro_rules! uint_module { ($T:ty, $T_i:ident) => (
12 #[cfg(test)]
13 mod tests {
14     use core::$T_i::*;
15     use core::num::Int;
16     use num;
17     use core::ops::{BitOr, BitAnd, BitXor, Shl, Shr, Not};
18
19     #[test]
20     fn test_overflows() {
21         assert!(MAX > 0);
22         assert!(MIN <= 0);
23         assert!(MIN + MAX + 1 == 0);
24     }
25
26     #[test]
27     fn test_num() {
28         num::test_num(10 as $T, 2 as $T);
29     }
30
31     #[test]
32     fn test_bitwise_operators() {
33         assert!(0b1110 as $T == (0b1100 as $T).bitor(0b1010 as $T));
34         assert!(0b1000 as $T == (0b1100 as $T).bitand(0b1010 as $T));
35         assert!(0b0110 as $T == (0b1100 as $T).bitxor(0b1010 as $T));
36         assert!(0b1110 as $T == (0b0111 as $T).shl(1));
37         assert!(0b0111 as $T == (0b1110 as $T).shr(1));
38         assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
39     }
40
41     const A: $T = 0b0101100;
42     const B: $T = 0b0100001;
43     const C: $T = 0b1111001;
44
45     const _0: $T = 0;
46     const _1: $T = !0;
47
48     #[test]
49     fn test_count_ones() {
50         assert!(A.count_ones() == 3);
51         assert!(B.count_ones() == 2);
52         assert!(C.count_ones() == 5);
53     }
54
55     #[test]
56     fn test_count_zeros() {
57         assert!(A.count_zeros() == BITS - 3);
58         assert!(B.count_zeros() == BITS - 2);
59         assert!(C.count_zeros() == BITS - 5);
60     }
61
62     #[test]
63     fn test_rotate() {
64         assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
65         assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
66         assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
67
68         // Rotating these should make no difference
69         //
70         // We test using 124 bits because to ensure that overlong bit shifts do
71         // not cause undefined behaviour. See #10183.
72         assert_eq!(_0.rotate_left(124), _0);
73         assert_eq!(_1.rotate_left(124), _1);
74         assert_eq!(_0.rotate_right(124), _0);
75         assert_eq!(_1.rotate_right(124), _1);
76
77         // Rotating by 0 should have no effect
78         assert_eq!(A.rotate_left(0), A);
79         assert_eq!(B.rotate_left(0), B);
80         assert_eq!(C.rotate_left(0), C);
81         // Rotating by a multiple of word size should also have no effect
82         assert_eq!(A.rotate_left(64), A);
83         assert_eq!(B.rotate_left(64), B);
84         assert_eq!(C.rotate_left(64), C);
85     }
86
87     #[test]
88     fn test_swap_bytes() {
89         assert_eq!(A.swap_bytes().swap_bytes(), A);
90         assert_eq!(B.swap_bytes().swap_bytes(), B);
91         assert_eq!(C.swap_bytes().swap_bytes(), C);
92
93         // Swapping these should make no difference
94         assert_eq!(_0.swap_bytes(), _0);
95         assert_eq!(_1.swap_bytes(), _1);
96     }
97
98     #[test]
99     fn test_le() {
100         assert_eq!(Int::from_le(A.to_le()), A);
101         assert_eq!(Int::from_le(B.to_le()), B);
102         assert_eq!(Int::from_le(C.to_le()), C);
103         assert_eq!(Int::from_le(_0), _0);
104         assert_eq!(Int::from_le(_1), _1);
105         assert_eq!(_0.to_le(), _0);
106         assert_eq!(_1.to_le(), _1);
107     }
108
109     #[test]
110     fn test_be() {
111         assert_eq!(Int::from_be(A.to_be()), A);
112         assert_eq!(Int::from_be(B.to_be()), B);
113         assert_eq!(Int::from_be(C.to_be()), C);
114         assert_eq!(Int::from_be(_0), _0);
115         assert_eq!(Int::from_be(_1), _1);
116         assert_eq!(_0.to_be(), _0);
117         assert_eq!(_1.to_be(), _1);
118     }
119
120     #[test]
121     fn test_unsigned_checked_div() {
122         assert!(10.checked_div(2) == Some(5));
123         assert!(5.checked_div(0) == None);
124     }
125 }
126
127 )}