aboutsummaryrefslogtreecommitdiffstats
path: root/mtest.h
blob: 4f1c3cf405ca69289349c02b22ae02b3b7bce24a (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* ==========================================================================
    Licensed under BSD 2clause license. See LICENSE file for more information
    Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
   ==========================================================================
     __________________________________________________________
    /                   mtest version v1.1.3                   \
    |                                                          |
    |    Simple test framework that uses TAP output format     |
    \                 http://testanything.org                  /
     ----------------------------------------------------------
            \    ,-^-.
             \   !oYo!
              \ /./=\.\______
                   ##        )\/\
                    ||-----w||
                    ||      ||

                   Cowth Vader
   ========================================================================== */


/* ==========================================================================
          _               __            __         ____ _  __
         (_)____   _____ / /__  __ ____/ /___     / __/(_)/ /___   _____
        / // __ \ / ___// // / / // __  // _ \   / /_ / // // _ \ / ___/
       / // / / // /__ / // /_/ // /_/ //  __/  / __// // //  __/(__  )
      /_//_/ /_/ \___//_/ \__,_/ \__,_/ \___/  /_/  /_//_/ \___//____/

   ========================================================================== */


#include <stdio.h>


/* ==========================================================================
                                        __     __ _
                         ____   __  __ / /_   / /(_)_____
                        / __ \ / / / // __ \ / // // ___/
                       / /_/ // /_/ // /_/ // // // /__
                      / .___/ \__,_//_.___//_//_/ \___/
                     /_/

                   ____ ___   ____ _ _____ _____ ____   _____
                  / __ `__ \ / __ `// ___// ___// __ \ / ___/
                 / / / / / // /_/ // /__ / /   / /_/ /(__  )
                /_/ /_/ /_/ \__,_/ \___//_/    \____//____/

   ========================================================================== */


/* ==========================================================================
    macro with definitions, call this macro no more and no less than ONCE in
    global scope. Its task is to define some variables used by mtest macros.
   ========================================================================== */


#define mt_defs()                                                              \
    const char *curr_test;                                                     \
    int mt_test_status;                                                        \
    int mt_total_tests = 0;                                                    \
    int mt_total_failed = 0;                                                   \
    int mt_total_checks = 0;                                                   \
    int mt_checks_failed = 0;                                                  \
    static void (*mt_prepare_test)(void);                                      \
    static void (*mt_cleanup_test)(void)


/* ==========================================================================
    macro with extern declarations of variables defined by mt_defs().   This
    macro should be called in any .c file,  that uses mt_* function and does
    not have mt_defs() called in.
   ========================================================================== */


#define mt_defs_ext()                                                          \
    extern const char *curr_test;                                              \
    extern int mt_test_status;                                                 \
    extern int mt_total_tests;                                                 \
    extern int mt_total_failed;                                                \
    extern int mt_total_checks;                                                \
    extern int mt_checks_failed;                                               \
    static void (*mt_prepare_test)(void);                                      \
    static void (*mt_cleanup_test)(void)


/* ==========================================================================
    macro runs test 'f'. 'f' is just a function (without parenthesis ()).
   ========================================================================== */


#define mt_run(f) do {                                                         \
    mt_run_named(f, #f);                                                       \
    } while (0)


/* ==========================================================================
    macro runs test 'f' and instead of printing function name as a test name
    it allows to provide custom name 'n'
   ========================================================================== */


#define mt_run_named(f, n) do {                                                \
    curr_test = n;                                                             \
    mt_test_status = 0;                                                        \
    ++mt_total_tests;                                                          \
    if (mt_prepare_test) mt_prepare_test();                                    \
    f();                                                                       \
    if (mt_cleanup_test) mt_cleanup_test();                                    \
    if (mt_test_status != 0)                                                   \
    {                                                                          \
        fprintf(stdout, "not ok %d - %s\n", mt_total_tests, curr_test);        \
        ++mt_total_failed;                                                     \
    }                                                                          \
    else                                                                       \
        fprintf(stdout, "ok %d - %s\n", mt_total_tests, curr_test);            \
    } while(0)


/* ==========================================================================
    simple assert, when expression 'e' is evaluated to false, assert message
    will be logged, and macro will force function to return
   ========================================================================== */


#define mt_assert(e) do {                                                      \
    ++mt_total_checks;                                                         \
    if (!(e))                                                                  \
    {                                                                          \
        fprintf(stdout, "# assert [%s:%d] %s, %s\n",                           \
                __FILE__, __LINE__, curr_test, #e);                            \
        mt_test_status = -1;                                                   \
        ++mt_checks_failed;                                                    \
        return;                                                                \
    } } while (0)


/* ==========================================================================
    same as mt_assert, but function is not forced to return,  and  test  can
    continue
   ========================================================================== */


#define mt_fail(e) do {                                                        \
    ++mt_total_checks;                                                         \
    if (!(e))                                                                  \
    {                                                                          \
        fprintf(stdout, "# assert [%s:%d] %s, %s\n",                           \
                __FILE__, __LINE__, curr_test, #e);                            \
        mt_test_status = -1;                                                   \
        ++mt_checks_failed;                                                    \
    } } while (0)


/* ==========================================================================
    shortcut macro to test if function exits with success (with return value
    set to 0)
   ========================================================================== */


#define mt_fok(e) mt_fail(e == 0)


/* ==========================================================================
    shortcut macro to test if function fails as expected, with return code
    set to -1, and expected errno errn
   ========================================================================== */


#define mt_ferr(e, errn) do {                                                  \
    errno = 0;                                                                 \
    mt_fail(e == -1);                                                          \
    mt_fail(errno == errn);                                                    \
    } while (0)

/* ==========================================================================
    prints test plan, in format 1..<number_of_test_run>.  If all tests  have
    passed, macro will return current function with code 0, else it  returns
    number of failed tests.  If number of failed tests exceeds 254, then 254
    will be returned
   ========================================================================== */


#define mt_return() do {                                                       \
    fprintf(stdout, "1..%d\n", mt_total_tests);                                \
    fprintf(stderr, "# total tests.......:%4d\n", mt_total_tests);            \
    fprintf(stderr, "# passed tests......:%4d\n", mt_total_tests - mt_total_failed); \
    fprintf(stderr, "# failed tests......:%4d\n", mt_total_failed);           \
    fprintf(stderr, "# total checks......:%4d\n", mt_total_checks);           \
    fprintf(stderr, "# passed checks.....:%4d\n", mt_total_checks - mt_checks_failed); \
    fprintf(stderr, "# failed checks.....:%4d\n", mt_checks_failed);          \
    return mt_total_failed > 254 ? 254 : mt_total_failed; } while(0)