| 1 | /* Common macros used by gnulib tests. | 
|---|
| 2 | Copyright (C) 2006-2022 Free Software Foundation, Inc. | 
|---|
| 3 |  | 
|---|
| 4 | This program is free software: you can redistribute it and/or modify | 
|---|
| 5 | it under the terms of the GNU General Public License as published by | 
|---|
| 6 | the Free Software Foundation, either version 3 of the License, or | 
|---|
| 7 | (at your option) any later version. | 
|---|
| 8 |  | 
|---|
| 9 | This program is distributed in the hope that it will be useful, | 
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 12 | GNU General Public License for more details. | 
|---|
| 13 |  | 
|---|
| 14 | You should have received a copy of the GNU General Public License | 
|---|
| 15 | along with this program.  If not, see <https://www.gnu.org/licenses/>.  */ | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | /* This file contains macros that are used by many gnulib tests. | 
|---|
| 19 | Put here only frequently used macros, say, used by 10 tests or more.  */ | 
|---|
| 20 |  | 
|---|
| 21 | #include <stdio.h> | 
|---|
| 22 | #include <stdlib.h> | 
|---|
| 23 |  | 
|---|
| 24 | #ifndef FALLTHROUGH | 
|---|
| 25 | # if (__GNUC__ >= 7) || (__clang_major__ >= 10) | 
|---|
| 26 | #  define FALLTHROUGH __attribute__ ((__fallthrough__)) | 
|---|
| 27 | # else | 
|---|
| 28 | #  define FALLTHROUGH ((void) 0) | 
|---|
| 29 | # endif | 
|---|
| 30 | #endif | 
|---|
| 31 |  | 
|---|
| 32 | /* Define ASSERT_STREAM before including this file if ASSERT must | 
|---|
| 33 | target a stream other than stderr.  */ | 
|---|
| 34 | #ifndef ASSERT_STREAM | 
|---|
| 35 | # define ASSERT_STREAM stderr | 
|---|
| 36 | #endif | 
|---|
| 37 |  | 
|---|
| 38 | /* ASSERT (condition); | 
|---|
| 39 | verifies that the specified condition is fulfilled.  If not, a message | 
|---|
| 40 | is printed to ASSERT_STREAM if defined (defaulting to stderr if | 
|---|
| 41 | undefined) and the program is terminated with an error code. | 
|---|
| 42 |  | 
|---|
| 43 | This macro has the following properties: | 
|---|
| 44 | - The programmer specifies the expected condition, not the failure | 
|---|
| 45 | condition.  This simplifies thinking. | 
|---|
| 46 | - The condition is tested always, regardless of compilation flags. | 
|---|
| 47 | (Unlike the macro from <assert.h>.) | 
|---|
| 48 | - On Unix platforms, the tester can debug the test program with a | 
|---|
| 49 | debugger (provided core dumps are enabled: "ulimit -c unlimited"). | 
|---|
| 50 | - For the sake of platforms where no debugger is available (such as | 
|---|
| 51 | some mingw systems), an error message is printed on the error | 
|---|
| 52 | stream that includes the source location of the ASSERT invocation. | 
|---|
| 53 | */ | 
|---|
| 54 | #define ASSERT(expr) \ | 
|---|
| 55 | do                                                                         \ | 
|---|
| 56 | {                                                                        \ | 
|---|
| 57 | if (!(expr))                                                           \ | 
|---|
| 58 | {                                                                    \ | 
|---|
| 59 | fprintf (ASSERT_STREAM, "%s:%d: assertion '%s' failed\n",          \ | 
|---|
| 60 | __FILE__, __LINE__, #expr);                               \ | 
|---|
| 61 | fflush (ASSERT_STREAM);                                            \ | 
|---|
| 62 | abort ();                                                          \ | 
|---|
| 63 | }                                                                    \ | 
|---|
| 64 | }                                                                        \ | 
|---|
| 65 | while (0) | 
|---|
| 66 |  | 
|---|
| 67 | /* Like ASSERT, except that it uses no stdio. | 
|---|
| 68 | Requires #include <string.h> and #include <unistd.h>.  */ | 
|---|
| 69 | #define ASSERT_NO_STDIO(expr) \ | 
|---|
| 70 | do                                                        \ | 
|---|
| 71 | {                                                       \ | 
|---|
| 72 | if (!(expr))                                          \ | 
|---|
| 73 | {                                                   \ | 
|---|
| 74 | WRITE_TO_STDERR (__FILE__);                       \ | 
|---|
| 75 | WRITE_TO_STDERR (":");                            \ | 
|---|
| 76 | WRITE_MACROEXPANDED_INTEGER_TO_STDERR (__LINE__); \ | 
|---|
| 77 | WRITE_TO_STDERR (": assertion '");                \ | 
|---|
| 78 | WRITE_TO_STDERR (#expr);                          \ | 
|---|
| 79 | WRITE_TO_STDERR ("' failed\n");                   \ | 
|---|
| 80 | abort ();                                         \ | 
|---|
| 81 | }                                                   \ | 
|---|
| 82 | }                                                       \ | 
|---|
| 83 | while (0) | 
|---|
| 84 | #define WRITE_MACROEXPANDED_INTEGER_TO_STDERR(integer) \ | 
|---|
| 85 | WRITE_INTEGER_TO_STDERR(integer) | 
|---|
| 86 | #define WRITE_INTEGER_TO_STDERR(integer) \ | 
|---|
| 87 | WRITE_TO_STDERR (#integer) | 
|---|
| 88 | #define WRITE_TO_STDERR(string_literal) \ | 
|---|
| 89 | {                                     \ | 
|---|
| 90 | const char *s = string_literal;     \ | 
|---|
| 91 | int ret = write (2, s, strlen (s)); \ | 
|---|
| 92 | (void) ret;                         \ | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | /* SIZEOF (array) | 
|---|
| 96 | returns the number of elements of an array.  It works for arrays that are | 
|---|
| 97 | declared outside functions and for local variables of array type.  It does | 
|---|
| 98 | *not* work for function parameters of array type, because they are actually | 
|---|
| 99 | parameters of pointer type.  */ | 
|---|
| 100 | #define SIZEOF(array) (sizeof (array) / sizeof (array[0])) | 
|---|
| 101 |  | 
|---|
| 102 | /* STREQ (str1, str2) | 
|---|
| 103 | Return true if two strings compare equal.  */ | 
|---|
| 104 | #define STREQ(a, b) (strcmp (a, b) == 0) | 
|---|
| 105 |  | 
|---|
| 106 | /* Some numbers in the interval [0,1).  */ | 
|---|
| 107 | extern const float randomf[1000]; | 
|---|
| 108 | extern const double randomd[1000]; | 
|---|
| 109 | extern const long double randoml[1000]; | 
|---|