summaryrefslogtreecommitdiff
path: root/testcases.c
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2014-08-03 13:24:36 -0500
committerBobby Bingham <koorogi@koorogi.info>2014-08-03 13:24:36 -0500
commit7e76ff5acd182ca1a83242e094f2465d4b9a6040 (patch)
tree83e82f88ffb65ea4f8b33ad69fd69a6b72336544 /testcases.c
parentf07937bb63ed12a50a203c3f8015649f37bc73e5 (diff)
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.
Diffstat (limited to 'testcases.c')
-rw-r--r--testcases.c29
1 files changed, 18 insertions, 11 deletions
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[] = {