char *test_bit_operation() { bit_t tmp; int i; bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 5, 99); bit_set(b, 100, 222); tmp = bit_union(a, b); for (i=5; i<=222; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_union is wrong.\n"); } mu_assert(bit_lt(a, tmp), "bit_union is wrong.\n"); mu_assert(bit_lt(b, tmp), "bit_union is wrong.\n"); bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 7, 99); bit_set(b, 7, 98); tmp = bit_union(a, b); for (i=7; i<=99; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_union is wrong.\n"); } mu_assert(bit_eq(a, tmp), "bit_union is wrong.\n"); mu_assert(bit_lt(b, tmp), "bit_union is wrong.\n"); bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 7, 88); bit_set(b, 50, 98); tmp = bit_union(a, b); for (i=7; i<=98; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_union is wrong.\n"); } mu_assert(bit_lt(a, tmp), "bit_union is wrong.\n"); mu_assert(bit_lt(b, tmp), "bit_union is wrong.\n"); bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); /* test bit_inter */ bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 7, 88); bit_set(b, 50, 98); tmp = bit_inter(a, b); for (i=50; i<=88; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_inter is wrong.\n"); } mu_assert(bit_lt(tmp, a), "bit_inter is wrong.\n"); mu_assert(bit_lt(tmp, b), "bit_inter is wrong.\n"); bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 18, 200); bit_set(b, 18, 200); tmp = bit_inter(a, b); mu_assert(bit_eq(tmp, a), "bit_inter is wrong.\n"); mu_assert(bit_eq(tmp, b), "bit_inter is wrong.\n"); bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 18, 100); bit_set(b, 111, 200); tmp = bit_inter(a, b); bit_clear(a, 0, 255); mu_assert(bit_eq(tmp, a), "bit_inter is wrong.\n"); bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); /* test of bit_minus */ bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 20, 201); bit_set(b, 111, 200); tmp = bit_minus(a, b); for (i=20; i<=110; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_minus is wrong.\n"); } for (i=111; i<=200; i++) { mu_assert(bit_get(tmp, i) == 0, "bit_minus is wrong.\n"); } mu_assert(bit_get(tmp, 201) == 1, "bit_minus is wrong.\n"); bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); tmp = bit_minus(b, a); bit_clear(a, 0, 255); mu_assert(bit_eq(tmp, a), "bit_minus is wrong.\n"); bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 20, 110); bit_set(b, 111, 200); tmp = bit_minus(a, b); mu_assert(bit_eq(tmp, a), "bit_minus is wrong.\n"); bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 20, 120); bit_set(b, 111, 200); tmp = bit_minus(a, b); for (i=20; i<=110; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_minus is wrong.\n"); } for (i=111; i<=200; i++) { mu_assert(bit_get(tmp, i) == 0, "bit_minus is wrong.\n"); } bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); /* test bit_diff */ bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 20, 222); bit_set(b, 111, 200); tmp = bit_diff(a, b); for (i=20; i<=110; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_diff is wrong.\n"); } for (i=111; i<=200; i++) { mu_assert(bit_get(tmp, i) == 0, "bit_diff is wrong.\n"); } for (i=201; i<=222; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_diff is wrong.\n"); } bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); tmp = bit_diff(b, a); for (i=20; i<=110; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_diff is wrong.\n"); } for (i=111; i<=200; i++) { mu_assert(bit_get(tmp, i) == 0, "bit_diff is wrong.\n"); } for (i=201; i<=222; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_diff is wrong.\n"); } bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 20, 110); bit_set(b, 111, 200); tmp = bit_diff(a, b); for (i = 20; i <= 200; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_diff is wrong.\n"); } bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); bit_clear(a, 0, 255); bit_clear(b, 0, 255); bit_set(a, 20, 158); bit_set(b, 111, 200); tmp = bit_diff(a, b); for (i = 20; i <= 110; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_diff is wrong.\n"); } for (i = 111; i <= 158; i++) { mu_assert(bit_get(tmp, i) == 0, "bit_diff is wrong.\n"); } for (i = 159; i <= 200; i++) { mu_assert(bit_get(tmp, i) == 1, "bit_diff is wrong.\n"); } bit_free(&tmp); mu_assert(tmp == NULL, "bit_free gets wrong.\n"); return NULL; }
void test_bit(void) { int i; void* B = bit_create(10); fprintf(stdout, "call function : %s\n", __func__); bit_object_show(B); fprintf(stdout, "\ntest function - bit_set/bit_get ===>\n"); { srand((unsigned int)time(0)); for (i = 0; i < bit_size(B); ++i) bit_set(B, i, rand() % 2); bit_object_show(B); for (i = 0; i < bit_size(B); ++i) fprintf(stdout, "\tbit values [%02d] => %d\n", i, bit_get(B, i)); } fprintf(stdout, "\ntest function - bit_clear_range ===>\n"); { bit_clear_range(B, 0, 4); bit_object_show(B); bit_for_each(B, bit_object_bit_show, NULL); } fprintf(stdout, "\ntest function - bit_set_range ===>\n"); { bit_set_range(B, 5, 9); bit_object_show(B); bit_for_each(B, bit_object_bit_show, NULL); } fprintf(stdout, "\ntest function - bit_not_range ===>\n"); { bit_not_range(B, 0, 9); bit_object_show(B); bit_for_each(B, bit_object_bit_show, NULL); } fprintf(stdout, "\ntest function - bit_clear ===>\n"); { bit_clear(B); bit_object_show(B); } fprintf(stdout, "\ntest function - bit_release ===>\n"); bit_release(&B); bit_object_show(B); fprintf(stdout, "\ntest function - bit_lt/bit_eq/bit_leq ===>\n"); { void* B1 = bit_create(5); void* B2 = bit_create(5); srand((unsigned int)time(0)); for (i = 0; i < 5; ++i) { bit_set(B1, i, rand() % 2); bit_set(B2, i, rand() % 2); } fprintf(stdout, " B1 object show:\n"); bit_for_each(B1, bit_object_bit_show, NULL); fprintf(stdout, " B2 object show:\n"); bit_for_each(B2, bit_object_bit_show, NULL); fprintf(stdout, " B1 < B2 ? '%s'\n", bit_lt(B1, B2) ? "yes" : "no"); fprintf(stdout, " B1 = B2 ? '%s'\n", bit_eq(B1, B2) ? "yes" : "no"); fprintf(stdout, " B1 <= B2 ? '%s'\n", bit_leq(B1, B2) ? "yes" : "no"); bit_release(&B1); bit_release(&B2); } fprintf(stdout, "\ntest function - union/inter/minus/diff ===>\n"); { void* B1 = bit_create(5); void* B2 = bit_create(5); srand((unsigned int)time(0)); for (i = 0; i < 5; ++i) { bit_set(B1, i, rand() % 2); bit_set(B2, i, rand() % 2); } fprintf(stdout, " B1 object show:\n"); bit_for_each(B1, bit_object_bit_show, NULL); fprintf(stdout, " B2 object show:\n"); bit_for_each(B2, bit_object_bit_show, NULL); B = bit_union(B1, B2); fprintf(stdout, " B1 union B2 ->\n"); bit_for_each(B, bit_object_bit_show, NULL); bit_release(&B); B = bit_inter(B1, B2); fprintf(stdout, " B1 inter B2 ->\n"); bit_for_each(B, bit_object_bit_show, NULL); bit_release(&B); B = bit_minus(B1, B2); fprintf(stdout, " B1 minus B2 ->\n"); bit_for_each(B, bit_object_bit_show, NULL); bit_release(&B); B = bit_diff(B1, B2); fprintf(stdout, " B1 diff B2 ->\n"); bit_for_each(B, bit_object_bit_show, NULL); bit_release(&B); bit_release(&B1); bit_release(&B2); } }