summaryrefslogtreecommitdiff
path: root/tests/assertions.c
blob: 280bb97200716e6cf77056de080dddb21e86aed7 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#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, "CHECK failed: 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, "CHECK failed: 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;
}


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

	int k = 5;
	CHECK_EQ(k, 5, "%d");
	CHECK_EQ(4, k, "%02d");

	if (lily_g.HEAD.next == NULL)
		return "CHECK_EQ did not append any failure message";
	
	if (lily_g.HEAD.next->next != NULL)
		return "CHECK_EQ appended more than one message";
	
	if (strcmp(lily_g.HEAD.next->msg, "CHECK failed: 4 == k\n    4 = 04\n    k = 05") != 0)
		return "incorrect message";
	
	lily_msg_destroy(lily_g.HEAD.next);
	return 0;
}


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

	lily_set_epsilon(0.2);
	CHECK_EQF(0.5, 0.4, "%0.1f");
	CHECK_EQF(0.5, 0.0, "%0.1f");

	if (lily_g.HEAD.next == NULL)
		return "CHECK_EQF did not append any failure message";

	if (lily_g.HEAD.next->next != NULL)
		return "CHECK_EQF appended more than one message";
	
	if (strcmp(
		lily_g.HEAD.next->msg, 
		"CHECK failed: 0.5 == 0.0\n    0.5 = 0.5\n    0.0 = 0.0\n    epsilon: 0.200000"
	) != 0)
		return "incorrect message";
	
	lily_msg_destroy(lily_g.HEAD.next);
	return 0;
}


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

	const char *a = "hi";
	const char *b = "bye";
	CHECK_EQS(a, b);
	CHECK_EQS("hi hi"+3, a);

	if (lily_g.HEAD.next == NULL)
		return "CHECK_EQS did not append any failure message";

	if (lily_g.HEAD.next->next != NULL)
		return "CHECK_EQS appended more than one message";
	
	if (strcmp(
		lily_g.HEAD.next->msg, 
		"CHECK failed: a == b\n    a = \"hi\"\n    b = \"bye\""
	) != 0)
		return "incorrect message";
	
	lily_msg_destroy(lily_g.HEAD.next);
	return 0;
}

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

	const char *a = "hi";
	const char *b = "bye";
	CHECK_EQS(a, NULL);
	CHECK_EQS(a, "hi");

	if (lily_g.HEAD.next == NULL)
		return "CHECK_EQS did not append any failure message";

	if (lily_g.HEAD.next->next != NULL)
		return "CHECK_EQS appended more than one message";
	
	if (strcmp(
		lily_g.HEAD.next->msg, 
		"CHECK failed: a == NULL\n    a = \"hi\"\n    NULL = \"(null)\""
	) != 0)
		return "incorrect message";
	
	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, "REQUIRE failed: 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!";
	}
}


void f_test_REQUIRE_EQ()
{
	REQUIRE_EQ(2, 2, "%d");
	REQUIRE_EQ(2, 4, "%d");
	REQUIRE_EQ(2, 1, "%d");
}

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

	if (test_failed) {
		if (!lily_g.failed)
			return "test did not mark itself as failed";

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

		if (strcmp(lily_g.HEAD.next->msg, "REQUIRE failed: 2 == 4\n    2 = 2\n    4 = 4") != 0)
			return "test generated incorrect message";

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

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


void f_test_REQUIRE_EQF()
{
	lily_set_epsilon(0.2);
	REQUIRE_EQF(0.5, 0.0, "%0.1f");
	REQUIRE_EQF(0.5, 0.4, "%0.1f");
}

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

	if (test_failed) {
		if (lily_g.HEAD.next == NULL)
			return "REQUIRE_EQF did not append any failure message";
	
		if (lily_g.HEAD.next->next != NULL)
			return "REQUIRE_EQF appended more than one message";
		
		if (strcmp(
			lily_g.HEAD.next->msg, 
			"REQUIRE failed: 0.5 == 0.0\n    0.5 = 0.5\n    0.0 = 0.0\n    epsilon: 0.200000"
		) != 0)
			return "incorrect message";
		
		lily_msg_destroy(lily_g.HEAD.next);
		return 0;
	}
	else {
		f_test_REQUIRE_EQF();
		return "test continued to run!";
	}
}


void f_test_REQUIRE_EQS()
{
	const char *a = "hi";
	const char *b = "bye";
	REQUIRE_EQS(a, b);
	REQUIRE_EQS("hi hi"+3, a);
}
const char * test_REQUIRE_EQS()
{
	lily_g.HEAD.next = NULL;
	lily_g.TAIL = &(lily_g.HEAD);
	lily_g.failed = 0;
	int test_failed = setjmp(lily_g.env);

	if (test_failed) {
		if (lily_g.HEAD.next == NULL)
			return "REQUIRE_EQS did not append any failure message";
	
		if (lily_g.HEAD.next->next != NULL)
			return "REQUIRE_EQS appended more than one message";
		
		if (strcmp(
			lily_g.HEAD.next->msg, 
			"REQUIRE failed: a == b\n    a = \"hi\"\n    b = \"bye\""
		) != 0)
			return "incorrect message";
		
		lily_msg_destroy(lily_g.HEAD.next);
		return 0;
	}
	else {
		f_test_REQUIRE_EQS();
		return "test continued to run!";
	}
}

void f_test_REQUIRE_EQS_NULL()
{
	const char *a = "hi";
	const char *b = "bye";
	REQUIRE_EQS(a, NULL); 
	REQUIRE_EQS("hi hi"+3, a);
}
const char * test_REQUIRE_EQS_NULL()
{
	lily_g.HEAD.next = NULL;
	lily_g.TAIL = &(lily_g.HEAD);
	lily_g.failed = 0;
	int test_failed = setjmp(lily_g.env);

	if (test_failed) {
		if (lily_g.HEAD.next == NULL)
			return "REQUIRE_EQS did not append any failure message";
	
		if (lily_g.HEAD.next->next != NULL)
			return "REQUIRE_EQS appended more than one message";
		
		if (strcmp(
			lily_g.HEAD.next->msg, 
			"REQUIRE failed: a == NULL\n    a = \"hi\"\n    NULL = \"(null)\""
		) != 0)
			return "incorrect message";
		
		lily_msg_destroy(lily_g.HEAD.next);
		return 0;
	}
	else {
		f_test_REQUIRE_EQS_NULL();
		return "test continued to run!";
	}

}