Browse Source

add tests

Chris Cromer 7 months ago
parent
commit
1097902899
Signed by: Chris Cromer <chris@cromer.cl> GPG Key ID: 39CC813FF3C8708A
3 changed files with 104 additions and 2 deletions
  1. 9
    2
      Makefile
  2. 15
    0
      test/Makefile
  3. 80
    0
      test/test.c

+ 9
- 2
Makefile View File

@@ -16,7 +16,11 @@ ifneq (, $(shell which pdflatex))
16 16
 	mv doc/Informe.pdf Informe.pdf
17 17
 endif
18 18
 
19
-clean: cleansort cleaninforme
19
+test:
20
+	make -C test
21
+	test/test
22
+
23
+clean: cleansort cleaninforme cleantest
20 24
 
21 25
 cleansort:
22 26
 	rm -f src/*.o sort
@@ -25,4 +29,7 @@ cleaninforme:
25 29
 	make -C doc clean
26 30
 	rm -f Informe.pdf
27 31
 
28
-.PHONY: all sort informe clean cleansort cleaninforme
32
+cleantest:
33
+	make -C test clean
34
+
35
+.PHONY: all sort informe test clean cleansort cleaninforme cleantest

+ 15
- 0
test/Makefile View File

@@ -0,0 +1,15 @@
1
+CC=gcc
2
+CFLAGS=-Wall -I../src/include -DDEBUG -g
3
+SRC=test.c
4
+OBJ=$(SRC:.c=.o)
5
+OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o
6
+
7
+all: test
8
+
9
+test: $(OBJ)
10
+	$(CC) -o $@ $^ $(LDFLAGS)
11
+
12
+clean:
13
+	rm -f *.o test
14
+
15
+.PHONY: all test clean

+ 80
- 0
test/test.c View File

@@ -0,0 +1,80 @@
1
+#include <stdio.h>
2
+#include <stdlib.h>
3
+#include <string.h>
4
+#include <unistd.h>
5
+#include "random.h"
6
+#include "bubble_sort.h"
7
+
8
+static int *test_case;
9
+static int *test_array;
10
+static int *qarray;
11
+
12
+/**
13
+ * Este función va a comparar valores para devolver los valores en orden ascendida
14
+ * @param a El primer valor a comparar
15
+ * @param b El segundo valor a comparar
16
+ * @return Retorna la comparación de los dos argumentos
17
+ */
18
+int compar(const void *a, const void *b) {
19
+	return (*(int*) a - *(int*) b);
20
+}
21
+
22
+/**
23
+ * Limpiar la memoria al salir
24
+ */
25
+void cleanup() {
26
+	free(test_case);
27
+	free(test_array);
28
+	free(qarray);
29
+}
30
+
31
+/**
32
+ * El programa de test
33
+ */
34
+int main(int argc, char **argv) {
35
+	int n = 50000;
36
+	int i;
37
+	int gen;
38
+
39
+	test_case = malloc(sizeof(int) * n);
40
+	qarray = malloc(sizeof(int) * n);
41
+	test_array = malloc(sizeof(int) * n);
42
+
43
+	atexit(cleanup);
44
+
45
+	fprintf(stdout, "Running tests:\n");
46
+
47
+	// Test random number generation
48
+	fprintf(stdout, "\trandom generation: ");
49
+	fflush(stdout);
50
+	for (i = 0; i < 5000000; i++) {
51
+		gen = gen_rand(-1000, 1000);
52
+		if (gen < -1000 || gen > 1000) {
53
+			fprintf(stdout, "fail\n");
54
+			return 1;
55
+		}
56
+	}
57
+	fprintf(stdout, "pass\n");
58
+
59
+	// Prepare for sort tests
60
+	for (i = 0; i < n; i++) {
61
+		test_case[i] = gen_rand(-20, 100);
62
+	}
63
+	memcpy(qarray, test_case, sizeof(int) * n);
64
+	memcpy(test_array, test_case, sizeof(int) * n);
65
+	qsort(qarray, n, sizeof(int), compar);
66
+
67
+	// Test bubble sort
68
+	fprintf(stdout, "\tbubble sort: ");
69
+	fflush(stdout);
70
+	bubble_sort(test_array, n);
71
+	for (i = 0; i < n; i++) {
72
+		if (test_array[i] != qarray[i]) {
73
+			fprintf(stdout, "fail\n");
74
+			return 1;
75
+		}
76
+	}
77
+	fprintf(stdout, "pass\n");
78
+
79
+	return 0;
80
+}

Loading…
Cancel
Save