-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcu.h
156 lines (130 loc) · 3.43 KB
/
cu.h
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
/***
* CU - C unit testing framework
* ---------------------------------
* Copyright (c)2007-2015 Daniel Fiser <[email protected]>
*
* This file is part of CU.
*
* Distributed under the OSI-approved BSD License (the "License");
* see accompanying file BDS-LICENSE for details or see
* <http://www.opensource.org/licenses/bsd-license.php>.
*
* This software is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the License for more information.
*/
#ifndef _CU_H_
#define _CU_H_
#ifdef CU_ENABLE_TIMER
# include <time.h>
#endif /* CU_ENABLE_TIMER */
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/***** PUBLIC API *****/
/**
* Define test
*/
#ifdef __cplusplus
# define TEST(name) \
extern "C" void name(void)
#else /* __cplusplus */
# define TEST(name) \
void name(void)
#endif /* __cplusplus */
/**
* Define testsuite
*/
#define TEST_SUITE(name) \
cu_test_suite_t test_suite_##name[] =
/**
* Must be on the end of list of tests.
*/
#define TEST_SUITE_CLOSURE \
{ NULL, NULL }
#define TEST_SUITES \
cu_test_suites_t cu_test_suites[] =
#define TEST_SUITES_CLOSURE \
{ NULL, NULL }
#define TEST_SUITE_ADD(name) \
{ #name, test_suite_##name }
/**
* Add test to testsuite
*/
#define TEST_ADD(name) \
{ #name, name }
#define CU_RUN(argc, argv) \
cu_run(argc, argv)
/**
* Set prefix for output files. Must contain trailing /.
*/
#define CU_SET_OUT_PREFIX(str) \
cu_set_out_prefix(str)
/**
* Enables (disables) output per test instead of a whole testsuite.
*/
#define CU_SET_OUT_PER_TEST(yes) \
cu_set_out_per_test(yes)
/**
* Assertions
* Assertions with suffix 'M' (e.g. assertTrueM) is variation of macro
* where is possible to specify error message.
*/
#define assertTrueM(a, message) \
if (a){ \
cu_success_assertion(); \
}else{ \
cu_fail_assertion(__FILE__, __LINE__, message); \
}
#define assertTrue(a) \
assertTrueM((a), #a " is not true")
#define assertFalseM(a, message) \
assertTrueM(!(a), message)
#define assertFalse(a) \
assertFalseM((a), #a " is not false")
#define assertEqualsM(a,b,message) \
assertTrueM((a) == (b), message)
#define assertEquals(a,b) \
assertEqualsM((a), (b), #a " not equals " #b)
#define assertNotEqualsM(a,b,message) \
assertTrueM((a) != (b), message)
#define assertNotEquals(a,b) \
assertNotEqualsM((a), (b), #a " equals " #b)
/***** PUBLIC API END *****/
#include <unistd.h>
#define CU_MAX_NAME_LENGTH 30
typedef void (*cu_test_func_t)(void);
typedef struct _cu_test_suite_t {
const char *name;
cu_test_func_t func;
} cu_test_suite_t;
typedef struct _cu_test_suites_t {
const char *name;
cu_test_suite_t *test_suite;
} cu_test_suites_t;
extern cu_test_suites_t cu_test_suites[];
int cu_run(int argc, char *argv[]);
void cu_success_assertion(void);
void cu_fail_assertion(const char *file, int line, const char *msg);
void cu_set_out_prefix(const char *str);
void cu_set_out_per_test(int yes);
/** Timer **/
#ifdef CU_ENABLE_TIMER
/**
* Returns value of timer. (as timespec struct)
*/
const struct timespec *cuTimer(void);
/**
* Starts timer.
*/
void cuTimerStart(void);
/**
* Stops timer and record elapsed time from last call of cuTimerStart().
* Returns current value of timer.
*/
const struct timespec *cuTimerStop(void);
#endif /* CU_ENABLE_TIMER */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif