lib: import ccan modules for tdb2
[samba.git] / lib / ccan / tally / test / run-total.c
1 #include <ccan/tally/tally.c>
2 #include <ccan/tap/tap.h>
3
4 int main(void)
5 {
6         struct tally *tally;
7         ssize_t total, overflow;
8         ssize_t min, max;
9
10         max = (ssize_t)~(1ULL << (sizeof(max)*CHAR_BIT - 1));
11         min = (ssize_t)(1ULL << (sizeof(max)*CHAR_BIT - 1));
12
13         plan_tests(15);
14
15         /* Simple case. */
16         tally = tally_new(0);
17         tally_add(tally, min);
18         ok1(tally_total(tally, NULL) == min);
19         ok1(tally_total(tally, &overflow) == min);
20         ok1(overflow == -1);
21
22         /* Underflow. */
23         tally_add(tally, min);
24         total = tally_total(tally, &overflow);
25         ok1(overflow == -1);
26         ok1((size_t)total == 0);
27         ok1(tally_total(tally, NULL) == min);
28         free(tally);
29
30         /* Simple case. */
31         tally = tally_new(0);
32         tally_add(tally, max);
33         ok1(tally_total(tally, NULL) == max);
34         ok1(tally_total(tally, &overflow) == max);
35         ok1(overflow == 0);
36
37         /* Overflow into sign bit... */
38         tally_add(tally, max);
39         total = tally_total(tally, &overflow);
40         ok1(overflow == 0);
41         ok1((size_t)total == (size_t)-2);
42         ok1(tally_total(tally, NULL) == max);
43
44         /* Overflow into upper size_t. */
45         tally_add(tally, max);
46         total = tally_total(tally, &overflow);
47         ok1(overflow == 1);
48         if (sizeof(size_t) == 4)
49                 ok1((size_t)total == 0x7FFFFFFD);
50         else if (sizeof(size_t) == 8)
51                 ok1((size_t)total == 0x7FFFFFFFFFFFFFFDULL);
52         ok1(tally_total(tally, NULL) == max);
53         free(tally);
54
55         return exit_status();
56 }