static void test_bv_xor(TestCase *tc, void *data) { # define XOR_SIZE 1000 static const int xor_cnt = 500; BitVector *xor_bv; BitVector *bv1 = bv_new(); BitVector *bv2 = bv_new(); char set[XOR_SIZE]; char set1[XOR_SIZE]; char set2[XOR_SIZE]; int i; int count = 0; (void)data; memset(set, 0, XOR_SIZE); memset(set1, 0, XOR_SIZE); memset(set2, 0, XOR_SIZE); for (i = 0; i < xor_cnt; i++) { int bit = rand() % XOR_SIZE; bv_set(bv1, bit); set1[bit] = 1; } for (i = 0; i < xor_cnt; i++) { int bit = rand() % XOR_SIZE; bv_set(bv2, bit); set2[bit] = 1; } for (i = 0; i < XOR_SIZE; i++) { if (set1[i] != set2[i]) { set[i] = 1; count++; } } xor_bv = bv_xor(bv1, bv2); Aiequal(count, xor_bv->count); for (i = 0; i < XOR_SIZE; i++) { if (!Aiequal(set[i], bv_get(xor_bv, i))) { Tmsg("At position %d, bv1 is %d and bv2 is %d\n", i, bv_get(bv1, i), bv_get(bv2, i)); } } bv1 = bv_xor_x(bv1, bv2); Assert(bv_eq(bv1, xor_bv), "BitVectors should be equal"); bv_destroy(bv2); bv_destroy(xor_bv); bv2 = bv_new(); xor_bv = bv_xor(bv1, bv2); Assert(bv_eq(bv1, xor_bv), "XORed BitVector equals bv1"); bv_destroy(bv1); bv_destroy(bv2); bv_destroy(xor_bv); }
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 -> xored_bv * bv1.xor(bv2) -> xored_bv * * Perform a boolean _xor_ operation on +bv1+ and * +bv2+ */ VALUE frt_bv_xor(VALUE self, VALUE other) { BitVector *bv1, *bv2; GET_BV(bv1, self); GET_BV(bv2, other); return Data_Wrap_Struct(cBitVector, NULL, &bv_destroy, bv_xor(bv1, bv2)); }