|
| 1 | +/** |
| 2 | + * @file mlib/test.h |
| 3 | + * @brief Testing utilities |
| 4 | + * @date 2025-01-30 |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2025 |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <mlib/config.h> |
| 23 | +#include <stdio.h> |
| 24 | +#include <stdlib.h> |
| 25 | + |
| 26 | +/** |
| 27 | + * @brief Place this macro at the head of a (compound) statement to assert that |
| 28 | + * executing that statement aborts the program with SIGABRT. |
| 29 | + * |
| 30 | + * Internally, this will fork the calling process and wait for the child process |
| 31 | + * to terminate. It asserts that the child exits abnormally with SIGABRT. This |
| 32 | + * test assertion is a no-op on Win32, since it does not have a suitable `fork` |
| 33 | + * API. |
| 34 | + * |
| 35 | + * Beware that the child process runs in a forked environment, so it is not |
| 36 | + * safe to use any non-fork-safe functionality, and any modifications to program |
| 37 | + * state will not be visible in the parent. Behavior of attempting to escape the |
| 38 | + * statement (goto/return) is undefined. |
| 39 | + * |
| 40 | + * If the child process does not abort, it will call `_Exit(71)` to indicate |
| 41 | + * to the parent that it did not terminate (the number 71 is chosen arbitrarily) |
| 42 | + * |
| 43 | + * If the token `debug` is passed as a macro argument, then the forking behavior |
| 44 | + * is suppressed, allowing for easier debugging of the statement. |
| 45 | + */ |
| 46 | +#define mlib_assert_aborts(...) MLIB_PASTE_3 (_mlibAssertAbortsStmt, _, __VA_ARGS__) () |
| 47 | + |
| 48 | +#ifndef _WIN32 |
| 49 | +#include <sys/wait.h> |
| 50 | +#define _mlibAssertAbortsStmt_() \ |
| 51 | + for (int once = 1, other_pid = fork (); once; once = 0) \ |
| 52 | + for (; once; once = 0) \ |
| 53 | + if (other_pid != 0) { \ |
| 54 | + /* We are the parent */ \ |
| 55 | + int wstatus; \ |
| 56 | + waitpid (other_pid, &wstatus, 0); \ |
| 57 | + if (WIFEXITED (wstatus)) { \ |
| 58 | + /* Normal exit! */ \ |
| 59 | + _mlib_stmt_did_not_abort (__FILE__, MLIB_FUNC, __LINE__, WEXITSTATUS (wstatus)); \ |
| 60 | + } else if (WIFSIGNALED (wstatus)) { \ |
| 61 | + /* Signalled */ \ |
| 62 | + if (WTERMSIG (wstatus) != SIGABRT) { \ |
| 63 | + fprintf (stderr, \ |
| 64 | + "%s:%d: [%s]: Child process did not exit with SIGABRT! (Exited %d)\n", \ |
| 65 | + __FILE__, \ |
| 66 | + __LINE__, \ |
| 67 | + MLIB_FUNC, \ |
| 68 | + WTERMSIG (wstatus)); \ |
| 69 | + fflush (stderr); \ |
| 70 | + abort (); \ |
| 71 | + } \ |
| 72 | + } \ |
| 73 | + } else /* We are the child */ \ |
| 74 | + for (;; _Exit (71)) \ |
| 75 | + for (;; _Exit (71)) /* Double loop to prevent the block from `break`ing out */ |
| 76 | + |
| 77 | +#else |
| 78 | +#define _mlibAssertAbortsStmt_() \ |
| 79 | + if (1) { \ |
| 80 | + } else |
| 81 | +#endif |
| 82 | + |
| 83 | +// Called when an assert-aborts statement does not terminate |
| 84 | +static inline void |
| 85 | +_mlib_stmt_did_not_abort (const char *file, const char *func, int line, int rc) |
| 86 | +{ |
| 87 | + /* Normal exit! */ |
| 88 | + if (rc == 71) { |
| 89 | + fprintf (stderr, "%s:%d: [%s]: Test case did not abort. The statement completed normally.\n", file, line, func); |
| 90 | + } else { |
| 91 | + fprintf (stderr, "%s:%d: [%s]: Test case did not abort (Exited %d)\n", file, line, func, rc); |
| 92 | + } |
| 93 | + fflush (stderr); |
| 94 | + abort (); |
| 95 | +} |
| 96 | + |
| 97 | +#define _mlibAssertAbortsStmt_debug() \ |
| 98 | + for (;; _mlib_stmt_did_not_abort (__FILE__, MLIB_FUNC, __LINE__, -1)) \ |
| 99 | + for (;; _mlib_stmt_did_not_abort (__FILE__, MLIB_FUNC, __LINE__, -1)) |
0 commit comments