1 #include "test/jemalloc_test.h"
2
3 #include "jemalloc/internal/ticker.h"
4
TEST_BEGIN(test_ticker_tick)5 TEST_BEGIN(test_ticker_tick) {
6 #define NREPS 2
7 #define NTICKS 3
8 ticker_t ticker;
9 int32_t i, j;
10
11 ticker_init(&ticker, NTICKS);
12 for (i = 0; i < NREPS; i++) {
13 for (j = 0; j < NTICKS; j++) {
14 assert_u_eq(ticker_read(&ticker), NTICKS - j,
15 "Unexpected ticker value (i=%d, j=%d)", i, j);
16 assert_false(ticker_tick(&ticker),
17 "Unexpected ticker fire (i=%d, j=%d)", i, j);
18 }
19 assert_u32_eq(ticker_read(&ticker), 0,
20 "Expected ticker depletion");
21 assert_true(ticker_tick(&ticker),
22 "Expected ticker fire (i=%d)", i);
23 assert_u32_eq(ticker_read(&ticker), NTICKS,
24 "Expected ticker reset");
25 }
26 #undef NTICKS
27 }
28 TEST_END
29
TEST_BEGIN(test_ticker_ticks)30 TEST_BEGIN(test_ticker_ticks) {
31 #define NTICKS 3
32 ticker_t ticker;
33
34 ticker_init(&ticker, NTICKS);
35
36 assert_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
37 assert_false(ticker_ticks(&ticker, NTICKS), "Unexpected ticker fire");
38 assert_u_eq(ticker_read(&ticker), 0, "Unexpected ticker value");
39 assert_true(ticker_ticks(&ticker, NTICKS), "Expected ticker fire");
40 assert_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
41
42 assert_true(ticker_ticks(&ticker, NTICKS + 1), "Expected ticker fire");
43 assert_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
44 #undef NTICKS
45 }
46 TEST_END
47
TEST_BEGIN(test_ticker_copy)48 TEST_BEGIN(test_ticker_copy) {
49 #define NTICKS 3
50 ticker_t ta, tb;
51
52 ticker_init(&ta, NTICKS);
53 ticker_copy(&tb, &ta);
54 assert_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
55 assert_true(ticker_ticks(&tb, NTICKS + 1), "Expected ticker fire");
56 assert_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
57
58 ticker_tick(&ta);
59 ticker_copy(&tb, &ta);
60 assert_u_eq(ticker_read(&tb), NTICKS - 1, "Unexpected ticker value");
61 assert_true(ticker_ticks(&tb, NTICKS), "Expected ticker fire");
62 assert_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
63 #undef NTICKS
64 }
65 TEST_END
66
67 int
main(void)68 main(void) {
69 return test(
70 test_ticker_tick,
71 test_ticker_ticks,
72 test_ticker_copy);
73 }
74