forked from google/flatbuffers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_assert.cpp
More file actions
59 lines (48 loc) · 1.83 KB
/
test_assert.cpp
File metadata and controls
59 lines (48 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <assert.h>
#include "test_assert.h"
#ifdef _MSC_VER
# include <crtdbg.h>
#endif
int testing_fails = 0;
static TestFailEventListener fail_listener_ = nullptr;
void TestFail(const char *expval, const char *val, const char *exp,
const char *file, int line, const char *func) {
TEST_OUTPUT_LINE("VALUE: \"%s\"", expval);
TEST_OUTPUT_LINE("EXPECTED: \"%s\"", val);
TEST_OUTPUT_LINE("TEST FAILED: %s:%d, %s in %s", file, line, exp,
func ? func : "");
testing_fails++;
// Notify, emulate 'gtest::OnTestPartResult' event handler.
if (fail_listener_) (*fail_listener_)(expval, val, exp, file, line, func);
assert(0); // ignored in Release if NDEBUG defined
}
void TestEqStr(const char *expval, const char *val, const char *exp,
const char *file, int line) {
if (strcmp(expval, val) != 0) { TestFail(expval, val, exp, file, line); }
}
#ifdef _MSC_VER
// Without this hook function the message box not suppressed.
int msvc_no_dialog_box_on_assert(int rpt_type, char *msg, int *ret_val) {
(void)ret_val;
TEST_OUTPUT_LINE("TEST ASSERTED: %d: %s", rpt_type, msg);
return 1;
}
#endif
void InitTestEngine(TestFailEventListener listener) {
testing_fails = 0;
// Disable stdout buffering to prevent information lost on assertion or core
// dump.
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
// clang-format off
#ifdef _MSC_VER
// Suppress pop-up message box on assertion (MSVC2010, MSVC2012).
// This message box hangs CI-test on the hour until timeout expired.
// Default mode is file, file is stderr.
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
_CrtSetReportHook(msvc_no_dialog_box_on_assert);
#endif
// clang-format on
fail_listener_ = listener;
}