summaryrefslogtreecommitdiff
path: root/lib/ccan/tally/test/run-bucket_of.c
blob: 5e12725757f6d5f4cc8ac1d096894029e11cf85a (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
#include <ccan/tally/tally.c>
#include <ccan/tap/tap.h>

int main(void)
{
	unsigned int i, max_step;
	ssize_t min, max;

	max = (ssize_t)~(1ULL << (sizeof(max)*CHAR_BIT - 1));
	min = (ssize_t)(1ULL << (sizeof(max)*CHAR_BIT - 1));
	max_step = sizeof(max)*CHAR_BIT;

	plan_tests(2 + 100 + 10 + 5
		   + 2 + 100 + 5 + 4
		   + (1 << 7) * (max_step - 7));

	/* Single step, single bucket == easy. */
	ok1(bucket_of(0, 0, 0) == 0);

	/* Double step, still in first bucket. */
	ok1(bucket_of(0, 1, 0) == 0);

	/* Step 8. */
	for (i = 0; i < 100; i++)
		ok1(bucket_of(0, 3, i) == i >> 3);

	/* 10 values in 5 buckets, step 2. */
	for (i = 0; i < 10; i++)
		ok1(bucket_of(0, 1, i) == i >> 1);

	/* Extreme cases. */
	ok1(bucket_of(min, 0, min) == 0);
	ok1(bucket_of(min, max_step-1, min) == 0);
	ok1(bucket_of(min, max_step-1, max) == 1);
	ok1(bucket_of(min, max_step, min) == 0);
	ok1(bucket_of(min, max_step, max) == 0);

	/* Now, bucket_min() should match: */
	ok1(bucket_min(0, 0, 0) == 0);

	/* Double step, val in first bucket still 0. */
	ok1(bucket_min(0, 1, 0) == 0);

	/* Step 8. */
	for (i = 0; i < 100; i++)
		ok1(bucket_min(0, 3, i) == i << 3);

	/* 10 values in 5 buckets, step 2. */
	for (i = 0; i < 5; i++)
		ok1(bucket_min(0, 1, i) == i << 1);

	/* Extreme cases. */
	ok1(bucket_min(min, 0, 0) == min);
	ok1(bucket_min(min, max_step-1, 0) == min);
	ok1(bucket_min(min, max_step-1, 1) == 0);
	ok1(bucket_min(min, max_step, 0) == min);

	/* Now, vary step and number of buckets, but bucket_min and bucket_of
	 * must agree. */
	for (i = 0; i < (1 << 7); i++) {
		unsigned int j;
		for (j = 0; j < max_step - 7; j++) {
			ssize_t val;

			val = bucket_min(-(ssize_t)i, j, i);
			ok1(bucket_of(-(ssize_t)i, j, val) == i);
		}
	}

	return exit_status();
}