• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef TEST_ASSERT_H
2 #define TEST_ASSERT_H
3 
4 #include "flatbuffers/base.h"
5 #include "flatbuffers/util.h"
6 
7 // clang-format off
8 
9 #ifdef __ANDROID__
10   #include <android/log.h>
11   #define TEST_OUTPUT_LINE(...) \
12       __android_log_print(ANDROID_LOG_INFO, "FlatBuffers", __VA_ARGS__)
13   #define FLATBUFFERS_NO_FILE_TESTS
14 #else
15   #define TEST_OUTPUT_LINE(...) \
16       { printf(__VA_ARGS__); printf("\n"); }
17 #endif
18 
19 #define TEST_EQ(exp, val) TestEq(exp, val, #exp, __FILE__, __LINE__)
20 #define TEST_ASSERT(exp) TestEq(exp, true, #exp, __FILE__, __LINE__)
21 #define TEST_NOTNULL(exp) TestEq(exp == NULL, false, #exp, __FILE__, __LINE__)
22 #define TEST_EQ_STR(exp, val) TestEqStr(exp, val, #exp, __FILE__, __LINE__)
23 
24 #ifdef _WIN32
25   #define TEST_ASSERT_FUNC(exp) TestEq(exp, true, #exp, __FILE__, __LINE__, __FUNCTION__)
26   #define TEST_EQ_FUNC(exp, val) TestEq(exp, val, #exp, __FILE__, __LINE__, __FUNCTION__)
27 #else
28   #define TEST_ASSERT_FUNC(exp) TestEq(exp, true, #exp, __FILE__, __LINE__, __PRETTY_FUNCTION__)
29   #define TEST_EQ_FUNC(exp, val) TestEq(exp, val, #exp, __FILE__, __LINE__, __PRETTY_FUNCTION__)
30 #endif
31 
32 // clang-format on
33 
34 extern int testing_fails;
35 
36 // Listener of TestFail, like 'gtest::OnTestPartResult' event handler.
37 // Called in TestFail after a failed assertion.
38 typedef bool (*TestFailEventListener)(const char *expval, const char *val,
39                                       const char *exp, const char *file,
40                                       int line, const char *func);
41 
42 // Prepare test engine (MSVC assertion setup, etc).
43 // listener - this function will be notified on each TestFail call.
44 void InitTestEngine(TestFailEventListener listener = nullptr);
45 
46 // Release all test-engine resources.
47 // Prints or schedule a debug report if all test passed.
48 // Returns 0 if all tests passed or 1 otherwise.
49 // Memory leak report: FLATBUFFERS_MEMORY_LEAK_TRACKING && _MSC_VER && _DEBUG.
50 int CloseTestEngine(bool force_report = false);
51 
52 // Write captured state to a log and terminate test run.
53 void TestFail(const char *expval, const char *val, const char *exp,
54               const char *file, int line, const char *func = 0);
55 
56 void TestEqStr(const char *expval, const char *val, const char *exp,
57                const char *file, int line);
58 
59 template<typename T, typename U>
60 void TestEq(T expval, U val, const char *exp, const char *file, int line,
61             const char *func = 0) {
62   if (U(expval) != val) {
63     TestFail(flatbuffers::NumToString(expval).c_str(),
64              flatbuffers::NumToString(val).c_str(), exp, file, line, func);
65   }
66 }
67 
68 #endif  // !TEST_ASSERT_H
69