summaryrefslogtreecommitdiffstats
path: root/src/lib/test-seq-range-array.c
blob: ca0178a60c4717b46876b41abc80f4196a542a70 (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */

#include "test-lib.h"
#include "array.h"
#include "seq-range-array.h"


static void
boundaries_permute(uint32_t *input, unsigned int i, unsigned int count)
{
	ARRAY_TYPE(seq_range) range;
	const struct seq_range *seqs;
	unsigned int seqs_count;
	uint32_t tmp;
	unsigned int j;

	if (i+1 < count) {
		for (j = i; j < count; j++) {
			tmp = input[i]; input[i] = input[j]; input[j] = tmp;
			boundaries_permute(input, i+1, count);
			tmp = input[i]; input[i] = input[j]; input[j] = tmp;
		}
		return;
	}
	t_array_init(&range, 4);
	for (i = 0; i < count; i++)
		seq_range_array_add(&range, input[i]);
	seqs = array_get(&range, &seqs_count);
	test_assert(seqs_count == 2);
	test_assert(seqs[0].seq1 == 0);
	test_assert(seqs[0].seq2 == 1);
	test_assert(seqs[1].seq1 == (uint32_t)-2);
	test_assert(seqs[1].seq2 == (uint32_t)-1);
}

static void test_seq_range_array_add_boundaries(void)
{
	static uint32_t input[] = { 0, 1, (uint32_t)-2, (uint32_t)-1 };

	boundaries_permute(input, 0, N_ELEMENTS(input));
}

static void test_seq_range_array_add_merge(void)
{
	ARRAY_TYPE(seq_range) range;

	test_begin("seq_range_array_add() merging");
	t_array_init(&range, 8);
	seq_range_array_add(&range, 4);
	seq_range_array_add(&range, 1);
	seq_range_array_add(&range, 2);
	test_assert(array_count(&range) == 2);

	seq_range_array_add_range(&range, 1, (uint32_t)-1);
	test_assert(array_count(&range) == 1);
	seq_range_array_add_range(&range, 1, (uint32_t)-1);
	test_assert(array_count(&range) == 1);
	test_end();
}

static void test_seq_range_array_merge_n(void)
{
	ARRAY_TYPE(seq_range) src, dest, dest2;
	struct seq_range_iter iter;
	const uint32_t seqs[] = { 4, 5, 7, 8, 9, 11 };
	uint32_t seq;

	test_begin("seq_range_array_merge_n()");
	t_array_init(&src, 16);
	t_array_init(&dest, 16);
	t_array_init(&dest2, 16);
	for (unsigned int i = 0; i < N_ELEMENTS(seqs); i++)
		seq_range_array_add(&src, seqs[i]);

	for (unsigned int i = 0; i <= N_ELEMENTS(seqs); i++) {
		array_clear(&dest);
		array_clear(&dest2);
		seq_range_array_merge_n(&dest, &src, i);
		test_assert_idx(seq_range_count(&dest) == I_MIN(i, N_ELEMENTS(seqs)), i);

		seq_range_array_iter_init(&iter, &src);
		for (unsigned int j = 0; j < i; j++) {
			test_assert_idx(seq_range_array_iter_nth(&iter, j, &seq), i);
			seq_range_array_add(&dest2, seq);
		}
		seq_range_array_invert(&dest2, 1, UINT32_MAX);
		seq_range_array_intersect(&dest2, &dest);
		test_assert_idx(array_count(&dest2) == 0, i);
	}
	test_end();
}

static void test_seq_range_array_remove_nth(void)
{
	ARRAY_TYPE(seq_range) range;
	const struct seq_range *r;

	test_begin("seq_range_array_remove_nth()");
	t_array_init(&range, 8);
	seq_range_array_add_range(&range, 1, 5);
	seq_range_array_add(&range, 7);
	seq_range_array_add_range(&range, 10,20);
	test_assert(array_count(&range) == 3);

	seq_range_array_remove_nth(&range, 0, 2);
	r = array_front(&range); test_assert(r->seq1 == 3 && r->seq2 == 5);

	seq_range_array_remove_nth(&range, 1, 4);
	r = array_front(&range); test_assert(r->seq1 == 3 && r->seq2 == 3);
	r = array_idx(&range, 1); test_assert(r->seq1 == 11 && r->seq2 == 20);

	seq_range_array_remove_nth(&range, 5, (uint32_t)-1);
	r = array_idx(&range, 1); test_assert(r->seq1 == 11 && r->seq2 == 14);

	test_assert(array_count(&range) == 2);
	test_end();
}

static void test_seq_range_array_remove_range(void)
{
	ARRAY_TYPE(seq_range) range;
	const struct seq_range *r;

	test_begin("seq_range_array_remove_range()");
	t_array_init(&range, 8);

	seq_range_array_add_range(&range, 0, (uint32_t)-2);
	test_assert(seq_range_array_remove_range(&range, 0, 2) == 3);
	r = array_front(&range); test_assert(r->seq1 == 3 && r->seq2 == (uint32_t)-2);

	seq_range_array_add_range(&range, 0, (uint32_t)-2);
	test_assert(array_count(&range) == 1);
	test_assert(seq_range_array_remove_range(&range, 0, (uint32_t)-2) == UINT_MAX);
	test_assert(array_count(&range) == 0);

	seq_range_array_add_range(&range, (uint32_t)-1, (uint32_t)-1);
	test_assert(seq_range_array_remove_range(&range, (uint32_t)-1, (uint32_t)-1) == 1);
	test_assert(array_count(&range) == 0);

	seq_range_array_add_range(&range, (uint32_t)-1, (uint32_t)-1);
	test_assert(seq_range_array_remove_range(&range, 1, (uint32_t)-1) == 1);
	test_assert(array_count(&range) == 0);

	seq_range_array_add_range(&range, 1, 10);
	test_assert(seq_range_array_remove_range(&range, 5, 6) == 2);
	test_assert(seq_range_array_remove_range(&range, 4, 7) == 2);
	test_assert(seq_range_array_remove_range(&range, 1, 4) == 3);
	test_assert(seq_range_array_remove_range(&range, 8, 10) == 3);
	test_assert(array_count(&range) == 0);

	test_end();
}

static void test_seq_range_array_random(void)
{
#define SEQ_RANGE_TEST_BUFSIZE 100
#define SEQ_RANGE_TEST_COUNT 20000
	unsigned char shadowbuf[SEQ_RANGE_TEST_BUFSIZE];
	ARRAY_TYPE(seq_range) range;
	const struct seq_range *seqs;
	uint32_t seq1, seq2;
	unsigned int i, j, ret, ret2, count;
	int test = -1;

	ret = ret2 = 0;
	i_array_init(&range, 1);
	memset(shadowbuf, 0, sizeof(shadowbuf));
	for (i = 0; i < SEQ_RANGE_TEST_COUNT; i++) {
		seq1 = i_rand_limit(SEQ_RANGE_TEST_BUFSIZE);
		seq2 = seq1 + i_rand_limit(SEQ_RANGE_TEST_BUFSIZE - seq1);
		test = i_rand_limit(4);
		switch (test) {
		case 0:
			ret = seq_range_array_add(&range, seq1) ? 0 : 1; /* FALSE == added */
			ret2 = shadowbuf[seq1] == 0 ? 1 : 0;
			shadowbuf[seq1] = 1;
			break;
		case 1:
			ret = seq_range_array_add_range_count(&range, seq1, seq2);
			for (ret2 = 0; seq1 <= seq2; seq1++) {
				if (shadowbuf[seq1] == 0) {
					ret2++;
					shadowbuf[seq1] = 1;
				}
			}
			break;
		case 2:
			ret = seq_range_array_remove(&range, seq1) ? 1 : 0;
			ret2 = shadowbuf[seq1] != 0 ? 1 : 0;
			shadowbuf[seq1] = 0;
			break;
		case 3:
			ret = seq_range_array_remove_range(&range, seq1, seq2);
			for (ret2 = 0; seq1 <= seq2; seq1++) {
				if (shadowbuf[seq1] != 0) {
					ret2++;
					shadowbuf[seq1] = 0;
				}
			}
			break;
		}
		if (ret != ret2)
			break;

		seqs = array_get(&range, &count);
		for (j = 0, seq1 = 0; j < count; j++) {
			if (j > 0 && seqs[j-1].seq2+1 >= seqs[j].seq1)
				goto fail;
			for (; seq1 < seqs[j].seq1; seq1++) {
				if (shadowbuf[seq1] != 0)
					goto fail;
			}
			for (; seq1 <= seqs[j].seq2; seq1++) {
				if (shadowbuf[seq1] == 0)
					goto fail;
			}
		}
		i_assert(seq1 <= SEQ_RANGE_TEST_BUFSIZE);
		for (; seq1 < SEQ_RANGE_TEST_BUFSIZE; seq1++) {
			if (shadowbuf[seq1] != 0)
				goto fail;
		}
	}
fail:
	if (i == SEQ_RANGE_TEST_COUNT)
		test_out("seq_range_array random", TRUE);
	else {
		test_out_reason("seq_range_array random", FALSE,
			t_strdup_printf("round %u test %d failed", i, test));
	}
	array_free(&range);
}

static void test_seq_range_array_invert_minmax(uint32_t min, uint32_t max)
{
	ARRAY_TYPE(seq_range) range = ARRAY_INIT;
	struct seq_range_iter iter;
	unsigned int n, inverse_mask, mask_inside, mask_size = max-min+1;
	uint32_t seq;

	i_assert(mask_size <= sizeof(unsigned int)*8);
	t_array_init(&range, 16);
	for (unsigned int mask = 0; mask < mask_size; mask++) {
		array_clear(&range);
		for (unsigned int i = 0; i < mask_size; i++) {
			if ((mask & (1 << i)) != 0)
				seq_range_array_add(&range, min+i);
		}
		seq_range_array_invert(&range, min, max);

		inverse_mask = 0;
		seq_range_array_iter_init(&iter, &range); n = 0;
		while (seq_range_array_iter_nth(&iter, n++, &seq)) {
			test_assert(seq >= min && seq <= max);
			inverse_mask |= 1 << (seq-min);
		}
		mask_inside = ((1 << mask_size)-1);
		test_assert_idx((inverse_mask & ~mask_inside) == 0, mask);
		test_assert_idx(inverse_mask == (mask ^ mask_inside), mask);
	}
}

static void test_seq_range_array_invert(void)
{
	test_begin("seq_range_array_invert()");
	/* first numbers */
	for (unsigned int min = 0; min <= 7; min++) {
		for (unsigned int max = min; max <= 7; max++) T_BEGIN {
			test_seq_range_array_invert_minmax(min, max);
		} T_END;
	}
	/* last numbers */
	for (uint64_t min = 0xffffffff-7; min <= 0xffffffff; min++) {
		for (uint64_t max = min; max <= 0xffffffff; max++) T_BEGIN {
			test_seq_range_array_invert_minmax(min, max);
		} T_END;
	}
	test_end();
}

static void test_seq_range_array_invert_edges(void)
{
	static const struct {
		int64_t a_seq1, a_seq2, b_seq1, b_seq2;
		int64_t resa_seq1, resa_seq2, resb_seq1, resb_seq2;
	} tests[] = {
		{ -1, -1, -1, -1,
		  0, 0xffffffff, -1, -1 },
		/*{ 0, 0xffffffff, -1, -1, too large, will assert-crash
		  -1, -1, -1, -1 }, */
		{ 0, 0xfffffffe, -1, -1,
		  0xffffffff, 0xffffffff, -1, -1 },
		{ 1, 0xfffffffe, -1, -1,
		  0, 0, 0xffffffff, 0xffffffff },
		{ 1, 0xffffffff, -1, -1,
		  0, 0, -1, -1 },
		{ 0, 0, 0xffffffff, 0xffffffff,
		  1, 0xfffffffe, -1, -1 },
		{ 0xffffffff, 0xffffffff, -1, -1,
		  0, 0xfffffffe, -1, -1 },
	};
	ARRAY_TYPE(seq_range) range = ARRAY_INIT;
	const struct seq_range *result;
	unsigned int count;

	test_begin("seq_range_array_invert() edges");
	for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) T_BEGIN {
		t_array_init(&range, 10);
		if (tests[i].a_seq1 != -1)
			seq_range_array_add_range(&range, tests[i].a_seq1, tests[i].a_seq2);
		if (tests[i].b_seq1 != -1)
			seq_range_array_add_range(&range, tests[i].b_seq1, tests[i].b_seq2);
		seq_range_array_invert(&range, 0, 0xffffffff);

		result = array_get(&range, &count);
		if (tests[i].resa_seq1 == -1)
			test_assert_idx(count == 0, i);
		else {
			test_assert(result[0].seq1 == tests[i].resa_seq1);
			test_assert(result[0].seq2 == tests[i].resa_seq2);
			if (tests[i].resb_seq1 == -1)
				test_assert_idx(count == 1, i);
			else {
				test_assert(result[1].seq1 == tests[i].resb_seq1);
				test_assert(result[1].seq2 == tests[i].resb_seq2);
			}
		}
	} T_END;
	test_end();
}

static void test_seq_range_create(ARRAY_TYPE(seq_range) *array, uint8_t byte)
{
	unsigned int i;

	array_clear(array);
	for (i = 0; i < 8; i++) {
		if ((byte & (1 << i)) != 0)
			seq_range_array_add(array, i + 1);
	}
}

static void test_seq_range_array_have_common(void)
{
	ARRAY_TYPE(seq_range) arr1, arr2;
	unsigned int i, j;
	bool ret1, ret2, success = TRUE;

	t_array_init(&arr1, 8);
	t_array_init(&arr2, 8);
	for (i = 0; i < 256; i++) {
		test_seq_range_create(&arr1, i);
		for (j = 0; j < 256; j++) {
			test_seq_range_create(&arr2, j);
			ret1 = seq_range_array_have_common(&arr1, &arr2);
			ret2 = (i & j) != 0;
			if (ret1 != ret2)
				success = FALSE;
		}
	}
	test_out("seq_range_array_have_common()", success);
}

void test_seq_range_array(void)
{
	test_seq_range_array_add_boundaries();
	test_seq_range_array_add_merge();
	test_seq_range_array_merge_n();
	test_seq_range_array_remove_nth();
	test_seq_range_array_remove_range();
	test_seq_range_array_invert();
	test_seq_range_array_invert_edges();
	test_seq_range_array_have_common();
	test_seq_range_array_random();
}

enum fatal_test_state fatal_seq_range_array(unsigned int stage)
{
	ARRAY_TYPE(seq_range) arr;
	struct seq_range *range;

	t_array_init(&arr, 2);
	switch (stage) {
	case 0:
		test_begin("seq_range_array fatals");
		test_expect_fatal_string("!seq_range_is_overflowed(array)");
		seq_range_array_add_range(&arr, 0, (uint32_t)-1);
		return FATAL_TEST_FAILURE;
	case 1:
		seq_range_array_add_range(&arr, 1, (uint32_t)-1);
		test_expect_fatal_string("!seq_range_is_overflowed(array)");
		seq_range_array_add(&arr, 0);
		return FATAL_TEST_FAILURE;
	case 2:
		seq_range_array_add_range(&arr, 0, (uint32_t)-2);
		test_expect_fatal_string("!seq_range_is_overflowed(array)");
		seq_range_array_add(&arr, (uint32_t)-1);
		return FATAL_TEST_FAILURE;
	case 3:
		range = array_append_space(&arr);
		range->seq2 = (uint32_t)-1;
		test_expect_fatal_string("range->seq1 > 0 || range->seq2 < (uint32_t)-1");
		i_error("This shouldn't return: %u", seq_range_count(&arr));
		return FATAL_TEST_FAILURE;
	case 4:
		range = array_append_space(&arr);
		range->seq2 = (uint32_t)-2;
		test_assert(seq_range_count(&arr) == (uint32_t)-1);

		range = array_append_space(&arr);
		range->seq1 = (uint32_t)-2;
		range->seq2 = (uint32_t)-1;
		test_expect_fatal_string("UINT_MAX - seq_count >= seq_range_length(range)");
		i_error("This shouldn't return: %u", seq_range_count(&arr));
		return FATAL_TEST_FAILURE;
	}
	test_end();
	return FATAL_TEST_FINISHED;
}