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