2018-11-11 18:45:53 -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.
|
|
|
|
*/
|
|
|
|
|
2018-11-11 18:20:01 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "random.h"
|
|
|
|
#include "bubble_sort.h"
|
2018-11-13 09:15:33 -03:00
|
|
|
#include "count_sort.h"
|
2018-11-13 11:59:21 -03:00
|
|
|
#include "quick_sort.h"
|
2018-11-14 17:53:45 -03:00
|
|
|
#include "merge_sort.h"
|
2018-11-11 18:20:01 -03:00
|
|
|
|
|
|
|
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;
|
2018-11-13 09:15:33 -03:00
|
|
|
int pass;
|
2018-11-11 18:45:53 -03:00
|
|
|
int passed = 0;
|
|
|
|
int failed = 0;
|
2018-11-11 18:20:01 -03:00
|
|
|
|
|
|
|
test_case = malloc(sizeof(int) * n);
|
2018-11-14 16:21:26 -03:00
|
|
|
if (test_case == NULL) {
|
2018-11-14 20:21:15 -03:00
|
|
|
fprintf(stderr, "Error: Out of heap space!\n");
|
2018-11-14 16:21:26 -03:00
|
|
|
exit(1);
|
|
|
|
}
|
2018-11-11 18:20:01 -03:00
|
|
|
qarray = malloc(sizeof(int) * n);
|
2018-11-14 16:21:26 -03:00
|
|
|
if (qarray == NULL) {
|
2018-11-14 20:21:15 -03:00
|
|
|
fprintf(stderr, "Error: Out of heap space!\n");
|
2018-11-14 16:21:26 -03:00
|
|
|
exit(1);
|
|
|
|
}
|
2018-11-11 18:20:01 -03:00
|
|
|
test_array = malloc(sizeof(int) * n);
|
2018-11-14 16:21:26 -03:00
|
|
|
if (test_array == NULL) {
|
2018-11-14 20:21:15 -03:00
|
|
|
fprintf(stderr, "Error: Out of heap space!\n");
|
2018-11-14 16:21:26 -03:00
|
|
|
exit(1);
|
|
|
|
}
|
2018-11-11 18:20:01 -03:00
|
|
|
|
|
|
|
atexit(cleanup);
|
|
|
|
|
|
|
|
fprintf(stdout, "Running tests:\n");
|
|
|
|
|
|
|
|
// Test random number generation
|
2018-11-13 09:15:33 -03:00
|
|
|
pass = 1;
|
2018-11-11 18:20:01 -03:00
|
|
|
fprintf(stdout, "\trandom generation: ");
|
|
|
|
fflush(stdout);
|
|
|
|
for (i = 0; i < 5000000; i++) {
|
|
|
|
gen = gen_rand(-1000, 1000);
|
2018-11-11 18:45:53 -03:00
|
|
|
if ((gen < -1000 || gen > 1000) && pass) {
|
2018-11-11 18:20:01 -03:00
|
|
|
fprintf(stdout, "fail\n");
|
2018-11-11 18:45:53 -03:00
|
|
|
failed++;
|
|
|
|
pass = 0;
|
2018-11-12 14:19:47 -03:00
|
|
|
break;
|
2018-11-11 18:20:01 -03:00
|
|
|
}
|
|
|
|
}
|
2018-11-11 18:45:53 -03:00
|
|
|
if (pass) {
|
|
|
|
fprintf(stdout, "pass\n");
|
|
|
|
passed++;
|
|
|
|
}
|
2018-11-11 18:20:01 -03:00
|
|
|
|
|
|
|
// Prepare for sort tests
|
|
|
|
for (i = 0; i < n; i++) {
|
2018-11-11 20:15:30 -03:00
|
|
|
test_case[i] = gen_rand(-100, 100);
|
2018-11-11 18:20:01 -03:00
|
|
|
}
|
|
|
|
memcpy(qarray, test_case, sizeof(int) * n);
|
|
|
|
qsort(qarray, n, sizeof(int), compar);
|
|
|
|
|
|
|
|
// Test bubble sort
|
2018-11-13 09:15:33 -03:00
|
|
|
pass = 1;
|
|
|
|
memcpy(test_array, test_case, sizeof(int) * n);
|
2018-11-11 18:20:01 -03:00
|
|
|
fprintf(stdout, "\tbubble sort: ");
|
|
|
|
fflush(stdout);
|
|
|
|
bubble_sort(test_array, n);
|
|
|
|
for (i = 0; i < n; i++) {
|
2018-11-13 09:15:33 -03:00
|
|
|
if (test_array[i] != qarray[i]) {
|
2018-11-11 18:20:01 -03:00
|
|
|
fprintf(stdout, "fail\n");
|
2018-11-11 18:45:53 -03:00
|
|
|
failed++;
|
|
|
|
pass = 0;
|
2018-11-12 14:19:47 -03:00
|
|
|
break;
|
2018-11-11 18:20:01 -03:00
|
|
|
}
|
|
|
|
}
|
2018-11-11 18:45:53 -03:00
|
|
|
if (pass) {
|
|
|
|
fprintf(stdout, "pass\n");
|
|
|
|
passed++;
|
|
|
|
}
|
2018-11-13 09:15:33 -03:00
|
|
|
|
|
|
|
// Test count sort
|
2018-11-11 18:45:53 -03:00
|
|
|
pass = 1;
|
2018-11-13 09:15:33 -03:00
|
|
|
memcpy(test_array, test_case, sizeof(int) * n);
|
|
|
|
fprintf(stdout, "\tcount sort: ");
|
|
|
|
fflush(stdout);
|
|
|
|
count_sort(test_array, n);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (test_array[i] != qarray[i]) {
|
|
|
|
fprintf(stdout, "fail\n");
|
|
|
|
failed++;
|
|
|
|
pass = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pass) {
|
|
|
|
fprintf(stdout, "pass\n");
|
|
|
|
passed++;
|
|
|
|
}
|
2018-11-11 18:20:01 -03:00
|
|
|
|
2018-11-13 11:59:21 -03:00
|
|
|
// Test quick sort
|
2018-11-13 12:20:01 -03:00
|
|
|
pass = 1;
|
|
|
|
memcpy(test_array, test_case, sizeof(int) * n);
|
2018-11-13 11:59:21 -03:00
|
|
|
fprintf(stdout, "\tquick sort: ");
|
|
|
|
fflush(stdout);
|
|
|
|
quick_sort(test_array, n);
|
|
|
|
for (i = 0; i < n; i++) {
|
2018-11-13 12:20:01 -03:00
|
|
|
if (test_array[i] != qarray[i]) {
|
2018-11-13 11:59:21 -03:00
|
|
|
fprintf(stdout, "fail\n");
|
|
|
|
failed++;
|
|
|
|
pass = 0;
|
2018-11-13 12:20:01 -03:00
|
|
|
break;
|
2018-11-13 11:59:21 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pass) {
|
|
|
|
fprintf(stdout, "pass\n");
|
|
|
|
passed++;
|
|
|
|
}
|
|
|
|
|
2018-11-14 17:53:45 -03:00
|
|
|
// Test merge sort
|
|
|
|
pass = 1;
|
|
|
|
memcpy(test_array, test_case, sizeof(int) * n);
|
|
|
|
fprintf(stdout, "\tmerge sort: ");
|
|
|
|
fflush(stdout);
|
|
|
|
merge_sort(test_array, n);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (test_array[i] != qarray[i]) {
|
|
|
|
fprintf(stdout, "fail\n");
|
|
|
|
failed++;
|
|
|
|
pass = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pass) {
|
|
|
|
fprintf(stdout, "pass\n");
|
|
|
|
passed++;
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:45:53 -03:00
|
|
|
fprintf(stdout, "%d tests passed\n", passed);
|
|
|
|
fprintf(stdout, "%d tests failed\n", failed);
|
|
|
|
|
|
|
|
if (failed == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-11-11 18:20:01 -03:00
|
|
|
}
|