summaryrefslogtreecommitdiff
path: root/lib/ccan/tally/test/run-mode.c
blob: cd2f23044367b894d9a30a8f0ed76c3f9c19a46a (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
#include <ccan/tally/tally.c>
#include <ccan/tap/tap.h>

int main(void)
{
	int i;
	struct tally *tally = tally_new(100);
	ssize_t min, max, mode;
	size_t err;

	max = (ssize_t)~(1ULL << (sizeof(max)*CHAR_BIT - 1));
	min = (ssize_t)(1ULL << (sizeof(max)*CHAR_BIT - 1));

	plan_tests(100 + 50 + 100 + 100 + 10);
	/* Simple mode test: should always be around 0 (we add that twice). */
	for (i = 0; i < 100; i++) {
		tally_add(tally, i);
		tally_add(tally, -i);
		mode = tally_approx_mode(tally, &err);
		if (i < 50)
			ok1(err == 0);
		ok1(mode - (ssize_t)err <= 0 && mode + (ssize_t)err >= 0);
	}

	/* Works for big values too... */
	for (i = 0; i < 100; i++) {
		tally_add(tally, max - i);
		tally_add(tally, min + 1 + i);
		mode = tally_approx_mode(tally, &err);
		ok1(mode - (ssize_t)err <= 0 && mode + (ssize_t)err >= 0);
	}
	free(tally);

	tally = tally_new(10);
	tally_add(tally, 0);
	for (i = 0; i < 100; i++) {
		tally_add(tally, i);
		mode = tally_approx_mode(tally, &err);
		if (i < 10)
			ok1(err == 0);
		ok1(mode - (ssize_t)err <= 0 && mode + (ssize_t)err >= 0);
	}

	free(tally);
	return exit_status();
}