summaryrefslogtreecommitdiff
path: root/test/framework.h
blob: d1aa6a715a5946ddbf4434f078ac89990d9df8d1 (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
// Copyright (C) 2010-2016 Joel Rosdahl
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#ifndef TEST_FRAMEWORK_H
#define TEST_FRAMEWORK_H

#include "../ccache.h"

// ============================================================================

#define TEST_SUITE(name) \
	unsigned suite_##name(unsigned _start_point) \
	{ \
		unsigned _test_counter = 0; \
		cct_suite_begin(#name); \
		{ \
			// Empty due to macro trickery.

#define TEST(name) \
			cct_test_end(); \
		} \
		++_test_counter; \
		{ static int name = 0; (void)name; /* Verify test name. */ } \
		if (_test_counter >= _start_point) { \
			cct_test_begin(#name);

#define TEST_SUITE_END \
			cct_test_end(); \
		} \
		cct_suite_end(); \
		return 0; /* We have reached the end. */ \
	}

// ============================================================================

#define CHECKM(assertion, message) \
	do { \
		if ((assertion)) { \
			cct_check_passed(__FILE__, __LINE__, #assertion); \
		} else { \
			cct_check_failed(__FILE__, __LINE__, #assertion, (message), NULL); \
			cct_test_end(); \
			cct_suite_end(); \
			return _test_counter; \
		} \
	} while (false)

#define CHECK(assertion) \
	CHECKM(assertion, NULL)

#define CHECK_POINTER_EQ_BASE(t, e, a, f1, f2) \
	do { \
		if (!cct_check_##t##_eq(__FILE__, __LINE__, #a, (e), (a), (f1), (f2))) { \
			cct_test_end(); \
			cct_suite_end(); \
			return _test_counter; \
		} \
	} while (false)

// ============================================================================

#define CHECK_INT_EQ(expected, actual) \
	do { \
		if (!cct_check_int_eq(__FILE__, __LINE__, #actual, (expected), \
		                      (actual))) { \
			cct_test_end(); \
			cct_suite_end(); \
			return _test_counter; \
		} \
	} while (false)

// ============================================================================

#define CHECK_FLOAT_EQ(expected, actual) \
	do { \
		if (!cct_check_float_eq(__FILE__, __LINE__, #actual, (expected), \
		                      (actual))) { \
			cct_test_end(); \
			cct_suite_end(); \
			return _test_counter; \
		} \
	} while (false)

// ============================================================================

#define CHECK_STR_EQ(expected, actual) \
	CHECK_POINTER_EQ_BASE(str, expected, actual, false, false)

#define CHECK_STR_EQ_FREE1(expected, actual) \
	CHECK_POINTER_EQ_BASE(str, expected, actual, true, false)

#define CHECK_STR_EQ_FREE2(expected, actual) \
	CHECK_POINTER_EQ_BASE(str, expected, actual, false, true)

#define CHECK_STR_EQ_FREE12(expected, actual) \
	CHECK_POINTER_EQ_BASE(str, expected, actual, true, true)

// ============================================================================

#define CHECK_ARGS_EQ(expected, actual) \
	CHECK_POINTER_EQ_BASE(args, expected, actual, false, false)

#define CHECK_ARGS_EQ_FREE1(expected, actual) \
	CHECK_POINTER_EQ_BASE(args, expected, actual, true, false)

#define CHECK_ARGS_EQ_FREE2(expected, actual) \
	CHECK_POINTER_EQ_BASE(args, expected, actual, false, true)

#define CHECK_ARGS_EQ_FREE12(expected, actual) \
	CHECK_POINTER_EQ_BASE(args, expected, actual, true, true)

// ============================================================================

typedef unsigned (*suite_fn)(unsigned);
int cct_run(suite_fn *suites, int verbose);

void cct_suite_begin(const char *name);
void cct_suite_end(void);
void cct_test_begin(const char *name);
void cct_test_end(void);
void cct_check_passed(const char *file, int line, const char *assertion);
void cct_check_failed(const char *file, int line, const char *assertion,
                      const char *expected, const char *actual);
bool cct_check_float_eq(const char *file, int line, const char *expression,
                        double expected, double actual);
bool cct_check_int_eq(const char *file, int line, const char *expression,
                      int64_t expected, int64_t actual);
bool cct_check_str_eq(const char *file, int line, const char *expression,
                      const char *expected, const char *actual, bool free1,
                      bool free2);
bool cct_check_args_eq(const char *file, int line, const char *expression,
                       struct args *expected, struct args *actual,
                       bool free1, bool free2);
void cct_chdir(const char *path);
void cct_wipe(const char *path);
void cct_create_fresh_dir(const char *path);

#endif