Debian Codegraph @

gnulib-tests/macros.h

raw
1 /* Common macros used by gnulib tests.
2 Copyright (C) 2006-2025 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 NO_MAIN_HERE before including this file, if this compilation unit
33 does not contain a main() function. */
34
35 /* Optionally define CONTINUE_AFTER_ASSERT to 1 before including this file,
36 if you wish execution to continue after an ASSERT or ASSERT_NO_STDIO
37 failure. */
38 #ifndef CONTINUE_AFTER_ASSERT
39 # define CONTINUE_AFTER_ASSERT 0
40 #endif
41
42 /* Define ASSERT_STREAM before including this file if ASSERT must
43 target a stream other than stderr. */
44 #ifndef ASSERT_STREAM
45 # define ASSERT_STREAM stderr
46 #endif
47
48 /* Define print_stack_trace() to a no-op, if the module 'stack-trace' is not
49 in use. */
50 #if !GNULIB_STACK_TRACE
51 # define print_stack_trace() /* nothing */
52 #endif
53
54 /* Exit status of the test.
55 Initialized to EXIT_SUCCESS.
56 Set to EXIT_FAILURE when an ASSERT or ASSERT_NO_STDIO fails. */
57 /* To satisfy the "one definition rule", we define the variable in the
58 compilation unit that contains the main() function. */
59 #ifdef NO_MAIN_HERE
60 extern int volatile test_exit_status;
61 #else
62 int volatile test_exit_status = EXIT_SUCCESS;
63 #endif
64
65 /* ASSERT (condition);
66 verifies that the specified condition is fulfilled. If not, a message
67 is printed to ASSERT_STREAM if defined (defaulting to stderr if
68 undefined) and the program is terminated with an error code.
69
70 This macro has the following properties:
71 - The programmer specifies the expected condition, not the failure
72 condition. This simplifies thinking.
73 - The condition is tested always, regardless of compilation flags.
74 (Unlike the macro from <assert.h>.)
75 - On Unix platforms, the tester can debug the test program with a
76 debugger (provided core dumps are enabled: "ulimit -c unlimited").
77 - For the sake of platforms where no debugger is available (such as
78 some mingw systems), an error message is printed on the error
79 stream that includes the source location of the ASSERT invocation.
80 */
81 #define ASSERT(expr) \
82 do \
83 { \
84 if (!(expr)) \
85 { \
86 fprintf (ASSERT_STREAM, "%s:%d: assertion '%s' failed\n", \
87 __FILE__, __LINE__, #expr); \
88 fflush (ASSERT_STREAM); \
89 if (CONTINUE_AFTER_ASSERT) \
90 { \
91 print_stack_trace (); \
92 test_exit_status = EXIT_FAILURE; \
93 } \
94 else \
95 abort (); \
96 } \
97 } \
98 while (0)
99
100 /* Like ASSERT, except that it uses no stdio.
101 Requires #include <string.h> and #include <unistd.h>. */
102 #define ASSERT_NO_STDIO(expr) \
103 do \
104 { \
105 if (!(expr)) \
106 { \
107 WRITE_TO_STDERR (__FILE__); \
108 WRITE_TO_STDERR (":"); \
109 WRITE_MACROEXPANDED_INTEGER_TO_STDERR (__LINE__); \
110 WRITE_TO_STDERR (": assertion '"); \
111 WRITE_TO_STDERR (#expr); \
112 WRITE_TO_STDERR ("' failed\n"); \
113 if (CONTINUE_AFTER_ASSERT) \
114 { \
115 print_stack_trace (); \
116 test_exit_status = EXIT_FAILURE; \
117 } \
118 else \
119 abort (); \
120 } \
121 } \
122 while (0)
123 #define WRITE_MACROEXPANDED_INTEGER_TO_STDERR(integer) \
124 WRITE_INTEGER_TO_STDERR(integer)
125 #define WRITE_INTEGER_TO_STDERR(integer) \
126 WRITE_TO_STDERR (#integer)
127 #define WRITE_TO_STDERR(string_literal) \
128 { \
129 const char *s = string_literal; \
130 int ret = write (2, s, strlen (s)); \
131 (void) ret; \
132 }
133
134 /* SIZEOF (array)
135 returns the number of elements of an array. It works for arrays that are
136 declared outside functions and for local variables of array type. It does
137 *not* work for function parameters of array type, because they are actually
138 parameters of pointer type. */
139 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))
140
141 /* STREQ (str1, str2)
142 Return true if two strings compare equal. */
143 #define STREQ(a, b) (strcmp (a, b) == 0)
144
145 /* Some numbers in the interval [0,1). */
146 extern const float randomf[1000];
147 extern const double randomd[1000];
148 extern const long double randoml[1000];
149 /* 4 KiB of random bytes. */
150 extern const char randomb[4096];