]> git.ozlabs.org Git - ccan/blob - ccan/likely/test/run-debug.c
ccan: Correct some poor conventions in _info includes
[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         char *bad;
32
33         plan_tests(14);
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         free(bad);
50
51         /* Nothing else above 90% */
52         ok1(!likely_stats(0, 90));
53
54         /* This should get everything. */
55         bad = likely_stats(0, 100);
56         ok(strends(bad, "run-debug.c:16:unlikely(val == 1) correct 100% (1/1)"),
57            "likely_stats returned %s", bad);
58         free(bad);
59
60         /* Nothing left (table is actually cleared) */
61         ok1(!likely_stats(0, 100));
62
63         /* Make sure unlikely works */
64         one_seems_unlikely(0);
65         one_seems_unlikely(2);
66         one_seems_unlikely(1);
67
68         bad = likely_stats(0, 90);
69         ok(strends(bad, "run-debug.c:16:unlikely(val == 1) correct 66% (2/3)"),
70            "likely_stats returned %s", bad);
71         free(bad);
72         ok1(!likely_stats(0, 100));
73
74         likely_one_unlikely_two(1, 1);
75         likely_one_unlikely_two(1, 1);
76         likely_one_unlikely_two(1, 1);
77         ok1(!likely_stats(0, 90));
78         likely_one_unlikely_two(1, 2);
79
80         bad = likely_stats(0, 90);
81         ok(strends(bad, "run-debug.c:24:unlikely(val2 == 2) correct 75% (3/4)"),
82            "likely_stats returned %s", bad);
83         free(bad);
84         bad = likely_stats(0, 100);
85         ok(strends(bad, "run-debug.c:24:likely(val1 == 1) correct 100% (4/4)"),
86            "likely_stats returned %s", bad);
87         free(bad);
88
89         ok1(!likely_stats(0, 100));
90
91         /* Check that reset works! */
92         one_seems_unlikely(0);
93         one_seems_unlikely(2);
94         one_seems_unlikely(1);
95         likely_stats_reset();
96
97         ok1(!likely_stats(0, 100));
98
99         exit(exit_status());
100 }
101
102 /* Fools ccanlint: it doesn't think we use str, htable or hash. */
103 #include <ccan/hash/hash.h>
104 #include <ccan/htable/htable.h>
105 #include <ccan/str/str.h>