summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/jemalloc-5.3.0/test/unit/ticker.c
blob: 0dd778619e87feddffbf0323a433eb4a389a8a64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "test/jemalloc_test.h"

#include "jemalloc/internal/ticker.h"

TEST_BEGIN(test_ticker_tick) {
#define NREPS 2
#define NTICKS 3
	ticker_t ticker;
	int32_t i, j;

	ticker_init(&ticker, NTICKS);
	for (i = 0; i < NREPS; i++) {
		for (j = 0; j < NTICKS; j++) {
			expect_u_eq(ticker_read(&ticker), NTICKS - j,
			    "Unexpected ticker value (i=%d, j=%d)", i, j);
			expect_false(ticker_tick(&ticker),
			    "Unexpected ticker fire (i=%d, j=%d)", i, j);
		}
		expect_u32_eq(ticker_read(&ticker), 0,
		    "Expected ticker depletion");
		expect_true(ticker_tick(&ticker),
		    "Expected ticker fire (i=%d)", i);
		expect_u32_eq(ticker_read(&ticker), NTICKS,
		    "Expected ticker reset");
	}
#undef NTICKS
}
TEST_END

TEST_BEGIN(test_ticker_ticks) {
#define NTICKS 3
	ticker_t ticker;

	ticker_init(&ticker, NTICKS);

	expect_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
	expect_false(ticker_ticks(&ticker, NTICKS), "Unexpected ticker fire");
	expect_u_eq(ticker_read(&ticker), 0, "Unexpected ticker value");
	expect_true(ticker_ticks(&ticker, NTICKS), "Expected ticker fire");
	expect_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");

	expect_true(ticker_ticks(&ticker, NTICKS + 1), "Expected ticker fire");
	expect_u_eq(ticker_read(&ticker), NTICKS, "Unexpected ticker value");
#undef NTICKS
}
TEST_END

TEST_BEGIN(test_ticker_copy) {
#define NTICKS 3
	ticker_t ta, tb;

	ticker_init(&ta, NTICKS);
	ticker_copy(&tb, &ta);
	expect_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
	expect_true(ticker_ticks(&tb, NTICKS + 1), "Expected ticker fire");
	expect_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");

	ticker_tick(&ta);
	ticker_copy(&tb, &ta);
	expect_u_eq(ticker_read(&tb), NTICKS - 1, "Unexpected ticker value");
	expect_true(ticker_ticks(&tb, NTICKS), "Expected ticker fire");
	expect_u_eq(ticker_read(&tb), NTICKS, "Unexpected ticker value");
#undef NTICKS
}
TEST_END

TEST_BEGIN(test_ticker_geom) {
	const int32_t ticks = 100;
	const uint64_t niters = 100 * 1000;

	ticker_geom_t ticker;
	ticker_geom_init(&ticker, ticks);
	uint64_t total_ticks = 0;
	/* Just some random constant. */
	uint64_t prng_state = 0x343219f93496db9fULL;
	for (uint64_t i = 0; i < niters; i++) {
		while(!ticker_geom_tick(&ticker, &prng_state)) {
			total_ticks++;
		}
	}
	/*
	 * In fact, with this choice of random seed and the PRNG implementation
	 * used at the time this was tested, total_ticks is 95.1% of the
	 * expected ticks.
	 */
	expect_u64_ge(total_ticks , niters * ticks * 9 / 10,
	    "Mean off by > 10%%");
	expect_u64_le(total_ticks , niters * ticks * 11 / 10,
	    "Mean off by > 10%%");
}
TEST_END

int
main(void) {
	return test(
	    test_ticker_tick,
	    test_ticker_ticks,
	    test_ticker_copy,
	    test_ticker_geom);
}