Browse Source

initial commit

Chris Cromer 5 months ago
commit
948c51ada1
14 changed files with 515 additions and 0 deletions
  1. 12
    0
      LICENSE
  2. 35
    0
      Makefile
  3. 23
    0
      README.md
  4. 125
    0
      doc/Informe.tex
  5. 9
    0
      doc/Makefile
  6. BIN
      doc/tar2-ada-2018-2.pdf
  7. BIN
      doc/ubblogo.png
  8. 19
    0
      src/include/random.h
  9. 22
    0
      src/include/timer.h
  10. 94
    0
      src/points.c
  11. 36
    0
      src/random.c
  12. 86
    0
      src/timer.c
  13. 16
    0
      test/Makefile
  14. 38
    0
      test/test.c

+ 12
- 0
LICENSE View File

@@ -0,0 +1,12 @@
1
+Copyright 2018 Chris Cromer
2
+Copyright 2018 Rodolfo Cuevas
3
+
4
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+
8
+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.
9
+
10
+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.
11
+
12
+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.

+ 35
- 0
Makefile View File

@@ -0,0 +1,35 @@
1
+CC=gcc
2
+CFLAGS=-Wall -Isrc/include -DDEBUG -g
3
+LDFLAGS=-lm
4
+SRC=src/points.c src/timer.c src/random.c
5
+OBJ=$(SRC:.c=.o)
6
+
7
+all: points informe
8
+
9
+points: $(OBJ)
10
+	$(CC) -o $@ $^ $(LDFLAGS)
11
+
12
+informe:
13
+# if pdflatex is installed create the informe
14
+ifneq (, $(shell which pdflatex))
15
+	make -C doc
16
+	mv doc/Informe.pdf Informe.pdf
17
+endif
18
+
19
+test:
20
+	make -C test
21
+	test/test
22
+
23
+clean: cleanpoints cleaninforme cleantest
24
+
25
+cleanpoints:
26
+	rm -f src/*.o points
27
+
28
+cleaninforme:
29
+	make -C doc clean
30
+	rm -f Informe.pdf
31
+
32
+cleantest:
33
+	make -C test clean
34
+
35
+.PHONY: all points informe test clean cleanpoints cleaninforme cleantest

+ 23
- 0
README.md View File

@@ -0,0 +1,23 @@
1
+# Points
2
+Una programa que deja encontar un par de puntos más cercano.
3
+[points](https://git.cromer.cl/UBB/points)
4
+
5
+## Autores
6
+Christopher Cromer
7
+
8
+Rodolfo Cuevas
9
+
10
+## Compilar
11
+Hay tres blancos disponible para usar con GNU Make:
12
+
13
+	make points
14
+Compilar el programa
15
+
16
+	make test
17
+Comprobar que los algoritmos y programa corren como esperado
18
+
19
+	make informe
20
+Compilar el informe si pdflatex está disponible en el sistema
21
+
22
+## Correr el programa
23
+	./points

+ 125
- 0
doc/Informe.tex View File

@@ -0,0 +1,125 @@
1
+\author{}
2
+\date{\today}
3
+
4
+\documentclass[12pt]{article}
5
+
6
+\usepackage[utf8]{inputenc} %\ This allows spanish tildes
7
+\usepackage[spanish]{babel}
8
+\usepackage{array}
9
+\usepackage{adjustbox}
10
+\usepackage{titling}
11
+\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm,a4paper]{geometry}
12
+\usepackage{amsmath}
13
+
14
+\title{Tarea 2: Encontrar el par de puntos más cercano}
15
+
16
+\usepackage{listings}
17
+\lstset{
18
+	basicstyle=\small\ttfamily,
19
+	columns=flexible,
20
+	breaklines=true,
21
+	inputencoding=utf8,
22
+	extendedchars=true,
23
+	literate={á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1 {ñ}{{\~n}}1 {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1 {Ñ}{{\~N}}1
24
+}
25
+
26
+\usepackage{hyperref}
27
+\hypersetup{colorlinks=true,allcolors=black,pdftitle={Tarea 1: Ordenamiento}}
28
+\usepackage{hypcap}
29
+
30
+\pretitle{%
31
+	\begin{center}
32
+		\LARGE
33
+		\includegraphics[width=4cm]{ubblogo.png}\\[\bigskipamount]
34
+		\Large
35
+		\textbf{Análisis y Diseño de Algoritmos}\\[\smallskipamount]
36
+	}
37
+
38
+\posttitle{\end{center}}
39
+
40
+\begin{document}
41
+\hypersetup{pageanchor=false}
42
+\clearpage\maketitle
43
+\thispagestyle{empty}
44
+
45
+\begin{flushright}
46
+\textbf{Integrantes:}\\
47
+Christopher Cromer\\
48
+Rodolfo Cuevas
49
+\end{flushright}
50
+
51
+\begin{flushright}
52
+\textbf{Profesor:}\\
53
+Pedro Rodríguez
54
+\end{flushright}
55
+
56
+\begin{flushright}
57
+\textbf{Ayudantes:}\\
58
+Carlos Faúndez\\
59
+Xavier Canales
60
+\end{flushright}
61
+
62
+\newpage
63
+
64
+\clearpage
65
+\thispagestyle{empty}
66
+\tableofcontents
67
+
68
+\newpage
69
+\hypersetup{pageanchor=true}
70
+\pagenumbering{arabic}
71
+\section{Pseudo código}
72
+\subsection{Brute Force}
73
+
74
+\newpage
75
+\subsection{Divide and Conquer}
76
+
77
+\newpage
78
+\section{Resultados}
79
+
80
+\subsection{Análisis temporal}
81
+\subsubsection{Brute Force}
82
+\underline{Caso Promedio:} $ \Theta(n log n)) $ \\
83
+ \bigskip
84
+
85
+\underline{Peor Caso:} $ O(n log n) $ \\
86
+ \bigskip
87
+
88
+\underline{Mejor Caso:} $ \Omega(n log n)$ \\
89
+ \bigskip
90
+
91
+\subsubsection{Divide and Conquer}
92
+\underline{Caso Promedio:} $ \Theta(n log n)) $ \\
93
+\bigskip
94
+
95
+\underline{Peor Caso:} $ O(n log n) $ \\
96
+\bigskip
97
+
98
+\underline{Mejor Caso:} $ \Omega(n log n)$ \\
99
+\bigskip
100
+
101
+\newpage
102
+\subsection{Datos}
103
+La siguiente tabla contiene los resultados de las pruebas de los 2 algoritmos medidos en segundos. Para las pruebas usábamos un computador que tiene 4 núcleos de 3.2GHz y 16GB de memoria RAM.
104
+\begin{center}
105
+	\begin{tabular}{|c|c|c|}
106
+		\hline
107
+		\multicolumn{3}{|c|}{Algoritmos para encontrar los 2 puntos más cercano} \\
108
+		\hline
109
+		\rule[-1ex]{0pt}{3.5ex} & Brute Force  & Divide and Conquer \\
110
+		\hline
111
+		\rule[-1ex]{0pt}{3.5ex} 1.000.000 & 0[s] & 0[s] \\
112
+		\hline
113
+		\rule[-1ex]{0pt}{3.5ex} 5.000.000 & 0[s] & 0[s] \\
114
+		\hline
115
+	\end{tabular}
116
+\end{center}
117
+
118
+\newpage
119
+\subsection{Gráfico}
120
+
121
+\newpage
122
+\section{Conclusiones}
123
+
124
+\end{document}
125
+

+ 9
- 0
doc/Makefile View File

@@ -0,0 +1,9 @@
1
+informe:
2
+	# we do this twice to make sure the toc is updated
3
+	pdflatex -synctex=1 -interaction=nonstopmode "Informe.tex"
4
+	pdflatex -synctex=1 -interaction=nonstopmode "Informe.tex"
5
+
6
+clean:
7
+	rm -f *.log *.toc *.gz *.aux *.out Informe.pdf
8
+
9
+.PHONY: informe clean

BIN
doc/tar2-ada-2018-2.pdf View File


BIN
doc/ubblogo.png View File


+ 19
- 0
src/include/random.h View File

@@ -0,0 +1,19 @@
1
+/*
2
+ * Copyright 2018 Christopher Cromer
3
+ * Copyright 2018 Rodolfo Cuevas
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+ *
9
+ * 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.
10
+ *
11
+ * 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.
12
+ *
13
+ * 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.
14
+ */
15
+
16
+#ifndef _POINTS_RANDOM
17
+	#define _POINTS_RANDOM
18
+	int gen_rand(int min, int max);
19
+#endif

+ 22
- 0
src/include/timer.h View File

@@ -0,0 +1,22 @@
1
+/*
2
+ * Copyright 2018 Christopher Cromer
3
+ * Copyright 2018 Rodolfo Cuevas
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+ *
9
+ * 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.
10
+ *
11
+ * 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.
12
+ *
13
+ * 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.
14
+ */
15
+
16
+#ifndef _POINTS_TIMER
17
+	#define _POINTS_TIMER
18
+	void start_timer();
19
+	void stop_timer();
20
+	void print_timer();
21
+	long int get_timer();
22
+#endif

+ 94
- 0
src/points.c View File

@@ -0,0 +1,94 @@
1
+/*
2
+ * Copyright 2018 Christopher Cromer
3
+ * Copyright 2018 Rodolfo Cuevas
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+ *
9
+ * 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.
10
+ *
11
+ * 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.
12
+ *
13
+ * 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.
14
+ */
15
+
16
+#include <stdio.h>
17
+#include <stdlib.h>
18
+#include <getopt.h>
19
+#include <string.h>
20
+#include <errno.h>
21
+#include <limits.h>
22
+#include "timer.h"
23
+
24
+#define POINTS_VERSION "1.0.0"
25
+
26
+/**
27
+ * Imprimir el uso del programa
28
+ */
29
+void print_usage() {
30
+	fprintf(stdout, "uso: points [OPCIÓN]\n");
31
+	fprintf(stdout, "  -a, --all                  usar todos los algoritmos de ordenamentio\n");
32
+	fprintf(stdout, "  -b, --brute                usar brute force\n");
33
+	fprintf(stdout, "  -d, --divide               usar divide and conquer\n");
34
+	fprintf(stdout, "  -h, --help                 mostrar como usar el programa\n");
35
+	fprintf(stdout, "  -v, --version              mostrar la versión del programa\n");
36
+}
37
+
38
+/**
39
+ * La entrada del programa
40
+ * @param argc La cantidad de argumentos pasado al programa
41
+ * @return Retorna el codigo de error o 0 por exito
42
+ */
43
+int main (int argc, char **argv) {
44
+	int brute = 0;
45
+	int divide = 0;
46
+	int opt;
47
+	int long_index = 0;
48
+	static struct option long_options[] = {
49
+		{"all",			no_argument,		0,	'a'},
50
+		{"brute",		no_argument,		0,	'b'},
51
+		{"divide",		no_argument,		0,	'd'},
52
+		{"help",		no_argument,		0,  'h'},
53
+		{"version",		no_argument,		0,	'v'},
54
+		{0, 0, 0, 0}
55
+	};
56
+
57
+	if (argc == 1) {
58
+		print_usage();
59
+		return 0;
60
+	}
61
+
62
+	while ((opt = getopt_long(argc, argv, "abdhv", long_options, &long_index)) != -1) {
63
+		switch (opt) {
64
+			case 'a':
65
+				brute = 1;
66
+				divide = 1;
67
+				break;
68
+			case 'b':
69
+				brute = 1;
70
+				break;
71
+			case 'd':
72
+				divide = 1;
73
+				break;
74
+			case 'h':
75
+				print_usage();
76
+				return 0;
77
+			case 'v':
78
+				printf("points versión: %s\n", POINTS_VERSION);
79
+				return 0;
80
+				break;
81
+			default:
82
+				print_usage();
83
+				return 1;
84
+		}
85
+	}
86
+
87
+	if (!brute && !divide) {
88
+		fprintf(stderr, "Error: No se seleccionó un algoritmo valido!\n");
89
+		print_usage();
90
+		return 4;
91
+	}
92
+
93
+	return 0;
94
+}

+ 36
- 0
src/random.c View File

@@ -0,0 +1,36 @@
1
+/*
2
+ * Copyright 2018 Christopher Cromer
3
+ * Copyright 2018 Rodolfo Cuevas
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+ *
9
+ * 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.
10
+ *
11
+ * 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.
12
+ *
13
+ * 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.
14
+ */
15
+
16
+#include <stdlib.h>
17
+#include <time.h>
18
+
19
+/**
20
+ * Flag para inicilizar srand solo una vez
21
+ */
22
+static int sort_rand_initialized = 0;
23
+
24
+/**
25
+ * Generar un númer al azar entre un mínimo y máximo
26
+ * @param min El limite míninmo
27
+ * @param max El limite máximo
28
+ * @return Retorna el número generado
29
+ */
30
+int gen_rand(int min, int max) {
31
+	if (sort_rand_initialized == 0) {
32
+		srand((unsigned int) time(NULL));
33
+		sort_rand_initialized = 1;
34
+	}
35
+	return rand() % (max + 1 - min) + min;
36
+}

+ 86
- 0
src/timer.c View File

@@ -0,0 +1,86 @@
1
+/*
2
+ * Copyright 2018 Christopher Cromer
3
+ * Copyright 2018 Rodolfo Cuevas
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+ *
9
+ * 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.
10
+ *
11
+ * 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.
12
+ *
13
+ * 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.
14
+ */
15
+
16
+#include <stdio.h>
17
+#include <time.h>
18
+#include <math.h>
19
+
20
+/**
21
+ * Cuando empezó el timer
22
+ */
23
+static struct timespec start_time;
24
+/**
25
+ * Cuando terminó el timer
26
+ */
27
+static struct timespec stop_time;
28
+
29
+/**
30
+ * Empezar el timer
31
+ */
32
+void start_timer() {
33
+	clock_gettime(CLOCK_MONOTONIC, &start_time);
34
+	stop_time.tv_nsec = 0;
35
+}
36
+
37
+/**
38
+ * Terminar el timer
39
+ */
40
+void stop_timer() {
41
+	if (start_time.tv_nsec == 0) {
42
+		fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n");
43
+	}
44
+	else {
45
+		clock_gettime(CLOCK_MONOTONIC, &stop_time);
46
+	}
47
+}
48
+
49
+/**
50
+ * Imprimir el tiempo de ejecución
51
+ */
52
+void print_timer() {
53
+	if (start_time.tv_nsec == 0) {
54
+		fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n");
55
+	}
56
+	else if (stop_time.tv_nsec == 0) {
57
+		fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n");
58
+	}
59
+	else {
60
+		time_t seconds  = stop_time.tv_sec - start_time.tv_sec;
61
+		unsigned long long milliseconds = round((stop_time.tv_nsec - start_time.tv_nsec)/ 1.0e6); // Convert nanoseconds to milliseconds
62
+		if (milliseconds > 999) {
63
+			seconds++;
64
+			milliseconds = 0;
65
+		}
66
+		unsigned int minutes = 0;
67
+		unsigned int hours = 0;
68
+		if (seconds >= 3600) {
69
+			hours = seconds / 3600;
70
+			seconds = seconds - (hours * 3600);
71
+			if (seconds >= 60) {
72
+				minutes = seconds / 60;
73
+				seconds = seconds - (minutes * 60);
74
+			}
75
+			fprintf(stdout, "Tiempo de ejecución: %d horas, %d minutos y %ld.%lld segundos\n", hours, minutes, seconds, milliseconds);
76
+		}
77
+		else if (seconds >= 60) {
78
+			minutes = seconds / 60;
79
+			seconds = seconds - (minutes * 60);
80
+			fprintf(stdout, "Tiempo de ejecución: %d minutos y %ld.%lld segundos\n", minutes, seconds, milliseconds);
81
+		}
82
+		else {
83
+			fprintf(stdout, "Tiempo de ejecución: %ld.%lld segundos\n", seconds, milliseconds);
84
+		}
85
+	}
86
+}

+ 16
- 0
test/Makefile View File

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

+ 38
- 0
test/test.c View File

@@ -0,0 +1,38 @@
1
+/*
2
+ * Copyright 2018 Christopher Cromer
3
+ * Copyright 2018 Rodolfo Cuevas
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+ *
7
+ * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
+ *
9
+ * 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.
10
+ *
11
+ * 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.
12
+ *
13
+ * 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.
14
+ */
15
+
16
+#include <stdio.h>
17
+#include <stdlib.h>
18
+#include <string.h>
19
+#include <unistd.h>
20
+
21
+/**
22
+ * El programa de test
23
+ */
24
+int main(int argc, char **argv) {
25
+	int pass;
26
+	int passed = 0;
27
+	int failed = 0;
28
+
29
+	fprintf(stdout, "%d tests passed\n", passed);
30
+	fprintf(stdout, "%d tests failed\n", failed);
31
+
32
+	if (failed == 0) {
33
+		return 0;
34
+	}
35
+	else {
36
+		return 1;
37
+	}
38
+}

Loading…
Cancel
Save