summaryrefslogtreecommitdiff
path: root/tests/assertions.c
blob: 156a484739b4bc05bbda767e9b918fc82fcebb28 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "lily-test.h"
#include "tests.h"


const char * test_INFO()
{
	lily_g.HEAD.next = NULL;
	lily_g.TAIL = &(lily_g.HEAD);
	lily_g.failed = 0;

	INFO("the number is %d", 10);
	INFO("the string is '%s'", "hello world");

	if (lily_g.failed)
		return "INFO caused failure!";
	
	if (lily_g.HEAD.next == NULL)
		return "INFO did not append any messages!";
	
	lily_test_msg_t *m = lily_g.HEAD.next;
	if (strcmp(m->msg, "the number is 10") != 0)
		return "the first message is incorrect";
	
	m = m->next;
	if (m == NULL)
		return "there is not a second message!";
	
	if (strcmp(m->msg, "the string is 'hello world'") != 0)
		return "the second message is incorrect";
	
	if (m->next != NULL)
		return "too many messages";
	
	lily_msg_destroy(lily_g.HEAD.next);
	return 0;
}


const char * test_CHECK()
{
	lily_g.HEAD.next = NULL;
	lily_g.TAIL = &(lily_g.HEAD);
	lily_g.failed = 0;
	CHECK(1 == 0);
	CHECK(0 == 0);
	CHECK(4 == 3);
	
	if (lily_g.HEAD.next == NULL)
		return "CHECK did not append any failure messages";
	lily_test_msg_t *m = lily_g.HEAD.next;

	if (strcmp(m->msg, "1 == 0") != 0)
		return "first assert has wrong message";

	if (m->next == NULL)
		return "only one message appended";
	m = m->next;
	
	if (strcmp(m->msg, "4 == 3") != 0)
		return "second message is incorrect";
	
	if (m->next != NULL)
		return "more than two messages appended!";

	if (!lily_g.failed)
		return "test did not fail!";

	lily_msg_destroy(lily_g.HEAD.next);
	
	return 0;
}


int aR, bR;
static void f_test_REQUIRE()
{
	aR = 5;
	REQUIRE(2+2 == 5);
	bR = 5;
}

const char * test_REQUIRE()
{
	lily_g.HEAD.next = NULL;
	lily_g.TAIL = &(lily_g.HEAD);
	lily_g.failed = 0;
	aR = 0; bR = 0;
	int test_failed = setjmp(lily_g.env);

	if (test_failed) {
		if (!lily_g.failed)
			return "test did not fail!";

		if (lily_g.HEAD.next == NULL)
			return "test did not generate any messages";

		if (strcmp(lily_g.HEAD.next->msg, "2+2 == 5") != 0)
			return "test generated incorrect message";

		if (lily_g.HEAD.next->next != NULL)
			return "test generated too many messages";

		if (bR == 5)
			return "test kept executing after failed REQUIRE";

		lily_msg_destroy(lily_g.HEAD.next);
		return 0;
	}
	else {
		f_test_REQUIRE();
		return "test function did not longjump!";
	}
}