Unifying External Interrupt System (#51)
[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                 int skipped;
17             };
18             int countings[3];
19         };
20
21         union {
22             struct {
23                 int total_passed;
24                 int total_failed;
25                 int total_skipped;
26             };
27             int total_countings[3];
28         };
29     } stats;
30 };
31
32 #define fmt_passed  "[\x1b[32;49mPASS\x1b[0m]"
33 #define fmt_failed  "[\x1b[31;49mFAIL\x1b[0m]"
34 #define fmt_skiped  "[\x1b[33;49mSKIP\x1b[0m]"
35
36 #define active_context      \
37     ({ extern struct test_context* __test_ctx; __test_ctx; })
38
39 #define _expect(expr, act, exp, type_fmt)                                   \
40     do {                                                                    \
41         if(should_skip_test()) {                                            \
42             printf("  @%s:%03d ....... ", __FILE__, __LINE__);              \
43             printf(fmt_skiped "\n");                                        \
44             active_context->stats.skipped++;                                \
45             break;                                                          \
46         }                                                                   \
47                                                                             \
48         int failed = !(expr);                                               \
49         if (failed) {                                                       \
50             printf("  @%s:%03d ....... ", __FILE__, __LINE__);              \
51             printf(fmt_failed " (expect: " type_fmt ", got: " type_fmt ")\n",\
52                     exp, act);                                              \
53         }                                                                   \
54         active_context->stats.countings[failed]++;                          \
55     } while(0)
56
57 #define new_aspect(expr, exp, act, typefmt)    expr, exp, act, typefmt
58
59 #define expect(aspect)  _expect(aspect)
60
61 #define expect_int(a, b)  \
62     _expect(a == b, a, b, "%d")
63
64 #define expect_uint(a, b) \
65     _expect(a == b, a, b, "%u")
66
67 #define expect_long(a, b) \
68     _expect(a == b, a, b, "%ld")
69
70 #define expect_ulong(a, b) \
71     _expect(a == b, a, b, "%lu")
72
73 #define expect_str(str_a, str_b)  \
74     _expect(!strcmp(str_a, str_b), str_a, str_b, "'%s'")
75
76 #define expect_notnull(ptr)  \
77     _expect(ptr != NULL, "null", "not null", "<%s>")
78
79 #define expect_null(ptr)  \
80     _expect(ptr == NULL, "not null", "null", "<%s>")
81
82 #define expect_true(val)  \
83     _expect(val, "false", "true", "<%s>")
84
85 #define expect_false(val)  \
86     _expect(!val, "true", "false", "<%s>")
87
88 #define testcase(name, body)    \
89     do { begin_testcase(name); body; end_testcase(); } while(0)
90
91 void
92 begin_testcase(const char* name);
93
94 void
95 end_testcase();
96
97 void 
98 run_test(int argc, const char* argv[]);
99
100 static inline int
101 should_skip_test()
102 {
103     return active_context->stats.failed;
104 }
105
106 #endif /* __COMMON_TEST_BASIC_H */