/** * ibs-utest.c * A naive unit test for binary search. */ #include #include #include "ibs.h" /** * Make a small "random" nonnegative integer. */ int smallrand () { return abs (random () % 100); } // smallrand int main (void) { int foo[5]; int errors = 0; foo[0] = 1 + smallrand (); foo[1] = foo[0] + smallrand (); foo[2] = foo[1] + smallrand (); foo[3] = foo[2] + smallrand (); foo[4] = foo[3] + smallrand (); if (ibs (foo, 5, foo[0]) != 0) { fprintf (stderr, "Failed to find value in position 0.\n"); ++errors; } if (ibs (foo, 5, foo[4]) != 4) { fprintf (stderr, "Failed to find value in position 4.\n"); ++errors; } fprintf (stderr, "%d errors encountered.\n", errors); return 0; } // main