From 7e76ff5acd182ca1a83242e094f2465d4b9a6040 Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Sun, 3 Aug 2014 13:24:36 -0500 Subject: Make global state explicit in testcase generators The quicksort-killer testcase will require more global state, unless we go to the effort of implementing qsort_r versions of all the sorting algorithms. Since we're not doing that, we'll simply make the global state explicit. --- testcases.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'testcases.c') diff --git a/testcases.c b/testcases.c index 612d1c2..eba3983 100644 --- a/testcases.c +++ b/testcases.c @@ -10,6 +10,13 @@ int buffer[MAX_SIZE]; unsigned long comparisons; +void assert_sorted(size_t size, cmpfun cmp) +{ + for (size_t i = 1; i < size; i++) { + if (cmp(buffer + i-1, buffer + i) > 0) abort(); + } +} + static int compare(const void *a, const void *b) { const int *aa = a; @@ -24,7 +31,7 @@ static int compare(const void *a, const void *b) return 0; } -static void init_random(int *buffer, size_t size) +static void init_random(size_t size) { srandom(1); for (size_t i = 0; i < size; i++) buffer[i] = random(); @@ -43,22 +50,22 @@ static void init_random(int *buffer, size_t size) #endif } -static void init_sorted(int *buffer, size_t size) +static void init_sorted(size_t size) { for (size_t i = 0; i < size; i++) buffer[i] = i; } -static void init_reverse(int *buffer, size_t size) +static void init_reverse(size_t size) { for (size_t i = 0; i < size; i++) buffer[i] = size - i - 1; } -static void init_constant(int *buffer, size_t size) +static void init_constant(size_t size) { for (size_t i = 0; i < size; i++) buffer[i] = 42; } -static void add_noise(int *buffer, size_t size) +static void add_noise(size_t size) { int noisemax = size / 4; int noiseoff = size / 8; @@ -71,16 +78,16 @@ static void add_noise(int *buffer, size_t size) } } -static void init_sorted_noise(int *buffer, size_t size) +static void init_sorted_noise(size_t size) { - init_sorted(buffer, size); - add_noise(buffer, size); + init_sorted(size); + add_noise(size); } -static void init_reverse_noise(int *buffer, size_t size) +static void init_reverse_noise(size_t size) { - init_reverse(buffer, size); - add_noise(buffer, size); + init_reverse(size); + add_noise(size); } const struct testcase testcases[] = { -- cgit v1.2.3