lib: talloc: Allow destructors to reparent the object they're called on.
[Samba.git] / lib / ccan / tally / test / run-bucket_of.c
blob5e12725757f6d5f4cc8ac1d096894029e11cf85a
1 #include <ccan/tally/tally.c>
2 #include <ccan/tap/tap.h>
4 int main(void)
6 unsigned int i, max_step;
7 ssize_t min, max;
9 max = (ssize_t)~(1ULL << (sizeof(max)*CHAR_BIT - 1));
10 min = (ssize_t)(1ULL << (sizeof(max)*CHAR_BIT - 1));
11 max_step = sizeof(max)*CHAR_BIT;
13 plan_tests(2 + 100 + 10 + 5
14 + 2 + 100 + 5 + 4
15 + (1 << 7) * (max_step - 7));
17 /* Single step, single bucket == easy. */
18 ok1(bucket_of(0, 0, 0) == 0);
20 /* Double step, still in first bucket. */
21 ok1(bucket_of(0, 1, 0) == 0);
23 /* Step 8. */
24 for (i = 0; i < 100; i++)
25 ok1(bucket_of(0, 3, i) == i >> 3);
27 /* 10 values in 5 buckets, step 2. */
28 for (i = 0; i < 10; i++)
29 ok1(bucket_of(0, 1, i) == i >> 1);
31 /* Extreme cases. */
32 ok1(bucket_of(min, 0, min) == 0);
33 ok1(bucket_of(min, max_step-1, min) == 0);
34 ok1(bucket_of(min, max_step-1, max) == 1);
35 ok1(bucket_of(min, max_step, min) == 0);
36 ok1(bucket_of(min, max_step, max) == 0);
38 /* Now, bucket_min() should match: */
39 ok1(bucket_min(0, 0, 0) == 0);
41 /* Double step, val in first bucket still 0. */
42 ok1(bucket_min(0, 1, 0) == 0);
44 /* Step 8. */
45 for (i = 0; i < 100; i++)
46 ok1(bucket_min(0, 3, i) == i << 3);
48 /* 10 values in 5 buckets, step 2. */
49 for (i = 0; i < 5; i++)
50 ok1(bucket_min(0, 1, i) == i << 1);
52 /* Extreme cases. */
53 ok1(bucket_min(min, 0, 0) == min);
54 ok1(bucket_min(min, max_step-1, 0) == min);
55 ok1(bucket_min(min, max_step-1, 1) == 0);
56 ok1(bucket_min(min, max_step, 0) == min);
58 /* Now, vary step and number of buckets, but bucket_min and bucket_of
59 * must agree. */
60 for (i = 0; i < (1 << 7); i++) {
61 unsigned int j;
62 for (j = 0; j < max_step - 7; j++) {
63 ssize_t val;
65 val = bucket_min(-(ssize_t)i, j, i);
66 ok1(bucket_of(-(ssize_t)i, j, val) == i);
70 return exit_status();