points/test/test.c

115 lines
3.4 KiB
C
Raw Normal View History

2018-12-04 14:40:14 -03:00
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2018-12-08 19:35:02 -03:00
#include <math.h>
#include <float.h>
#include "points.h"
#include "read_file.h"
#include "brute_force.h"
2018-12-12 11:43:12 -03:00
#include "divide_and_conquer.h"
2018-12-08 19:35:02 -03:00
/**
* Comparar 2 double para ver si son casi igual
* @param d1 El primer double
* @param d2 El segudno double
* @return Retorna 1 si son igual o 0 sino
*/
2018-12-12 18:48:54 -03:00
inline int compare_double(double d1, double d2) {
2018-12-08 19:35:02 -03:00
double precision = 0.000000001;
if (((d1 - precision) < d2) && ((d1 + precision) > d2)) {
return 1;
}
else {
return 0;
}
}
2018-12-04 14:40:14 -03:00
/**
* El programa de test
2018-12-08 19:35:02 -03:00
* @param argc La cantidad de argumentos
* @param argv Los argumentos
* @return Retorna 0 por exito o 1 si falló algun test
2018-12-04 14:40:14 -03:00
*/
int main(int argc, char **argv) {
int passed = 0;
int failed = 0;
2018-12-08 19:35:02 -03:00
point_t *points = NULL;
unsigned int n = 0;
double dist = DBL_MAX;
fprintf(stdout, "Running tests:\n");
if (read_file("test/100.pto", &points, &n) == 0) {
fprintf(stdout, "\tread file: passed\n");
passed++;
}
else {
fprintf(stdout, "\tread file: failed\n");
failed++;
}
if (failed > 0) {
fprintf(stdout, "\tbrute force: failed\n");
failed++;
}
else {
fprintf(stdout, "\tbrute force: ");
fflush(stdout);
brute_force(points, n, &dist);
2018-12-12 18:48:54 -03:00
if (compare_double(sqrt(dist), 0.067687840)) {
2018-12-08 19:35:02 -03:00
fprintf(stdout, "passed\n");
passed++;
}
else {
fprintf(stdout, "failed\n");
failed++;
}
}
2018-12-04 14:40:14 -03:00
2018-12-12 11:43:12 -03:00
if (failed > 0) {
fprintf(stdout, "\tdivide and conquer: failed\n");
failed++;
}
else {
fprintf(stdout, "\tdivide and conquer: ");
fflush(stdout);
divide_and_conquer(points, n, &dist);
2018-12-12 18:48:54 -03:00
if (compare_double(sqrt(dist), 0.067687840)) {
2018-12-12 11:43:12 -03:00
fprintf(stdout, "passed\n");
passed++;
}
else {
fprintf(stdout, "failed\n");
failed++;
}
}
2018-12-04 14:40:14 -03:00
fprintf(stdout, "%d tests passed\n", passed);
fprintf(stdout, "%d tests failed\n", failed);
2018-12-08 19:35:02 -03:00
free(points);
2018-12-04 14:40:14 -03:00
if (failed == 0) {
return 0;
}
else {
return 1;
}
}