8d8de76ec6119abecd88093b0bd52bd8e1bb08e3
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / test.qh
1 #pragma once
2
3 // public:
4
5 /** Use UpperCamelCase for suite and test only */
6 #define TEST(suite, test) \
7         void _TEST_##suite##_##test(); \
8         ACCUMULATE int TEST_RunAll_accumulated(int f) { \
9                 if (!TEST_Run(#suite "_" #test)) ++f; \
10                 return = f; \
11         } \
12         void _TEST_##suite##_##test()
13
14 /** Must be present at the end of a test */
15 #define SUCCEED() (TEST_ok = true)
16
17 /** Add a failure, but continue */
18 #define ADD_FAILURE(msg) MACRO_BEGIN { ++TEST_failed; LOG_WARN(msg); } MACRO_END
19
20 /** Add a failure and return */
21 #define FAIL(msg) _TEST_ASSERT(ADD_FAILURE(msg))
22
23 #define HasFatalFailure() (TEST_fatal > 0)
24
25 bool RUN_ALL_TESTS();
26
27 // difference between expect/assert: assert returns early
28
29 #define EXPECT_EQ(expected_, actual_) MACRO_BEGIN { \
30         int expected = expected_; \
31         int actual = actual_; \
32         if ((expected) != (actual)) { \
33                 ADD_FAILURE(sprintf( \
34                         "Value of: " #actual_ "\n" \
35                         "  Actual: %d\n" \
36                         "Expected: %d\n", \
37                         actual, expected \
38                 )); \
39         } \
40 } MACRO_END
41 #define ASSERT_EQ(expected, actual) _TEST_ASSERT(EXPECT_EQ(expected, actual))
42
43 #define EXPECT_TRUE(condition) EXPECT_EQ(true, condition)
44 #define ASSERT_TRUE(condition) ASSERT_EQ(true, condition)
45
46 #define EXPECT_FALSE(condition) EXPECT_EQ(false, condition)
47 #define ASSERT_FALSE(condition) ASSERT_EQ(false, condition)
48
49 #define EXPECT_NE(val1, val2) EXPECT_TRUE((val1) != (val2))
50 #define ASSERT_NE(val1, val2) _TEST_ASSERT(EXPECT_NE(val1, val2))
51
52 #define EXPECT_LT(val1, val2) EXPECT_TRUE((val1) < (val2))
53 #define ASSERT_LT(val1, val2) _TEST_ASSERT(EXPECT_LT(val1, val2))
54
55 #define EXPECT_LE(val1, val2) EXPECT_TRUE((val1) <= (val2))
56 #define ASSERT_LE(val1, val2) _TEST_ASSERT(EXPECT_LE(val1, val2))
57
58 #define EXPECT_GT(val1, val2) EXPECT_TRUE((val1) > (val2))
59 #define ASSERT_GT(val1, val2) _TEST_ASSERT(EXPECT_GT(val1, val2))
60
61 #define EXPECT_GE(val1, val2) EXPECT_TRUE((val1) >= (val2))
62 #define ASSERT_GE(val1, val2) _TEST_ASSERT(EXPECT_GE(val1, val2))
63
64 #define EXPECT_NO_FATAL_FAILURE(statement) EXPECT_NO_FATAL_FAILURE_(statement, { })
65 #define ASSERT_NO_FATAL_FAILURE(statement) EXPECT_NO_FATAL_FAILURE_(statement, { ++TEST_fatal; return; })
66
67 // private:
68
69 bool TEST_Run(string test);
70 int TEST_fatal;
71 bool TEST_ok;
72 int TEST_failed;
73
74 #define _TEST_ASSERT(statement) \
75         MACRO_BEGIN { \
76                 LAMBDA(statement); \
77                 ++TEST_fatal; return; \
78         } MACRO_END
79
80 #define EXPECT_NO_FATAL_FAILURE__(statement, then) \
81         MACRO_BEGIN { \
82                 int TEST_prevfatal = TEST_fatal; \
83                 LAMBDA(statement); \
84                 if (TEST_fatal != TEST_prevfatal) \
85                         LAMBDA(then); \
86         } MACRO_END
87
88 #define EXPECT_NO_FATAL_FAILURE_(statement, then) \
89         EXPECT_NO_FATAL_FAILURE__(statement, { \
90                 LOG_WARNF( \
91                         "  Actual: %d fatal failures\n" \
92                         "Expected: no fatal failures\n", \
93                         TEST_fatal - TEST_prevfatal \
94                 ); \
95                 LAMBDA(then); \
96         })