1 #define CCAN_LIKELY_DEBUG 1
2 #include <ccan/likely/likely.c>
3 #include <ccan/likely/likely.h>
4 #include <ccan/tap/tap.h>
7 static bool one_seems_likely(unsigned int val)
14 static bool one_seems_unlikely(unsigned int val)
16 if (unlikely(val == 1))
21 static bool likely_one_unlikely_two(unsigned int val1, unsigned int val2)
23 /* Same line, check we don't get confused! */
24 if (likely(val1 == 1) && unlikely(val2 == 2))
29 int main(int argc, char *argv[])
35 /* Correct guesses. */
37 ok1(likely_stats(0, 90) == NULL);
38 one_seems_unlikely(2);
39 ok1(likely_stats(0, 90) == NULL);
41 /* Incorrect guesses. */
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);
51 /* Nothing else above 90% */
52 ok1(!likely_stats(0, 90));
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);
60 /* Nothing left (table is actually cleared) */
61 ok1(!likely_stats(0, 100));
63 /* Make sure unlikely works */
64 one_seems_unlikely(0);
65 one_seems_unlikely(2);
66 one_seems_unlikely(1);
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);
72 ok1(!likely_stats(0, 100));
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);
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);
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);
89 ok1(!likely_stats(0, 100));
91 /* Check that reset works! */
92 one_seems_unlikely(0);
93 one_seems_unlikely(2);
94 one_seems_unlikely(1);
97 ok1(!likely_stats(0, 100));
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>