Proyecto de ordenamiento para el curso de Análisis y Diseño de Algoritmos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
5.6KB

  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. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #include "random.h"
  20. #include "bubble_sort.h"
  21. #include "count_sort.h"
  22. #include "selection_sort.h"
  23. #include "bitonic_sort.h"
  24. #include "merge_sort.h"
  25. #include "quick_sort.h"
  26. static int *test_case;
  27. static int *test_array;
  28. static int *qarray;
  29. /**
  30. * Este función va a comparar valores para devolver los valores en orden ascendida
  31. * @param a El primer valor a comparar
  32. * @param b El segundo valor a comparar
  33. * @return Retorna la comparación de los dos argumentos
  34. */
  35. int compar(const void *a, const void *b) {
  36. return (*(int*) a - *(int*) b);
  37. }
  38. /**
  39. * Limpiar la memoria al salir
  40. */
  41. void cleanup() {
  42. free(test_case);
  43. free(test_array);
  44. free(qarray);
  45. }
  46. /**
  47. * El programa de test
  48. */
  49. int main(int argc, char **argv) {
  50. int n = 32768;
  51. int i;
  52. int gen;
  53. int pass;
  54. int passed = 0;
  55. int failed = 0;
  56. test_case = malloc(sizeof(int) * n);
  57. if (test_case == NULL) {
  58. fprintf(stderr, "Error: Out of heap space!\n");
  59. exit(1);
  60. }
  61. qarray = malloc(sizeof(int) * n);
  62. if (qarray == NULL) {
  63. fprintf(stderr, "Error: Out of heap space!\n");
  64. exit(1);
  65. }
  66. test_array = malloc(sizeof(int) * n);
  67. if (test_array == NULL) {
  68. fprintf(stderr, "Error: Out of heap space!\n");
  69. exit(1);
  70. }
  71. atexit(cleanup);
  72. fprintf(stdout, "Running tests:\n");
  73. // Test random number generation
  74. pass = 1;
  75. fprintf(stdout, "\trandom generation: ");
  76. fflush(stdout);
  77. for (i = 0; i < 5000000; i++) {
  78. gen = gen_rand(-1000, 1000);
  79. if ((gen < -1000 || gen > 1000) && pass) {
  80. fprintf(stdout, "fail\n");
  81. failed++;
  82. pass = 0;
  83. break;
  84. }
  85. }
  86. if (pass) {
  87. fprintf(stdout, "pass\n");
  88. passed++;
  89. }
  90. // Prepare for sort tests
  91. for (i = 0; i < n; i++) {
  92. test_case[i] = gen_rand(-100, 100);
  93. }
  94. memcpy(qarray, test_case, sizeof(int) * n);
  95. qsort(qarray, n, sizeof(int), compar);
  96. // Test bubble sort
  97. pass = 1;
  98. memcpy(test_array, test_case, sizeof(int) * n);
  99. fprintf(stdout, "\tbubble sort: ");
  100. fflush(stdout);
  101. bubble_sort(test_array, n);
  102. for (i = 0; i < n; i++) {
  103. if (test_array[i] != qarray[i]) {
  104. fprintf(stdout, "fail\n");
  105. failed++;
  106. pass = 0;
  107. break;
  108. }
  109. }
  110. if (pass) {
  111. fprintf(stdout, "pass\n");
  112. passed++;
  113. }
  114. // Test count sort
  115. pass = 1;
  116. memcpy(test_array, test_case, sizeof(int) * n);
  117. fprintf(stdout, "\tcount sort: ");
  118. fflush(stdout);
  119. count_sort(test_array, n);
  120. for (i = 0; i < n; i++) {
  121. if (test_array[i] != qarray[i]) {
  122. fprintf(stdout, "fail\n");
  123. failed++;
  124. pass = 0;
  125. break;
  126. }
  127. }
  128. if (pass) {
  129. fprintf(stdout, "pass\n");
  130. passed++;
  131. }
  132. // Test quick sort
  133. pass = 1;
  134. memcpy(test_array, test_case, sizeof(int) * n);
  135. fprintf(stdout, "\tquick sort: ");
  136. fflush(stdout);
  137. quick_sort(test_array, n);
  138. for (i = 0; i < n; i++) {
  139. if (test_array[i] != qarray[i]) {
  140. fprintf(stdout, "fail\n");
  141. failed++;
  142. pass = 0;
  143. break;
  144. }
  145. }
  146. if (pass) {
  147. fprintf(stdout, "pass\n");
  148. passed++;
  149. }
  150. // Test bitonic sort
  151. pass = 1;
  152. memcpy(test_array, test_case, sizeof(int) * n);
  153. fprintf(stdout, "\tbitonic sort: ");
  154. fflush(stdout);
  155. bitonic_sort(test_array, n);
  156. for (i = 0; i < n; i++) {
  157. if (test_array[i] != qarray[i]) {
  158. fprintf(stdout, "fail\n");
  159. failed++;
  160. pass = 0;
  161. break;
  162. }
  163. }
  164. if (pass) {
  165. fprintf(stdout, "pass\n");
  166. passed++;
  167. }
  168. // Test selection sort
  169. pass = 1;
  170. memcpy(test_array, test_case, sizeof(int) * n);
  171. fprintf(stdout, "\tselection sort: ");
  172. fflush(stdout);
  173. selection_sort(test_array, n);
  174. for (i = 0; i < n; i++) {
  175. if (test_array[i] != qarray[i]) {
  176. fprintf(stdout, "fail\n");
  177. failed++;
  178. pass = 0;
  179. break;
  180. }
  181. }
  182. if (pass) {
  183. fprintf(stdout, "pass\n");
  184. passed++;
  185. }
  186. // Test merge sort
  187. pass = 1;
  188. memcpy(test_array, test_case, sizeof(int) * n);
  189. fprintf(stdout, "\tmerge sort: ");
  190. fflush(stdout);
  191. merge_sort(test_array, n);
  192. for (i = 0; i < n; i++) {
  193. if (test_array[i] != qarray[i]) {
  194. fprintf(stdout, "fail\n");
  195. failed++;
  196. pass = 0;
  197. break;
  198. }
  199. }
  200. if (pass) {
  201. fprintf(stdout, "pass\n");
  202. passed++;
  203. }
  204. fprintf(stdout, "%d tests passed\n", passed);
  205. fprintf(stdout, "%d tests failed\n", failed);
  206. if (failed == 0) {
  207. return 0;
  208. }
  209. else {
  210. return 1;
  211. }
  212. }