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

int main(void)
{
	int a, b;

	/* This is how many tests you plan to run */
	plan_tests(23);

	ok1(min(1, 2) == 1);
	ok1(max(1, 2) == 2);
	ok1(min(-1, 1) == -1);
	ok1(max(-1, 1) == 1);

	ok1(min(-1U, 1U) == 1U);
	ok1(max(-1U, 1U) == -1U);

	ok1(max_t(signed int, -1, 1U) == 1);
	ok1(max_t(unsigned int, -1, 1) == -1U);

	ok1(min_t(signed int, -1, 1U) == -1);
	ok1(min_t(unsigned int, -1, 1) == 1U);

	ok1(clamp(1, 2, 5) == 2);
	ok1(clamp(2, 2, 5) == 2);
	ok1(clamp(3, 2, 5) == 3);
	ok1(clamp(5, 2, 5) == 5);
	ok1(clamp(6, 2, 5) == 5);

	ok1(clamp(-1, 2, 5) == 2);
	ok1(clamp(-1U, 2U, 5U) == 5U);

	ok1(clamp_t(signed int, -1, 2, 5) == 2);
	ok1(clamp_t(unsigned int, -1, 2, 5) == 5);

	/* test for double evaluation */
	a = b = 0;
	ok1(min(a++, b++) == 0);
	ok1((a == 1) && (b == 1));
	ok1(max(++a, ++b) == 2);
	ok1((a == 2) && (b == 2));

	/* This exits depending on whether all tests passed */
	return exit_status();
}