2
0

qr.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "test/jemalloc_test.h"
  2. /* Number of ring entries, in [2..26]. */
  3. #define NENTRIES 9
  4. /* Split index, in [1..NENTRIES). */
  5. #define SPLIT_INDEX 5
  6. typedef struct ring_s ring_t;
  7. struct ring_s {
  8. qr(ring_t) link;
  9. char id;
  10. };
  11. static void
  12. init_entries(ring_t *entries)
  13. {
  14. unsigned i;
  15. for (i = 0; i < NENTRIES; i++) {
  16. qr_new(&entries[i], link);
  17. entries[i].id = 'a' + i;
  18. }
  19. }
  20. static void
  21. test_independent_entries(ring_t *entries)
  22. {
  23. ring_t *t;
  24. unsigned i, j;
  25. for (i = 0; i < NENTRIES; i++) {
  26. j = 0;
  27. qr_foreach(t, &entries[i], link) {
  28. j++;
  29. }
  30. assert_u_eq(j, 1,
  31. "Iteration over single-element ring should visit precisely "
  32. "one element");
  33. }
  34. for (i = 0; i < NENTRIES; i++) {
  35. j = 0;
  36. qr_reverse_foreach(t, &entries[i], link) {
  37. j++;
  38. }
  39. assert_u_eq(j, 1,
  40. "Iteration over single-element ring should visit precisely "
  41. "one element");
  42. }
  43. for (i = 0; i < NENTRIES; i++) {
  44. t = qr_next(&entries[i], link);
  45. assert_ptr_eq(t, &entries[i],
  46. "Next element in single-element ring should be same as "
  47. "current element");
  48. }
  49. for (i = 0; i < NENTRIES; i++) {
  50. t = qr_prev(&entries[i], link);
  51. assert_ptr_eq(t, &entries[i],
  52. "Previous element in single-element ring should be same as "
  53. "current element");
  54. }
  55. }
  56. TEST_BEGIN(test_qr_one)
  57. {
  58. ring_t entries[NENTRIES];
  59. init_entries(entries);
  60. test_independent_entries(entries);
  61. }
  62. TEST_END
  63. static void
  64. test_entries_ring(ring_t *entries)
  65. {
  66. ring_t *t;
  67. unsigned i, j;
  68. for (i = 0; i < NENTRIES; i++) {
  69. j = 0;
  70. qr_foreach(t, &entries[i], link) {
  71. assert_c_eq(t->id, entries[(i+j) % NENTRIES].id,
  72. "Element id mismatch");
  73. j++;
  74. }
  75. }
  76. for (i = 0; i < NENTRIES; i++) {
  77. j = 0;
  78. qr_reverse_foreach(t, &entries[i], link) {
  79. assert_c_eq(t->id, entries[(NENTRIES+i-j-1) %
  80. NENTRIES].id, "Element id mismatch");
  81. j++;
  82. }
  83. }
  84. for (i = 0; i < NENTRIES; i++) {
  85. t = qr_next(&entries[i], link);
  86. assert_c_eq(t->id, entries[(i+1) % NENTRIES].id,
  87. "Element id mismatch");
  88. }
  89. for (i = 0; i < NENTRIES; i++) {
  90. t = qr_prev(&entries[i], link);
  91. assert_c_eq(t->id, entries[(NENTRIES+i-1) % NENTRIES].id,
  92. "Element id mismatch");
  93. }
  94. }
  95. TEST_BEGIN(test_qr_after_insert)
  96. {
  97. ring_t entries[NENTRIES];
  98. unsigned i;
  99. init_entries(entries);
  100. for (i = 1; i < NENTRIES; i++)
  101. qr_after_insert(&entries[i - 1], &entries[i], link);
  102. test_entries_ring(entries);
  103. }
  104. TEST_END
  105. TEST_BEGIN(test_qr_remove)
  106. {
  107. ring_t entries[NENTRIES];
  108. ring_t *t;
  109. unsigned i, j;
  110. init_entries(entries);
  111. for (i = 1; i < NENTRIES; i++)
  112. qr_after_insert(&entries[i - 1], &entries[i], link);
  113. for (i = 0; i < NENTRIES; i++) {
  114. j = 0;
  115. qr_foreach(t, &entries[i], link) {
  116. assert_c_eq(t->id, entries[i+j].id,
  117. "Element id mismatch");
  118. j++;
  119. }
  120. j = 0;
  121. qr_reverse_foreach(t, &entries[i], link) {
  122. assert_c_eq(t->id, entries[NENTRIES - 1 - j].id,
  123. "Element id mismatch");
  124. j++;
  125. }
  126. qr_remove(&entries[i], link);
  127. }
  128. test_independent_entries(entries);
  129. }
  130. TEST_END
  131. TEST_BEGIN(test_qr_before_insert)
  132. {
  133. ring_t entries[NENTRIES];
  134. ring_t *t;
  135. unsigned i, j;
  136. init_entries(entries);
  137. for (i = 1; i < NENTRIES; i++)
  138. qr_before_insert(&entries[i - 1], &entries[i], link);
  139. for (i = 0; i < NENTRIES; i++) {
  140. j = 0;
  141. qr_foreach(t, &entries[i], link) {
  142. assert_c_eq(t->id, entries[(NENTRIES+i-j) %
  143. NENTRIES].id, "Element id mismatch");
  144. j++;
  145. }
  146. }
  147. for (i = 0; i < NENTRIES; i++) {
  148. j = 0;
  149. qr_reverse_foreach(t, &entries[i], link) {
  150. assert_c_eq(t->id, entries[(i+j+1) % NENTRIES].id,
  151. "Element id mismatch");
  152. j++;
  153. }
  154. }
  155. for (i = 0; i < NENTRIES; i++) {
  156. t = qr_next(&entries[i], link);
  157. assert_c_eq(t->id, entries[(NENTRIES+i-1) % NENTRIES].id,
  158. "Element id mismatch");
  159. }
  160. for (i = 0; i < NENTRIES; i++) {
  161. t = qr_prev(&entries[i], link);
  162. assert_c_eq(t->id, entries[(i+1) % NENTRIES].id,
  163. "Element id mismatch");
  164. }
  165. }
  166. TEST_END
  167. static void
  168. test_split_entries(ring_t *entries)
  169. {
  170. ring_t *t;
  171. unsigned i, j;
  172. for (i = 0; i < NENTRIES; i++) {
  173. j = 0;
  174. qr_foreach(t, &entries[i], link) {
  175. if (i < SPLIT_INDEX) {
  176. assert_c_eq(t->id,
  177. entries[(i+j) % SPLIT_INDEX].id,
  178. "Element id mismatch");
  179. } else {
  180. assert_c_eq(t->id, entries[(i+j-SPLIT_INDEX) %
  181. (NENTRIES-SPLIT_INDEX) + SPLIT_INDEX].id,
  182. "Element id mismatch");
  183. }
  184. j++;
  185. }
  186. }
  187. }
  188. TEST_BEGIN(test_qr_meld_split)
  189. {
  190. ring_t entries[NENTRIES];
  191. unsigned i;
  192. init_entries(entries);
  193. for (i = 1; i < NENTRIES; i++)
  194. qr_after_insert(&entries[i - 1], &entries[i], link);
  195. qr_split(&entries[0], &entries[SPLIT_INDEX], link);
  196. test_split_entries(entries);
  197. qr_meld(&entries[0], &entries[SPLIT_INDEX], link);
  198. test_entries_ring(entries);
  199. qr_meld(&entries[0], &entries[SPLIT_INDEX], link);
  200. test_split_entries(entries);
  201. qr_split(&entries[0], &entries[SPLIT_INDEX], link);
  202. test_entries_ring(entries);
  203. qr_split(&entries[0], &entries[0], link);
  204. test_entries_ring(entries);
  205. qr_meld(&entries[0], &entries[0], link);
  206. test_entries_ring(entries);
  207. }
  208. TEST_END
  209. int
  210. main(void)
  211. {
  212. return (test(
  213. test_qr_one,
  214. test_qr_after_insert,
  215. test_qr_remove,
  216. test_qr_before_insert,
  217. test_qr_meld_split));
  218. }