From 385f97c07c29fdccf3cc0e65faf684b03a93d2ea Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 13:44:24 -0300 Subject: [PATCH 01/16] change flags --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 31c3f17..ba049cd 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ CC=gcc -CFLAGS=-Wall -Werror -CPPFLAGS+=-Isrc/include +CFLAGS=-Wall -Isrc/include -DDEBUG -g #LDFLAGS=-lm SRC=src/sort.c src/random.c OBJ=$(SRC:.c=.o) -- 2.30.2 From 2419f57c8c4e5cfe6aa9e1ae020ab82c4bd28a7d Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 16:30:19 -0300 Subject: [PATCH 02/16] ignore .out and core dumps --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index b80ba84..12fbe88 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ Informe.pdf doc/*.aux +doc/*.out doc/*.log doc/*.synctex.gz doc/*.toc *.o sort +core -- 2.30.2 From 9a293471981e59affc34e3b0acd87d56511ddf8a Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 16:49:23 -0300 Subject: [PATCH 03/16] add bubble sort documentation --- doc/Informe.tex | 12 ++++++++++++ doc/psuedo/bubblesort.txt | 9 +++++++++ 2 files changed, 21 insertions(+) create mode 100644 doc/psuedo/bubblesort.txt diff --git a/doc/Informe.tex b/doc/Informe.tex index 6521b74..fb352e7 100644 --- a/doc/Informe.tex +++ b/doc/Informe.tex @@ -12,6 +12,17 @@ \usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm,a4paper]{geometry} \usepackage{amsmath} +\usepackage{listings} +\lstset{ + basicstyle=\footnotesize\ttfamily, + columns=flexible, + breaklines=true +} + +\usepackage{hyperref} +\hypersetup{pdftex,colorlinks=true,allcolors=black,bookmarks} +\usepackage{hypcap} + \pretitle{% \begin{center} \LARGE @@ -59,6 +70,7 @@ Xavier Canales \subsection{Quick Sort} \subsection{Bubble Sort} +\lstinputlisting{psuedo/bubblesort.txt} \subsection{Bitonic Sort} diff --git a/doc/psuedo/bubblesort.txt b/doc/psuedo/bubblesort.txt new file mode 100644 index 0000000..c3cc70c --- /dev/null +++ b/doc/psuedo/bubblesort.txt @@ -0,0 +1,9 @@ +cuentaDeElementos := n +repetir + haCambiado := falso + disminuir cuentaDeElementos + repetir con indice desde 1 a cuentaDeElementos + if (elemento en indice) > (elemento en (indice + 1)) + intercambiar (elemento en indice) con (elemento en (indice + 1)) + haCambiado := falso +hasta haCambiado = verdad -- 2.30.2 From 65be25f92bf7c45860093b06bf2abe3ef3d508d2 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 16:50:31 -0300 Subject: [PATCH 04/16] add bubble sort --- src/bubble_sort.c | 33 +++++++++++++++++++++++++++++++++ src/include/bubble_sort.h | 19 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/bubble_sort.c create mode 100644 src/include/bubble_sort.h diff --git a/src/bubble_sort.c b/src/bubble_sort.c new file mode 100644 index 0000000..5b10ec0 --- /dev/null +++ b/src/bubble_sort.c @@ -0,0 +1,33 @@ +/* + * Copyright 2018 Christopher Cromer + * Copyright 2018 Rodolfo Cuevas + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +void bubble_sort(int *array, int n) { + int i; + int j = n; + int temp; + int flag = 1; + while (flag) { + flag = 0; + for (i = 1; i < j; i++) { + if (array[i] < array[i - 1]) { + temp = array[i]; + array[i] = array[i - 1]; + array[i - 1] = temp; + flag = 1; + } + } + j--; + } +} diff --git a/src/include/bubble_sort.h b/src/include/bubble_sort.h new file mode 100644 index 0000000..fc31615 --- /dev/null +++ b/src/include/bubble_sort.h @@ -0,0 +1,19 @@ +/* + * Copyright 2018 Christopher Cromer + * Copyright 2018 Rodolfo Cuevas + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SORT_BUBBLE + #define _SORT_BUBBLE + void bubble_sort(int *array, int n); +#endif -- 2.30.2 From e7655e3f5d1fd53977323ca42cb62cbfbc8e342b Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 16:51:01 -0300 Subject: [PATCH 05/16] add timer functionality --- src/include/timer.h | 21 +++++++++++++++++++++ src/timer.c | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/include/timer.h create mode 100644 src/timer.c diff --git a/src/include/timer.h b/src/include/timer.h new file mode 100644 index 0000000..8a4f740 --- /dev/null +++ b/src/include/timer.h @@ -0,0 +1,21 @@ +/* + * Copyright 2018 Christopher Cromer + * Copyright 2018 Rodolfo Cuevas + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SORT_TIMER + #define _SORT_TIMER + void start_timer(); + void stop_timer(); + void print_timer(); +#endif diff --git a/src/timer.c b/src/timer.c new file mode 100644 index 0000000..662ea0d --- /dev/null +++ b/src/timer.c @@ -0,0 +1,46 @@ +/* + * Copyright 2018 Christopher Cromer + * Copyright 2018 Rodolfo Cuevas + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +/** + * El tiempo que demoró en ejecutar algo + */ +static time_t start_time = 0; +static time_t stop_time = 0; + +void start_timer() { + if (start_time != 0) { + fprintf(stderr, "Error: El temporizador ya comenzó!\n"); + } + else { + time(&start_time); + stop_time = 0; + } +} + +void stop_timer() { + if (start_time == 0) { + fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n"); + } + else { + time(&stop_time); + } +} + +void print_timer() { + fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", (long int) (stop_time - start_time)); +} -- 2.30.2 From 5b77033905912f029c6a5a976c0e393734491f49 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 16:51:49 -0300 Subject: [PATCH 06/16] add bubble sort call and time its execution --- src/sort.c | 78 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/src/sort.c b/src/sort.c index d779225..8e17ddc 100644 --- a/src/sort.c +++ b/src/sort.c @@ -17,6 +17,8 @@ #include #include #include "random.h" +#include "timer.h" +#include "bubble_sort.h" #define SORT_VERSION "1.0.0" @@ -36,6 +38,7 @@ void print_usage() { fprintf(stdout, " -e, --elegir el usuario debe elegir los \"n\" valores de\n"); fprintf(stdout, " elementos a ordenar, sin esta opción los\n"); fprintf(stdout, " valores son elegido por el programa al azar\n"); + fprintf(stdout, " -i, --imprimir imprimir el array antes y despues de ordenar\n"); fprintf(stdout, " -v, --version mostrar la versión del programa\n"); } @@ -48,15 +51,24 @@ void extra_algorithm() { exit(2); } +void print_array(int *array, int n) { + int i; + for (i = 0; i < n; i++) { + fprintf(stdout, "%d ", array[i]); + } + fprintf(stdout, "\n"); +} + /** * La entrada del programa * @param argc La cantidad de argumentos pasado al programa * @return Retorna el codigo de error o 0 por exito */ int main (int argc, char **argv) { - int i; + long long i; int n = 10; int elegir = 0; + int imprimir = 0; int algoritmo = -1; int opt; int long_index = 0; @@ -69,6 +81,7 @@ int main (int argc, char **argv) { {"selection", no_argument, 0, 's'}, {"n", required_argument, 0, 'n'}, {"elegir", no_argument, 0, 'e'}, + {"imprimir", no_argument, 0, 'i'}, {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; @@ -117,7 +130,7 @@ int main (int argc, char **argv) { algoritmo = 5; break; case 'n': - n = atoi(optarg); + n = atol(optarg); if (n <= 1) { fprintf(stderr, "Error: n tiene que ser mayor de 1!\n"); return 3; @@ -126,6 +139,9 @@ int main (int argc, char **argv) { case 'e': elegir = 1; break; + case 'i': + imprimir = 1; + break; case 'v': printf("sort versión: %s\n", SORT_VERSION); return 0; @@ -136,23 +152,14 @@ int main (int argc, char **argv) { } } - if (algoritmo == -1) { - fprintf(stderr, "Error: No se seleccionó un algoritmo!\n"); - print_usage(); - return 4; - } - - int array[n]; + int *array = malloc(sizeof(int) * n); // Llenar el array con valores para ordenar después for (i = 0; i < n; i++) { - if (elegir == 0) { - array[i] = gen_rand(0, 100000); - } - else { + if (elegir) { opt = 0; while (opt <= 1) { - fprintf(stdout, "Elegir elemento %d: ", i + 1); + fprintf(stdout, "Elegir elemento %lli: ", i + 1); fscanf(stdin, "%d", &opt); if (opt <= 1) { fprintf(stdout, "Número invalido! Tiene que ser mayor de 1!\n"); @@ -160,6 +167,49 @@ int main (int argc, char **argv) { } array[i] = opt; } + else { + array[i] = gen_rand(-1000, 5000); + } } + + if (imprimir) { + fprintf(stdout, "\nAntes:\n"); + print_array(array, n); + } + + if (algoritmo == 0) { + // merge sort + } + else if (algoritmo == 1) { + // quick sort + } + else if (algoritmo == 2) { + start_timer(); + bubble_sort(array, n); + stop_timer(); + } + else if (algoritmo == 3) { + // bitonic sort + } + else if (algoritmo == 4) { + // count sort + } + else if (algoritmo == 5) { + // selection sort + } + else { + fprintf(stderr, "Error: No se seleccionó un algoritmo valido!\n"); + print_usage(); + return 4; + } + + if (imprimir) { + fprintf(stdout, "\nDespués:\n"); + print_array(array, n); + } + + free(array); + + print_timer(); return 0; } -- 2.30.2 From 498cd1d143599e67144c4b1c9a1fce2dfbd15477 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 16:52:27 -0300 Subject: [PATCH 07/16] add bubble sort and timer to makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ba049cd..3ada494 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=gcc CFLAGS=-Wall -Isrc/include -DDEBUG -g #LDFLAGS=-lm -SRC=src/sort.c src/random.c +SRC=src/sort.c src/random.c src/bubble_sort.c src/timer.c OBJ=$(SRC:.c=.o) all: sort informe -- 2.30.2 From 7c196f27a8a1e4444d207e5b7cdaa5371b4a98f4 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 16:54:12 -0300 Subject: [PATCH 08/16] delete .out file on clean --- doc/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/Makefile b/doc/Makefile index 788bb80..a0a7aae 100644 --- a/doc/Makefile +++ b/doc/Makefile @@ -4,6 +4,6 @@ informe: pdflatex -synctex=1 -interaction=nonstopmode "Informe.tex" clean: - rm -f *.log *.toc *.gz *.aux Informe.pdf + rm -f *.log *.toc *.gz *.aux *.out Informe.pdf .PHONY: informe clean -- 2.30.2 From 8d4e695bec948183c6698bb283c8a41077f92938 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 16:57:16 -0300 Subject: [PATCH 09/16] make font bigger --- doc/Informe.tex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/Informe.tex b/doc/Informe.tex index fb352e7..910bc03 100644 --- a/doc/Informe.tex +++ b/doc/Informe.tex @@ -14,7 +14,7 @@ \usepackage{listings} \lstset{ - basicstyle=\footnotesize\ttfamily, + basicstyle=\small\ttfamily, columns=flexible, breaklines=true } -- 2.30.2 From 0d2d59e84b7073a80582aab5fb7e59e2b19f57e4 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 18:07:23 -0300 Subject: [PATCH 10/16] add cleanup --- src/sort.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/sort.c b/src/sort.c index 8e17ddc..5e98ec5 100644 --- a/src/sort.c +++ b/src/sort.c @@ -22,6 +22,8 @@ #define SORT_VERSION "1.0.0" +static int *array; + /** * Imprimir el uso del programa */ @@ -59,6 +61,10 @@ void print_array(int *array, int n) { fprintf(stdout, "\n"); } +void cleanup() { + free(array); +} + /** * La entrada del programa * @param argc La cantidad de argumentos pasado al programa @@ -152,7 +158,8 @@ int main (int argc, char **argv) { } } - int *array = malloc(sizeof(int) * n); + array = malloc(sizeof(int) * n); + atexit(cleanup); // Llenar el array con valores para ordenar después for (i = 0; i < n; i++) { @@ -184,6 +191,7 @@ int main (int argc, char **argv) { // quick sort } else if (algoritmo == 2) { + fprintf(stdout, "Bubble sort corriendo...\n"); start_timer(); bubble_sort(array, n); stop_timer(); @@ -208,8 +216,6 @@ int main (int argc, char **argv) { print_array(array, n); } - free(array); - print_timer(); return 0; } -- 2.30.2 From 6ab7930b24c4abd1da6611a2000b2fcc59037bc2 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 18:09:18 -0300 Subject: [PATCH 11/16] flush stdout --- src/sort.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/sort.c b/src/sort.c index 5e98ec5..528bf8c 100644 --- a/src/sort.c +++ b/src/sort.c @@ -192,6 +192,7 @@ int main (int argc, char **argv) { } else if (algoritmo == 2) { fprintf(stdout, "Bubble sort corriendo...\n"); + fflush(stdout); start_timer(); bubble_sort(array, n); stop_timer(); -- 2.30.2 From 1097902899f8e59d68a2c0d4b75bc80647bdbefb Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 18:20:01 -0300 Subject: [PATCH 12/16] add tests --- Makefile | 11 +++++-- test/Makefile | 15 ++++++++++ test/test.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 test/Makefile create mode 100644 test/test.c 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; +} -- 2.30.2 From 9c26ab2fb7e558089924b54af1f84b16e8374a9e Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 18:43:36 -0300 Subject: [PATCH 13/16] add test to git ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 12fbe88..18c1846 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ doc/*.out doc/*.log doc/*.synctex.gz doc/*.toc +test/test *.o sort core -- 2.30.2 From 02a0926252a428dade3bcef56ddc96e5a5e1cff0 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 18:45:18 -0300 Subject: [PATCH 14/16] move check higher to improve performance --- src/sort.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/sort.c b/src/sort.c index 528bf8c..b22c41b 100644 --- a/src/sort.c +++ b/src/sort.c @@ -53,6 +53,11 @@ void extra_algorithm() { exit(2); } +/** + * Imprimir un array + * @param *array El array a imprimir + * @param n La cantidad de elementos que están en el array + */ void print_array(int *array, int n) { int i; for (i = 0; i < n; i++) { @@ -61,6 +66,9 @@ void print_array(int *array, int n) { fprintf(stdout, "\n"); } +/** + * Liberar la memoria al salir + */ void cleanup() { free(array); } @@ -158,6 +166,12 @@ int main (int argc, char **argv) { } } + if (algoritmo < 0 || algoritmo > 5) { + fprintf(stderr, "Error: No se seleccionó un algoritmo valido!\n"); + print_usage(); + return 4; + } + array = malloc(sizeof(int) * n); atexit(cleanup); @@ -206,11 +220,6 @@ int main (int argc, char **argv) { else if (algoritmo == 5) { // selection sort } - else { - fprintf(stderr, "Error: No se seleccionó un algoritmo valido!\n"); - print_usage(); - return 4; - } if (imprimir) { fprintf(stdout, "\nDespués:\n"); -- 2.30.2 From 16cf47155e6773f44e98d3261b3a7d2bc493f84a Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 18:45:53 -0300 Subject: [PATCH 15/16] add count of how many tests pass and fail --- test/test.c | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/test/test.c b/test/test.c index 4bbae28..05e246f 100644 --- a/test/test.c +++ b/test/test.c @@ -1,3 +1,18 @@ +/* + * Copyright 2018 Christopher Cromer + * Copyright 2018 Rodolfo Cuevas + * + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + #include #include #include @@ -35,6 +50,9 @@ int main(int argc, char **argv) { int n = 50000; int i; int gen; + int pass = 1; + int passed = 0; + int failed = 0; test_case = malloc(sizeof(int) * n); qarray = malloc(sizeof(int) * n); @@ -49,12 +67,17 @@ int main(int argc, char **argv) { fflush(stdout); for (i = 0; i < 5000000; i++) { gen = gen_rand(-1000, 1000); - if (gen < -1000 || gen > 1000) { + if ((gen < -1000 || gen > 1000) && pass) { fprintf(stdout, "fail\n"); - return 1; + failed++; + pass = 0; } } - fprintf(stdout, "pass\n"); + if (pass) { + fprintf(stdout, "pass\n"); + passed++; + } + pass = 1; // Prepare for sort tests for (i = 0; i < n; i++) { @@ -69,12 +92,25 @@ int main(int argc, char **argv) { fflush(stdout); bubble_sort(test_array, n); for (i = 0; i < n; i++) { - if (test_array[i] != qarray[i]) { + if (test_array[i] != qarray[i] && pass) { fprintf(stdout, "fail\n"); - return 1; + failed++; + pass = 0; } } - fprintf(stdout, "pass\n"); + if (pass) { + fprintf(stdout, "pass\n"); + passed++; + } + pass = 1; - return 0; + fprintf(stdout, "%d tests passed\n", passed); + fprintf(stdout, "%d tests failed\n", failed); + + if (failed == 0) { + return 0; + } + else { + return 1; + } } -- 2.30.2 From 051f6b75e007eb99edf55bf1a9d94ba085d2a085 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sun, 11 Nov 2018 19:14:06 -0300 Subject: [PATCH 16/16] add get_timer function and comments --- src/include/timer.h | 1 + src/timer.c | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/include/timer.h b/src/include/timer.h index 8a4f740..c899263 100644 --- a/src/include/timer.h +++ b/src/include/timer.h @@ -18,4 +18,5 @@ void start_timer(); void stop_timer(); void print_timer(); + long int get_timer(); #endif diff --git a/src/timer.c b/src/timer.c index 662ea0d..014633c 100644 --- a/src/timer.c +++ b/src/timer.c @@ -15,13 +15,20 @@ #include #include +#include "timer.h" /** - * El tiempo que demoró en ejecutar algo + * Cuando empezó el timer */ static time_t start_time = 0; +/** + * Cuando terminó el timer + */ static time_t stop_time = 0; +/** + * Empezar el timer + */ void start_timer() { if (start_time != 0) { fprintf(stderr, "Error: El temporizador ya comenzó!\n"); @@ -32,6 +39,9 @@ void start_timer() { } } +/** + * Terminar el timer + */ void stop_timer() { if (start_time == 0) { fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n"); @@ -41,6 +51,17 @@ void stop_timer() { } } +/** + * Imprimir el tiempo de ejecución + */ void print_timer() { fprintf(stdout, "Tiempo de ejecución: %ld segundos\n", (long int) (stop_time - start_time)); } + +/** + * Devolver el tiempo que demoró en ejecutarse + * @return El tiempo que demoró + */ +long int get_timer() { + return (long int) (stop_time - start_time); +} -- 2.30.2