00001
00038 #ifndef TEST_SUITE_H_INCLUDED
00039 #define TEST_SUITE_H_INCLUDED
00040
00041 #include <compiler.h>
00042 #include <stdio.h>
00043 #if defined(XMEGA)
00044 # include <progmem.h>
00045 #endif
00046
00057 #if defined(XMEGA)
00058 #define dbg(__fmt_) \
00059 printf_P(PSTR(__fmt_))
00060 #define dbg_info(__fmt_, ...) \
00061 printf_P(PSTR(__fmt_), __VA_ARGS__)
00062 #define dbg_error(_x, ...) \
00063 printf_P(PSTR(_x), __VA_ARGS__)
00064 #define dbg_putchar(c) \
00065 putc(c, stdout)
00066 #define dbg_vprintf_pgm(...) \
00067 vfprintf_P(stdout, __VA_ARGS__)
00068 #else
00069 #define dbg(__fmt_) \
00070 printf(__fmt_)
00071 #define dbg_info(__fmt_, ...) \
00072 printf(__fmt_, __VA_ARGS__)
00073 #define dbg_error(_x, ...) \
00074 printf(_x, __VA_ARGS__)
00075 #define dbg_putchar(c) \
00076 putc(c, stdout)
00077 #define dbg_vprintf_pgm(...) \
00078 vfprintf(stdout, __VA_ARGS__)
00079 #endif
00080
00087 #define __progmem_arg
00088
00089
00096 enum test_status {
00097 TEST_PASS = 0,
00098 TEST_FAILED = 1,
00099 };
00100
00107 struct test_case {
00115 void (*setup)(const struct test_case *test);
00117 void (*run)(const struct test_case *test);
00126 void (*cleanup)(const struct test_case *test);
00128 const char *name;
00129 };
00130
00139 #define DEFINE_TEST_CASE(_sym, _setup, _run, _cleanup, _name) \
00140 static const char _test_str_##_sym[] = _name; \
00141 static const struct test_case _sym = { \
00142 .setup = _setup, \
00143 .run = _run, \
00144 .cleanup = _cleanup, \
00145 .name = _test_str_##_sym \
00146 }
00147
00152 #define DEFINE_TEST_ARRAY(_sym) \
00153 const struct test_case *const _sym[]
00154
00155
00162 struct test_suite {
00164 unsigned int nr_tests;
00166 const struct test_case *const *tests;
00168 const char *name;
00169 };
00170 #ifndef ARRAY_LEN
00171 # define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
00172 #endif
00173
00180 #define DEFINE_TEST_SUITE(_sym, _test_array, _name) \
00181 static const char _test_str_##_sym[] = _name; \
00182 const struct test_suite _sym = { \
00183 .nr_tests = ARRAY_LEN(_test_array), \
00184 .tests = _test_array, \
00185 .name = _test_str_##_sym \
00186 }
00187 #define DECLARE_TEST_SUITE(_sym) \
00188 const struct test_suite _sym
00189
00190 extern int test_suite_run(const struct test_suite *suite);
00191
00192 extern void *test_priv_data;
00193
00200 static inline void test_set_data(void *data)
00201 {
00202 test_priv_data = data;
00203 }
00204
00211 static inline void *test_get_data(void)
00212 {
00213 return test_priv_data;
00214 }
00215
00216
00217
00218
00219
00220 extern void test_priv_fail(const struct test_case *test,
00221 int result, const char *file, unsigned int line,
00222 const char __progmem_arg *fmt, ...);
00223
00237 #if defined(CONFIG_PROGMEM)
00238 # define test_priv_fail_ps(test, result, format, ...) \
00239 do { \
00240 static PROGMEM_DECLARE(char, _fmtstr[]) = format "%s"; \
00241 test_priv_fail(test, result, __FILE__, __LINE__, \
00242 _fmtstr, __VA_ARGS__); \
00243 } while (0)
00244
00245 # define test_fail(test, result, ...) \
00246 test_priv_fail_ps(test, result, __VA_ARGS__, "")
00247 #else
00248 # define test_fail(test, result, ...) \
00249 test_priv_fail(test, result, __FILE__, __LINE__, \
00250 __VA_ARGS__)
00251 #endif
00252
00263 #define test_fail_unless(test, condition, ...) \
00264 do { \
00265 if (!(condition)) \
00266 test_fail(test, TEST_FAILED, __VA_ARGS__); \
00267 } while (0)
00268
00270
00271 #endif