/** * ibs-utest.c * Sam's quick and dirty unit test for binary search. */ #include #include "ibs.h" int errors = 0; void test (int size) { int A[size]; int i; int result; for (i = 0; i < size; i++) A[i] = 2*i; for (i = 0; i < size; i++) { // Even value should be there. result = ibs (A, size, 2*i); if (result != i) { fprintf (stderr, "Searching for %d in size %d, expected %d, got %d\n", 2*i, size, i, result); ++errors; } // Odd value should not be there result = ibs (A, size, 2*i - 1); if (result != -1) { fprintf (stderr, "Searching for %d in size %d, expected -1, got %d\n", 2*i-1, size, result); ++errors; } } // for result = ibs (A, size, 2*size - 1); if (result != -1) { fprintf (stderr, "Searching for %d in size %d, expected -1, got %d\n", 2*size-1, size, result); ++errors; } } int main (void) { int j; for (j = 0; j < 33; j++) test (j); fprintf (stderr, "%d errors encountered.\n", errors); return errors; }