summaryrefslogtreecommitdiff
path: root/deps/jemalloc/test/unit/ql.c
blob: f9130582f31534f5acf333e1499d954867bf403f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "test/jemalloc_test.h"

#include "jemalloc/internal/ql.h"

/* Number of ring entries, in [2..26]. */
#define NENTRIES 9

typedef struct list_s list_t;
typedef ql_head(list_t) list_head_t;

struct list_s {
	ql_elm(list_t) link;
	char id;
};

static void
test_empty_list(list_head_t *head) {
	list_t *t;
	unsigned i;

	expect_true(ql_empty(head), "Unexpected element for empty list");
	expect_ptr_null(ql_first(head), "Unexpected element for empty list");
	expect_ptr_null(ql_last(head, link),
	    "Unexpected element for empty list");

	i = 0;
	ql_foreach(t, head, link) {
		i++;
	}
	expect_u_eq(i, 0, "Unexpected element for empty list");

	i = 0;
	ql_reverse_foreach(t, head, link) {
		i++;
	}
	expect_u_eq(i, 0, "Unexpected element for empty list");
}

TEST_BEGIN(test_ql_empty) {
	list_head_t head;

	ql_new(&head);
	test_empty_list(&head);
}
TEST_END

static void
init_entries(list_t *entries, unsigned nentries) {
	unsigned i;

	for (i = 0; i < nentries; i++) {
		entries[i].id = 'a' + i;
		ql_elm_new(&entries[i], link);
	}
}

static void
test_entries_list(list_head_t *head, list_t *entries, unsigned nentries) {
	list_t *t;
	unsigned i;

	expect_false(ql_empty(head), "List should not be empty");
	expect_c_eq(ql_first(head)->id, entries[0].id, "Element id mismatch");
	expect_c_eq(ql_last(head, link)->id, entries[nentries-1].id,
	    "Element id mismatch");

	i = 0;
	ql_foreach(t, head, link) {
		expect_c_eq(t->id, entries[i].id, "Element id mismatch");
		i++;
	}

	i = 0;
	ql_reverse_foreach(t, head, link) {
		expect_c_eq(t->id, entries[nentries-i-1].id,
		    "Element id mismatch");
		i++;
	}

	for (i = 0; i < nentries-1; i++) {
		t = ql_next(head, &entries[i], link);
		expect_c_eq(t->id, entries[i+1].id, "Element id mismatch");
	}
	expect_ptr_null(ql_next(head, &entries[nentries-1], link),
	    "Unexpected element");

	expect_ptr_null(ql_prev(head, &entries[0], link), "Unexpected element");
	for (i = 1; i < nentries; i++) {
		t = ql_prev(head, &entries[i], link);
		expect_c_eq(t->id, entries[i-1].id, "Element id mismatch");
	}
}

TEST_BEGIN(test_ql_tail_insert) {
	list_head_t head;
	list_t entries[NENTRIES];
	unsigned i;

	ql_new(&head);
	init_entries(entries, sizeof(entries)/sizeof(list_t));
	for (i = 0; i < NENTRIES; i++) {
		ql_tail_insert(&head, &entries[i], link);
	}

	test_entries_list(&head, entries, NENTRIES);
}
TEST_END

TEST_BEGIN(test_ql_tail_remove) {
	list_head_t head;
	list_t entries[NENTRIES];
	unsigned i;

	ql_new(&head);
	init_entries(entries, sizeof(entries)/sizeof(list_t));
	for (i = 0; i < NENTRIES; i++) {
		ql_tail_insert(&head, &entries[i], link);
	}

	for (i = 0; i < NENTRIES; i++) {
		test_entries_list(&head, entries, NENTRIES-i);
		ql_tail_remove(&head, list_t, link);
	}
	test_empty_list(&head);
}
TEST_END

TEST_BEGIN(test_ql_head_insert) {
	list_head_t head;
	list_t entries[NENTRIES];
	unsigned i;

	ql_new(&head);
	init_entries(entries, sizeof(entries)/sizeof(list_t));
	for (i = 0; i < NENTRIES; i++) {
		ql_head_insert(&head, &entries[NENTRIES-i-1], link);
	}

	test_entries_list(&head, entries, NENTRIES);
}
TEST_END

TEST_BEGIN(test_ql_head_remove) {
	list_head_t head;
	list_t entries[NENTRIES];
	unsigned i;

	ql_new(&head);
	init_entries(entries, sizeof(entries)/sizeof(list_t));
	for (i = 0; i < NENTRIES; i++) {
		ql_head_insert(&head, &entries[NENTRIES-i-1], link);
	}

	for (i = 0; i < NENTRIES; i++) {
		test_entries_list(&head, &entries[i], NENTRIES-i);
		ql_head_remove(&head, list_t, link);
	}
	test_empty_list(&head);
}
TEST_END

TEST_BEGIN(test_ql_insert) {
	list_head_t head;
	list_t entries[8];
	list_t *a, *b, *c, *d, *e, *f, *g, *h;

	ql_new(&head);
	init_entries(entries, sizeof(entries)/sizeof(list_t));
	a = &entries[0];
	b = &entries[1];
	c = &entries[2];
	d = &entries[3];
	e = &entries[4];
	f = &entries[5];
	g = &entries[6];
	h = &entries[7];

	/*
	 * ql_remove(), ql_before_insert(), and ql_after_insert() are used
	 * internally by other macros that are already tested, so there's no
	 * need to test them completely.  However, insertion/deletion from the
	 * middle of lists is not otherwise tested; do so here.
	 */
	ql_tail_insert(&head, f, link);
	ql_before_insert(&head, f, b, link);
	ql_before_insert(&head, f, c, link);
	ql_after_insert(f, h, link);
	ql_after_insert(f, g, link);
	ql_before_insert(&head, b, a, link);
	ql_after_insert(c, d, link);
	ql_before_insert(&head, f, e, link);

	test_entries_list(&head, entries, sizeof(entries)/sizeof(list_t));
}
TEST_END

static void
test_concat_split_entries(list_t *entries, unsigned nentries_a,
    unsigned nentries_b) {
	init_entries(entries, nentries_a + nentries_b);

	list_head_t head_a;
	ql_new(&head_a);
	for (unsigned i = 0; i < nentries_a; i++) {
		ql_tail_insert(&head_a, &entries[i], link);
	}
	if (nentries_a == 0) {
		test_empty_list(&head_a);
	} else {
		test_entries_list(&head_a, entries, nentries_a);
	}

	list_head_t head_b;
	ql_new(&head_b);
	for (unsigned i = 0; i < nentries_b; i++) {
		ql_tail_insert(&head_b, &entries[nentries_a + i], link);
	}
	if (nentries_b == 0) {
		test_empty_list(&head_b);
	} else {
		test_entries_list(&head_b, entries + nentries_a, nentries_b);
	}

	ql_concat(&head_a, &head_b, link);
	if (nentries_a + nentries_b == 0) {
		test_empty_list(&head_a);
	} else {
		test_entries_list(&head_a, entries, nentries_a + nentries_b);
	}
	test_empty_list(&head_b);

	if (nentries_b == 0) {
		return;
	}

	list_head_t head_c;
	ql_split(&head_a, &entries[nentries_a], &head_c, link);
	if (nentries_a == 0) {
		test_empty_list(&head_a);
	} else {
		test_entries_list(&head_a, entries, nentries_a);
	}
	test_entries_list(&head_c, entries + nentries_a, nentries_b);
}

TEST_BEGIN(test_ql_concat_split) {
	list_t entries[NENTRIES];

	test_concat_split_entries(entries, 0, 0);

	test_concat_split_entries(entries, 0, 1);
	test_concat_split_entries(entries, 1, 0);

	test_concat_split_entries(entries, 0, NENTRIES);
	test_concat_split_entries(entries, 1, NENTRIES - 1);
	test_concat_split_entries(entries, NENTRIES / 2,
	    NENTRIES - NENTRIES / 2);
	test_concat_split_entries(entries, NENTRIES - 1, 1);
	test_concat_split_entries(entries, NENTRIES, 0);
}
TEST_END

TEST_BEGIN(test_ql_rotate) {
	list_head_t head;
	list_t entries[NENTRIES];
	unsigned i;

	ql_new(&head);
	init_entries(entries, sizeof(entries)/sizeof(list_t));
	for (i = 0; i < NENTRIES; i++) {
		ql_tail_insert(&head, &entries[i], link);
	}

	char head_id = ql_first(&head)->id;
	for (i = 0; i < NENTRIES; i++) {
		assert_c_eq(ql_first(&head)->id, head_id, "");
		ql_rotate(&head, link);
		assert_c_eq(ql_last(&head, link)->id, head_id, "");
		head_id++;
	}
	test_entries_list(&head, entries, NENTRIES);
}
TEST_END

TEST_BEGIN(test_ql_move) {
	list_head_t head_dest, head_src;
	list_t entries[NENTRIES];
	unsigned i;

	ql_new(&head_src);
	ql_move(&head_dest, &head_src);
	test_empty_list(&head_src);
	test_empty_list(&head_dest);

	init_entries(entries, sizeof(entries)/sizeof(list_t));
	for (i = 0; i < NENTRIES; i++) {
		ql_tail_insert(&head_src, &entries[i], link);
	}
	ql_move(&head_dest, &head_src);
	test_empty_list(&head_src);
	test_entries_list(&head_dest, entries, NENTRIES);
}
TEST_END

int
main(void) {
	return test(
	    test_ql_empty,
	    test_ql_tail_insert,
	    test_ql_tail_remove,
	    test_ql_head_insert,
	    test_ql_head_remove,
	    test_ql_insert,
	    test_ql_concat_split,
	    test_ql_rotate,
	    test_ql_move);
}