@@ -9,19 +9,16 @@ using namespace simdjson;
9
9
using namespace simdjson ::builtin;
10
10
11
11
// 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) {
13
13
// 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 ;
22
19
}
23
20
24
- int test_check_point () {
21
+ bool test_check_point () {
25
22
auto json = R"(
26
23
{
27
24
"x": 1,
@@ -32,10 +29,34 @@ int test_check_point() {
32
29
return check_point (doc[" x" ], doc[" y" ]);
33
30
}
34
31
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);
39
60
}
40
61
41
62
int main (void ) {
@@ -45,12 +66,9 @@ int main(void) {
45
66
// test to have passed.
46
67
// From https://stackoverflow.com/a/33694733
47
68
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 ;
56
74
}
0 commit comments