]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/num/uint_macros.rs
Fix rotate_{left, right} for multiple of bitsize rotation amounts
[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_escape]
12
13 macro_rules! uint_module (($T:ty, $T_i:ident) => (
14 #[cfg(test)]
15 mod tests {
16     use core::$T_i::*;
17     use num;
18     use core::num::CheckedDiv;
19
20     #[test]
21     fn test_overflows() {
22         assert!(MAX > 0);
23         assert!(MIN <= 0);
24         assert!(MIN + MAX + 1 == 0);
25     }
26
27     #[test]
28     fn test_num() {
29         num::test_num(10 as $T, 2 as $T);
30     }
31
32     #[test]
33     fn test_bitwise_operators() {
34         assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
35         assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
36         assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
37         assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
38         assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
39         assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
40     }
41
42     static A: $T = 0b0101100;
43     static B: $T = 0b0100001;
44     static C: $T = 0b1111001;
45
46     static _0: $T = 0;
47     static _1: $T = !0;
48
49     #[test]
50     fn test_count_ones() {
51         assert!(A.count_ones() == 3);
52         assert!(B.count_ones() == 2);
53         assert!(C.count_ones() == 5);
54     }
55
56     #[test]
57     fn test_count_zeros() {
58         assert!(A.count_zeros() == BITS as $T - 3);
59         assert!(B.count_zeros() == BITS as $T - 2);
60         assert!(C.count_zeros() == BITS as $T - 5);
61     }
62
63     #[test]
64     fn test_rotate() {
65         assert_eq!(A.rotate_left(6).rotate_right(2).rotate_right(4), A);
66         assert_eq!(B.rotate_left(3).rotate_left(2).rotate_right(5), B);
67         assert_eq!(C.rotate_left(6).rotate_right(2).rotate_right(4), C);
68
69         // Rotating these should make no difference
70         //
71         // We test using 124 bits because to ensure that overlong bit shifts do
72         // not cause undefined behaviour. See #10183.
73         assert_eq!(_0.rotate_left(124), _0);
74         assert_eq!(_1.rotate_left(124), _1);
75         assert_eq!(_0.rotate_right(124), _0);
76         assert_eq!(_1.rotate_right(124), _1);
77
78         // Rotating by 0 should have no effect
79         assert_eq!(A.rotate_left(0), A);
80         assert_eq!(B.rotate_left(0), B);
81         assert_eq!(C.rotate_left(0), C);
82         // Rotating by a multiple of word size should also have no effect
83         assert_eq!(A.rotate_left(64), A);
84         assert_eq!(B.rotate_left(64), B);
85         assert_eq!(C.rotate_left(64), C);
86     }
87
88     #[test]
89     fn test_swap_bytes() {
90         assert_eq!(A.swap_bytes().swap_bytes(), A);
91         assert_eq!(B.swap_bytes().swap_bytes(), B);
92         assert_eq!(C.swap_bytes().swap_bytes(), C);
93
94         // Swapping these should make no difference
95         assert_eq!(_0.swap_bytes(), _0);
96         assert_eq!(_1.swap_bytes(), _1);
97     }
98
99     #[test]
100     fn test_le() {
101         assert_eq!(Int::from_le(A.to_le()), A);
102         assert_eq!(Int::from_le(B.to_le()), B);
103         assert_eq!(Int::from_le(C.to_le()), C);
104         assert_eq!(Int::from_le(_0), _0);
105         assert_eq!(Int::from_le(_1), _1);
106         assert_eq!(_0.to_le(), _0);
107         assert_eq!(_1.to_le(), _1);
108     }
109
110     #[test]
111     fn test_be() {
112         assert_eq!(Int::from_be(A.to_be()), A);
113         assert_eq!(Int::from_be(B.to_be()), B);
114         assert_eq!(Int::from_be(C.to_be()), C);
115         assert_eq!(Int::from_be(_0), _0);
116         assert_eq!(Int::from_be(_1), _1);
117         assert_eq!(_0.to_be(), _0);
118         assert_eq!(_1.to_be(), _1);
119     }
120
121     #[test]
122     fn test_unsigned_checked_div() {
123         assert!(10u.checked_div(&2) == Some(5));
124         assert!(5u.checked_div(&0) == None);
125     }
126 }
127 ))