test_container_heap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2008 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Nathan Evans
  18. * @file util/test_container_heap.c
  19. * @brief Test of heap operations
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. static int
  24. iterator_callback (void *cls,
  25. struct GNUNET_CONTAINER_HeapNode *node,
  26. void *element, GNUNET_CONTAINER_HeapCostType cost)
  27. {
  28. return GNUNET_OK;
  29. }
  30. static int
  31. nstrcmp (const char *a, const char *b)
  32. {
  33. GNUNET_assert (a != NULL);
  34. GNUNET_assert (b != NULL);
  35. return strcmp (a, b);
  36. }
  37. static int
  38. check ()
  39. {
  40. struct GNUNET_CONTAINER_Heap *myHeap;
  41. struct GNUNET_CONTAINER_HeapNode *n1;
  42. struct GNUNET_CONTAINER_HeapNode *n2;
  43. struct GNUNET_CONTAINER_HeapNode *n3;
  44. struct GNUNET_CONTAINER_HeapNode *n4;
  45. struct GNUNET_CONTAINER_HeapNode *n5;
  46. struct GNUNET_CONTAINER_HeapNode *n6;
  47. struct GNUNET_CONTAINER_HeapNode *n7;
  48. struct GNUNET_CONTAINER_HeapNode *n8;
  49. const char *r;
  50. myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  51. // GNUNET_CONTAINER_heap_remove_root heap empty, taking if-branch
  52. n1 = GNUNET_CONTAINER_heap_remove_root (myHeap);
  53. GNUNET_assert (NULL == n1);
  54. // GNUNET_CONTAINER_heap_peek heap empty, taking if-branch
  55. n1 = GNUNET_CONTAINER_heap_peek (myHeap);
  56. GNUNET_assert (NULL == n1);
  57. // GNUNET_CONTAINER_heap_walk_get_next: heap empty, taking if-branch
  58. n1 = GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  59. GNUNET_assert (NULL == n1);
  60. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "11", 11);
  61. GNUNET_assert (NULL != n1);
  62. // GNUNET_CONTAINER_heap_peek not empty, taking if-branch
  63. n2 = NULL;
  64. n2 = GNUNET_CONTAINER_heap_peek (myHeap);
  65. GNUNET_assert (NULL != n2);
  66. // GNUNET_CONTAINER_heap_walk_get_next: 1 element
  67. n1 = NULL;
  68. n1 = GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  69. GNUNET_assert (NULL != n1);
  70. GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
  71. GNUNET_assert (1 == GNUNET_CONTAINER_heap_get_size (myHeap));
  72. n2 = GNUNET_CONTAINER_heap_insert (myHeap, "78", 78);
  73. GNUNET_assert (2 == GNUNET_CONTAINER_heap_get_size (myHeap));
  74. GNUNET_assert (0 == strcmp ("78", GNUNET_CONTAINER_heap_remove_node (n2)));
  75. GNUNET_assert (1 == GNUNET_CONTAINER_heap_get_size (myHeap));
  76. GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
  77. n3 = GNUNET_CONTAINER_heap_insert (myHeap, "15", 5);
  78. GNUNET_CONTAINER_heap_update_cost (n3, 15);
  79. GNUNET_assert (2 == GNUNET_CONTAINER_heap_get_size (myHeap));
  80. GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
  81. n4 = GNUNET_CONTAINER_heap_insert (myHeap, "50", 50);
  82. GNUNET_CONTAINER_heap_update_cost (n4, 50);
  83. GNUNET_assert (3 == GNUNET_CONTAINER_heap_get_size (myHeap));
  84. GNUNET_CONTAINER_heap_iterate (myHeap, &iterator_callback, NULL);
  85. n5 = GNUNET_CONTAINER_heap_insert (myHeap, "100", 100);
  86. n6 = GNUNET_CONTAINER_heap_insert (myHeap, "30/200", 30);
  87. GNUNET_assert (5 == GNUNET_CONTAINER_heap_get_size (myHeap));
  88. GNUNET_CONTAINER_heap_remove_node (n5);
  89. r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n1 */
  90. GNUNET_assert (NULL != r);
  91. GNUNET_assert (0 == strcmp ("11", r));
  92. GNUNET_CONTAINER_heap_update_cost (n6, 200);
  93. GNUNET_CONTAINER_heap_remove_node (n3);
  94. r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n4 */
  95. GNUNET_assert (NULL != r);
  96. GNUNET_assert (0 == strcmp ("50", r));
  97. r = GNUNET_CONTAINER_heap_remove_root (myHeap); /* n6 */
  98. GNUNET_assert (NULL != r);
  99. GNUNET_assert (0 == strcmp ("30/200", r));
  100. GNUNET_assert (0 == GNUNET_CONTAINER_heap_get_size (myHeap));
  101. GNUNET_CONTAINER_heap_destroy (myHeap);
  102. // My additions to a complete testcase
  103. // Testing a GNUNET_CONTAINER_HEAP_ORDER_MIN
  104. // Testing remove_node
  105. myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  106. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
  107. GNUNET_CONTAINER_heap_update_cost (n1, 15);
  108. r = GNUNET_CONTAINER_heap_remove_node (n1);
  109. GNUNET_assert (NULL != r);
  110. GNUNET_assert (0 == strcmp ("10", r));
  111. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
  112. n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
  113. GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  114. r = GNUNET_CONTAINER_heap_remove_node (n2);
  115. GNUNET_assert (NULL != r);
  116. GNUNET_assert (0 == strcmp ("20", r));
  117. r = GNUNET_CONTAINER_heap_remove_node (n1);
  118. GNUNET_assert (NULL != r);
  119. GNUNET_assert (0 == strcmp ("10", r));
  120. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
  121. n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
  122. n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 10);
  123. GNUNET_CONTAINER_heap_remove_node (n2);
  124. GNUNET_CONTAINER_heap_remove_node (n1);
  125. r = GNUNET_CONTAINER_heap_remove_root (myHeap);
  126. GNUNET_assert (NULL != r);
  127. GNUNET_assert (0 == strcmp ("30", r));
  128. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
  129. n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
  130. n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 10);
  131. GNUNET_CONTAINER_heap_remove_node (n2);
  132. GNUNET_CONTAINER_heap_remove_node (n1);
  133. r = GNUNET_CONTAINER_heap_remove_node (n3);
  134. GNUNET_assert (NULL != r);
  135. GNUNET_assert (0 == strcmp ("30", r));
  136. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
  137. n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 20);
  138. n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 30);
  139. GNUNET_assert (0 == nstrcmp ("20", GNUNET_CONTAINER_heap_remove_node (n2)));
  140. GNUNET_assert (0 ==
  141. nstrcmp ("10", GNUNET_CONTAINER_heap_remove_root (myHeap)));
  142. GNUNET_assert (0 ==
  143. nstrcmp ("30", GNUNET_CONTAINER_heap_remove_root (myHeap)));
  144. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
  145. n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 20);
  146. n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 30);
  147. n4 = GNUNET_CONTAINER_heap_insert (myHeap, "40", 40);
  148. n5 = GNUNET_CONTAINER_heap_insert (myHeap, "50", 50);
  149. n6 = GNUNET_CONTAINER_heap_insert (myHeap, "60", 60);
  150. // Inserting nodes deeper in the tree with lower costs
  151. n7 = GNUNET_CONTAINER_heap_insert (myHeap, "70", 10);
  152. n8 = GNUNET_CONTAINER_heap_insert (myHeap, "80", 10);
  153. GNUNET_assert (0 == nstrcmp ("30", GNUNET_CONTAINER_heap_remove_node (n3)));
  154. // Cleaning up...
  155. GNUNET_assert (0 == nstrcmp ("60", GNUNET_CONTAINER_heap_remove_node (n6)));
  156. GNUNET_assert (0 == nstrcmp ("50", GNUNET_CONTAINER_heap_remove_node (n5)));
  157. // Testing heap_walk_get_next
  158. GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  159. GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  160. GNUNET_CONTAINER_heap_walk_get_next (myHeap);;
  161. GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  162. GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  163. GNUNET_assert (0 == nstrcmp ("10", GNUNET_CONTAINER_heap_remove_node (n1)));
  164. GNUNET_assert (0 == nstrcmp ("20", GNUNET_CONTAINER_heap_remove_node (n2)));
  165. GNUNET_assert (0 == nstrcmp ("40", GNUNET_CONTAINER_heap_remove_node (n4)));
  166. GNUNET_assert (0 == nstrcmp ("70", GNUNET_CONTAINER_heap_remove_node (n7)));
  167. GNUNET_assert (0 == nstrcmp ("80", GNUNET_CONTAINER_heap_remove_node (n8)));
  168. // End Testing remove_node
  169. // Testing a GNUNET_CONTAINER_HEAP_ORDER_MAX
  170. GNUNET_CONTAINER_heap_destroy (myHeap);
  171. myHeap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MAX);
  172. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
  173. GNUNET_CONTAINER_heap_update_cost (n1, 15);
  174. GNUNET_assert (0 == nstrcmp ("10", GNUNET_CONTAINER_heap_remove_node (n1)));
  175. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
  176. n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
  177. GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  178. GNUNET_assert (0 == nstrcmp ("20", GNUNET_CONTAINER_heap_remove_node (n2)));
  179. GNUNET_assert (0 == nstrcmp ("10", GNUNET_CONTAINER_heap_remove_node (n1)));
  180. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
  181. n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
  182. n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 10);
  183. GNUNET_CONTAINER_heap_remove_node (n2);
  184. GNUNET_CONTAINER_heap_remove_node (n1);
  185. GNUNET_assert (0 ==
  186. nstrcmp ("30", GNUNET_CONTAINER_heap_remove_root (myHeap)));
  187. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
  188. n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 10);
  189. n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 10);
  190. GNUNET_CONTAINER_heap_remove_node (n2);
  191. GNUNET_CONTAINER_heap_remove_node (n1);
  192. GNUNET_assert (0 == nstrcmp ("30", GNUNET_CONTAINER_heap_remove_node (n3)));
  193. n1 = GNUNET_CONTAINER_heap_insert (myHeap, "10", 10);
  194. n2 = GNUNET_CONTAINER_heap_insert (myHeap, "20", 20);
  195. n3 = GNUNET_CONTAINER_heap_insert (myHeap, "30", 30);
  196. n4 = GNUNET_CONTAINER_heap_insert (myHeap, "40", 40);
  197. n5 = GNUNET_CONTAINER_heap_insert (myHeap, "50", 50);
  198. n6 = GNUNET_CONTAINER_heap_insert (myHeap, "60", 60);
  199. // Inserting nodes deeper in the tree with lower costs
  200. n7 = GNUNET_CONTAINER_heap_insert (myHeap, "70", 10);
  201. n8 = GNUNET_CONTAINER_heap_insert (myHeap, "80", 10);
  202. GNUNET_assert (0 == nstrcmp ("30", GNUNET_CONTAINER_heap_remove_node (n3)));
  203. // Cleaning up...
  204. GNUNET_assert (0 == nstrcmp ("60", GNUNET_CONTAINER_heap_remove_node (n6)));
  205. GNUNET_assert (0 == nstrcmp ("50", GNUNET_CONTAINER_heap_remove_node (n5)));
  206. // Testing heap_walk_get_next
  207. GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  208. GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  209. GNUNET_CONTAINER_heap_walk_get_next (myHeap);;
  210. GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  211. GNUNET_CONTAINER_heap_walk_get_next (myHeap);
  212. GNUNET_assert (0 == nstrcmp ("10", GNUNET_CONTAINER_heap_remove_node (n1)));
  213. GNUNET_assert (0 == nstrcmp ("20", GNUNET_CONTAINER_heap_remove_node (n2)));
  214. GNUNET_assert (0 == nstrcmp ("40", GNUNET_CONTAINER_heap_remove_node (n4)));
  215. GNUNET_assert (0 == nstrcmp ("70", GNUNET_CONTAINER_heap_remove_node (n7)));
  216. GNUNET_assert (0 == nstrcmp ("80", GNUNET_CONTAINER_heap_remove_node (n8)));
  217. // End Testing remove_node
  218. GNUNET_CONTAINER_heap_destroy (myHeap);
  219. return 0;
  220. }
  221. int
  222. main (int argc, char **argv)
  223. {
  224. GNUNET_log_setup ("test-container-heap", "WARNING", NULL);
  225. return check ();
  226. }
  227. /* end of test_container_heap.c */