All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
src/test/test.h
Go to the documentation of this file.
1 #pragma once
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdarg.h>
7 
8 /******************************************************************************
9  * MACROS
10  *****************************************************************************/
11 
12 #define ATF_PLAN_SUITE_MAX 128
13 #define ATF_SUITE_TEST_MAX 128
14 
15 /******************************************************************************
16  * atf_test
17  *****************************************************************************/
18 
19 typedef struct atf_test_s atf_test;
20 typedef struct atf_test_result_s atf_test_result;
21 
22 struct atf_test_s {
23  const char * name;
24  const char * desc;
25  void (* run)(atf_test *, atf_test_result *);
26 };
27 
28 struct atf_test_result_s {
29  atf_test * test;
30  bool success;
31  char message[128];
32 };
33 
34 atf_test_result * atf_test_run(atf_test * test);
35 
36 atf_test_result * atf_test_result_new(atf_test * test);
37 void atf_test_result_destroy(atf_test_result * test_result);
38 
39 
40 #define TEST(__test_name, __test_desc) \
41  static void test_spec__##__test_name(atf_test *, atf_test_result *); \
42  static atf_test test__##__test_name = { \
43  .name = #__test_name, \
44  .desc = __test_desc, \
45  .run = test_spec__##__test_name \
46  }; \
47  atf_test * __test_name = & test__##__test_name; \
48  static void test_spec__##__test_name(atf_test * self, atf_test_result * __result__)
49 
50 /******************************************************************************
51  * atf_suite
52  *****************************************************************************/
53 
54 typedef struct atf_suite_s atf_suite;
55 typedef struct atf_suite_result_s atf_suite_result;
56 
57 struct atf_suite_s {
58  const char * name;
59  const char * desc;
60  atf_test * tests[ATF_SUITE_TEST_MAX];
61  uint32_t size;
62  void (* init)(atf_suite *);
63  bool (* before)(atf_suite *);
64  bool (* after)(atf_suite *);
65 };
66 
67 struct atf_suite_result_s {
68  atf_suite * suite;
69  atf_test_result * tests[ATF_SUITE_TEST_MAX];
70  uint32_t size;
71  uint32_t success;
72 };
73 
74 atf_suite * atf_suite_add(atf_suite * suite, atf_test * test);
75 uint32_t atf_suite_size(atf_suite * suite);
76 atf_suite_result * atf_suite_run(atf_suite * suite);
77 
78 atf_suite * atf_suite_after(atf_suite * suite, bool (* after)(atf_suite * suite));
79 atf_suite * atf_suite_before(atf_suite * suite, bool (* before)(atf_suite * suite));
80 
81 atf_suite_result * atf_suite_result_new(atf_suite * suite);
82 void atf_suite_result_destroy(atf_suite_result * result);
83 
84 atf_suite_result * atf_suite_result_add(atf_suite_result * suite_result, atf_test_result * test_result);
85 void atf_suite_result_print(atf_suite_result * suite_result);
86 
87 
88 #define SUITE(__suite_name, __suite_desc) \
89  static void suite_spec__##__suite_name(atf_suite *); \
90  static atf_suite suite__##__suite_name = { \
91  .name = #__suite_name, \
92  .desc = __suite_desc, \
93  .tests = {NULL}, \
94  .size = 0, \
95  .init = suite_spec__##__suite_name, \
96  .before = NULL, \
97  .after = NULL \
98  }; \
99  atf_suite * __suite_name = & suite__##__suite_name; \
100  static void suite_spec__##__suite_name(atf_suite * self)
101 
102 #define suite_add(__test) \
103  extern atf_test * __test; \
104  atf_suite_add(self, __test)
105 
106 #define suite_before(__func) \
107  atf_suite_before(self, __func)
108 
109 #define suite_after(__func) \
110  atf_suite_after(self, __func)
111 
112 
113 /******************************************************************************
114  * atf_plan
115  *****************************************************************************/
116 
117 typedef struct atf_plan_s atf_plan;
118 typedef struct atf_plan_result_s atf_plan_result;
119 
120 struct atf_plan_s {
121  const char * name;
122  atf_suite * suites[ATF_PLAN_SUITE_MAX];
123  uint32_t size;
124  bool (* before)(atf_plan *);
125  bool (* after)(atf_plan *);
126 };
127 
128 struct atf_plan_result_s {
129  atf_plan * plan;
130  atf_suite_result * suites[ATF_PLAN_SUITE_MAX];
131  uint32_t size;
132 };
133 
134 atf_plan * atf_plan_add(atf_plan * self, atf_suite * suite);
135 int atf_plan_run(atf_plan * self, atf_plan_result * result);
136 
137 atf_plan * atf_plan_after(atf_plan * plan, bool (* after)(atf_plan * plan));
138 atf_plan * atf_plan_before(atf_plan * plan, bool (* before)(atf_plan * plan));
139 
140 atf_plan_result * atf_plan_result_add(atf_plan_result * plan_result, atf_suite_result * suite_result);
141 
142 atf_plan_result * atf_plan_result_new(atf_plan * plan);
143 void atf_plan_result_destroy(atf_plan_result * result);
144 
145 
146 #define PLAN(__plan_name)\
147  static void plan_spec__##__plan_name(atf_plan * self); \
148  static atf_plan plan__##__plan_name = { \
149  .name = #__plan_name, \
150  .suites = {NULL}, \
151  .size = 0, \
152  .before = NULL, \
153  .after = NULL \
154  }; \
155  atf_plan * __plan_name = & plan__##__plan_name; \
156  int main(int argc, char ** args) { \
157  g_argc = argc; \
158  g_argv = args; \
159  atf_plan_result * result = atf_plan_result_new(__plan_name); \
160  plan_spec__##__plan_name(__plan_name); \
161  int rc = atf_plan_run(__plan_name, result); \
162  return rc; \
163  }\
164  static void plan_spec__##__plan_name(atf_plan * self) \
165 
166 
167 #define plan_add(__suite) \
168  extern atf_suite * __suite; \
169  atf_plan_add(self, __suite)
170 
171 #define plan_before(__func) \
172  atf_plan_before(self, __func)
173 
174 #define plan_after(__func) \
175  atf_plan_after(self, __func)
176 
177 /******************************************************************************
178  * atf_assert
179  *****************************************************************************/
180 
181 void atf_assert(atf_test_result * test_result, const char * exp, const char * file, int line);
182 
183 void atf_assert_true(atf_test_result * test_result, const char * exp, const char * file, int line);
184 void atf_assert_false(atf_test_result * test_result, const char * exp, const char * file, int line);
185 
186 void atf_assert_null(atf_test_result * test_result, const char * exp, const char * file, int line);
187 void atf_assert_not_null(atf_test_result * test_result, const char * exp, const char * file, int line);
188 
189 void atf_assert_int_eq(atf_test_result * result, const char * actual_exp, int64_t actual, int64_t expected, const char * file, int line);
190 void atf_assert_int_ne(atf_test_result * result, const char * actual_exp, int64_t actual, int64_t expected, const char * file, int line);
191 
192 void atf_assert_string_eq(atf_test_result * result, const char * actual_exp, const char * actual, const char * expected, const char * file, int line);
193 
194 void atf_assert_log(atf_test_result * result, const char * exp, const char * file, int line, const char * fmt, ...);
195 
196 
197 #define assert(EXP) \
198  if ( (EXP) != true ) return atf_assert(__result__, #EXP, __FILE__, __LINE__);
199 
200 #define assert_true(EXP) \
201  if ( (EXP) != true ) return atf_assert_true(__result__, #EXP, __FILE__, __LINE__);
202 
203 #define assert_false(EXP) \
204  if ( (EXP) == true ) return atf_assert_false(__result__, #EXP, __FILE__, __LINE__);
205 
206 #define assert_null(EXP) \
207  if ( (EXP) != NULL ) return atf_assert_null(__result__, #EXP, __FILE__, __LINE__);
208 
209 #define assert_not_null(EXP) \
210  if ( (EXP) == NULL ) return atf_assert_not_null(__result__, #EXP, __FILE__, __LINE__);
211 
212 
213 #define assert_int_eq(ACTUAL, EXPECTED) \
214  if ( (ACTUAL) != (EXPECTED) ) return atf_assert_int_eq(__result__, #ACTUAL, ACTUAL, EXPECTED, __FILE__, __LINE__);
215 
216 #define assert_int_ne(ACTUAL, EXPECTED) \
217  if ( (ACTUAL) == (EXPECTED) ) return atf_assert_int_ne(__result__, #ACTUAL, ACTUAL, EXPECTED, __FILE__, __LINE__);
218 
219 
220 #define assert_string_eq(ACTUAL, EXPECTED) \
221  if ( strcmp(ACTUAL, EXPECTED) != 0 ) return atf_assert_string_eq(__result__, #ACTUAL, ACTUAL, EXPECTED, __FILE__, __LINE__);
222 
223 
224 #define assert_log(EXP, fmt, args ... ) \
225  if ( (EXP) == true ) return atf_assert_log(__result__, #EXP, __FILE__, __LINE__, fmt, ##args );
226 
227 /******************************************************************************
228  * atf_log
229  *****************************************************************************/
230 
231 #define ATF_LOG_PREFIX " "
232 
233 #define debug(fmt, args...) \
234  atf_log_line(stderr, "DEBUG", ATF_LOG_PREFIX, __FILE__, __LINE__, fmt, ## args);
235 
236 #define info(fmt, args...) \
237  atf_log(stderr, "INFO", ATF_LOG_PREFIX, __FILE__, __LINE__, fmt, ## args);
238 
239 #define warn(fmt, args...) \
240  atf_log(stderr, "WARN", ATF_LOG_PREFIX, __FILE__, __LINE__, fmt, ## args);
241 
242 #define error(fmt, args...) \
243  atf_log(stderr, "ERROR", ATF_LOG_PREFIX, __FILE__, __LINE__, fmt, ## args);
244 
245 void atf_log(FILE * f, const char * level, const char * prefix, const char * file, int line, const char * fmt, ...);
246 
247 void atf_logv(FILE * f, const char * level, const char * prefix, const char * file, int line, const char * fmt, va_list ap);
248 
249 void atf_log_line(FILE * f, const char * level, const char * prefix, const char * file, int line, const char * fmt, ...);
250 
251 void atf_log_line(FILE * f, const char * level, const char * prefix, const char * file, int line, const char * fmt, ...);
atf_plan_result * atf_plan_result_add(atf_plan_result *plan_result, atf_suite_result *suite_result)
atf_test_result * tests[ATF_SUITE_TEST_MAX]
void atf_log(FILE *f, const char *level, const char *prefix, const char *file, int line, const char *fmt,...)
uint32_t atf_suite_size(atf_suite *suite)
atf_plan * atf_plan_before(atf_plan *plan, bool(*before)(atf_plan *plan))
atf_test_result * atf_test_result_new(atf_test *test)
bool(* after)(atf_suite *)
bool(* after)(atf_plan *)
atf_suite_result * atf_suite_run(atf_suite *suite)
void atf_logv(FILE *f, const char *level, const char *prefix, const char *file, int line, const char *fmt, va_list ap)
atf_plan * atf_plan_add(atf_plan *self, atf_suite *suite)
int atf_plan_run(atf_plan *self, atf_plan_result *result)
void atf_assert_not_null(atf_test_result *test_result, const char *exp, const char *file, int line)
void atf_assert_string_eq(atf_test_result *result, const char *actual_exp, const char *actual, const char *expected, const char *file, int line)
void(* init)(atf_suite *)
atf_suite * atf_suite_before(atf_suite *suite, bool(*before)(atf_suite *suite))
#define ATF_SUITE_TEST_MAX
Definition: src/test/test.h:13
cl_msg_field f
atf_suite_result * atf_suite_result_new(atf_suite *suite)
void atf_assert_int_eq(atf_test_result *result, const char *actual_exp, int64_t actual, int64_t expected, const char *file, int line)
atf_suite * atf_suite_after(atf_suite *suite, bool(*after)(atf_suite *suite))
bool(* before)(atf_plan *)
#define ATF_PLAN_SUITE_MAX
Definition: src/test/test.h:12
atf_test_result * atf_test_run(atf_test *test)
void atf_assert_true(atf_test_result *test_result, const char *exp, const char *file, int line)
void atf_log_line(FILE *f, const char *level, const char *prefix, const char *file, int line, const char *fmt,...)
atf_plan_result * atf_plan_result_new(atf_plan *plan)
atf_suite * atf_suite_add(atf_suite *suite, atf_test *test)
void atf_assert_log(atf_test_result *result, const char *exp, const char *file, int line, const char *fmt,...)
void(* run)(atf_test *, atf_test_result *)
atf_plan * atf_plan_after(atf_plan *plan, bool(*after)(atf_plan *plan))
void atf_test_result_destroy(atf_test_result *test_result)
void atf_assert(atf_test_result *test_result, const char *exp, const char *file, int line)
atf_suite_result * suites[ATF_PLAN_SUITE_MAX]
atf_suite_result * atf_suite_result_add(atf_suite_result *suite_result, atf_test_result *test_result)
bool(* before)(atf_suite *)
atf_test * tests[ATF_SUITE_TEST_MAX]
void atf_assert_null(atf_test_result *test_result, const char *exp, const char *file, int line)
void atf_suite_result_print(atf_suite_result *suite_result)
void atf_suite_result_destroy(atf_suite_result *result)
void atf_plan_result_destroy(atf_plan_result *result)
atf_suite * suites[ATF_PLAN_SUITE_MAX]
void atf_assert_int_ne(atf_test_result *result, const char *actual_exp, int64_t actual, int64_t expected, const char *file, int line)
void atf_assert_false(atf_test_result *test_result, const char *exp, const char *file, int line)