Skip to content

Commit

Permalink
add tests and fix the bugs they found
Browse files Browse the repository at this point in the history
  • Loading branch information
tspiteri committed Feb 27, 2018
1 parent b190dec commit 94de40d
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 32 deletions.
38 changes: 35 additions & 3 deletions src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ fn bitand_pos_neg(a: &mut Vec<BigDigit>, b: &Vec<BigDigit>) {
let abs_b = negate_carry(bi, &mut carry_b);
*ai &= abs_b;
}
if a.len() > b.len() {
debug_assert!(carry_b == 0);
}
}

// -1 & +ff = ...ffff & ...00ff = ...00ff = +ff
Expand All @@ -252,6 +255,7 @@ fn bitand_neg_pos(a: &mut Vec<BigDigit>, b: &Vec<BigDigit>) {
*ai = abs_a & bi;
}
if b.len() > a.len() {
debug_assert!(carry_a == 0);
let extra = &b[a.len()..];
a.extend(extra.iter().cloned());
} else {
Expand All @@ -261,7 +265,8 @@ fn bitand_neg_pos(a: &mut Vec<BigDigit>, b: &Vec<BigDigit>) {

// -1 & -ff = ...ffff & ...ff01 = ...ff01 = -ff
// -ff & -1 = ...ff01 & ...ffff = ...ff01 = -ff
// answer is neg, has length of longest
// -ff & -fe = ...ff01 & ...ff02 = ...ff00 = -100
// answer is neg, has length of longest with possible carry
fn bitand_neg_neg(a: &mut Vec<BigDigit>, b: &Vec<BigDigit>) {
let mut carry_a = 1;
let mut carry_b = 1;
Expand All @@ -283,6 +288,10 @@ fn bitand_neg_neg(a: &mut Vec<BigDigit>, b: &Vec<BigDigit>) {
*ai = negate_carry(abs_a, &mut carry_and);
}
}
debug_assert!(carry_a == 0 && carry_b == 0);
if carry_and != 0 {
a.push(1);
}
}

impl<'a> BitAnd<&'a BigInt> for BigInt {
Expand Down Expand Up @@ -357,6 +366,10 @@ fn bitor_pos_neg(a: &mut Vec<BigDigit>, b: &Vec<BigDigit>) {
} else {
a.truncate(b.len());
}
debug_assert!(carry_b == 0);
if carry_or != 0 {
a.push(1);
}
}

// -1 | +ff = ...ffff | ...00ff = ...ffff = -1
Expand All @@ -369,6 +382,16 @@ fn bitor_neg_pos(a: &mut Vec<BigDigit>, b: &Vec<BigDigit>) {
let abs_a = negate_carry(*ai, &mut carry_a);
*ai = negate_carry(abs_a | bi, &mut carry_or);
}
if a.len() > b.len() {
for ai in a[b.len()..].iter_mut() {
let abs_a = negate_carry(*ai, &mut carry_a);
*ai = negate_carry(abs_a, &mut carry_or);
}
}
debug_assert!(carry_a == 0);
if carry_or != 0 {
a.push(1);
}
}

// -1 | -ff = ...ffff | ...ff01 = ...ffff = -1
Expand All @@ -383,7 +406,13 @@ fn bitor_neg_neg(a: &mut Vec<BigDigit>, b: &Vec<BigDigit>) {
let abs_b = negate_carry(bi, &mut carry_b);
*ai = negate_carry(abs_a | abs_b, &mut carry_or);
}
if b.len() > a.len() {
debug_assert!(carry_a == 0);
} else {
debug_assert!(carry_b == 0);
}
a.truncate(b.len());
debug_assert!(carry_or == 0);
}

impl<'a> BitOr<&'a BigInt> for BigInt {
Expand Down Expand Up @@ -461,7 +490,8 @@ fn bitxor_pos_neg(a: &mut Vec<BigDigit>, b: &Vec<BigDigit>) {
*ai = negate_carry(*ai ^ abs_b, &mut carry_xor);
}
}
if carry_xor ^ carry_b != 0 {
debug_assert!(carry_b == 0);
if carry_xor != 0 {
a.push(1);
}
}
Expand All @@ -488,7 +518,8 @@ fn bitxor_neg_pos(a: &mut Vec<BigDigit>, b: &Vec<BigDigit>) {
*ai = negate_carry(abs_a, &mut carry_xor);
}
}
if carry_xor ^ carry_a > 0 {
debug_assert!(carry_a == 0);
if carry_xor != 0 {
a.push(1);
}
}
Expand Down Expand Up @@ -518,6 +549,7 @@ fn bitxor_neg_neg(a: &mut Vec<BigDigit>, b: &Vec<BigDigit>) {
*ai = abs_a ^ abs_b;
}
}
debug_assert!(carry_a == 0 && carry_b == 0);
}

impl<'a> BitXor<&'a BigInt> for BigInt {
Expand Down
98 changes: 69 additions & 29 deletions tests/bigint_bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate num_traits;

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

enum ValueVec {
N,
Expand All @@ -22,6 +23,17 @@ impl ToBigInt for ValueVec {
}
}

// 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),
Expand All @@ -48,6 +60,45 @@ const BITWISE_VALUES: &'static [(ValueVec, ValueVec, ValueVec, ValueVec, ValueVe
(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() {
Expand All @@ -58,15 +109,15 @@ fn test_bitwise() {
let xor = xor.to_bigint().unwrap();

// sanity check for tests that fit in i64
if let (Some(pa), Some(pb)) = (a.to_i64(), b.to_i64()) {
if let Some(pand) = and.to_i64() {
assert_eq!(pa & pb, pand);
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(por) = or.to_i64() {
assert_eq!(pa | pb, por);
if let Some(prim_or) = or.to_i64() {
assert_eq!(prim_a | prim_b, prim_or);
}
if let Some(pxor) = xor.to_i64() {
assert_eq!(pa ^ pb, pxor);
if let Some(prim_xor) = xor.to_i64() {
assert_eq!(prim_a ^ prim_b, prim_xor);
}
}

Expand All @@ -79,29 +130,18 @@ fn test_bitwise() {
}
}

// 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]))];

#[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(pa), Some(pnot)) = (a.to_i64(), not.to_i64()) {
assert_eq!(!pa, pnot);
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);
}

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

0 comments on commit 94de40d

Please sign in to comment.