1 #include <ccan/tally/tally.c>
2 #include <ccan/tap/tap.h>
6 unsigned int i
, max_step
;
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
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);
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);
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);
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);
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
60 for (i
= 0; i
< (1 << 7); i
++) {
62 for (j
= 0; j
< max_step
- 7; j
++) {
65 val
= bucket_min(-(ssize_t
)i
, j
, i
);
66 ok1(bucket_of(-(ssize_t
)i
, j
, val
) == i
);