Refactor common macros used in tests.
[pspp] / tests / macros.h
1 /* Common macros used by gnulib tests.
2    Copyright (C) 2006-2009 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 <http://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 /* ASSERT (condition);
25    verifies that the specified condition is fulfilled.  If not, a message
26    is printed to stderr and the program is terminated with an error code.
27
28    This macro has the following properties:
29      - The programmer specifies the expected condition, not the failure
30        condition.  This simplifies thinking.
31      - The condition is tested always, regardless of compilation flags.
32        (Unlike the macro from <assert.h>.)
33      - On Unix platforms, the tester can debug the test program with a
34        debugger (provided core dumps are enabled: "ulimit -c unlimited").
35      - For the sake of platforms where no debugger is available (such as
36        some mingw systems), an error message is printed on stderr that
37        includes the source location of the ASSERT invocation.
38  */
39 #define ASSERT(expr) \
40   do                                                                         \
41     {                                                                        \
42       if (!(expr))                                                           \
43         {                                                                    \
44           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
45           fflush (stderr);                                                   \
46           abort ();                                                          \
47         }                                                                    \
48     }                                                                        \
49   while (0)
50
51 /* SIZEOF (array)
52    returns the number of elements of an array.  It works for arrays that are
53    declared outside functions and for local variables of array type.  It does
54    *not* work for function parameters of array type, because they are actually
55    parameters of pointer type.  */
56 #define SIZEOF(array) (sizeof (array) / sizeof (array[0]))