static void test_bv_combined_boolean_ops(TestCase *tc, void *data) { BitVector *bv1 = bv_new(); BitVector *bv2 = bv_new(); BitVector *bv3; BitVector *bv4; BitVector *bv5; BitVector *bv_empty = bv_new(); (void)data; set_bits(bv1, "1, 5, 7"); set_bits(bv2, "1, 8, 20"); bv3 = bv_not(bv1); Aiequal(bv3->size, bv1->size); bv4 = bv_and(bv1, bv3); Assert(bv_eq(bv4, bv_empty), "bv & ~bv == empty BitVector"); bv_destroy(bv4); bv4 = bv_and(bv2, bv3); bv5 = set_bits(bv_new(), "8, 20"); Assert(bv_eq(bv4, bv5), "~[1,5,7] & [1,8,20] == [8,20]"); bv_destroy(bv4); bv_destroy(bv5); bv4 = bv_or(bv1, bv3); bv5 = bv_not(bv_empty); Assert(bv_eq(bv4, bv5), "bv | ~bv == all 1s"); bv_destroy(bv4); bv_destroy(bv5); bv4 = bv_or(bv2, bv3); bv5 = bv_not_x(set_bits(bv_new(), "5, 7")); Assert(bv_eq(bv4, bv5), "~[1,5,7] | [1,8,20] == ~[5, 7]"); bv_destroy(bv4); bv_destroy(bv5); bv4 = bv_xor(bv1, bv3); bv5 = bv_not(bv_empty); Assert(bv_eq(bv4, bv5), "bv ^ ~bv == full BitVector"); bv_destroy(bv4); bv_destroy(bv5); bv4 = bv_xor(bv2, bv3); bv5 = bv_not_x(set_bits(bv_new(), "5, 7, 8, 20")); Assert(bv_eq(bv4, bv5), "~[1,5,7] ^ [1,8,20] == ~[5, 7, 8, 20]"); bv_destroy(bv4); bv_destroy(bv5); bv_destroy(bv1); bv_destroy(bv2); bv_destroy(bv3); bv_destroy(bv_empty); }
static void test_bv_eq_hash(TestCase *tc, void *data) { static int const COUNT = 1000; int i; BitVector *bv1 = bv_new(); BitVector *bv2 = bv_new(); (void)data; test_bveq(bv1, bv2); Assert(bv_eq(bv1, bv1), "bv_eq on self should work"); bv_set(bv1, 1); test_bvneq(bv1, bv2); bv_set(bv2, 11); test_bvneq(bv1, bv2); bv_set(bv1, 11); bv_set(bv2, 1); test_bveq(bv1, bv2); /* This will increase size of bv1 to 1000 */ bv_unset(bv1, 1000); /* difference in size shouldn't matter */ test_bveq(bv1, bv2); for (i = 0; i < COUNT; i++) { int bit = rand() % COUNT; bv_set(bv1, bit); bv_set(bv2, bit); } test_bveq(bv1, bv2); bv_destroy(bv1); bv_destroy(bv2); /* although the saet bits will be equal, the extension will be different*/ bv1 = set_bits(bv_new(), "1, 3, 5"); bv2 = bv_not_x(set_bits(bv_new(), "0, 2, 4")); bv_set(bv2, 5); test_bvneq(bv1, bv2); bv_destroy(bv2); bv2 = set_bits(bv_new(), "1, 3, 5"); bv1 = bv_not_x(bv1); bv2 = bv_not_x(bv2); bv_unset(bv1, 1000); test_bvneq(bv1, bv2); bv_destroy(bv1); bv_destroy(bv2); }
/* * call-seq: * bv.not! -> self * * Perform a boolean _not_ operation on +bv+ in-place */ VALUE frt_bv_not_x(VALUE self) { BitVector *bv; GET_BV(bv, self); bv_not_x(bv); return self; }
static void test_bv_not(TestCase *tc, void *data) { BitVector *bv = bv_new(), *not_bv; int i; (void)data; set_bits(bv, "1, 5, 25, 41, 97, 185"); Aiequal(186, bv->size); not_bv = bv_not(bv); Aiequal(bv->count, not_bv->count); for (i = 0; i < 300; i++) { Aiequal(1 - bv_get(bv, i), bv_get(not_bv, i)); } bv_not_x(bv); Assert(bv_eq(bv, not_bv), "BitVectors equal"); bv_destroy(bv); bv_destroy(not_bv); }
static void test_bv_and(TestCase *tc, void *data) { # define AND_SIZE 1000 static const int and_cnt = 500; BitVector *and_bv; BitVector *bv1 = bv_new(); BitVector *bv2 = bv_new(); BitVector *not_bv1, *not_bv2, *or_bv, *not_and_bv; char set1[AND_SIZE]; char set2[AND_SIZE]; int i; int count = 0; (void)data; memset(set1, 0, AND_SIZE); memset(set2, 0, AND_SIZE); for (i = 0; i < and_cnt; i++) { int bit = rand() % AND_SIZE; bv_set(bv1, bit); set1[bit] = 1; } for (i = 0; i < and_cnt; i++) { int bit = rand() % AND_SIZE; bv_set(bv2, bit); if (1 == set1[bit] && !set2[bit]) { count++; set2[bit] = 1; } } not_bv1 = bv_not(bv1); not_bv2 = bv_not(bv2); and_bv = bv_and(not_bv1, not_bv2); not_and_bv = bv_not(and_bv); or_bv = bv_or(bv1, bv2); Assert(bv_eq(not_and_bv, or_bv), "BitVectors should be equal"); bv_destroy(not_bv1); bv_destroy(not_bv2); bv_destroy(and_bv); bv_destroy(not_and_bv); bv_destroy(or_bv); and_bv = bv_and(bv1, bv2); Aiequal(count, and_bv->count); for (i = 0; i < AND_SIZE; i++) { Aiequal(set2[i], bv_get(and_bv, i)); } bv1 = bv_and_x(bv1, bv2); Assert(bv_eq(bv1, and_bv), "BitVectors should be equal"); bv_destroy(bv2); bv_destroy(and_bv); bv2 = bv_new(); and_bv = bv_and(bv1, bv2); Assert(bv_eq(bv2, and_bv), "ANDed BitVector should be empty"); bv_destroy(bv1); bv_destroy(bv2); bv_destroy(and_bv); bv1 = bv_new(); bv_not_x(bv1); bv2 = bv_new(); bv_set(bv2, 10); bv_set(bv2, 11); bv_set(bv2, 20); and_bv = bv_and(bv1, bv2); Assert(bv_eq(bv2, and_bv), "ANDed BitVector should be equal"); bv_destroy(bv1); bv_destroy(bv2); bv_destroy(and_bv); }