list-test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright © 2012 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <assert.h>
  28. #include "wayland-private.h"
  29. #include "test-runner.h"
  30. TEST(list_init)
  31. {
  32. struct wl_list list;
  33. wl_list_init(&list);
  34. assert(list.next == &list);
  35. assert(list.prev == &list);
  36. assert(wl_list_empty(&list));
  37. }
  38. struct element {
  39. int i;
  40. struct wl_list link;
  41. };
  42. TEST(list_insert)
  43. {
  44. struct wl_list list;
  45. struct element e;
  46. wl_list_init(&list);
  47. wl_list_insert(&list, &e.link);
  48. assert(list.next == &e.link);
  49. assert(list.prev == &e.link);
  50. assert(e.link.next == &list);
  51. assert(e.link.prev == &list);
  52. }
  53. TEST(list_length)
  54. {
  55. struct wl_list list;
  56. struct element e;
  57. wl_list_init(&list);
  58. assert(wl_list_length(&list) == 0);
  59. wl_list_insert(&list, &e.link);
  60. assert(wl_list_length(&list) == 1);
  61. wl_list_remove(&e.link);
  62. assert(wl_list_length(&list) == 0);
  63. }
  64. TEST(list_iterator)
  65. {
  66. struct wl_list list;
  67. struct element e1, e2, e3, e4, *e;
  68. int reference[] = { 708090, 102030, 5588, 12 };
  69. unsigned int i;
  70. e1.i = 708090;
  71. e2.i = 102030;
  72. e3.i = 5588;
  73. e4.i = 12;
  74. wl_list_init(&list);
  75. wl_list_insert(list.prev, &e1.link);
  76. wl_list_insert(list.prev, &e2.link);
  77. wl_list_insert(list.prev, &e3.link);
  78. wl_list_insert(list.prev, &e4.link);
  79. i = 0;
  80. wl_list_for_each(e, &list, link) {
  81. assert(i < ARRAY_LENGTH(reference));
  82. assert(e->i == reference[i]);
  83. i++;
  84. }
  85. assert(i == ARRAY_LENGTH(reference));
  86. i = 0;
  87. wl_list_for_each_reverse(e, &list, link) {
  88. assert(i < ARRAY_LENGTH(reference));
  89. assert(e->i == reference[ARRAY_LENGTH(reference) - i - 1]);
  90. i++;
  91. }
  92. assert(i == ARRAY_LENGTH(reference));
  93. }
  94. static int
  95. validate_list(struct wl_list *list, int *reference, int length)
  96. {
  97. struct element *e;
  98. int i;
  99. i = 0;
  100. wl_list_for_each(e, list, link) {
  101. if (i >= length)
  102. return 0;
  103. if (e->i != reference[i])
  104. return 0;
  105. i++;
  106. }
  107. if (i != length)
  108. return 0;
  109. return 1;
  110. }
  111. TEST(list_remove)
  112. {
  113. struct wl_list list;
  114. struct element e1, e2, e3;
  115. int reference1[] = { 17, 8888, 1000 }, reference2[] = { 17, 1000 };
  116. e1.i = 17;
  117. e2.i = 8888;
  118. e3.i = 1000;
  119. wl_list_init(&list);
  120. wl_list_insert(&list, &e1.link);
  121. wl_list_insert(list.prev, &e2.link);
  122. wl_list_insert(list.prev, &e3.link);
  123. assert(validate_list(&list, reference1, ARRAY_LENGTH(reference1)));
  124. wl_list_remove(&e2.link);
  125. assert(validate_list(&list, reference2, ARRAY_LENGTH(reference2)));
  126. }
  127. TEST(list_insert_list)
  128. {
  129. struct wl_list list, other;
  130. struct element e1, e2, e3, e4, e5, e6;
  131. int reference1[] = { 17, 8888, 1000 };
  132. int reference2[] = { 76543, 1, -500 };
  133. int reference3[] = { 17, 76543, 1, -500, 8888, 1000 };
  134. e1.i = 17;
  135. e2.i = 8888;
  136. e3.i = 1000;
  137. wl_list_init(&list);
  138. wl_list_insert(&list, &e1.link);
  139. wl_list_insert(list.prev, &e2.link);
  140. wl_list_insert(list.prev, &e3.link);
  141. assert(validate_list(&list, reference1, ARRAY_LENGTH(reference1)));
  142. e4.i = 76543;
  143. e5.i = 1;
  144. e6.i = -500;
  145. wl_list_init(&other);
  146. wl_list_insert(&other, &e4.link);
  147. wl_list_insert(other.prev, &e5.link);
  148. wl_list_insert(other.prev, &e6.link);
  149. assert(validate_list(&other, reference2, ARRAY_LENGTH(reference2)));
  150. wl_list_insert_list(list.next, &other);
  151. assert(validate_list(&list, reference3, ARRAY_LENGTH(reference3)));
  152. }