File: minunit.h

package info (click to toggle)
stringencoders 3.10.3%2Bgit20180306-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,132 kB
  • sloc: ansic: 17,159; javascript: 1,067; cpp: 407; python: 319; makefile: 180; sh: 34
file content (82 lines) | stat: -rw-r--r-- 4,977 bytes parent folder | download | duplicates (3)
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

#ifndef MINUNIT_H
#define MINUNIT_H

/* http://www.jera.com/techinfo/jtns/jtn002.html */

#define mu_assert(test)                                                                      \
    do {                                                                                     \
        if (!(test)) {                                                                       \
            snprintf(mu_buf, sizeof(mu_buf), "ASSERTION FAILED: %s:%d", __FILE__, __LINE__); \
            return mu_buf;                                                                   \
        }                                                                                    \
    } while (0)

#define mu_assert_msg(msg, test)                                                                     \
    do {                                                                                             \
        if (!(test)) {                                                                               \
            snprintf(mu_buf, sizeof(mu_buf), "ASSERTION FAILED: %s %s:%d", msg, __FILE__, __LINE__); \
            return mu_buf;                                                                           \
        }                                                                                            \
    } while (0)

#define mu_assert_int_equals(lhs, rhs)                                                       \
    do {                                                                                     \
        if ((int)lhs != (int)rhs) {                                                          \
            snprintf(mu_buf, sizeof(mu_buf), "ASSERTION FAILED: %s:%d", __FILE__, __LINE__); \
            return mu_buf;                                                                   \
        }                                                                                    \
    } while (0)

#define mu_assert_str_equals(lhs, rhs)                                                                          \
    do {                                                                                                        \
        if (strcmp(lhs, rhs) != 0) {                                                                            \
            snprintf(mu_buf, sizeof(mu_buf), "ASSERTION FAILED: %s:%d %s != %s", __FILE__, __LINE__, lhs, rhs); \
            return mu_buf;                                                                                      \
        }                                                                                                       \
    } while (0)

#define mu_assert_str_equals_msg(msg, lhs, rhs)                                                                          \
    do {                                                                                                                 \
        if (strcmp(lhs, rhs) != 0) {                                                                                     \
            snprintf(mu_buf, sizeof(mu_buf), "ASSERTION FAILED: %s  %s:%d %s != %s", msg, __FILE__, __LINE__, lhs, rhs); \
            return mu_buf;                                                                                               \
        }                                                                                                                \
    } while (0)

#define mu_assert_int_equals_msg(msg, lhs, rhs)                                                       \
    do {                                                                                              \
        if (lhs != rhs) {                                                                             \
            snprintf(mu_buf, sizeof(mu_buf), "ASSERTION FAILED: %s  %s:%d", msg, __FILE__, __LINE__); \
            return mu_buf;                                                                            \
        }                                                                                             \
    } while (0)

#define mu_run_test(test)       \
    do {                        \
        printf(".");            \
        fflush(stdout);         \
        char* message = test(); \
        tests_run++;            \
        if (message)            \
            return message;     \
    } while (0)

#define UNITTESTS                                 \
    int main(void)                                \
    {                                             \
        printf("%s ", __FILE__);                  \
        fflush(stdout);                           \
        char* result = all_tests();               \
        if (result != 0) {                        \
            printf("%s\n", result);               \
        } else {                                  \
            printf("OK (%d tests)\n", tests_run); \
        }                                         \
        return result != 0;                       \
    }

static int tests_run = 0;
static char mu_buf[1024];

#endif