static void testutil_newrand(const size_t bit_sz, const unsigned int seed) { // 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(bit_sz); assert(test_bitarray != NULL); // Reseed the RNG with whatever we were passed; this ensures that we can // repeat the test deterministically by specifying the same seed. srand(seed); for (size_t i = 0; i < bit_sz; i++) { bitarray_set(test_bitarray, i, randbit()); } // If we were asked to be verbose, go ahead and show the bit array and // the random seed. if (test_verbose) { bitarray_fprint(stdout, test_bitarray); fprintf(stdout, " newrand sz=%zu, seed=%u\n", bit_sz, seed); } }
/* Create a new bitarray in test_ba of the specified size and fill it with random data based on the seed given. For a given seed number, the pseudorandom data will be the same (at least on the same glibc implementation). */ static void testutil_newrand(size_t bit_sz, unsigned int seed) { size_t i; if (test_ba != NULL) bitarray_free(test_ba); test_ba = bitarray_new(bit_sz); assert(test_ba != NULL); srand(seed); for (i = 0; i < bit_sz; i++) { bool temp = randbit() ; bitarray_set(test_ba, i, temp); } if (test_verbose) { bitarray_fprint(stdout, test_ba); fprintf(stdout, "\n"); fprintf(stdout, " newrand sz=%llu, seed=%u\n", (unsigned long long) bit_sz, seed); } }
/* 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); } }