summaryrefslogtreecommitdiff
path: root/unittest/unittest.h
blob: 0ed005fed2c6012c0f7d6599ee8537062a49ca53 (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
#ifndef _UNITTEST_H_
#define _UNITTEST_H_

#include <stdlib.h>
#include <stdio.h>
#include "stringify.h"

#define TEST_PASS	0
#define TEST_FAIL	1

#define assert_err(fmt, args...) \
	fprintf(stderr, "%s:%d: Assertion error: " fmt, __FILE__, __LINE__, ##args)

/* Assert the expression 'expr' */
#define test_assert(expr) ({if (!(expr)) {		\
		assert_err(__stringify(expr) "\n");	\
		exit(TEST_FAIL);			\
		}})

/* Assert the expression 'expr' and print 'msg' on fail */
#define test_assert_msg(expr, msg) ({if (!(expr)) {	\
		assert_err(msg "\n");			\
		exit(TEST_FAIL);			\
		}})

/* Assert that a == b */
#define test_assert_eq(a, b) ({		\
	if ((a) != (b)) {		\
		assert_err(__stringify(a) " != " __stringify(b) "\n"); \
		exit(TEST_FAIL);	\
	}})

/* Assert that a != b */
#define test_assert_neq(a, b) ({	\
	if ((a) == (b)) {		\
		assert_err(__stringify(a) " != " __stringify(b) "\n"); \
		exit(TEST_FAIL);	\
	}})

#endif /* _UNITTEST_H_ */