summaryrefslogtreecommitdiff
path: root/ccan/bitmap/test/run-ranges.c
blob: 5ba383caf534a1690d60354aa8d901acca1d5fce (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
#include <ccan/bitmap/bitmap.h>
#include <ccan/tap/tap.h>
#include <ccan/array_size/array_size.h>
#include <ccan/foreach/foreach.h>

#include <ccan/bitmap/bitmap.c>

int bitmap_sizes[] = {
	1, 2, 3, 4, 5, 6, 7, 8,
	16, 24, 32, 64, 256,
	/*
	 * Don't put too big sizes in here, or it will take forever to
	 * run under valgrind (the test is O(n^3)).
	 */
};
#define NSIZES ARRAY_SIZE(bitmap_sizes)
#define NTESTS 2

static void test_size(int nbits)
{
	BITMAP_DECLARE(bitmap, nbits);
	uint32_t marker = 0xdeadbeef;
	int i, j, k;
	bool wrong;

	for (i = 0; i < nbits; i++) {
		for (j = i; j <= nbits; j++) {
			bitmap_zero(bitmap, nbits);
			bitmap_fill_range(bitmap, i, j);

			wrong = false;
			for (k = 0; k < nbits; k++) {
				bool inrange = (k >= i) && (k < j);
				wrong = wrong || (bitmap_test_bit(bitmap, k) != inrange);
			}
			ok1(!wrong);
		}
	}

	for (i = 0; i < nbits; i++) {
		for (j = i; j <= nbits; j++) {
			bitmap_fill(bitmap, nbits);
			bitmap_zero_range(bitmap, i, j);

			wrong = false;
			for (k = 0; k < nbits; k++) {
				bool inrange = (k >= i) && (k < j);
				wrong = wrong || (bitmap_test_bit(bitmap, k) == inrange);
			}
			ok1(!wrong);
		}
	}

	ok1(marker == 0xdeadbeef);
}

int main(void)
{
	int totaltests = 0;
	int i;

	for (i = 0; i < NSIZES; i++) {
		int size = bitmap_sizes[i];

		/* Summing the arithmetic series gives: */
		totaltests += size*(size + 3) + 1;
	}
	plan_tests(totaltests);

	for (i = 0; i < NSIZES; i++) {
		diag("Testing %d-bit bitmap", bitmap_sizes[i]);
		test_size(bitmap_sizes[i]);
	}

	/* This exits depending on whether all tests passed */
	return exit_status();
}