Coverage Report

Created: 2025-10-10 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/flatbuffers/tests/test_assert.h
Line
Count
Source
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
0
      do { printf(__VA_ARGS__); printf("\n"); } while(!flatbuffers::IsConstTrue(true))
17
#endif
18
19
12.1k
#define TEST_EQ(exp, val) TestEq(exp, val, "'" #exp "' != '" #val "'", __FILE__, __LINE__, "")
20
#define TEST_NE(exp, val) TestNe(exp, val, "'" #exp "' == '" #val "'", __FILE__, __LINE__, "")
21
0
#define TEST_ASSERT(val)  TestEq(true, !!(val), "'" "true" "' != '" #val "'", __FILE__, __LINE__, "")
22
204k
#define TEST_NULL(val) TestEq(true, (val) == nullptr, "'" "nullptr" "' != '" #val "'", __FILE__, __LINE__, "")
23
541
#define TEST_NOTNULL(val) TestEq(true, (val) != nullptr, "'" "nullptr" "' == '" #val "'", __FILE__, __LINE__, "")
24
94.6k
#define TEST_EQ_STR(exp, val) TestEqStr(exp, val, "'" #exp "' != '" #val "'", __FILE__, __LINE__, "")
25
26
#ifdef _WIN32
27
  #define TEST_ASSERT_FUNC(val) TestEq(true, !!(val), "'" "true" "' != '" #val "'", __FILE__, __LINE__, __FUNCTION__)
28
  #define TEST_EQ_FUNC(exp, val) TestEq(exp, val, "'" #exp "' != '" #val "'", __FILE__, __LINE__, __FUNCTION__)
29
#else
30
  #define TEST_ASSERT_FUNC(val) TestEq(true, !!(val), "'" "true" "' != '" #val "'", __FILE__, __LINE__, __PRETTY_FUNCTION__)
31
339k
  #define TEST_EQ_FUNC(exp, val) TestEq(exp, val, "'" #exp "' != '" #val "'", __FILE__, __LINE__, __PRETTY_FUNCTION__)
32
#endif
33
34
// clang-format on
35
36
extern int testing_fails;
37
38
// Listener of TestFail, like 'gtest::OnTestPartResult' event handler.
39
// Called in TestFail after a failed assertion.
40
typedef bool (*TestFailEventListener)(const char* expval, const char* val,
41
                                      const char* exp, const char* file,
42
                                      int line, const char* func);
43
44
// Prepare test engine (MSVC assertion setup, etc).
45
// listener - this function will be notified on each TestFail call.
46
void InitTestEngine(TestFailEventListener listener = nullptr);
47
48
// Release all test-engine resources.
49
// Prints or schedule a debug report if all test passed.
50
// Returns 0 if all tests passed or 1 otherwise.
51
// Memory leak report: FLATBUFFERS_MEMORY_LEAK_TRACKING && _MSC_VER && _DEBUG.
52
int CloseTestEngine(bool force_report = false);
53
54
// Write captured state to a log and terminate test run.
55
void TestFail(const char* expval, const char* val, const char* exp,
56
              const char* file, int line, const char* func = nullptr);
57
58
void TestEqStr(const char* expval, const char* val, const char* exp,
59
               const char* file, int line, const char* func = nullptr);
60
61
// Workaround for `enum class` printing.
62
// There is an issue with the printing of enums with a fixed underlying type.
63
// These enums are generated by `flatc` if `--scoped-enums` is active.
64
// All modern compilers have problems with `std::stringstream&<<(T v)` if T is
65
// an enum with fixed type. For details see DR1601:
66
// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1601
67
// https://stackoverflow.com/questions/34336024/ambiguous-overload-when-writing-an-enum-with-an-enum-base-but-only-with-clang
68
69
template <typename T, bool is_enum_type = flatbuffers::is_enum<T>::value>
70
struct underlying_of_scalar {
71
  static_assert(flatbuffers::is_scalar<T>::value, "invalid type T");
72
  typedef T type;
73
};
74
75
template <typename T>
76
struct underlying_of_scalar<T, true> {
77
  // clang-format off
78
  // There are old compilers without full C++11 support (see stl_emulation.h).
79
  #if defined(FLATBUFFERS_TEMPLATES_ALIASES)
80
  using type = typename std::underlying_type<T>::type;
81
  #else
82
  typedef int64_t type;
83
  #endif
84
  // clang-format on
85
};
86
87
template <typename T>
88
0
typename underlying_of_scalar<T>::type scalar_as_underlying(T v) {
89
0
  return static_cast<typename underlying_of_scalar<T>::type>(v);
90
0
}
91
92
template <typename T, typename U>
93
void TestEq(T expval, U val, const char* exp, const char* file, int line,
94
553k
            const char* func) {
95
553k
  if (static_cast<U>(expval) != val) {
96
0
    TestFail(flatbuffers::NumToString(scalar_as_underlying(expval)).c_str(),
97
0
             flatbuffers::NumToString(scalar_as_underlying(val)).c_str(), exp,
98
0
             file, line, func);
99
0
  }
100
553k
}
101
102
template <>
103
inline void TestEq<std::string, std::string>(std::string expval,
104
                                             std::string val, const char* exp,
105
                                             const char* file, int line,
106
3.62k
                                             const char* func) {
107
3.62k
  if (expval != val) {
108
0
    TestFail(expval.c_str(), val.c_str(), exp, file, line, func);
109
0
  }
110
3.62k
}
111
112
template <typename T, typename U>
113
void TestNe(T expval, U val, const char* exp, const char* file, int line,
114
            const char* func) {
115
  if (static_cast<U>(expval) == val) {
116
    TestFail(flatbuffers::NumToString(scalar_as_underlying(expval)).c_str(),
117
             flatbuffers::NumToString(scalar_as_underlying(val)).c_str(), exp,
118
             file, line, func);
119
  }
120
}
121
122
template <>
123
inline void TestNe<std::string, std::string>(std::string expval,
124
                                             std::string val, const char* exp,
125
                                             const char* file, int line,
126
0
                                             const char* func) {
127
0
  if (expval == val) {
128
0
    TestFail(expval.c_str(), val.c_str(), exp, file, line, func);
129
0
  }
130
0
}
131
132
#endif  // !TEST_ASSERT_H