/*
 * Testing binary search
 * 15-122 Principles of Imperative Computation, Spring 2011
 * William Lovas
 */

#use <conio>

void test_binsearch(string test, int x, int[] A, int n) {
    println(test);
    binsearch(x, A, n);
}

int main() {
    int[] emptyarray = alloc_array(int, 0);
    test_binsearch("searching for 5 in []", 5, emptyarray, 0);

    int[] singleton = alloc_array(int, 1);
    singleton[0] = 5;
    // look for the element
    test_binsearch("searching for 5 in [5]", 5, singleton, 1);
    // look for something smaller
    test_binsearch("searching for 3 in [5]", 3, singleton, 1);
    // look for something larger
    test_binsearch("searching for 7 in [5]", 7, singleton, 1);

    int[] doubleton = alloc_array(int, 2);
    doubleton[0] = 5;
    doubleton[1] = 7;
    test_binsearch("searching for 2 in [5, 7]", 2, doubleton, 2);
    test_binsearch("searching for 5 in [5, 7]", 5, doubleton, 2);
    test_binsearch("searching for 6 in [5, 7]", 6, doubleton, 2);
    test_binsearch("seacrhing for 7 in [5, 7]", 7, doubleton, 2);
    test_binsearch("searching for 9 in [5, 7]", 9, doubleton, 2);

    doubleton[0] = 5;
    doubleton[1] = 5;
    test_binsearch("searching for 2 in [5, 5]", 2, doubleton, 2);
    test_binsearch("searching for 5 in [5, 5]", 5, doubleton, 2);
    test_binsearch("searching for 6 in [5, 5]", 6, doubleton, 2);

    return 0;
}
