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