ESDM
Middleware for Earth System Data
test_util.h
1 /* This file is part of ESDM.
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with ESDM. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef TEST_UTIL_H
18 #define TEST_UTIL_H
19 
20 #include <stdint.h>
21 #include <sys/wait.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <esdm-debug.h>
25 
26 // generate test patterns
27 void etest_gen_buffer(int dims, int64_t bounds[], uint64_t ** out_buff);
28 int etest_verify_buffer(int dims, int64_t bounds[], uint64_t * buff);
29 void etest_memset_buffer(int dims, int64_t bounds[], uint64_t * buff);
30 
31 //A variant of eassert() that allows us to check that the call actually *crashes* the program.
32 //This is used to check for the presence of the appropriate eassert() calls to check function contracts.
33 //Checks for abnormal termination by some signal.
34 #ifdef NDEBUG
35  //Must compile out `eassert_crash()` if `assert()` is compiled out as it is supposed to check for a crash produced by some other `assert()`.
36  //If that other `assert()` were compiled out in a production built while the checking `eassert_crash()` is left in,
37  //the result would be a production-built-only failure.
38  #define eassert_crash(call)
39 #else
40  #define eassert_crash(call) do { \
41  printf("checking for expected crash...\n"); \
42  pid_t child = fork(); \
43  if(child) { \
44  int status; \
45  pid_t result = waitpid(child, &status, 0); \
46  eassert(result == child); \
47  eassert(WIFSIGNALED(status)); \
48  } else { \
49  call; \
50  exit(0); \
51  } \
52  printf("... OK, child crashed successfully\n"); \
53  } while(0)
54 #endif
55 
56 //A variant of eassert() that allows us to check that the call triggers a fatal error bailout.
57 //This is used to check for the presence of the appropriate exit() or ERROR_ESDM*() calls to check function contracts.
58 //Checks for normal termination with a non-zero status.
59 #define eassert_bailout(call) do { \
60  printf("checking for expected error...\n"); \
61  pid_t child = fork(); \
62  if(child) { \
63  int status; \
64  pid_t result = waitpid(child, &status, 0); \
65  eassert(result == child); \
66  eassert(WIFEXITED(status)); \
67  eassert(WEXITSTATUS(status)); \
68  } else { \
69  call; \
70  exit(0); \
71  } \
72  printf("... OK, child exited with an error\n"); \
73 } while(0)
74 
75 
76 
77 #endif