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); }
/* * call-seq: * bv1 & bv2 -> anded_bv * bv1.and(bv2) -> anded_bv * * Perform a boolean _and_ operation on +bv1+ and * +bv2+ */ VALUE frt_bv_and(VALUE self, VALUE other) { BitVector *bv1, *bv2; GET_BV(bv1, self); GET_BV(bv2, other); return Data_Wrap_Struct(cBitVector, NULL, &bv_destroy, bv_and(bv1, bv2)); }
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); }