Repository navigation
Expand file tree
/
Copy pathtest_assert.cpp
More file actions
106 lines (89 loc) · 2.79 KB
/
Copy pathtest_assert.cpp
File metadata and controls
106 lines (89 loc) · 2.79 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "test_assert.h"
#include <assert.h>
#ifdef _MSC_VER
#include <crtdbg.h>
#include <windows.h>
#else
#include <string.h>
#endif
int testing_fails = 0;
int testing_success = 0;
static TestFailEventListener fail_listener_ = nullptr;
void TestFail(const char *expval, const char *val, const char *file, int line, const char *func)
{
TEST_OUTPUT_LINE("VALUE:\n%s", val);
TEST_OUTPUT_LINE("EXPECTED:\n%s", expval);
if (func)
{
TEST_OUTPUT_LINE("%s(%d): error: Failure in %s: expected %s but was %s", file, line, func, expval, val);
}
else
{
TEST_OUTPUT_LINE("%s(%d): error: expected %s but was %s", file, line, expval, val);
}
++testing_fails;
// Notify, emulate 'gtest::OnTestPartResult' event handler.
if (fail_listener_)
(*fail_listener_)(expval, val, file, line, func);
//assert(0); // ignored in Release if NDEBUG defined
}
void TestEqStr(const char *expval, const char *val, const char *file, int line)
{
if (strcmp(expval, val) != 0)
{
TestFail(expval, val, file, line);
}
else
++testing_success;
}
#if defined(MEMORY_LEAK_TRACKING) && defined(_MSC_VER)
#ifndef NDEBUG
#define MEMORY_LEAK_TRACKING_MSVC
#endif
#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);
#ifdef _MSC_VER
// By default, send all reports to STDOUT to prevent CI hangs.
// Enable assert report box [Abort|Retry|Ignore] if a debugger is present.
const int dbg_mode = (_CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG) |
(IsDebuggerPresent() ? _CRTDBG_MODE_WNDW : 0);
(void)dbg_mode; // release mode fix
// CrtDebug reports to _CRT_WARN channel.
_CrtSetReportMode(_CRT_WARN, dbg_mode);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
// The assert from <assert.h> reports to _CRT_ERROR channel
_CrtSetReportMode(_CRT_ERROR, dbg_mode);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
// Internal CRT assert channel?
_CrtSetReportMode(_CRT_ASSERT, dbg_mode);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
#endif
// clang-format off
#if defined(MEMORY_LEAK_TRACKING_MSVC)
// For more thorough checking:
// _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF
auto flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
_CrtSetDbgFlag(flags | _CRTDBG_ALLOC_MEM_DF);
#endif
// clang-format on
fail_listener_ = listener;
}
int CloseTestEngine(bool force_report)
{
if (!testing_fails || force_report)
{
#if defined(MEMORY_LEAK_TRACKING_MSVC)
auto flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flags &= ~_CRTDBG_DELAY_FREE_MEM_DF;
flags |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(flags);
#endif
}
return (0 != testing_fails);
}