Skip to content

Commit 93807bf

Browse files
authored
Merge pull request simdjson#1352 from simdjson/jkeiser/assert-test-release
Make assert test succeed (not run anything) in Release builds
2 parents 0b67847 + ad4f718 commit 93807bf

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

tests/ondemand/ondemand_assert_out_of_order_values.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,16 @@ using namespace simdjson;
99
using namespace simdjson::builtin;
1010

1111
// This ensures the compiler can't rearrange them into the proper order (which causes it to work!)
12-
simdjson_never_inline int check_point(simdjson_result<ondemand::value> xval, simdjson_result<ondemand::value> yval) {
12+
simdjson_never_inline bool check_point(simdjson_result<ondemand::value> xval, simdjson_result<ondemand::value> yval) {
1313
// Verify the expected release behavior
14-
error_code error;
15-
uint64_t x = 0;
16-
if ((error = xval.get(x))) { std::cerr << "error getting x: " << error << std::endl; }
17-
else if (x != 2) { std::cerr << "expected x to (wrongly) be 2, was " << x << std::endl; }
18-
uint64_t y = 0;
19-
if ((error = yval.get(y))) { std::cerr << "error getting y: " << error << std::endl; }
20-
else if (y != 3) { std::cerr << "expected y to (wrongly) be 3, was " << y << std::endl; }
21-
return 0;
14+
uint64_t x, y;
15+
if (!xval.get(x)) { return false; }
16+
if (!yval.get(y)) { return false; }
17+
std::cout << x << "," << y << std::endl;
18+
return true;
2219
}
2320

24-
int test_check_point() {
21+
bool test_check_point() {
2522
auto json = R"(
2623
{
2724
"x": 1,
@@ -32,10 +29,34 @@ int test_check_point() {
3229
return check_point(doc["x"], doc["y"]);
3330
}
3431

35-
bool fork_failed_with_assert() {
36-
int status = 0;
37-
wait(&status);
38-
return WIFSIGNALED(status);
32+
// Run a test function in a fork and check whether it exited with an assert signal
33+
template<typename F>
34+
bool assert_test(const F& f) {
35+
pid_t pid = fork();
36+
if (pid == -1) {
37+
std::cerr << "fork failed" << std::endl;
38+
exit(1);
39+
}
40+
41+
if (!pid) {
42+
//
43+
// This code runs in the fork (so we run the test function)
44+
//
45+
bool succeeded = f();
46+
exit(succeeded ? 0 : 1);
47+
}
48+
49+
//
50+
// This code runs in the original executable (so we wait for the fork and check the exit code)
51+
//
52+
int exit_code = 0;
53+
if (waitpid(pid, &exit_code, 0) == pid_t(-1)) {
54+
std::cerr << "waitpid failed: " << std::string_view(strerror(errno)) << std::endl;
55+
exit(1);
56+
}
57+
58+
// Test passes if the child exited with an assert signal
59+
return WIFSIGNALED(exit_code);
3960
}
4061

4162
int main(void) {
@@ -45,12 +66,9 @@ int main(void) {
4566
// test to have passed.
4667
// From https://stackoverflow.com/a/33694733
4768

48-
pid_t pid = fork();
49-
if (pid == -1) { std::cerr << "fork failed" << std::endl; return 1; }
50-
if (pid) {
51-
// Parent - wait child and interpret its result
52-
return fork_failed_with_assert() ? 0 : 1;
53-
} else {
54-
test_check_point();
55-
}
69+
bool succeeded = true;
70+
#ifndef NDEBUG
71+
succeeded |= assert_test(test_check_point);
72+
#endif
73+
return succeeded ? 0 : 1;
5674
}

0 commit comments

Comments
 (0)