1 extern crate num_bigint;
2 extern crate num_traits;
3 
4 use num_bigint::{BigInt, Sign, ToBigInt};
5 use num_traits::ToPrimitive;
6 use std::{i32, i64, u32};
7 
8 enum ValueVec {
9     N,
10     P(&'static [u32]),
11     M(&'static [u32]),
12 }
13 
14 use ValueVec::*;
15 
16 impl ToBigInt for ValueVec {
to_bigint(&self) -> Option<BigInt>17     fn to_bigint(&self) -> Option<BigInt> {
18         match self {
19             &N => Some(BigInt::from_slice(Sign::NoSign, &[])),
20             &P(s) => Some(BigInt::from_slice(Sign::Plus, s)),
21             &M(s) => Some(BigInt::from_slice(Sign::Minus, s)),
22         }
23     }
24 }
25 
26 // a, !a
27 const NOT_VALUES: &'static [(ValueVec, ValueVec)] = &[
28     (N, M(&[1])),
29     (P(&[1]), M(&[2])),
30     (P(&[2]), M(&[3])),
31     (P(&[!0 - 2]), M(&[!0 - 1])),
32     (P(&[!0 - 1]), M(&[!0])),
33     (P(&[!0]), M(&[0, 1])),
34     (P(&[0, 1]), M(&[1, 1])),
35     (P(&[1, 1]), M(&[2, 1])),
36 ];
37 
38 // a, b, a & b, a | b, a ^ b
39 const BITWISE_VALUES: &'static [(ValueVec, ValueVec, ValueVec, ValueVec, ValueVec)] = &[
40     (N, N, N, N, N),
41     (N, P(&[1]), N, P(&[1]), P(&[1])),
42     (N, P(&[!0]), N, P(&[!0]), P(&[!0])),
43     (N, P(&[0, 1]), N, P(&[0, 1]), P(&[0, 1])),
44     (N, M(&[1]), N, M(&[1]), M(&[1])),
45     (N, M(&[!0]), N, M(&[!0]), M(&[!0])),
46     (N, M(&[0, 1]), N, M(&[0, 1]), M(&[0, 1])),
47     (P(&[1]), P(&[!0]), P(&[1]), P(&[!0]), P(&[!0 - 1])),
48     (P(&[!0]), P(&[!0]), P(&[!0]), P(&[!0]), N),
49     (P(&[!0]), P(&[1, 1]), P(&[1]), P(&[!0, 1]), P(&[!0 - 1, 1])),
50     (P(&[1]), M(&[!0]), P(&[1]), M(&[!0]), M(&[0, 1])),
51     (P(&[!0]), M(&[1]), P(&[!0]), M(&[1]), M(&[0, 1])),
52     (P(&[!0]), M(&[!0]), P(&[1]), M(&[1]), M(&[2])),
53     (P(&[!0]), M(&[1, 1]), P(&[!0]), M(&[1, 1]), M(&[0, 2])),
54     (P(&[1, 1]), M(&[!0]), P(&[1, 1]), M(&[!0]), M(&[0, 2])),
55     (M(&[1]), M(&[!0]), M(&[!0]), M(&[1]), P(&[!0 - 1])),
56     (M(&[!0]), M(&[!0]), M(&[!0]), M(&[!0]), N),
57     (M(&[!0]), M(&[1, 1]), M(&[!0, 1]), M(&[1]), P(&[!0 - 1, 1])),
58 ];
59 
60 const I32_MIN: i64 = i32::MIN as i64;
61 const I32_MAX: i64 = i32::MAX as i64;
62 const U32_MAX: i64 = u32::MAX as i64;
63 
64 // some corner cases
65 const I64_VALUES: &'static [i64] = &[
66     i64::MIN,
67     i64::MIN + 1,
68     i64::MIN + 2,
69     i64::MIN + 3,
70     -U32_MAX - 3,
71     -U32_MAX - 2,
72     -U32_MAX - 1,
73     -U32_MAX,
74     -U32_MAX + 1,
75     -U32_MAX + 2,
76     -U32_MAX + 3,
77     I32_MIN - 3,
78     I32_MIN - 2,
79     I32_MIN - 1,
80     I32_MIN,
81     I32_MIN + 1,
82     I32_MIN + 2,
83     I32_MIN + 3,
84     -3,
85     -2,
86     -1,
87     0,
88     1,
89     2,
90     3,
91     I32_MAX - 3,
92     I32_MAX - 2,
93     I32_MAX - 1,
94     I32_MAX,
95     I32_MAX + 1,
96     I32_MAX + 2,
97     I32_MAX + 3,
98     U32_MAX - 3,
99     U32_MAX - 2,
100     U32_MAX - 1,
101     U32_MAX,
102     U32_MAX + 1,
103     U32_MAX + 2,
104     U32_MAX + 3,
105     i64::MAX - 3,
106     i64::MAX - 2,
107     i64::MAX - 1,
108     i64::MAX,
109 ];
110 
111 #[test]
test_not()112 fn test_not() {
113     for &(ref a, ref not) in NOT_VALUES.iter() {
114         let a = a.to_bigint().unwrap();
115         let not = not.to_bigint().unwrap();
116 
117         // sanity check for tests that fit in i64
118         if let (Some(prim_a), Some(prim_not)) = (a.to_i64(), not.to_i64()) {
119             assert_eq!(!prim_a, prim_not);
120         }
121 
122         assert_eq!(!a.clone(), not, "!{:x}", a);
123         assert_eq!(!not.clone(), a, "!{:x}", not);
124     }
125 }
126 
127 #[test]
test_not_i64()128 fn test_not_i64() {
129     for &prim_a in I64_VALUES.iter() {
130         let a = prim_a.to_bigint().unwrap();
131         let not = (!prim_a).to_bigint().unwrap();
132         assert_eq!(!a.clone(), not, "!{:x}", a);
133     }
134 }
135 
136 #[test]
test_bitwise()137 fn test_bitwise() {
138     for &(ref a, ref b, ref and, ref or, ref xor) in BITWISE_VALUES.iter() {
139         let a = a.to_bigint().unwrap();
140         let b = b.to_bigint().unwrap();
141         let and = and.to_bigint().unwrap();
142         let or = or.to_bigint().unwrap();
143         let xor = xor.to_bigint().unwrap();
144 
145         // sanity check for tests that fit in i64
146         if let (Some(prim_a), Some(prim_b)) = (a.to_i64(), b.to_i64()) {
147             if let Some(prim_and) = and.to_i64() {
148                 assert_eq!(prim_a & prim_b, prim_and);
149             }
150             if let Some(prim_or) = or.to_i64() {
151                 assert_eq!(prim_a | prim_b, prim_or);
152             }
153             if let Some(prim_xor) = xor.to_i64() {
154                 assert_eq!(prim_a ^ prim_b, prim_xor);
155             }
156         }
157 
158         assert_eq!(a.clone() & &b, and, "{:x} & {:x}", a, b);
159         assert_eq!(b.clone() & &a, and, "{:x} & {:x}", b, a);
160         assert_eq!(a.clone() | &b, or, "{:x} | {:x}", a, b);
161         assert_eq!(b.clone() | &a, or, "{:x} | {:x}", b, a);
162         assert_eq!(a.clone() ^ &b, xor, "{:x} ^ {:x}", a, b);
163         assert_eq!(b.clone() ^ &a, xor, "{:x} ^ {:x}", b, a);
164     }
165 }
166 
167 #[test]
test_bitwise_i64()168 fn test_bitwise_i64() {
169     for &prim_a in I64_VALUES.iter() {
170         let a = prim_a.to_bigint().unwrap();
171         for &prim_b in I64_VALUES.iter() {
172             let b = prim_b.to_bigint().unwrap();
173             let and = (prim_a & prim_b).to_bigint().unwrap();
174             let or = (prim_a | prim_b).to_bigint().unwrap();
175             let xor = (prim_a ^ prim_b).to_bigint().unwrap();
176             assert_eq!(a.clone() & &b, and, "{:x} & {:x}", a, b);
177             assert_eq!(a.clone() | &b, or, "{:x} | {:x}", a, b);
178             assert_eq!(a.clone() ^ &b, xor, "{:x} ^ {:x}", a, b);
179         }
180     }
181 }
182