1*1208bc7eSAndroid Build Coastguard Worker #include "test/jemalloc_test.h"
2*1208bc7eSAndroid Build Coastguard Worker
3*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/ql.h"
4*1208bc7eSAndroid Build Coastguard Worker
5*1208bc7eSAndroid Build Coastguard Worker /* Number of ring entries, in [2..26]. */
6*1208bc7eSAndroid Build Coastguard Worker #define NENTRIES 9
7*1208bc7eSAndroid Build Coastguard Worker
8*1208bc7eSAndroid Build Coastguard Worker typedef struct list_s list_t;
9*1208bc7eSAndroid Build Coastguard Worker typedef ql_head(list_t) list_head_t;
10*1208bc7eSAndroid Build Coastguard Worker
11*1208bc7eSAndroid Build Coastguard Worker struct list_s {
12*1208bc7eSAndroid Build Coastguard Worker ql_elm(list_t) link;
13*1208bc7eSAndroid Build Coastguard Worker char id;
14*1208bc7eSAndroid Build Coastguard Worker };
15*1208bc7eSAndroid Build Coastguard Worker
16*1208bc7eSAndroid Build Coastguard Worker static void
test_empty_list(list_head_t * head)17*1208bc7eSAndroid Build Coastguard Worker test_empty_list(list_head_t *head) {
18*1208bc7eSAndroid Build Coastguard Worker list_t *t;
19*1208bc7eSAndroid Build Coastguard Worker unsigned i;
20*1208bc7eSAndroid Build Coastguard Worker
21*1208bc7eSAndroid Build Coastguard Worker assert_ptr_null(ql_first(head), "Unexpected element for empty list");
22*1208bc7eSAndroid Build Coastguard Worker assert_ptr_null(ql_last(head, link),
23*1208bc7eSAndroid Build Coastguard Worker "Unexpected element for empty list");
24*1208bc7eSAndroid Build Coastguard Worker
25*1208bc7eSAndroid Build Coastguard Worker i = 0;
26*1208bc7eSAndroid Build Coastguard Worker ql_foreach(t, head, link) {
27*1208bc7eSAndroid Build Coastguard Worker i++;
28*1208bc7eSAndroid Build Coastguard Worker }
29*1208bc7eSAndroid Build Coastguard Worker assert_u_eq(i, 0, "Unexpected element for empty list");
30*1208bc7eSAndroid Build Coastguard Worker
31*1208bc7eSAndroid Build Coastguard Worker i = 0;
32*1208bc7eSAndroid Build Coastguard Worker ql_reverse_foreach(t, head, link) {
33*1208bc7eSAndroid Build Coastguard Worker i++;
34*1208bc7eSAndroid Build Coastguard Worker }
35*1208bc7eSAndroid Build Coastguard Worker assert_u_eq(i, 0, "Unexpected element for empty list");
36*1208bc7eSAndroid Build Coastguard Worker }
37*1208bc7eSAndroid Build Coastguard Worker
TEST_BEGIN(test_ql_empty)38*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_ql_empty) {
39*1208bc7eSAndroid Build Coastguard Worker list_head_t head;
40*1208bc7eSAndroid Build Coastguard Worker
41*1208bc7eSAndroid Build Coastguard Worker ql_new(&head);
42*1208bc7eSAndroid Build Coastguard Worker test_empty_list(&head);
43*1208bc7eSAndroid Build Coastguard Worker }
44*1208bc7eSAndroid Build Coastguard Worker TEST_END
45*1208bc7eSAndroid Build Coastguard Worker
46*1208bc7eSAndroid Build Coastguard Worker static void
init_entries(list_t * entries,unsigned nentries)47*1208bc7eSAndroid Build Coastguard Worker init_entries(list_t *entries, unsigned nentries) {
48*1208bc7eSAndroid Build Coastguard Worker unsigned i;
49*1208bc7eSAndroid Build Coastguard Worker
50*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < nentries; i++) {
51*1208bc7eSAndroid Build Coastguard Worker entries[i].id = 'a' + i;
52*1208bc7eSAndroid Build Coastguard Worker ql_elm_new(&entries[i], link);
53*1208bc7eSAndroid Build Coastguard Worker }
54*1208bc7eSAndroid Build Coastguard Worker }
55*1208bc7eSAndroid Build Coastguard Worker
56*1208bc7eSAndroid Build Coastguard Worker static void
test_entries_list(list_head_t * head,list_t * entries,unsigned nentries)57*1208bc7eSAndroid Build Coastguard Worker test_entries_list(list_head_t *head, list_t *entries, unsigned nentries) {
58*1208bc7eSAndroid Build Coastguard Worker list_t *t;
59*1208bc7eSAndroid Build Coastguard Worker unsigned i;
60*1208bc7eSAndroid Build Coastguard Worker
61*1208bc7eSAndroid Build Coastguard Worker assert_c_eq(ql_first(head)->id, entries[0].id, "Element id mismatch");
62*1208bc7eSAndroid Build Coastguard Worker assert_c_eq(ql_last(head, link)->id, entries[nentries-1].id,
63*1208bc7eSAndroid Build Coastguard Worker "Element id mismatch");
64*1208bc7eSAndroid Build Coastguard Worker
65*1208bc7eSAndroid Build Coastguard Worker i = 0;
66*1208bc7eSAndroid Build Coastguard Worker ql_foreach(t, head, link) {
67*1208bc7eSAndroid Build Coastguard Worker assert_c_eq(t->id, entries[i].id, "Element id mismatch");
68*1208bc7eSAndroid Build Coastguard Worker i++;
69*1208bc7eSAndroid Build Coastguard Worker }
70*1208bc7eSAndroid Build Coastguard Worker
71*1208bc7eSAndroid Build Coastguard Worker i = 0;
72*1208bc7eSAndroid Build Coastguard Worker ql_reverse_foreach(t, head, link) {
73*1208bc7eSAndroid Build Coastguard Worker assert_c_eq(t->id, entries[nentries-i-1].id,
74*1208bc7eSAndroid Build Coastguard Worker "Element id mismatch");
75*1208bc7eSAndroid Build Coastguard Worker i++;
76*1208bc7eSAndroid Build Coastguard Worker }
77*1208bc7eSAndroid Build Coastguard Worker
78*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < nentries-1; i++) {
79*1208bc7eSAndroid Build Coastguard Worker t = ql_next(head, &entries[i], link);
80*1208bc7eSAndroid Build Coastguard Worker assert_c_eq(t->id, entries[i+1].id, "Element id mismatch");
81*1208bc7eSAndroid Build Coastguard Worker }
82*1208bc7eSAndroid Build Coastguard Worker assert_ptr_null(ql_next(head, &entries[nentries-1], link),
83*1208bc7eSAndroid Build Coastguard Worker "Unexpected element");
84*1208bc7eSAndroid Build Coastguard Worker
85*1208bc7eSAndroid Build Coastguard Worker assert_ptr_null(ql_prev(head, &entries[0], link), "Unexpected element");
86*1208bc7eSAndroid Build Coastguard Worker for (i = 1; i < nentries; i++) {
87*1208bc7eSAndroid Build Coastguard Worker t = ql_prev(head, &entries[i], link);
88*1208bc7eSAndroid Build Coastguard Worker assert_c_eq(t->id, entries[i-1].id, "Element id mismatch");
89*1208bc7eSAndroid Build Coastguard Worker }
90*1208bc7eSAndroid Build Coastguard Worker }
91*1208bc7eSAndroid Build Coastguard Worker
TEST_BEGIN(test_ql_tail_insert)92*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_ql_tail_insert) {
93*1208bc7eSAndroid Build Coastguard Worker list_head_t head;
94*1208bc7eSAndroid Build Coastguard Worker list_t entries[NENTRIES];
95*1208bc7eSAndroid Build Coastguard Worker unsigned i;
96*1208bc7eSAndroid Build Coastguard Worker
97*1208bc7eSAndroid Build Coastguard Worker ql_new(&head);
98*1208bc7eSAndroid Build Coastguard Worker init_entries(entries, sizeof(entries)/sizeof(list_t));
99*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < NENTRIES; i++) {
100*1208bc7eSAndroid Build Coastguard Worker ql_tail_insert(&head, &entries[i], link);
101*1208bc7eSAndroid Build Coastguard Worker }
102*1208bc7eSAndroid Build Coastguard Worker
103*1208bc7eSAndroid Build Coastguard Worker test_entries_list(&head, entries, NENTRIES);
104*1208bc7eSAndroid Build Coastguard Worker }
105*1208bc7eSAndroid Build Coastguard Worker TEST_END
106*1208bc7eSAndroid Build Coastguard Worker
TEST_BEGIN(test_ql_tail_remove)107*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_ql_tail_remove) {
108*1208bc7eSAndroid Build Coastguard Worker list_head_t head;
109*1208bc7eSAndroid Build Coastguard Worker list_t entries[NENTRIES];
110*1208bc7eSAndroid Build Coastguard Worker unsigned i;
111*1208bc7eSAndroid Build Coastguard Worker
112*1208bc7eSAndroid Build Coastguard Worker ql_new(&head);
113*1208bc7eSAndroid Build Coastguard Worker init_entries(entries, sizeof(entries)/sizeof(list_t));
114*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < NENTRIES; i++) {
115*1208bc7eSAndroid Build Coastguard Worker ql_tail_insert(&head, &entries[i], link);
116*1208bc7eSAndroid Build Coastguard Worker }
117*1208bc7eSAndroid Build Coastguard Worker
118*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < NENTRIES; i++) {
119*1208bc7eSAndroid Build Coastguard Worker test_entries_list(&head, entries, NENTRIES-i);
120*1208bc7eSAndroid Build Coastguard Worker ql_tail_remove(&head, list_t, link);
121*1208bc7eSAndroid Build Coastguard Worker }
122*1208bc7eSAndroid Build Coastguard Worker test_empty_list(&head);
123*1208bc7eSAndroid Build Coastguard Worker }
124*1208bc7eSAndroid Build Coastguard Worker TEST_END
125*1208bc7eSAndroid Build Coastguard Worker
TEST_BEGIN(test_ql_head_insert)126*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_ql_head_insert) {
127*1208bc7eSAndroid Build Coastguard Worker list_head_t head;
128*1208bc7eSAndroid Build Coastguard Worker list_t entries[NENTRIES];
129*1208bc7eSAndroid Build Coastguard Worker unsigned i;
130*1208bc7eSAndroid Build Coastguard Worker
131*1208bc7eSAndroid Build Coastguard Worker ql_new(&head);
132*1208bc7eSAndroid Build Coastguard Worker init_entries(entries, sizeof(entries)/sizeof(list_t));
133*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < NENTRIES; i++) {
134*1208bc7eSAndroid Build Coastguard Worker ql_head_insert(&head, &entries[NENTRIES-i-1], link);
135*1208bc7eSAndroid Build Coastguard Worker }
136*1208bc7eSAndroid Build Coastguard Worker
137*1208bc7eSAndroid Build Coastguard Worker test_entries_list(&head, entries, NENTRIES);
138*1208bc7eSAndroid Build Coastguard Worker }
139*1208bc7eSAndroid Build Coastguard Worker TEST_END
140*1208bc7eSAndroid Build Coastguard Worker
TEST_BEGIN(test_ql_head_remove)141*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_ql_head_remove) {
142*1208bc7eSAndroid Build Coastguard Worker list_head_t head;
143*1208bc7eSAndroid Build Coastguard Worker list_t entries[NENTRIES];
144*1208bc7eSAndroid Build Coastguard Worker unsigned i;
145*1208bc7eSAndroid Build Coastguard Worker
146*1208bc7eSAndroid Build Coastguard Worker ql_new(&head);
147*1208bc7eSAndroid Build Coastguard Worker init_entries(entries, sizeof(entries)/sizeof(list_t));
148*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < NENTRIES; i++) {
149*1208bc7eSAndroid Build Coastguard Worker ql_head_insert(&head, &entries[NENTRIES-i-1], link);
150*1208bc7eSAndroid Build Coastguard Worker }
151*1208bc7eSAndroid Build Coastguard Worker
152*1208bc7eSAndroid Build Coastguard Worker for (i = 0; i < NENTRIES; i++) {
153*1208bc7eSAndroid Build Coastguard Worker test_entries_list(&head, &entries[i], NENTRIES-i);
154*1208bc7eSAndroid Build Coastguard Worker ql_head_remove(&head, list_t, link);
155*1208bc7eSAndroid Build Coastguard Worker }
156*1208bc7eSAndroid Build Coastguard Worker test_empty_list(&head);
157*1208bc7eSAndroid Build Coastguard Worker }
158*1208bc7eSAndroid Build Coastguard Worker TEST_END
159*1208bc7eSAndroid Build Coastguard Worker
TEST_BEGIN(test_ql_insert)160*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_ql_insert) {
161*1208bc7eSAndroid Build Coastguard Worker list_head_t head;
162*1208bc7eSAndroid Build Coastguard Worker list_t entries[8];
163*1208bc7eSAndroid Build Coastguard Worker list_t *a, *b, *c, *d, *e, *f, *g, *h;
164*1208bc7eSAndroid Build Coastguard Worker
165*1208bc7eSAndroid Build Coastguard Worker ql_new(&head);
166*1208bc7eSAndroid Build Coastguard Worker init_entries(entries, sizeof(entries)/sizeof(list_t));
167*1208bc7eSAndroid Build Coastguard Worker a = &entries[0];
168*1208bc7eSAndroid Build Coastguard Worker b = &entries[1];
169*1208bc7eSAndroid Build Coastguard Worker c = &entries[2];
170*1208bc7eSAndroid Build Coastguard Worker d = &entries[3];
171*1208bc7eSAndroid Build Coastguard Worker e = &entries[4];
172*1208bc7eSAndroid Build Coastguard Worker f = &entries[5];
173*1208bc7eSAndroid Build Coastguard Worker g = &entries[6];
174*1208bc7eSAndroid Build Coastguard Worker h = &entries[7];
175*1208bc7eSAndroid Build Coastguard Worker
176*1208bc7eSAndroid Build Coastguard Worker /*
177*1208bc7eSAndroid Build Coastguard Worker * ql_remove(), ql_before_insert(), and ql_after_insert() are used
178*1208bc7eSAndroid Build Coastguard Worker * internally by other macros that are already tested, so there's no
179*1208bc7eSAndroid Build Coastguard Worker * need to test them completely. However, insertion/deletion from the
180*1208bc7eSAndroid Build Coastguard Worker * middle of lists is not otherwise tested; do so here.
181*1208bc7eSAndroid Build Coastguard Worker */
182*1208bc7eSAndroid Build Coastguard Worker ql_tail_insert(&head, f, link);
183*1208bc7eSAndroid Build Coastguard Worker ql_before_insert(&head, f, b, link);
184*1208bc7eSAndroid Build Coastguard Worker ql_before_insert(&head, f, c, link);
185*1208bc7eSAndroid Build Coastguard Worker ql_after_insert(f, h, link);
186*1208bc7eSAndroid Build Coastguard Worker ql_after_insert(f, g, link);
187*1208bc7eSAndroid Build Coastguard Worker ql_before_insert(&head, b, a, link);
188*1208bc7eSAndroid Build Coastguard Worker ql_after_insert(c, d, link);
189*1208bc7eSAndroid Build Coastguard Worker ql_before_insert(&head, f, e, link);
190*1208bc7eSAndroid Build Coastguard Worker
191*1208bc7eSAndroid Build Coastguard Worker test_entries_list(&head, entries, sizeof(entries)/sizeof(list_t));
192*1208bc7eSAndroid Build Coastguard Worker }
193*1208bc7eSAndroid Build Coastguard Worker TEST_END
194*1208bc7eSAndroid Build Coastguard Worker
195*1208bc7eSAndroid Build Coastguard Worker int
main(void)196*1208bc7eSAndroid Build Coastguard Worker main(void) {
197*1208bc7eSAndroid Build Coastguard Worker return test(
198*1208bc7eSAndroid Build Coastguard Worker test_ql_empty,
199*1208bc7eSAndroid Build Coastguard Worker test_ql_tail_insert,
200*1208bc7eSAndroid Build Coastguard Worker test_ql_tail_remove,
201*1208bc7eSAndroid Build Coastguard Worker test_ql_head_insert,
202*1208bc7eSAndroid Build Coastguard Worker test_ql_head_remove,
203*1208bc7eSAndroid Build Coastguard Worker test_ql_insert);
204*1208bc7eSAndroid Build Coastguard Worker }
205