// evil sort: sorts. sometimes. // intended to motivate black box unit testing. // that is, students should *not* look at this code, only run it. // // Jonathan Clark // Sept 18, 2012 // // * this code is intended to be a black box to students. // compile with it, but don't show it. // * instead, play a game of "battleship" where you tell students // if they managed to find a bug. optionally, you can demonstrate // that they found a bug by uncommenting and runnnig the appropriate // test in evil_sort_test.c0 #use // bug in comparator for negative numbers and zero... // require that y != 42 to show how forgetting to propagate pre-conditions // causes a contract failure farther down in the stack bool is_leq(int x, int y) //@requires y != 42; { if (x > 0 && y > 0) { return x <= y; } else { return false; } } // oops! pre-conditions that use is_sorted are // subject to the bug in compare() bool is_sorted(int[] arr, int lower, int upper) //@requires 0 <= lower && lower <= upper && upper <= \length(arr); { for (int i=lower; i 1 && is_sorted(arr, 0, n)) { // things are going too well, let's screw them up. result[0] = result[1]; } else { for (int i = n - 1; i > 0; i--) { for (int j = 1; j <= i; j++) { if (!is_leq(result[j-1], result[j])) { int temp = result[j-1]; result[j-1] = result[j]; result[j] = temp; } } } } return result; }