test-queue.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <glib.h>
  14. #include "src/shared/util.h"
  15. #include "src/shared/queue.h"
  16. #include "src/shared/tester.h"
  17. static void test_basic(const void *data)
  18. {
  19. struct queue *queue;
  20. unsigned int n, i;
  21. queue = queue_new();
  22. g_assert(queue != NULL);
  23. for (n = 0; n < 1024; n++) {
  24. for (i = 1; i < n + 2; i++)
  25. queue_push_tail(queue, UINT_TO_PTR(i));
  26. g_assert(queue_length(queue) == n + 1);
  27. for (i = 1; i < n + 2; i++) {
  28. void *ptr;
  29. ptr = queue_pop_head(queue);
  30. g_assert(ptr != NULL);
  31. g_assert(i == PTR_TO_UINT(ptr));
  32. }
  33. g_assert(queue_isempty(queue) == true);
  34. }
  35. queue_destroy(queue, NULL);
  36. tester_test_passed();
  37. }
  38. static void foreach_destroy(void *data, void *user_data)
  39. {
  40. struct queue *queue = user_data;
  41. queue_destroy(queue, NULL);
  42. }
  43. static void test_foreach_destroy(const void *data)
  44. {
  45. struct queue *queue;
  46. queue = queue_new();
  47. g_assert(queue != NULL);
  48. queue_push_tail(queue, UINT_TO_PTR(1));
  49. queue_push_tail(queue, UINT_TO_PTR(2));
  50. queue_foreach(queue, foreach_destroy, queue);
  51. tester_test_passed();
  52. }
  53. static void foreach_remove(void *data, void *user_data)
  54. {
  55. struct queue *queue = user_data;
  56. g_assert(queue_remove(queue, data));
  57. }
  58. static void test_foreach_remove(const void *data)
  59. {
  60. struct queue *queue;
  61. queue = queue_new();
  62. g_assert(queue != NULL);
  63. queue_push_tail(queue, UINT_TO_PTR(1));
  64. queue_push_tail(queue, UINT_TO_PTR(2));
  65. queue_foreach(queue, foreach_remove, queue);
  66. queue_destroy(queue, NULL);
  67. tester_test_passed();
  68. }
  69. static void foreach_remove_all(void *data, void *user_data)
  70. {
  71. struct queue *queue = user_data;
  72. queue_remove_all(queue, NULL, NULL, NULL);
  73. }
  74. static void test_foreach_remove_all(const void *data)
  75. {
  76. struct queue *queue;
  77. queue = queue_new();
  78. g_assert(queue != NULL);
  79. queue_push_tail(queue, UINT_TO_PTR(1));
  80. queue_push_tail(queue, UINT_TO_PTR(2));
  81. queue_foreach(queue, foreach_remove_all, queue);
  82. queue_destroy(queue, NULL);
  83. tester_test_passed();
  84. }
  85. static void foreach_remove_backward(void *data, void *user_data)
  86. {
  87. struct queue *queue = user_data;
  88. queue_remove(queue, UINT_TO_PTR(2));
  89. queue_remove(queue, UINT_TO_PTR(1));
  90. }
  91. static void test_foreach_remove_backward(const void *data)
  92. {
  93. struct queue *queue;
  94. queue = queue_new();
  95. g_assert(queue != NULL);
  96. queue_push_tail(queue, UINT_TO_PTR(1));
  97. queue_push_tail(queue, UINT_TO_PTR(2));
  98. queue_foreach(queue, foreach_remove_backward, queue);
  99. queue_destroy(queue, NULL);
  100. tester_test_passed();
  101. }
  102. static struct queue *static_queue;
  103. static void destroy_remove(void *user_data)
  104. {
  105. queue_remove(static_queue, user_data);
  106. }
  107. static void test_destroy_remove(const void *data)
  108. {
  109. static_queue = queue_new();
  110. g_assert(static_queue != NULL);
  111. queue_push_tail(static_queue, UINT_TO_PTR(1));
  112. queue_push_tail(static_queue, UINT_TO_PTR(2));
  113. queue_destroy(static_queue, destroy_remove);
  114. tester_test_passed();
  115. }
  116. static void test_push_after(const void *data)
  117. {
  118. struct queue *queue;
  119. unsigned int len, i;
  120. queue = queue_new();
  121. g_assert(queue != NULL);
  122. /*
  123. * Pre-populate queue. Initial elements are:
  124. * [ NULL, 2, 5 ]
  125. */
  126. g_assert(queue_push_tail(queue, NULL));
  127. g_assert(queue_push_tail(queue, UINT_TO_PTR(2)));
  128. g_assert(queue_push_tail(queue, UINT_TO_PTR(5)));
  129. g_assert(queue_length(queue) == 3);
  130. /* Invalid insertion */
  131. g_assert(!queue_push_after(queue, UINT_TO_PTR(6), UINT_TO_PTR(1)));
  132. /* Valid insertions */
  133. g_assert(queue_push_after(queue, NULL, UINT_TO_PTR(1)));
  134. g_assert(queue_push_after(queue, UINT_TO_PTR(2), UINT_TO_PTR(3)));
  135. g_assert(queue_push_after(queue, UINT_TO_PTR(3), UINT_TO_PTR(4)));
  136. g_assert(queue_push_after(queue, UINT_TO_PTR(5), UINT_TO_PTR(6)));
  137. g_assert(queue_peek_head(queue) == NULL);
  138. g_assert(queue_peek_tail(queue) == UINT_TO_PTR(6));
  139. /*
  140. * Queue should contain 7 elements:
  141. * [ NULL, 1, 2, 3, 4, 5, 6 ]
  142. */
  143. len = queue_length(queue);
  144. g_assert(len == 7);
  145. for (i = 0; i < 7; i++)
  146. g_assert(queue_pop_head(queue) == UINT_TO_PTR(i));
  147. /* Test with identical elements */
  148. g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
  149. g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
  150. g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
  151. g_assert(queue_push_after(queue, UINT_TO_PTR(1), UINT_TO_PTR(0)));
  152. g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
  153. g_assert(queue_pop_head(queue) == UINT_TO_PTR(0));
  154. g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
  155. g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
  156. queue_destroy(queue, NULL);
  157. tester_test_passed();
  158. }
  159. static bool match_int(const void *a, const void *b)
  160. {
  161. int i = PTR_TO_INT(a);
  162. int j = PTR_TO_INT(b);
  163. return i == j;
  164. }
  165. static bool match_ptr(const void *a, const void *b)
  166. {
  167. return a == b;
  168. }
  169. static void test_remove_all(const void *data)
  170. {
  171. struct queue *queue;
  172. queue = queue_new();
  173. g_assert(queue != NULL);
  174. g_assert(queue_push_tail(queue, INT_TO_PTR(10)));
  175. g_assert(queue_remove_all(queue, match_int, INT_TO_PTR(10), NULL) == 1);
  176. g_assert(queue_isempty(queue));
  177. g_assert(queue_push_tail(queue, NULL));
  178. g_assert(queue_remove_all(queue, match_ptr, NULL, NULL) == 1);
  179. g_assert(queue_isempty(queue));
  180. g_assert(queue_push_tail(queue, UINT_TO_PTR(0)));
  181. g_assert(queue_remove_all(queue, match_int, UINT_TO_PTR(0), NULL) == 1);
  182. g_assert(queue_isempty(queue));
  183. queue_destroy(queue, NULL);
  184. tester_test_passed();
  185. }
  186. int main(int argc, char *argv[])
  187. {
  188. tester_init(&argc, &argv);
  189. tester_add("/queue/basic", NULL, NULL, test_basic, NULL);
  190. tester_add("/queue/foreach_destroy", NULL, NULL,
  191. test_foreach_destroy, NULL);
  192. tester_add("/queue/foreach_remove", NULL, NULL,
  193. test_foreach_remove, NULL);
  194. tester_add("/queue/foreach_remove_all", NULL, NULL,
  195. test_foreach_remove_all, NULL);
  196. tester_add("/queue/foreach_remove_backward", NULL, NULL,
  197. test_foreach_remove_backward, NULL);
  198. tester_add("/queue/destroy_remove", NULL, NULL,
  199. test_destroy_remove, NULL);
  200. tester_add("/queue/push_after", NULL, NULL, test_push_after, NULL);
  201. tester_add("/queue/remove_all", NULL, NULL, test_remove_all, NULL);
  202. return tester_run();
  203. }