-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathtest.cpp
More file actions
334 lines (262 loc) · 11 KB
/
test.cpp
File metadata and controls
334 lines (262 loc) · 11 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright Antony Polukhin, 2016-2026.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/stacktrace/stacktrace_fwd.hpp>
#include <boost/stacktrace.hpp>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cctype>
#include <boost/core/lightweight_test.hpp>
#include <boost/functional/hash.hpp>
#include "test_impl.hpp"
using boost::stacktrace::stacktrace;
using boost::stacktrace::frame;
#if (defined(BOOST_GCC) && defined(BOOST_WINDOWS) && !defined(BOOST_STACKTRACE_USE_BACKTRACE) && !defined(BOOST_STACKTRACE_USE_ADDR2LINE)) \
|| defined(BOOST_STACKTRACE_TEST_NO_DEBUG_AT_ALL)
# define BOOST_STACKTRACE_TEST_SHOULD_OUTPUT_READABLE_NAMES 0
#else
# define BOOST_STACKTRACE_TEST_SHOULD_OUTPUT_READABLE_NAMES 1
#endif
void test_deeply_nested_namespaces() {
std::stringstream ss;
ss << return_from_nested_namespaces();
std::cout << ss.str() << '\n';
#if BOOST_STACKTRACE_TEST_SHOULD_OUTPUT_READABLE_NAMES
BOOST_TEST(ss.str().find("main") != std::string::npos);
BOOST_TEST(ss.str().find("get_backtrace_from_nested_namespaces") != std::string::npos
|| ss.str().find("1# return_from_nested_namespaces") != std::string::npos); // GCC with -O1 has strange inlining, so this line is true while the prev one is false.
BOOST_TEST(ss.str().find("return_from_nested_namespaces") != std::string::npos);
#endif
stacktrace ns1 = return_from_nested_namespaces();
BOOST_TEST(ns1 != return_from_nested_namespaces()); // Different addresses in test_deeply_nested_namespaces() function
}
std::size_t count_unprintable_chars(const std::string& s) {
std::size_t result = 0;
for (std::size_t i = 0; i < s.size(); ++i) {
result += (std::isprint(s[i]) ? 0 : 1);
}
return result;
}
void test_frames_string_data_validity() {
stacktrace trace = return_from_nested_namespaces();
for (std::size_t i = 0; i < trace.size(); ++i) {
BOOST_TEST_EQ(count_unprintable_chars(trace[i].source_file()), 0);
BOOST_TEST_EQ(count_unprintable_chars(trace[i].name()), 0);
}
BOOST_TEST(to_string(trace).find('\0') == std::string::npos);
}
// Template parameter Depth is to produce different functions on each Depth. This simplifies debugging when one of the tests catches error
template <std::size_t Depth>
void test_nested(bool print = true) {
std::pair<stacktrace, stacktrace> res = function_from_library(Depth, function_from_main_translation_unit);
std::stringstream ss1, ss2;
ss1 << res.first;
ss2 << res.second;
if (print) {
std::cout << "'" << ss1.str() << "'\n\n" << ss2.str() << std::endl;
}
BOOST_TEST(!ss1.str().empty());
BOOST_TEST(!ss2.str().empty());
BOOST_TEST(ss1.str().find(" 0# ") != std::string::npos);
BOOST_TEST(ss2.str().find(" 0# ") != std::string::npos);
BOOST_TEST(ss1.str().find(" 1# ") != std::string::npos);
BOOST_TEST(ss2.str().find(" 1# ") != std::string::npos);
BOOST_TEST(ss1.str().find(" in ") != std::string::npos);
BOOST_TEST(ss2.str().find(" in ") != std::string::npos);
#if BOOST_STACKTRACE_TEST_SHOULD_OUTPUT_READABLE_NAMES
BOOST_TEST(ss1.str().find("main") != std::string::npos);
BOOST_TEST(ss2.str().find("main") != std::string::npos);
BOOST_TEST(ss1.str().find("function_from_library") != std::string::npos);
BOOST_TEST(ss2.str().find("function_from_library") != std::string::npos);
BOOST_TEST(ss1.str().find("function_from_main_translation_unit") != std::string::npos);
BOOST_TEST(ss2.str().find("function_from_main_translation_unit") != std::string::npos);
#endif
}
template <class Bt>
void test_comparisons_base(Bt nst, Bt st) {
Bt cst(st);
st = st;
cst = cst;
BOOST_TEST(nst);
BOOST_TEST(st);
#if !defined(BOOST_MSVC) && !defined(BOOST_STACKTRACE_USE_WINDBG)
// This is very dependent on compiler and link flags. No sane way to make it work, because
// BOOST_NOINLINE could be ignored by MSVC compiler if link-time optimization is enabled.
BOOST_TEST(nst[0] != st[0]);
#endif
BOOST_TEST(nst != st);
BOOST_TEST(st != nst);
BOOST_TEST(st == st);
BOOST_TEST(nst == nst);
BOOST_TEST(nst != cst);
BOOST_TEST(cst != nst);
BOOST_TEST(cst == st);
BOOST_TEST(cst == cst);
BOOST_TEST(nst < st || nst > st);
BOOST_TEST(st < nst || nst < st);
BOOST_TEST(st <= st);
BOOST_TEST(nst <= nst);
BOOST_TEST(st >= st);
BOOST_TEST(nst >= nst);
BOOST_TEST(nst < cst || cst < nst);
BOOST_TEST(nst > cst || cst > nst);
BOOST_TEST(hash_value(nst) == hash_value(nst));
BOOST_TEST(hash_value(cst) == hash_value(st));
BOOST_TEST(hash_value(nst) != hash_value(cst));
BOOST_TEST(hash_value(st) != hash_value(nst));
}
void test_comparisons() {
stacktrace nst = return_from_nested_namespaces();
stacktrace st;
test_comparisons_base(nst, st);
}
void test_iterators() {
stacktrace st;
BOOST_TEST(st.begin() == st.begin());
BOOST_TEST(st.cbegin() == st.cbegin());
BOOST_TEST(st.crbegin() == st.crbegin());
BOOST_TEST(st.rbegin() == st.rbegin());
BOOST_TEST(st.begin() + 1 == st.begin() + 1);
BOOST_TEST(st.cbegin() + 1 == st.cbegin() + 1);
BOOST_TEST(st.crbegin() + 1 == st.crbegin() + 1);
BOOST_TEST(st.rbegin() + 1 == st.rbegin() + 1);
BOOST_TEST(st.end() == st.end());
BOOST_TEST(st.cend() == st.cend());
BOOST_TEST(st.crend() == st.crend());
BOOST_TEST(st.rend() == st.rend());
BOOST_TEST(st.end() > st.begin());
BOOST_TEST(st.end() > st.cbegin());
BOOST_TEST(st.cend() > st.cbegin());
BOOST_TEST(st.cend() > st.begin());
BOOST_TEST(st.size() == static_cast<std::size_t>(st.end() - st.begin()));
BOOST_TEST(st.size() == static_cast<std::size_t>(st.end() - st.cbegin()));
BOOST_TEST(st.size() == static_cast<std::size_t>(st.cend() - st.cbegin()));
BOOST_TEST(st.size() == static_cast<std::size_t>(st.cend() - st.begin()));
BOOST_TEST(st.size() == static_cast<std::size_t>(std::distance(st.rbegin(), st.rend())));
BOOST_TEST(st.size() == static_cast<std::size_t>(std::distance(st.crbegin(), st.rend())));
BOOST_TEST(st.size() == static_cast<std::size_t>(std::distance(st.crbegin(), st.crend())));
BOOST_TEST(st.size() == static_cast<std::size_t>(std::distance(st.rbegin(), st.crend())));
boost::stacktrace::stacktrace::iterator it = st.begin();
++ it;
BOOST_TEST(it == st.begin() + 1);
}
void test_frame() {
stacktrace nst = return_from_nested_namespaces();
stacktrace st = make_some_stacktrace1();
const std::size_t min_size = (nst.size() < st.size() ? nst.size() : st.size());
BOOST_TEST(min_size > 2);
for (std::size_t i = 0; i < min_size; ++i) {
BOOST_TEST(st[i] == st[i]);
BOOST_TEST(st[i].source_file() == st[i].source_file());
BOOST_TEST(st[i].source_line() == st[i].source_line());
BOOST_TEST(st[i] <= st[i]);
BOOST_TEST(st[i] >= st[i]);
frame fv = nst[i];
BOOST_TEST(fv);
if (i > 1 && i < min_size - 3) { // Begin ...and end of the trace may match, skipping
BOOST_TEST(st[i] != fv);
#if !(defined(BOOST_STACKTRACE_TEST_NO_DEBUG_AT_ALL) && defined(BOOST_MSVC))
// MSVC can not get function name withhout debug symbols even if it is exported
BOOST_TEST(st[i].name() != fv.name());
BOOST_TEST(st[i] != fv);
BOOST_TEST(st[i] < fv || st[i] > fv);
BOOST_TEST(hash_value(st[i]) != hash_value(fv));
#endif
if (st[i].source_line()) {
BOOST_TEST(st[i].source_file() != fv.source_file() || st[i].source_line() != fv.source_line());
}
BOOST_TEST(st[i]);
}
fv = st[i];
BOOST_TEST(hash_value(st[i]) == hash_value(fv));
}
boost::stacktrace::frame empty_frame;
BOOST_TEST(!empty_frame);
BOOST_TEST_EQ(empty_frame.source_file(), "");
BOOST_TEST_EQ(empty_frame.name(), "");
BOOST_TEST_EQ(empty_frame.source_line(), 0);
}
// Template parameter bool BySkip is to produce different functions on each BySkip. This simplifies debugging when one of the tests catches error
template <bool BySkip>
void test_empty_basic_stacktrace() {
typedef boost::stacktrace::stacktrace st_t;
st_t st = BySkip ? st_t(100500, 1024) : st_t(0, 0);
BOOST_TEST(!st);
BOOST_TEST(st.empty());
BOOST_TEST(st.size() == 0);
BOOST_TEST(st.begin() == st.end());
BOOST_TEST(st.cbegin() == st.end());
BOOST_TEST(st.cbegin() == st.cend());
BOOST_TEST(st.begin() == st.cend());
BOOST_TEST(st.rbegin() == st.rend());
BOOST_TEST(st.crbegin() == st.rend());
BOOST_TEST(st.crbegin() == st.crend());
BOOST_TEST(st.rbegin() == st.crend());
BOOST_TEST(hash_value(st) == hash_value(st_t(0, 0)));
BOOST_TEST(st == st_t(0, 0));
BOOST_TEST(!(st < st_t(0, 0)));
BOOST_TEST(!(st > st_t(0, 0)));
}
void test_stacktrace_limits()
{
BOOST_TEST_EQ(boost::stacktrace::stacktrace(0, 1).size(), 1);
BOOST_TEST_EQ(boost::stacktrace::stacktrace(1, 1).size(), 1);
}
std::size_t get_file_size(const char* file_name) {
std::ifstream file(file_name, std::ios::binary | std::ios::ate);
const auto file_size = file.tellg();
BOOST_TEST(file_size > 0);
return static_cast<std::size_t>(file_size);
}
uintptr_t get_address_from_frame(const std::string& frame) {
std::size_t address = 0;
std::string hex_address;
std::size_t pos = frame.find("0x");
if (pos != std::string::npos) {
// Extract the hex address substring
hex_address = frame.substr(pos + 2); // Skip "0x"
// Convert hex string to std::size_t
std::stringstream ss;
ss << std::hex << hex_address;
ss >> address;
}
return address;
}
void test_relative_virtual_address(const char* file_path)
{
const auto frame = to_string(boost::stacktrace::stacktrace(0, 1).as_vector().front());
// Skip the test if the frame does not contain an address
if (frame.find("0x") == std::string::npos) {
return;
}
const auto file_size = get_file_size(file_path);
BOOST_TEST(file_size > 0);
const auto address = get_address_from_frame(frame);
BOOST_TEST(address > 0);
// Verify that the address is within the binary
BOOST_TEST(address <= file_size);
}
int main(const int, const char* argv[]) {
test_deeply_nested_namespaces();
test_frames_string_data_validity();
test_nested<15>();
test_comparisons();
test_iterators();
test_frame();
test_empty_basic_stacktrace<true>();
test_empty_basic_stacktrace<false>();
BOOST_TEST(&make_some_stacktrace1 != &make_some_stacktrace2);
boost::stacktrace::stacktrace b1 = make_some_stacktrace1();
BOOST_TEST(b1.size() == 4);
boost::stacktrace::stacktrace b2 = make_some_stacktrace2();
BOOST_TEST(b2.size() == 4);
test_comparisons_base(make_some_stacktrace1(), make_some_stacktrace2());
test_nested<260>(false);
test_stacktrace_limits();
test_relative_virtual_address(argv[0]);
return boost::report_errors();
}