htable: HTABLE_INITIALIZER() for static initialization.
[ccan] / ccan / likely / test / run-debug.c
1 #define CCAN_LIKELY_DEBUG 1
2 #include <ccan/likely/likely.c>
3 #include <ccan/likely/likely.h>
4 #include <ccan/tap/tap.h>
5 #include <stdlib.h>
6
7 static bool one_seems_likely(unsigned int val)
8 {
9         if (likely(val == 1))
10                 return true;
11         return false;
12 }
13
14 static bool one_seems_unlikely(unsigned int val)
15 {
16         if (unlikely(val == 1))
17                 return true;
18         return false;
19 }
20
21 static bool likely_one_unlikely_two(unsigned int val1, unsigned int val2)
22 {
23         /* Same line, check we don't get confused! */
24         if (likely(val1 == 1) && unlikely(val2 == 2))
25                 return true;
26         return false;
27 }
28
29 int main(int argc, char *argv[])
30 {
31         const char *bad;
32
33         plan_tests(13);
34
35         /* Correct guesses. */
36         one_seems_likely(1);
37         ok1(likely_stats(0, 90) == NULL);
38         one_seems_unlikely(2);
39         ok1(likely_stats(0, 90) == NULL);
40
41         /* Incorrect guesses. */
42         one_seems_likely(0);
43         one_seems_likely(2);
44         /* Hasn't been hit 4 times, so this fails */
45         ok1(!likely_stats(4, 90));
46         bad = likely_stats(3, 90);
47         ok(strends(bad, "run-debug.c:9:likely(val == 1) correct 33% (1/3)"),
48            "likely_stats returned %s", bad);
49
50         /* Nothing else above 90% */
51         ok1(!likely_stats(0, 90));
52
53         /* This should get everything. */
54         bad = likely_stats(0, 100);
55         ok(strends(bad, "run-debug.c:16:unlikely(val == 1) correct 100% (1/1)"),
56            "likely_stats returned %s", bad);
57
58         /* Nothing left (table is actually cleared) */
59         ok1(!likely_stats(0, 100));
60
61         /* Make sure unlikely works */
62         one_seems_unlikely(0);
63         one_seems_unlikely(2);
64         one_seems_unlikely(1);
65
66         bad = likely_stats(0, 90);
67         ok(strends(bad, "run-debug.c:16:unlikely(val == 1) correct 66% (2/3)"),
68            "likely_stats returned %s", bad);
69         ok1(!likely_stats(0, 100));
70
71         likely_one_unlikely_two(1, 1);
72         likely_one_unlikely_two(1, 1);
73         likely_one_unlikely_two(1, 1);
74         ok1(!likely_stats(0, 90));
75         likely_one_unlikely_two(1, 2);
76
77         bad = likely_stats(0, 90);
78         ok(strends(bad, "run-debug.c:24:unlikely(val2 == 2) correct 75% (3/4)"),
79            "likely_stats returned %s", bad);
80         bad = likely_stats(0, 100);
81         ok(strends(bad, "run-debug.c:24:likely(val1 == 1) correct 100% (4/4)"),
82            "likely_stats returned %s", bad);
83
84         ok1(!likely_stats(0, 100));
85
86         exit(exit_status());
87 }