Xonotic
test.qh
Go to the documentation of this file.
1 #pragma once
2 
3 // public:
4 
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 
15 #define SUCCEED() (TEST_ok = true)
16 
18 #define ADD_FAILURE(msg) MACRO_BEGIN \
19  ++TEST_failed; \
20  LOG_WARN(msg); \
21 MACRO_END
22 
24 #define FAIL(msg) _TEST_ASSERT(ADD_FAILURE(msg))
25 
26 #define HasFatalFailure() (TEST_fatal > 0)
27 
28 bool RUN_ALL_TESTS();
29 
30 // difference between expect/assert: assert returns early
31 
32 #define EXPECT_EQ(expected_, actual_) MACRO_BEGIN \
33  int expected = expected_; \
34  int actual = actual_; \
35  if ((expected) != (actual)) { \
36  ADD_FAILURE(sprintf( \
37  "Value of: " #actual_ "\n" \
38  " Actual: %d\n" \
39  "Expected: %d\n", \
40  actual, expected \
41  )); \
42  } \
43 MACRO_END
44 #define ASSERT_EQ(expected, actual) _TEST_ASSERT(EXPECT_EQ(expected, actual))
45 
46 #define EXPECT_TRUE(condition) EXPECT_EQ(true, condition)
47 #define ASSERT_TRUE(condition) ASSERT_EQ(true, condition)
48 
49 #define EXPECT_FALSE(condition) EXPECT_EQ(false, condition)
50 #define ASSERT_FALSE(condition) ASSERT_EQ(false, condition)
51 
52 #define EXPECT_NE(val1, val2) EXPECT_TRUE((val1) != (val2))
53 #define ASSERT_NE(val1, val2) _TEST_ASSERT(EXPECT_NE(val1, val2))
54 
55 #define EXPECT_LT(val1, val2) EXPECT_TRUE((val1) < (val2))
56 #define ASSERT_LT(val1, val2) _TEST_ASSERT(EXPECT_LT(val1, val2))
57 
58 #define EXPECT_LE(val1, val2) EXPECT_TRUE((val1) <= (val2))
59 #define ASSERT_LE(val1, val2) _TEST_ASSERT(EXPECT_LE(val1, val2))
60 
61 #define EXPECT_GT(val1, val2) EXPECT_TRUE((val1) > (val2))
62 #define ASSERT_GT(val1, val2) _TEST_ASSERT(EXPECT_GT(val1, val2))
63 
64 #define EXPECT_GE(val1, val2) EXPECT_TRUE((val1) >= (val2))
65 #define ASSERT_GE(val1, val2) _TEST_ASSERT(EXPECT_GE(val1, val2))
66 
67 #define EXPECT_NO_FATAL_FAILURE(statement) EXPECT_NO_FATAL_FAILURE_(statement, { })
68 #define ASSERT_NO_FATAL_FAILURE(statement) EXPECT_NO_FATAL_FAILURE_(statement, { ++TEST_fatal; return; })
69 
70 // private:
71 
72 bool TEST_Run(string test);
74 bool TEST_ok;
76 
77 #define _TEST_ASSERT(statement) \
78  MACRO_BEGIN \
79  LAMBDA(statement); \
80  ++TEST_fatal; return; \
81  MACRO_END
82 
83 #define EXPECT_NO_FATAL_FAILURE__(statement, then) \
84  MACRO_BEGIN \
85  int TEST_prevfatal = TEST_fatal; \
86  LAMBDA(statement); \
87  if (TEST_fatal != TEST_prevfatal) \
88  LAMBDA(then); \
89  MACRO_END
90 
91 #define EXPECT_NO_FATAL_FAILURE_(statement, then) \
92  EXPECT_NO_FATAL_FAILURE__(statement, { \
93  LOG_WARNF( \
94  " Actual: %d fatal failures\n" \
95  "Expected: no fatal failures\n", \
96  TEST_fatal - TEST_prevfatal \
97  ); \
98  LAMBDA(then); \
99  })
bool TEST_Run(string test)
Definition: test.qc:19
int TEST_failed
Definition: test.qh:75
int TEST_fatal
Definition: test.qh:73
bool RUN_ALL_TESTS()
Definition: test.qc:4
bool TEST_ok
Definition: test.qh:74