diff --git a/Makefile b/Makefile index 3ada494..def505a 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,11 @@ ifneq (, $(shell which pdflatex)) mv doc/Informe.pdf Informe.pdf endif -clean: cleansort cleaninforme +test: + make -C test + test/test + +clean: cleansort cleaninforme cleantest cleansort: rm -f src/*.o sort @@ -25,4 +29,7 @@ cleaninforme: make -C doc clean rm -f Informe.pdf -.PHONY: all sort informe clean cleansort cleaninforme +cleantest: + make -C test clean + +.PHONY: all sort informe test clean cleansort cleaninforme cleantest diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..2b8cab0 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,15 @@ +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 + +all: test + +test: $(OBJ) + $(CC) -o $@ $^ $(LDFLAGS) + +clean: + rm -f *.o test + +.PHONY: all test clean diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..4bbae28 --- /dev/null +++ b/test/test.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include "random.h" +#include "bubble_sort.h" + +static int *test_case; +static int *test_array; +static int *qarray; + +/** + * Este funciĆ³n va a comparar valores para devolver los valores en orden ascendida + * @param a El primer valor a comparar + * @param b El segundo valor a comparar + * @return Retorna la comparaciĆ³n de los dos argumentos + */ +int compar(const void *a, const void *b) { + return (*(int*) a - *(int*) b); +} + +/** + * Limpiar la memoria al salir + */ +void cleanup() { + free(test_case); + free(test_array); + free(qarray); +} + +/** + * El programa de test + */ +int main(int argc, char **argv) { + int n = 50000; + int i; + int gen; + + test_case = malloc(sizeof(int) * n); + qarray = malloc(sizeof(int) * n); + test_array = malloc(sizeof(int) * n); + + atexit(cleanup); + + fprintf(stdout, "Running tests:\n"); + + // Test random number generation + fprintf(stdout, "\trandom generation: "); + fflush(stdout); + for (i = 0; i < 5000000; i++) { + gen = gen_rand(-1000, 1000); + if (gen < -1000 || gen > 1000) { + fprintf(stdout, "fail\n"); + return 1; + } + } + fprintf(stdout, "pass\n"); + + // Prepare for sort tests + for (i = 0; i < n; i++) { + test_case[i] = gen_rand(-20, 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 + fprintf(stdout, "\tbubble sort: "); + fflush(stdout); + bubble_sort(test_array, n); + for (i = 0; i < n; i++) { + if (test_array[i] != qarray[i]) { + fprintf(stdout, "fail\n"); + return 1; + } + } + fprintf(stdout, "pass\n"); + + return 0; +}