/test/list.c

https://gitlab.com/Manizuca/xserver · C · 386 lines · 267 code · 84 blank · 35 comment · 50 complexity · ff633b1ad0a1efd72a8c2ecdff92b006 MD5 · raw file

  1. /**
  2. * Copyright © 2011 Red Hat, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifdef HAVE_DIX_CONFIG_H
  24. #include <dix-config.h>
  25. #endif
  26. #include <X11/Xlib.h>
  27. #include <list.h>
  28. #include <string.h>
  29. #include <assert.h>
  30. #include <stdlib.h>
  31. struct parent {
  32. int a;
  33. struct xorg_list children;
  34. int b;
  35. };
  36. struct child {
  37. int foo;
  38. int bar;
  39. struct xorg_list node;
  40. };
  41. static void
  42. test_xorg_list_init(void)
  43. {
  44. struct parent parent, tmp;
  45. memset(&parent, 0, sizeof(parent));
  46. parent.a = 0xa5a5a5;
  47. parent.b = ~0xa5a5a5;
  48. tmp = parent;
  49. xorg_list_init(&parent.children);
  50. /* test we haven't touched anything else. */
  51. assert(parent.a == tmp.a);
  52. assert(parent.b == tmp.b);
  53. assert(xorg_list_is_empty(&parent.children));
  54. }
  55. static void
  56. test_xorg_list_add(void)
  57. {
  58. struct parent parent = { 0 };
  59. struct child child[3];
  60. struct child *c;
  61. xorg_list_init(&parent.children);
  62. xorg_list_add(&child[0].node, &parent.children);
  63. assert(!xorg_list_is_empty(&parent.children));
  64. c = xorg_list_first_entry(&parent.children, struct child, node);
  65. assert(memcmp(c, &child[0], sizeof(struct child)) == 0);
  66. /* note: xorg_list_add prepends */
  67. xorg_list_add(&child[1].node, &parent.children);
  68. c = xorg_list_first_entry(&parent.children, struct child, node);
  69. assert(memcmp(c, &child[1], sizeof(struct child)) == 0);
  70. xorg_list_add(&child[2].node, &parent.children);
  71. c = xorg_list_first_entry(&parent.children, struct child, node);
  72. assert(memcmp(c, &child[2], sizeof(struct child)) == 0);
  73. };
  74. static void
  75. test_xorg_list_append(void)
  76. {
  77. struct parent parent = { 0 };
  78. struct child child[3];
  79. struct child *c;
  80. int i;
  81. xorg_list_init(&parent.children);
  82. xorg_list_append(&child[0].node, &parent.children);
  83. assert(!xorg_list_is_empty(&parent.children));
  84. c = xorg_list_first_entry(&parent.children, struct child, node);
  85. assert(memcmp(c, &child[0], sizeof(struct child)) == 0);
  86. c = xorg_list_last_entry(&parent.children, struct child, node);
  87. assert(memcmp(c, &child[0], sizeof(struct child)) == 0);
  88. xorg_list_append(&child[1].node, &parent.children);
  89. c = xorg_list_first_entry(&parent.children, struct child, node);
  90. assert(memcmp(c, &child[0], sizeof(struct child)) == 0);
  91. c = xorg_list_last_entry(&parent.children, struct child, node);
  92. assert(memcmp(c, &child[1], sizeof(struct child)) == 0);
  93. xorg_list_append(&child[2].node, &parent.children);
  94. c = xorg_list_first_entry(&parent.children, struct child, node);
  95. assert(memcmp(c, &child[0], sizeof(struct child)) == 0);
  96. c = xorg_list_last_entry(&parent.children, struct child, node);
  97. assert(memcmp(c, &child[2], sizeof(struct child)) == 0);
  98. i = 0;
  99. xorg_list_for_each_entry(c, &parent.children, node) {
  100. assert(memcmp(c, &child[i++], sizeof(struct child)) == 0);
  101. }
  102. };
  103. static void
  104. test_xorg_list_del(void)
  105. {
  106. struct parent parent = { 0 };
  107. struct child child[2];
  108. struct child *c;
  109. xorg_list_init(&parent.children);
  110. xorg_list_add(&child[0].node, &parent.children);
  111. assert(!xorg_list_is_empty(&parent.children));
  112. xorg_list_del(&parent.children);
  113. assert(xorg_list_is_empty(&parent.children));
  114. xorg_list_add(&child[0].node, &parent.children);
  115. xorg_list_del(&child[0].node);
  116. assert(xorg_list_is_empty(&parent.children));
  117. xorg_list_add(&child[0].node, &parent.children);
  118. xorg_list_add(&child[1].node, &parent.children);
  119. c = xorg_list_first_entry(&parent.children, struct child, node);
  120. assert(memcmp(c, &child[1], sizeof(struct child)) == 0);
  121. /* delete first node */
  122. xorg_list_del(&child[1].node);
  123. assert(!xorg_list_is_empty(&parent.children));
  124. assert(xorg_list_is_empty(&child[1].node));
  125. c = xorg_list_first_entry(&parent.children, struct child, node);
  126. assert(memcmp(c, &child[0], sizeof(struct child)) == 0);
  127. /* delete last node */
  128. xorg_list_add(&child[1].node, &parent.children);
  129. xorg_list_del(&child[0].node);
  130. c = xorg_list_first_entry(&parent.children, struct child, node);
  131. assert(memcmp(c, &child[1], sizeof(struct child)) == 0);
  132. /* delete list head */
  133. xorg_list_add(&child[0].node, &parent.children);
  134. xorg_list_del(&parent.children);
  135. assert(xorg_list_is_empty(&parent.children));
  136. assert(!xorg_list_is_empty(&child[0].node));
  137. assert(!xorg_list_is_empty(&child[1].node));
  138. }
  139. static void
  140. test_xorg_list_for_each(void)
  141. {
  142. struct parent parent = { 0 };
  143. struct child child[3];
  144. struct child *c;
  145. int i = 0;
  146. xorg_list_init(&parent.children);
  147. xorg_list_add(&child[2].node, &parent.children);
  148. xorg_list_add(&child[1].node, &parent.children);
  149. xorg_list_add(&child[0].node, &parent.children);
  150. xorg_list_for_each_entry(c, &parent.children, node) {
  151. assert(memcmp(c, &child[i], sizeof(struct child)) == 0);
  152. i++;
  153. }
  154. /* foreach on empty list */
  155. xorg_list_del(&parent.children);
  156. assert(xorg_list_is_empty(&parent.children));
  157. xorg_list_for_each_entry(c, &parent.children, node) {
  158. assert(0); /* we must not get here */
  159. }
  160. }
  161. struct foo {
  162. char a;
  163. struct foo *next;
  164. char b;
  165. };
  166. static void
  167. test_nt_list_init(void)
  168. {
  169. struct foo foo;
  170. foo.a = 10;
  171. foo.b = 20;
  172. nt_list_init(&foo, next);
  173. assert(foo.a == 10);
  174. assert(foo.b == 20);
  175. assert(foo.next == NULL);
  176. assert(nt_list_next(&foo, next) == NULL);
  177. }
  178. static void
  179. test_nt_list_append(void)
  180. {
  181. int i;
  182. struct foo *foo = calloc(10, sizeof(struct foo));
  183. struct foo *item;
  184. for (item = foo, i = 1; i <= 10; i++, item++) {
  185. item->a = i;
  186. item->b = i * 2;
  187. nt_list_init(item, next);
  188. if (item != foo)
  189. nt_list_append(item, foo, struct foo, next);
  190. }
  191. /* Test using nt_list_next */
  192. for (item = foo, i = 1; i <= 10; i++, item = nt_list_next(item, next)) {
  193. assert(item->a == i);
  194. assert(item->b == i * 2);
  195. }
  196. /* Test using nt_list_for_each_entry */
  197. i = 1;
  198. nt_list_for_each_entry(item, foo, next) {
  199. assert(item->a == i);
  200. assert(item->b == i * 2);
  201. i++;
  202. }
  203. assert(i == 11);
  204. }
  205. static void
  206. test_nt_list_insert(void)
  207. {
  208. int i;
  209. struct foo *foo = calloc(10, sizeof(struct foo));
  210. struct foo *item;
  211. foo->a = 1;
  212. foo->b = 2;
  213. nt_list_init(foo, next);
  214. for (item = &foo[1], i = 10; i > 1; i--, item++) {
  215. item->a = i;
  216. item->b = i * 2;
  217. nt_list_init(item, next);
  218. nt_list_insert(item, foo, struct foo, next);
  219. }
  220. /* Test using nt_list_next */
  221. for (item = foo, i = 1; i <= 10; i++, item = nt_list_next(item, next)) {
  222. assert(item->a == i);
  223. assert(item->b == i * 2);
  224. }
  225. /* Test using nt_list_for_each_entry */
  226. i = 1;
  227. nt_list_for_each_entry(item, foo, next) {
  228. assert(item->a == i);
  229. assert(item->b == i * 2);
  230. i++;
  231. }
  232. assert(i == 11);
  233. }
  234. static void
  235. test_nt_list_delete(void)
  236. {
  237. int i = 1;
  238. struct foo *list = calloc(10, sizeof(struct foo));
  239. struct foo *foo = list;
  240. struct foo *item, *tmp;
  241. struct foo *empty_list = foo;
  242. nt_list_init(empty_list, next);
  243. nt_list_del(empty_list, empty_list, struct foo, next);
  244. assert(!empty_list);
  245. for (item = foo, i = 1; i <= 10; i++, item++) {
  246. item->a = i;
  247. item->b = i * 2;
  248. nt_list_init(item, next);
  249. if (item != foo)
  250. nt_list_append(item, foo, struct foo, next);
  251. }
  252. i = 0;
  253. nt_list_for_each_entry(item, foo, next) {
  254. i++;
  255. }
  256. assert(i == 10);
  257. /* delete last item */
  258. nt_list_del(&foo[9], foo, struct foo, next);
  259. i = 0;
  260. nt_list_for_each_entry(item, foo, next) {
  261. assert(item->a != 10); /* element 10 is gone now */
  262. i++;
  263. }
  264. assert(i == 9); /* 9 elements left */
  265. /* delete second item */
  266. nt_list_del(foo->next, foo, struct foo, next);
  267. assert(foo->next->a == 3);
  268. i = 0;
  269. nt_list_for_each_entry(item, foo, next) {
  270. assert(item->a != 10); /* element 10 is gone now */
  271. assert(item->a != 2); /* element 2 is gone now */
  272. i++;
  273. }
  274. assert(i == 8); /* 9 elements left */
  275. item = foo;
  276. /* delete first item */
  277. nt_list_del(foo, foo, struct foo, next);
  278. assert(item != foo);
  279. assert(item->next == NULL);
  280. assert(foo->a == 3);
  281. assert(foo->next->a == 4);
  282. nt_list_for_each_entry_safe(item, tmp, foo, next) {
  283. nt_list_del(item, foo, struct foo, next);
  284. }
  285. assert(!foo);
  286. assert(!item);
  287. free(list);
  288. }
  289. int
  290. main(int argc, char **argv)
  291. {
  292. test_xorg_list_init();
  293. test_xorg_list_add();
  294. test_xorg_list_append();
  295. test_xorg_list_del();
  296. test_xorg_list_for_each();
  297. test_nt_list_init();
  298. test_nt_list_append();
  299. test_nt_list_insert();
  300. test_nt_list_delete();
  301. return 0;
  302. }