]> git.lizzy.rs Git - rust.git/blob - src/libcore/num/uint_macros.rs
Add a ByteOrder trait for abstracting over endian conversions
[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     use num::Bitwise;
30
31     #[test]
32     fn test_overflows() {
33         assert!(MAX > 0);
34         assert!(MIN <= 0);
35         assert!(MIN + MAX + 1 == 0);
36     }
37
38     #[test]
39     fn test_num() {
40         num::test_num(10 as $T, 2 as $T);
41     }
42
43     #[test]
44     fn test_bitwise() {
45         assert!(0b1110 as $T == (0b1100 as $T).bitor(&(0b1010 as $T)));
46         assert!(0b1000 as $T == (0b1100 as $T).bitand(&(0b1010 as $T)));
47         assert!(0b0110 as $T == (0b1100 as $T).bitxor(&(0b1010 as $T)));
48         assert!(0b1110 as $T == (0b0111 as $T).shl(&(1 as $T)));
49         assert!(0b0111 as $T == (0b1110 as $T).shr(&(1 as $T)));
50         assert!(MAX - (0b1011 as $T) == (0b1011 as $T).not());
51     }
52
53     #[test]
54     fn test_count_ones() {
55         assert!((0b0101100 as $T).count_ones() == 3);
56         assert!((0b0100001 as $T).count_ones() == 2);
57         assert!((0b1111001 as $T).count_ones() == 5);
58     }
59
60     #[test]
61     fn test_count_zeros() {
62         assert!((0b0101100 as $T).count_zeros() == BITS as $T - 3);
63         assert!((0b0100001 as $T).count_zeros() == BITS as $T - 2);
64         assert!((0b1111001 as $T).count_zeros() == BITS as $T - 5);
65     }
66
67     #[test]
68     fn test_rotate() {
69         let n: $T = 0b0101100; assert_eq!(n.rotate_left(6).rotate_right(2).rotate_right(4), n);
70         let n: $T = 0b0100001; assert_eq!(n.rotate_left(3).rotate_left(2).rotate_right(5),  n);
71         let n: $T = 0b1111001; assert_eq!(n.rotate_left(6).rotate_right(2).rotate_right(4), n);
72
73         // Rotating these should make no difference
74         //
75         // We test using 124 bits because to ensure that overlong bit shifts do
76         // not cause undefined behaviour. See #10183.
77         let n: $T = 0;   assert_eq!(n.rotate_left(124), n);
78         let n: $T = MAX; assert_eq!(n.rotate_left(124), n);
79         let n: $T = 0;   assert_eq!(n.rotate_right(124), n);
80         let n: $T = MAX; assert_eq!(n.rotate_right(124), n);
81     }
82
83     #[test]
84     fn test_unsigned_checked_div() {
85         assert!(10u.checked_div(&2) == Some(5));
86         assert!(5u.checked_div(&0) == None);
87     }
88 }
89
90 ))