/* * call-seq: * bv1.and!(bv2) -> self * * Perform a boolean _and_ operation on +bv1+ and * +bv2+ in place on +bv1+ */ VALUE frt_bv_and_x(VALUE self, VALUE other) { BitVector *bv1, *bv2; GET_BV(bv1, self); GET_BV(bv2, other); bv_and_x(bv1, bv2); return self; }
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); }