summaryrefslogtreecommitdiff
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
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.
-rw-r--r--bench.c4
-rw-r--r--sorters.c10
-rw-r--r--sorters.h2
-rw-r--r--testcases.c29
-rw-r--r--testcases.h4
5 files changed, 23 insertions, 26 deletions
diff --git a/bench.c b/bench.c
index e0629b2..75bf32e 100644
--- a/bench.c
+++ b/bench.c
@@ -34,14 +34,14 @@ int main()
printf("%-*s ", SORT_WIDTH, size == MIN_SIZE ? s->name : "");
for (const struct testcase *t = testcases; t->name; t++) {
comparisons = 0;
- t->init(buffer, size);
+ t->init(size);
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &start)) abort();
s->func(buffer, size, sizeof(int), t->cmp);
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &stop)) abort();
printf("%*lu %*lu ", CMP_WIDTH, comparisons, MS_WIDTH, timediff_ms(&start, &stop));
- assert_sorted(buffer, size);
+ assert_sorted(size, t->cmp);
}
printf(" %*zu\n", SIZE_WIDTH, size);
}
diff --git a/sorters.c b/sorters.c
index ffa9e7a..433d71f 100644
--- a/sorters.c
+++ b/sorters.c
@@ -14,13 +14,3 @@ const struct sorter sorters[] = {
{ .name = "grailsort (ref)", .func = grailsort_ref },
{ 0 }
};
-
-void assert_sorted(int *buffer, size_t size)
-{
- if (!size) return;
- int prev = buffer[0];
- for (size_t i = 1; i < size; i++) {
- if (prev > buffer[i]) abort();
- prev = buffer[i];
- }
-}
diff --git a/sorters.h b/sorters.h
index a5c0f14..4a2b01b 100644
--- a/sorters.h
+++ b/sorters.h
@@ -3,8 +3,6 @@
#include <stddef.h>
-void assert_sorted(int *buffer, size_t size);
-
typedef int (*cmpfun)(const void *, const void *);
typedef void (*sorterfn)(void *, size_t, size_t, cmpfun);
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[] = {
diff --git a/testcases.h b/testcases.h
index 036fe5d..fc2ffd9 100644
--- a/testcases.h
+++ b/testcases.h
@@ -8,7 +8,9 @@
extern int buffer[MAX_SIZE];
extern unsigned long comparisons;
-typedef void (*testinit)(int *, size_t);
+void assert_sorted(size_t size, cmpfun cmp);
+
+typedef void (*testinit)(size_t);
extern const struct testcase {
const char *name;