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_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_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); }
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); }
void parse_and_run_tests(const char *filename, int selected_test) { test_verbose = false; fprintf(stderr, "Testing file %s.\n", filename); FILE *f = fopen(filename, "r"); char buf[BUF_SIZE]; int test = -1; int line = 0; bool ready_to_run = false; if (f == NULL) { fprintf(stderr, "Error opening file.\n"); return; } while (fgets(buf, BUF_SIZE, f) != NULL) { line++; char* token = strtok(buf, " "); switch (token[0]) { case '\n': case '#': continue; case 't': test = NEXT_ARG_INT(); ready_to_run = (test == selected_test || selected_test == -1); if (!ready_to_run) { continue; } fprintf(stderr, "\nRunning test #%d...\n", test); break; case 'n': if (!ready_to_run) { continue; } testutil_frmstr(next_arg_char()); break; case 'e': if (!ready_to_run) { continue; } { char *expected = next_arg_char(); testutil_expect_internal(expected, filename, line); } break; case 'r': if (!ready_to_run) { continue; } { size_t offset = NEXT_ARG_INT(); size_t length = NEXT_ARG_INT(); ssize_t amount = NEXT_ARG_INT(); testutil_require_valid_input(offset, length, amount, filename, line); testutil_rotate(offset, length, amount); } break; default: fprintf(stderr, "Unknown command %s", buf); } } fprintf(stderr, "Done testing file %s.\n", filename); }