cb_list_test.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * SPDX-License-Identifier: Apache-2.0
  3. *
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2022-05-14 tyx first implementation
  7. */
  8. #include <gtest/gtest.h>
  9. #include "cb_list.h"
  10. TEST(testCase, cb_list_test01)
  11. {
  12. cb_list_t l = CB_LIST_OBJECT_INIT(l);
  13. EXPECT_EQ(l.next, l.prev);
  14. EXPECT_EQ(l.next, &l);
  15. }
  16. TEST(testCase, cb_list_test02)
  17. {
  18. cb_list_t l;
  19. cb_list_init(&l);
  20. EXPECT_EQ(l.next, l.prev);
  21. EXPECT_EQ(l.next, &l);
  22. }
  23. TEST(testCase, cb_list_test03)
  24. {
  25. cb_list_t h = CB_LIST_OBJECT_INIT(h);
  26. cb_list_t n0, n1;
  27. cb_list_init(&n0);
  28. cb_list_init(&n1);
  29. cb_list_insert_after(&h, &n1);
  30. EXPECT_EQ(h.next, &n1);
  31. EXPECT_EQ(h.prev, &n1);
  32. EXPECT_EQ(n1.next, &h);
  33. EXPECT_EQ(n1.prev, &h);
  34. cb_list_insert_after(&h, &n0);
  35. EXPECT_EQ(h.next, &n0);
  36. EXPECT_EQ(h.prev, &n1);
  37. EXPECT_EQ(n0.next, &n1);
  38. EXPECT_EQ(n0.prev, &h);
  39. EXPECT_EQ(n1.next, &h);
  40. EXPECT_EQ(n1.prev, &n0);
  41. }
  42. TEST(testCase, cb_list_test04)
  43. {
  44. cb_list_t h = CB_LIST_OBJECT_INIT(h);
  45. cb_list_t n0, n1;
  46. cb_list_init(&n0);
  47. cb_list_init(&n1);
  48. cb_list_insert_before(&h, &n0);
  49. EXPECT_EQ(h.next, &n0);
  50. EXPECT_EQ(h.prev, &n0);
  51. EXPECT_EQ(n0.next, &h);
  52. EXPECT_EQ(n0.prev, &h);
  53. cb_list_insert_before(&h, &n1);
  54. EXPECT_EQ(h.next, &n0);
  55. EXPECT_EQ(h.prev, &n1);
  56. EXPECT_EQ(n0.next, &n1);
  57. EXPECT_EQ(n0.prev, &h);
  58. EXPECT_EQ(n1.next, &h);
  59. EXPECT_EQ(n1.prev, &n0);
  60. }
  61. TEST(testCase, cb_list_test05)
  62. {
  63. cb_list_t h = CB_LIST_OBJECT_INIT(h);
  64. cb_list_t n0, n1;
  65. cb_list_init(&n0);
  66. cb_list_init(&n1);
  67. EXPECT_EQ(cb_list_first(&h), &h);
  68. cb_list_insert_after(&h, &n1);
  69. EXPECT_EQ(cb_list_first(&h), &n1);
  70. cb_list_insert_after(&h, &n0);
  71. EXPECT_EQ(cb_list_first(&h), &n0);
  72. }
  73. TEST(testCase, cb_list_test06)
  74. {
  75. cb_list_t h = CB_LIST_OBJECT_INIT(h);
  76. cb_list_t n0, n1;
  77. cb_list_init(&n0);
  78. cb_list_init(&n1);
  79. EXPECT_EQ(cb_list_first(&h), &h);
  80. cb_list_insert_before(&h, &n0);
  81. EXPECT_EQ(cb_list_first(&h), &n0);
  82. cb_list_insert_before(&h, &n1);
  83. EXPECT_EQ(cb_list_first(&h), &n0);
  84. }
  85. TEST(testCase, cb_list_test07)
  86. {
  87. cb_list_t h = CB_LIST_OBJECT_INIT(h);
  88. cb_list_t n0, n1;
  89. cb_list_init(&n0);
  90. cb_list_init(&n1);
  91. EXPECT_NE(cb_list_isempty(&h), 0);
  92. cb_list_insert_after(&h, &n1);
  93. EXPECT_EQ(cb_list_isempty(&h), 0);
  94. cb_list_insert_after(&h, &n0);
  95. EXPECT_EQ(cb_list_isempty(&h), 0);
  96. }
  97. TEST(testCase, cb_list_test08)
  98. {
  99. cb_list_t h = CB_LIST_OBJECT_INIT(h);
  100. cb_list_t n0, n1;
  101. cb_list_init(&n0);
  102. cb_list_init(&n1);
  103. cb_list_insert_after(&h, &n1);
  104. cb_list_remove(&n1);
  105. EXPECT_NE(cb_list_isempty(&h), 0);
  106. cb_list_insert_after(&h, &n1);
  107. cb_list_insert_after(&h, &n0);
  108. cb_list_remove(&n0);
  109. EXPECT_EQ(cb_list_isempty(&h), 0);
  110. EXPECT_EQ(cb_list_first(&h), &n1);
  111. cb_list_insert_after(&h, &n0);
  112. cb_list_remove(&n1);
  113. EXPECT_EQ(cb_list_isempty(&h), 0);
  114. EXPECT_EQ(cb_list_first(&h), &n0);
  115. cb_list_remove(&n0);
  116. EXPECT_NE(cb_list_isempty(&h), 0);
  117. }
  118. TEST(testCase, cb_list_test09)
  119. {
  120. cb_list_t h = CB_LIST_OBJECT_INIT(h);
  121. cb_list_t n0, n1;
  122. cb_list_init(&n0);
  123. cb_list_init(&n1);
  124. EXPECT_EQ(cb_list_len(&h), 0);
  125. cb_list_insert_after(&h, &n1);
  126. EXPECT_EQ(cb_list_len(&h), 1);
  127. cb_list_insert_after(&h, &n0);
  128. EXPECT_EQ(cb_list_len(&h), 2);
  129. cb_list_remove(&n1);
  130. EXPECT_EQ(cb_list_len(&h), 1);
  131. cb_list_remove(&n0);
  132. EXPECT_EQ(cb_list_len(&h), 0);
  133. }
  134. TEST(testCase, cb_list_test10)
  135. {
  136. struct test_object
  137. {
  138. cb_uint8_t i0;
  139. cb_uint8_t i1;
  140. cb_uint8_t i2;
  141. cb_uint32_t i4;
  142. };
  143. struct test_object obj;
  144. obj.i0 = 0;
  145. obj.i1 = 0;
  146. obj.i2 = 0;
  147. obj.i4 = 0;
  148. EXPECT_EQ(cb_container_of(&obj.i0, struct test_object, i0), &obj);
  149. EXPECT_EQ(cb_container_of(&obj.i1, struct test_object, i1), &obj);
  150. EXPECT_EQ(cb_container_of(&obj.i2, struct test_object, i2), &obj);
  151. EXPECT_EQ(cb_container_of(&obj.i4, struct test_object, i4), &obj);
  152. }
  153. TEST(testCase, cb_list_test11)
  154. {
  155. struct test_object
  156. {
  157. cb_list_t n0;
  158. cb_uint8_t i0;
  159. cb_list_t n1;
  160. cb_uint8_t i1;
  161. cb_list_t n2;
  162. cb_uint8_t i2;
  163. cb_list_t n3;
  164. cb_uint32_t i4;
  165. cb_list_t n4;
  166. };
  167. struct test_object obj;
  168. obj.i0 = 0;
  169. obj.i1 = 0;
  170. obj.i2 = 0;
  171. obj.i4 = 0;
  172. cb_list_init(&obj.n0);
  173. cb_list_init(&obj.n1);
  174. cb_list_init(&obj.n2);
  175. cb_list_init(&obj.n3);
  176. cb_list_init(&obj.n4);
  177. EXPECT_EQ(cb_list_entry(&obj.n0, struct test_object, n0), &obj);
  178. EXPECT_EQ(cb_list_entry(&obj.n1, struct test_object, n1), &obj);
  179. EXPECT_EQ(cb_list_entry(&obj.n2, struct test_object, n2), &obj);
  180. EXPECT_EQ(cb_list_entry(&obj.n3, struct test_object, n3), &obj);
  181. EXPECT_EQ(cb_list_entry(&obj.n4, struct test_object, n4), &obj);
  182. }
  183. TEST(testCase, cb_list_test12)
  184. {
  185. cb_list_t h = CB_LIST_OBJECT_INIT(h);
  186. cb_list_t n0, n1;
  187. int index = 0;
  188. cb_list_init(&n0);
  189. cb_list_init(&n1);
  190. cb_list_insert_after(&h, &n1);
  191. cb_list_insert_after(&h, &n0);
  192. cb_list_for_each(n, &h)
  193. {
  194. switch (index)
  195. {
  196. case 0:
  197. EXPECT_EQ(n, &n0);
  198. break;
  199. case 1:
  200. EXPECT_EQ(n, &n1);
  201. break;
  202. }
  203. index = index + 1;
  204. }
  205. EXPECT_EQ(index, 2);
  206. }
  207. TEST(testCase, cb_list_test13)
  208. {
  209. cb_list_t h = CB_LIST_OBJECT_INIT(h);
  210. struct test_object
  211. {
  212. cb_uint8_t i0;
  213. cb_list_t n;
  214. };
  215. struct test_object obj[2];
  216. obj[0].i0 = 0;
  217. obj[1].i0 = 0;
  218. cb_list_init(&obj[0].n);
  219. cb_list_init(&obj[1].n);
  220. cb_list_insert_after(&h, &obj[1].n);
  221. EXPECT_EQ(cb_list_first_entry(&h, struct test_object, n), &obj[1]);
  222. cb_list_insert_after(&h, &obj[0].n);
  223. EXPECT_EQ(cb_list_first_entry(&h, struct test_object, n), &obj[0]);
  224. }