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       do { printf(__VA_ARGS__); printf("\n"); } while(!IsConstTrue(true))
17 #endif
18 
19 #define TEST_EQ(exp, val) TestEq(exp, val, "'" #exp "' != '" #val "'", __FILE__, __LINE__, "")
20 #define TEST_ASSERT(val)  TestEq(true, !!(val), "'" "true" "' != '" #val "'", __FILE__, __LINE__, "")
21 #define TEST_NOTNULL(val) TestEq(true, (val) != nullptr, "'" "nullptr" "' == '" #val "'", __FILE__, __LINE__, "")
22 #define TEST_EQ_STR(exp, val) TestEqStr(exp, val, "'" #exp "' != '" #val "'", __FILE__, __LINE__, "")
23 
24 #ifdef _WIN32
25   #define TEST_ASSERT_FUNC(val) TestEq(true, !!(val), "'" "true" "' != '" #val "'", __FILE__, __LINE__, __FUNCTION__)
26   #define TEST_EQ_FUNC(exp, val) TestEq(exp, val, "'" #exp "' != '" #val "'", __FILE__, __LINE__, __FUNCTION__)
27 #else
28   #define TEST_ASSERT_FUNC(val) TestEq(true, !!(val), "'" "true" "' != '" #val "'", __FILE__, __LINE__, __PRETTY_FUNCTION__)
29   #define TEST_EQ_FUNC(exp, val) TestEq(exp, val, "'" #exp "' != '" #val "'", __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, const char *func = 0);
58 
59 // Workaround for `enum class` printing.
60 // There is an issue with the printing of enums with a fixed underlying type.
61 // These enums are generated by `flatc` if `--scoped-enums` is active.
62 // All modern compilers have problems with `std::stringstream&<<(T v)` if T is
63 // an enum with fixed type. For details see DR1601:
64 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1601
65 // https://stackoverflow.com/questions/34336024/ambiguous-overload-when-writing-an-enum-with-an-enum-base-but-only-with-clang
66 
67 template<typename T, bool is_enum_type = flatbuffers::is_enum<T>::value>
68 struct underlying_of_scalar {
69   static_assert(flatbuffers::is_scalar<T>::value, "invalid type T");
70   typedef T type;
71 };
72 
73 template<typename T> struct underlying_of_scalar<T, true> {
74 // clang-format off
75   // There are old compilers without full C++11 support (see stl_emulation.h).
76   #if defined(FLATBUFFERS_TEMPLATES_ALIASES) && !defined(FLATBUFFERS_CPP98_STL)
77   using type = typename std::underlying_type<T>::type;
78   #else
79   typedef int64_t type;
80   #endif
81   // clang-format on
82 };
83 
84 template<typename T>
85 typename underlying_of_scalar<T>::type scalar_as_underlying(T v) {
86   return static_cast<typename underlying_of_scalar<T>::type>(v);
87 }
88 
89 template<typename T, typename U>
90 void TestEq(T expval, U val, const char *exp, const char *file, int line,
91             const char *func) {
92   if (static_cast<U>(expval) != val) {
93     TestFail(flatbuffers::NumToString(scalar_as_underlying(expval)).c_str(),
94              flatbuffers::NumToString(scalar_as_underlying(val)).c_str(), exp,
95              file, line, func);
96   }
97 }
98 
99 template<>
100 inline void TestEq<std::string, std::string>(std::string expval,
101                                              std::string val, const char *exp,
102                                              const char *file, int line,
103                                              const char *func) {
104   if (expval != val) {
105     TestFail(expval.c_str(), val.c_str(), exp, file, line, func);
106   }
107 }
108 
109 #endif  // !TEST_ASSERT_H
110