summaryrefslogtreecommitdiffstats
path: root/third_party/rust/num-bigint/tests/bigint_bitwise.rs
blob: cc0c493cb5e0810f10651a6d6b8b788cd37bcadd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
extern crate num_bigint;
extern crate num_traits;

use num_bigint::{BigInt, Sign, ToBigInt};
use num_traits::ToPrimitive;
use std::{i32, i64, u32};

enum ValueVec {
    N,
    P(&'static [u32]),
    M(&'static [u32]),
}

use ValueVec::*;

impl ToBigInt for ValueVec {
    fn to_bigint(&self) -> Option<BigInt> {
        match self {
            &N => Some(BigInt::from_slice(Sign::NoSign, &[])),
            &P(s) => Some(BigInt::from_slice(Sign::Plus, s)),
            &M(s) => Some(BigInt::from_slice(Sign::Minus, s)),
        }
    }
}

// a, !a
const NOT_VALUES: &'static [(ValueVec, ValueVec)] = &[
    (N, M(&[1])),
    (P(&[1]), M(&[2])),
    (P(&[2]), M(&[3])),
    (P(&[!0 - 2]), M(&[!0 - 1])),
    (P(&[!0 - 1]), M(&[!0])),
    (P(&[!0]), M(&[0, 1])),
    (P(&[0, 1]), M(&[1, 1])),
    (P(&[1, 1]), M(&[2, 1])),
];

// a, b, a & b, a | b, a ^ b
const BITWISE_VALUES: &'static [(ValueVec, ValueVec, ValueVec, ValueVec, ValueVec)] = &[
    (N, N, N, N, N),
    (N, P(&[1]), N, P(&[1]), P(&[1])),
    (N, P(&[!0]), N, P(&[!0]), P(&[!0])),
    (N, P(&[0, 1]), N, P(&[0, 1]), P(&[0, 1])),
    (N, M(&[1]), N, M(&[1]), M(&[1])),
    (N, M(&[!0]), N, M(&[!0]), M(&[!0])),
    (N, M(&[0, 1]), N, M(&[0, 1]), M(&[0, 1])),
    (P(&[1]), P(&[!0]), P(&[1]), P(&[!0]), P(&[!0 - 1])),
    (P(&[!0]), P(&[!0]), P(&[!0]), P(&[!0]), N),
    (P(&[!0]), P(&[1, 1]), P(&[1]), P(&[!0, 1]), P(&[!0 - 1, 1])),
    (P(&[1]), M(&[!0]), P(&[1]), M(&[!0]), M(&[0, 1])),
    (P(&[!0]), M(&[1]), P(&[!0]), M(&[1]), M(&[0, 1])),
    (P(&[!0]), M(&[!0]), P(&[1]), M(&[1]), M(&[2])),
    (P(&[!0]), M(&[1, 1]), P(&[!0]), M(&[1, 1]), M(&[0, 2])),
    (P(&[1, 1]), M(&[!0]), P(&[1, 1]), M(&[!0]), M(&[0, 2])),
    (M(&[1]), M(&[!0]), M(&[!0]), M(&[1]), P(&[!0 - 1])),
    (M(&[!0]), M(&[!0]), M(&[!0]), M(&[!0]), N),
    (M(&[!0]), M(&[1, 1]), M(&[!0, 1]), M(&[1]), P(&[!0 - 1, 1])),
];

const I32_MIN: i64 = i32::MIN as i64;
const I32_MAX: i64 = i32::MAX as i64;
const U32_MAX: i64 = u32::MAX as i64;

// some corner cases
const I64_VALUES: &'static [i64] = &[
    i64::MIN,
    i64::MIN + 1,
    i64::MIN + 2,
    i64::MIN + 3,
    -U32_MAX - 3,
    -U32_MAX - 2,
    -U32_MAX - 1,
    -U32_MAX,
    -U32_MAX + 1,
    -U32_MAX + 2,
    -U32_MAX + 3,
    I32_MIN - 3,
    I32_MIN - 2,
    I32_MIN - 1,
    I32_MIN,
    I32_MIN + 1,
    I32_MIN + 2,
    I32_MIN + 3,
    -3,
    -2,
    -1,
    0,
    1,
    2,
    3,
    I32_MAX - 3,
    I32_MAX - 2,
    I32_MAX - 1,
    I32_MAX,
    I32_MAX + 1,
    I32_MAX + 2,
    I32_MAX + 3,
    U32_MAX - 3,
    U32_MAX - 2,
    U32_MAX - 1,
    U32_MAX,
    U32_MAX + 1,
    U32_MAX + 2,
    U32_MAX + 3,
    i64::MAX - 3,
    i64::MAX - 2,
    i64::MAX - 1,
    i64::MAX,
];

#[test]
fn test_not() {
    for &(ref a, ref not) in NOT_VALUES.iter() {
        let a = a.to_bigint().unwrap();
        let not = not.to_bigint().unwrap();

        // sanity check for tests that fit in i64
        if let (Some(prim_a), Some(prim_not)) = (a.to_i64(), not.to_i64()) {
            assert_eq!(!prim_a, prim_not);
        }

        assert_eq!(!a.clone(), not, "!{:x}", a);
        assert_eq!(!not.clone(), a, "!{:x}", not);
    }
}

#[test]
fn test_not_i64() {
    for &prim_a in I64_VALUES.iter() {
        let a = prim_a.to_bigint().unwrap();
        let not = (!prim_a).to_bigint().unwrap();
        assert_eq!(!a.clone(), not, "!{:x}", a);
    }
}

#[test]
fn test_bitwise() {
    for &(ref a, ref b, ref and, ref or, ref xor) in BITWISE_VALUES.iter() {
        let a = a.to_bigint().unwrap();
        let b = b.to_bigint().unwrap();
        let and = and.to_bigint().unwrap();
        let or = or.to_bigint().unwrap();
        let xor = xor.to_bigint().unwrap();

        // sanity check for tests that fit in i64
        if let (Some(prim_a), Some(prim_b)) = (a.to_i64(), b.to_i64()) {
            if let Some(prim_and) = and.to_i64() {
                assert_eq!(prim_a & prim_b, prim_and);
            }
            if let Some(prim_or) = or.to_i64() {
                assert_eq!(prim_a | prim_b, prim_or);
            }
            if let Some(prim_xor) = xor.to_i64() {
                assert_eq!(prim_a ^ prim_b, prim_xor);
            }
        }

        assert_eq!(a.clone() & &b, and, "{:x} & {:x}", a, b);
        assert_eq!(b.clone() & &a, and, "{:x} & {:x}", b, a);
        assert_eq!(a.clone() | &b, or, "{:x} | {:x}", a, b);
        assert_eq!(b.clone() | &a, or, "{:x} | {:x}", b, a);
        assert_eq!(a.clone() ^ &b, xor, "{:x} ^ {:x}", a, b);
        assert_eq!(b.clone() ^ &a, xor, "{:x} ^ {:x}", b, a);
    }
}

#[test]
fn test_bitwise_i64() {
    for &prim_a in I64_VALUES.iter() {
        let a = prim_a.to_bigint().unwrap();
        for &prim_b in I64_VALUES.iter() {
            let b = prim_b.to_bigint().unwrap();
            let and = (prim_a & prim_b).to_bigint().unwrap();
            let or = (prim_a | prim_b).to_bigint().unwrap();
            let xor = (prim_a ^ prim_b).to_bigint().unwrap();
            assert_eq!(a.clone() & &b, and, "{:x} & {:x}", a, b);
            assert_eq!(a.clone() | &b, or, "{:x} | {:x}", a, b);
            assert_eq!(a.clone() ^ &b, xor, "{:x} ^ {:x}", a, b);
        }
    }
}