]> git.lizzy.rs Git - nothing.git/blob - test/test.h
(#458) Unit test read_all_exprs_from_string
[nothing.git] / test / test.h
1 #ifndef TEST_H_
2 #define TEST_H_
3
4 #include "math.h"
5
6 #define TEST_RUN(name)                          \
7     if (name() < 0) {                           \
8         return -1;                              \
9     }
10
11 #define TEST_IGNORE(name)                       \
12     (void)(name)                                \
13
14 #define TEST(name)                              \
15     static int name##_body(void);               \
16     static int name(void) {                     \
17         printf("%s() ...", #name);              \
18         if (name##_body() == 0) {               \
19             printf(" OK\n");                    \
20             return 0;                           \
21         } else {                                \
22             printf(" FAILED\n");                \
23             return -1;                          \
24         }                                       \
25     }                                           \
26     static int name##_body(void)
27
28 // TODO(#308): ASSERT_* macros evaluate expressions several times
29
30 #define ASSERT_STREQN(expected, actual, n)                              \
31     if (strncmp(expected, actual, n) != 0) {                            \
32         fprintf(stderr, "\n%s:%d: ASSERT_STREQN: \n",                   \
33                 __FILE__, __LINE__);                                    \
34         fprintf(stderr, "  Expected: ");                                \
35         fwrite(expected, sizeof(char), n, stderr);                      \
36         fprintf(stderr, "\n");                                          \
37         fprintf(stderr, "  Actual: ");                                  \
38         fwrite(actual, sizeof(char), n, stderr);                        \
39         fprintf(stderr, "\n");                                          \
40         return -1;                                                      \
41     }
42
43 #define ASSERT_STREQ(expected, actual)                                  \
44     if (strcmp(expected, actual) != 0) {                                \
45         fprintf(stderr, "\n%s:%d: ASSERT_STREQ: \n",                    \
46                 __FILE__, __LINE__);                                    \
47         fprintf(stderr, "  Expected: %s\n", expected);                  \
48         fprintf(stderr, "  Actual: %s\n", actual);                      \
49         return -1;                                                      \
50     }
51
52 #define ASSERT_INTEQ(expected, actual)                                  \
53     if (expected != actual) {                                           \
54         fprintf(stderr, "\n%s:%d: ASSERT_INTEQ: \n",                    \
55                 __FILE__, __LINE__);                                    \
56         fprintf(stderr, "  Expected: %d\n", expected);                  \
57         fprintf(stderr, "  Actual: %d\n", actual);                      \
58         return -1;                                                      \
59     }
60
61 #define ASSERT_LONGINTEQ(expected, actual)                              \
62     if (expected != actual) {                                           \
63         fprintf(stderr, "\n%s:%d: ASSERT_LONGINTEQ: \n",                \
64                 __FILE__, __LINE__);                                    \
65         fprintf(stderr, "  Expected: %ld\n", expected);                 \
66         fprintf(stderr, "  Actual: %ld\n", actual);                     \
67         return -1;                                                      \
68     }
69
70
71 #define ASSERT_FLOATEQ(expected, actual, margin)                        \
72     if (fabsf(expected - actual) > margin) {                            \
73         fprintf(stderr, "\n%s:%d: ASSERT_FLOATEQ: \n",                  \
74                 __FILE__, __LINE__);                                    \
75         fprintf(stderr, "  Expected: %f\n", expected);                  \
76         fprintf(stderr, "  Actual: %f\n", actual);                      \
77         fprintf(stderr, "  Margin: %f\n", margin);                      \
78         return -1;                                                      \
79     }
80
81 #define ASSERT_EQ(type, expected, actual, handler)                      \
82     {                                                                   \
83         type _expected = (expected);                                    \
84         type _actual = (actual);                                        \
85         if (_expected != _actual) {                                     \
86             fprintf(stderr, "\n%s:%d: ASSERT_EQ: \n",                   \
87                     __FILE__, __LINE__);                                \
88             handler                                                     \
89             return -1;                                                  \
90         }                                                               \
91     }
92
93 #define ASSERT_TRUE(condition, message)                                 \
94     if (!(condition)) {                                                 \
95         fprintf(stderr, "\n%s:%d: ASSERT_TRUE: false\n",                \
96                 __FILE__, __LINE__);                                    \
97         fprintf(stderr, "%s\n", message);                               \
98         return -1;                                                      \
99     }
100
101 #define ASSERT_FALSE(condition, handler)                                \
102     if (condition) {                                                    \
103         fprintf(stderr, "\n%s:%d: ASSERT_FALSE: false\n",               \
104                 __FILE__, __LINE__);                                    \
105         handler                                                         \
106         return -1;                                                      \
107     }
108
109 #define TEST_SUITE(name)                        \
110     static int name##_body(void);               \
111     static int name(void) {                     \
112         if (name##_body() < 0) {                \
113             return -1;                          \
114         }                                       \
115         return 0;                               \
116     }                                           \
117     static int name##_body(void)
118
119 #define TEST_MAIN()                             \
120     static int main_body(void);                 \
121     int main(void) {                            \
122         if (main_body() < 0) {                  \
123             return -1;                          \
124         }                                       \
125         return 0;                               \
126     }                                           \
127     static int main_body(void)
128
129 #endif  // TEST_H_