add count sort
This commit is contained in:
@@ -2,7 +2,7 @@ CC=gcc
|
||||
CFLAGS=-Wall -I../src/include -DDEBUG -g
|
||||
SRC=test.c
|
||||
OBJ=$(SRC:.c=.o)
|
||||
OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o
|
||||
OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o
|
||||
|
||||
all: test
|
||||
|
||||
|
28
test/test.c
28
test/test.c
@@ -19,6 +19,7 @@
|
||||
#include <unistd.h>
|
||||
#include "random.h"
|
||||
#include "bubble_sort.h"
|
||||
#include "count_sort.h"
|
||||
|
||||
static int *test_case;
|
||||
static int *test_array;
|
||||
@@ -50,7 +51,7 @@ int main(int argc, char **argv) {
|
||||
int n = 50000;
|
||||
int i;
|
||||
int gen;
|
||||
int pass = 1;
|
||||
int pass;
|
||||
int passed = 0;
|
||||
int failed = 0;
|
||||
|
||||
@@ -63,6 +64,7 @@ int main(int argc, char **argv) {
|
||||
fprintf(stdout, "Running tests:\n");
|
||||
|
||||
// Test random number generation
|
||||
pass = 1;
|
||||
fprintf(stdout, "\trandom generation: ");
|
||||
fflush(stdout);
|
||||
for (i = 0; i < 5000000; i++) {
|
||||
@@ -78,22 +80,22 @@ int main(int argc, char **argv) {
|
||||
fprintf(stdout, "pass\n");
|
||||
passed++;
|
||||
}
|
||||
pass = 1;
|
||||
|
||||
// Prepare for sort tests
|
||||
for (i = 0; i < n; i++) {
|
||||
test_case[i] = gen_rand(-100, 100);
|
||||
}
|
||||
memcpy(qarray, test_case, sizeof(int) * n);
|
||||
memcpy(test_array, test_case, sizeof(int) * n);
|
||||
qsort(qarray, n, sizeof(int), compar);
|
||||
|
||||
// Test bubble sort
|
||||
pass = 1;
|
||||
memcpy(test_array, test_case, sizeof(int) * n);
|
||||
fprintf(stdout, "\tbubble sort: ");
|
||||
fflush(stdout);
|
||||
bubble_sort(test_array, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
if (test_array[i] != qarray[i] && pass) {
|
||||
if (test_array[i] != qarray[i]) {
|
||||
fprintf(stdout, "fail\n");
|
||||
failed++;
|
||||
pass = 0;
|
||||
@@ -104,7 +106,25 @@ int main(int argc, char **argv) {
|
||||
fprintf(stdout, "pass\n");
|
||||
passed++;
|
||||
}
|
||||
|
||||
// Test count sort
|
||||
pass = 1;
|
||||
memcpy(test_array, test_case, sizeof(int) * n);
|
||||
fprintf(stdout, "\tcount sort: ");
|
||||
fflush(stdout);
|
||||
count_sort(test_array, n);
|
||||
for (i = 0; i < n; i++) {
|
||||
if (test_array[i] != qarray[i]) {
|
||||
fprintf(stdout, "fail\n");
|
||||
failed++;
|
||||
pass = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (pass) {
|
||||
fprintf(stdout, "pass\n");
|
||||
passed++;
|
||||
}
|
||||
|
||||
fprintf(stdout, "%d tests passed\n", passed);
|
||||
fprintf(stdout, "%d tests failed\n", failed);
|
||||
|
Reference in New Issue
Block a user