Unit testing framework and devicetree framework refactoring (#50)
[lunaix-os.git] / lunaix-os / tests / includes / testing / basic.h
1 #ifndef __COMMON_TEST_BASIC_H
2 #define __COMMON_TEST_BASIC_H
3
4 #include <stdio.h>
5 #include "strutils.h"
6
7 struct test_context
8 {
9     const char* name;
10
11     struct {
12         union {
13             struct {
14                 int passed;
15                 int failed;
16             };
17             int countings[2];
18         };
19
20         union {
21             struct {
22                 int total_passed;
23                 int total_failed;
24             };
25             int total_countings[2];
26         };
27     } stats;
28 };
29
30 #define fmt_passed  "[\x1b[32;49mPASS\x1b[0m]"
31 #define fmt_failed  "[\x1b[31;49mFAIL\x1b[0m]"
32
33 #define active_context      \
34     ({ extern struct test_context* __test_ctx; __test_ctx; })
35
36 #define _expect(expr, act, exp, type_fmt)                                   \
37     do {                                                                    \
38         int failed = !(expr);                                               \
39         printf("  @%s:%03d ....... ", __FILE__, __LINE__);                    \
40         if (failed)                                                         \
41             printf(fmt_failed " (expect: " type_fmt ", got: " type_fmt ")\n",\
42                     exp, act);                                              \
43         else                                                                \
44             printf(fmt_passed "\n");                                       \
45         active_context->stats.countings[failed]++;                          \
46     } while(0)
47
48 #define new_aspect(expr, exp, act, typefmt)    expr, exp, act, typefmt
49
50 #define expect(aspect)  _expect(aspect)
51
52 #define expect_int(a, b)  \
53     _expect(a == b, a, b, "%d")
54
55 #define expect_uint(a, b) \
56     _expect(a == b, a, b, "%u")
57
58 #define expect_long(a, b) \
59     _expect(a == b, a, b, "%ld")
60
61 #define expect_ulong(a, b) \
62     _expect(a == b, a, b, "%lu")
63
64 #define expect_str(str_a, str_b)  \
65     _expect(!strcmp(str_a, str_b), str_a, str_b, "'%s'")
66
67 #define expect_notnull(ptr)  \
68     _expect(ptr != NULL, "null", "not null", "<%s>")
69
70 #define expect_null(ptr)  \
71     _expect(ptr == NULL, "not null", "null", "<%s>")
72
73 #define expect_true(val)  \
74     _expect(val, "false", "true", "<%s>")
75
76 #define expect_false(val)  \
77     _expect(!val, "true", "false", "<%s>")
78
79 #define testcase(name, body)    \
80     do { begin_testcase(name); body; end_testcase(); } while(0)
81
82 void
83 begin_testcase(const char* name);
84
85 void
86 end_testcase();
87
88 void 
89 run_test(int argc, const char* argv[]);
90
91 #endif /* __COMMON_TEST_BASIC_H */