1 /* Common macros used by gnulib tests. 2 Copyright (C) 2006-2020 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 26 # define FALLTHROUGH ((void) 0) 27 # else 28 # define FALLTHROUGH __attribute__ ((__fallthrough__)) 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 /* SIZEOF (array) 68 returns the number of elements of an array. It works for arrays that are 69 declared outside functions and for local variables of array type. It does 70 *not* work for function parameters of array type, because they are actually 71 parameters of pointer type. */ 72 #define SIZEOF(array) (sizeof (array) / sizeof (array[0])) 73 74 /* STREQ (str1, str2) 75 Return true if two strings compare equal. */ 76 #define STREQ(a, b) (strcmp (a, b) == 0) 77 78 /* Some numbers in the interval [0,1). */ 79 extern const float randomf[1000]; 80 extern const double randomd[1000]; 81 extern const long double randoml[1000]; 82