static void test_headerexamples(void) { /* Verify the examples given in bitarray.h. */ testutil_frmstr("10010110"); testutil_expect("10010110", 5); testutil_rotate(0, 8, -1); testutil_expect("00101101", 5); testutil_frmstr("10010110"); testutil_rotate(2, 5, 2); testutil_expect("10110100", 5); }
static void test_12bit(void) { testutil_frmstr("010000101000"); testutil_rotate(0, 12, 1); testutil_expect("001000010100", 6); testutil_frmstr("010000101000"); testutil_rotate(0, 12, -1); testutil_expect("100001010000", 5); testutil_rotate(0, 12, -1); testutil_expect("000010100001", 5); testutil_rotate(0, 12, -1); testutil_expect("000101000010", 6); testutil_frmstr("010000101000"); testutil_rotate(0, 12, 0); testutil_expect("010000101000", 6); testutil_rotate(0, 12, 1); testutil_expect("001000010100", 6); testutil_rotate(0, 12, -1); testutil_expect("010000101000", 6); testutil_rotate(0, 12, -1); testutil_expect("100001010000", 5); testutil_rotate(0, 12, -(3 + 12)); testutil_expect("001010000100", 6); }
static void test_8bit(void) { testutil_frmstr("10000101"); testutil_rotate(0, 8, 0); testutil_expect("10000101", 4); testutil_rotate(0, 8, 1); testutil_expect("11000010", 3); testutil_rotate(0, 8, -1); testutil_expect("10000101", 4); testutil_rotate(0, 8, -1); testutil_expect("00001011", 3); testutil_rotate(0, 8, -(3 + 8)); testutil_expect("01011000", 4); }
/* Create a new bitarray in test_ba by parsing a string of 0s and 1s, e.g. "0101011011". */ static void testutil_frmstr(const char *bitstr) { size_t sl = strlen(bitstr), i; if (test_ba != NULL) bitarray_free(test_ba); test_ba = bitarray_new(sl); assert(test_ba != NULL); size_t myflipcount = 0; bool curbit, prevbit = false; for (i = 0; i < sl; i++) { curbit = boolfromchar(bitstr[i]); if (i != 0 && curbit != prevbit) myflipcount++; bitarray_set(test_ba, i, curbit); prevbit = curbit; } bitarray_fprint(stdout, test_ba); if (test_verbose) { fprintf(stdout, " newstr lit=%s\n", bitstr); testutil_expect(bitstr, myflipcount); } }
void testutil_frmstr(const char *const bitstring) { const size_t bitstring_length = strlen(bitstring); // If we somehow managed to avoid freeing test_bitarray after a previous // test, go free it now. if (test_bitarray != NULL) { bitarray_free(test_bitarray); } test_bitarray = bitarray_new(bitstring_length); assert(test_bitarray != NULL); bool current_bit; for (size_t i = 0; i < bitstring_length; i++) { current_bit = boolfromchar(bitstring[i]); bitarray_set(test_bitarray, i, current_bit); } bitarray_fprint(stdout, test_bitarray); if (test_verbose) { fprintf(stdout, " newstr lit=%s\n", bitstring); testutil_expect(bitstring); } }
static void test_lotsmore(void) { testutil_frmstr("00101"); testutil_rotate(0, 5, 0); testutil_expect("00101", 3); testutil_rotate(0, 5, 1); testutil_expect("10010", 3); testutil_rotate(0, 5, -1); testutil_expect("00101", 3); testutil_rotate(0, 5, -1); testutil_expect("01010", 4); testutil_rotate(0, 5, -(3 + 5)); testutil_expect("10010", 3); testutil_frmstr("11000101"); testutil_rotate(3, 5, 0); testutil_expect("11000101", 4); testutil_rotate(3, 5, 1); testutil_expect("11010010", 5); testutil_rotate(3, 5, -1); testutil_expect("11000101", 4); testutil_rotate(3, 5, -1); testutil_expect("11001010", 5); testutil_rotate(3, 5, -(3 + 5)); testutil_expect("11010010", 5); testutil_frmstr("11000101000010"); testutil_rotate(3, 5, 0); testutil_expect("11000101000010", 7); testutil_rotate(3, 5, 1); testutil_expect("11010010000010", 7); testutil_rotate(3, 5, -1); testutil_expect("11000101000010", 7); testutil_rotate(3, 5, -1); testutil_expect("11001010000010", 7); testutil_rotate(3, 5, -(3 + 5 * 99)); testutil_expect("11010010000010", 7); testutil_frmstr(""); testutil_rotate(0, 0, -5); testutil_expect("", 0); testutil_frmstr("1"); testutil_rotate(0, 0, -5); testutil_expect("1", 0); testutil_rotate(0, 1, -5); testutil_expect("1", 0); testutil_frmstr("11000101000010010100100101000110010101101001010010000100010"); /* 59 bits */ testutil_rotate(0, 0, -5); testutil_expect("11000101000010010100100101000110010101101001010010000100010", 37); testutil_rotate(0, 59, 15); testutil_expect("01001000010001011000101000010010100100101000110010101101001", 37); testutil_rotate(14, 27, 3); /* ..............***************************..................*/ testutil_expect("01001000010001101011000101000010010100100000110010101101001", 35); testutil_rotate(57, 2, -3); testutil_expect("01001000010001101011000101000010010100100000110010101101010", 36); testutil_frmstr("11000101000010010100100101000110010101101001010010000100010110011001"); /* 68 bits */ testutil_rotate(0, 0, -5); testutil_expect("11000101000010010100100101000110010101101001010010000100010110011001", 42); testutil_rotate(0, 68, 15); testutil_expect("10001011001100111000101000010010100100101000110010101101001010010000", 41); testutil_rotate(14, 27, 3); /* ..............***************************...........................*/ testutil_expect("10001011001100101111000101000010010100100000110010101101001010010000", 39); testutil_rotate(57, 2, -3); testutil_expect("10001011001100101111000101000010010100100000110010101101010010010000", 39); }