/* Verify that the test_ba bitarray has the expected content. Output FAIL or PASS as appropriate for the Python testing script to parse. Use the testutil_expect() macro to run this function. */ static void testutil_expect_internal(const char *bitstr, size_t flipcount, const char* name) { const char *bad = NULL; assert(test_ba != NULL); size_t sl = strlen(bitstr), i; if (sl != bitarray_get_bit_sz(test_ba)) bad = "bitarray size"; for (i = 0; i < sl; i++) { if (bitarray_get(test_ba, i) != boolfromchar(bitstr[i])) bad = "bitarray content"; } if (bad != NULL) { bitarray_fprint(stdout, test_ba); fprintf(stdout, " expect bits=%s \n", bitstr); TEST_FAIL(name, "incorrect %s", bad); } else { TEST_PASS_WITH_NAME(name); } }
static void testutil_expect_internal(const char *bitstring, const char *const func_name, const int line) { // The reason why the test fails. If the test passes, this will stay // NULL. const char *bad = NULL; assert(test_bitarray != NULL); // Check the length of the bit array under test. const size_t bitstring_length = strlen(bitstring); if (bitstring_length != bitarray_get_bit_sz(test_bitarray)) { bad = "bitarray size"; } // Check the content. for (size_t i = 0; i < bitstring_length; i++) { if (bitarray_get(test_bitarray, i) != boolfromchar(bitstring[i])) { bad = "bitarray content"; } } // Obtain a string for the actual bitstring. const size_t actual_bitstring_length = bitarray_get_bit_sz(test_bitarray); char* actual_bitstring = malloc(sizeof(char) * bitstring_length); for (size_t i = 0; i < actual_bitstring_length; i++) { if (bitarray_get(test_bitarray, i)) { actual_bitstring[i] = '1'; } else { actual_bitstring[i] = '0'; } } if (bad != NULL) { bitarray_fprint(stdout, test_bitarray); fprintf(stdout, " expect bits=%s \n", bitstring); TEST_FAIL_WITH_NAME(func_name, line, " Incorrect %s.\n Expected: %s\n Actual: %s", bad, bitstring, actual_bitstring); } else { TEST_PASS_WITH_NAME(func_name, line); } free(actual_bitstring); }
/* 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); } }