/src/flatbuffers/tests/test_assert.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "test_assert.h" |
2 | | |
3 | | #include <assert.h> |
4 | | |
5 | | #ifdef _MSC_VER |
6 | | # include <crtdbg.h> |
7 | | # include <windows.h> |
8 | | #endif |
9 | | |
10 | | int testing_fails = 0; |
11 | | static TestFailEventListener fail_listener_ = nullptr; |
12 | | |
13 | | void TestFail(const char *expval, const char *val, const char *exp, |
14 | 0 | const char *file, int line, const char *func) { |
15 | 0 | TEST_OUTPUT_LINE("EXPECTED: \"%s\"", expval); |
16 | 0 | TEST_OUTPUT_LINE("VALUE: \"%s\"", val); |
17 | 0 | TEST_OUTPUT_LINE("TEST FAILED: %s:%d, %s in %s", file, line, exp, |
18 | 0 | func ? func : ""); |
19 | 0 | testing_fails++; |
20 | | |
21 | | // Notify, emulate 'gtest::OnTestPartResult' event handler. |
22 | 0 | if (fail_listener_) (*fail_listener_)(expval, val, exp, file, line, func); |
23 | |
|
24 | 0 | assert(0); // ignored in Release if NDEBUG defined |
25 | 0 | } |
26 | | |
27 | | void TestEqStr(const char *expval, const char *val, const char *exp, |
28 | 0 | const char *file, int line, const char *func) { |
29 | 0 | if (strcmp(expval, val) != 0) { |
30 | 0 | TestFail(expval, val, exp, file, line, func); |
31 | 0 | } |
32 | 0 | } |
33 | | |
34 | | #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING) && defined(_MSC_VER) && \ |
35 | | defined(_DEBUG) |
36 | | # define FLATBUFFERS_MEMORY_LEAK_TRACKING_MSVC |
37 | | #endif |
38 | | |
39 | 2 | void InitTestEngine(TestFailEventListener listener) { |
40 | 2 | testing_fails = 0; |
41 | | // Disable stdout buffering to prevent information lost on assertion or core |
42 | | // dump. |
43 | 2 | setvbuf(stdout, nullptr, _IONBF, 0); |
44 | 2 | setvbuf(stderr, nullptr, _IONBF, 0); |
45 | | |
46 | | // clang-format off |
47 | | |
48 | | #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING_MSVC) |
49 | | // For more thorough checking: |
50 | | // _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF |
51 | | auto flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); |
52 | | _CrtSetDbgFlag(flags | _CRTDBG_ALLOC_MEM_DF); |
53 | | #endif |
54 | | // clang-format on |
55 | | |
56 | 2 | fail_listener_ = listener; |
57 | 2 | } |
58 | | |
59 | 0 | int CloseTestEngine(bool force_report) { |
60 | 0 | if (!testing_fails || force_report) { |
61 | | #if defined(FLATBUFFERS_MEMORY_LEAK_TRACKING_MSVC) |
62 | | auto flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); |
63 | | flags &= ~_CRTDBG_DELAY_FREE_MEM_DF; |
64 | | flags |= _CRTDBG_LEAK_CHECK_DF; |
65 | | _CrtSetDbgFlag(flags); |
66 | | #endif |
67 | 0 | } |
68 | 0 | return (0 != testing_fails); |
69 | 0 | } |