When testing a header file, let's check whether all required macros are defined before including any other header file. This makes the tests somewhat stronger.
2023-03-25 Bruno Haible <br...@clisp.org> Make some header file tests a bit stronger. * tests/test-float.c: Include fpucw.h and macros.h only after checking the macros. * tests/test-glob-h.c: Include macros.h only after checking the types and macros. * tests/test-inttypes.c: Don't include <stddef.h>. * tests/test-math.c: Include <limits.h> and macros.h only after checking the types and macros. * tests/test-stddef.c: Include <limits.h> only after checking the types and macros. * tests/test-sys_select.c: Include signature.h only after checking the types. * tests/test-sys_socket.c: Include <errno.h> only after checking the types and macros. * tests/test-wctype-h.c: Include macros.h only after checking the types. diff --git a/tests/test-float.c b/tests/test-float.c index 315c3d6610..07c3ad1cf4 100644 --- a/tests/test-float.c +++ b/tests/test-float.c @@ -20,12 +20,48 @@ #include <float.h> -#include "fpucw.h" -#include "macros.h" - /* Check that FLT_RADIX is a constant expression. */ int a[] = { FLT_RADIX }; +/* ----------------------- Check macros for 'float' ----------------------- */ + +/* Check that the FLT_* macros expand to constant expressions. */ +int fb[] = + { + FLT_MANT_DIG, FLT_MIN_EXP, FLT_MAX_EXP, + FLT_DIG, FLT_MIN_10_EXP, FLT_MAX_10_EXP + }; +float fc[] = { FLT_EPSILON, FLT_MIN, FLT_MAX }; + +/* ----------------------- Check macros for 'double' ----------------------- */ + +/* Check that the DBL_* macros expand to constant expressions. */ +int db[] = + { + DBL_MANT_DIG, DBL_MIN_EXP, DBL_MAX_EXP, + DBL_DIG, DBL_MIN_10_EXP, DBL_MAX_10_EXP + }; +double dc[] = { DBL_EPSILON, DBL_MIN, DBL_MAX }; + +/* -------------------- Check macros for 'long double' -------------------- */ + +/* Check that the LDBL_* macros expand to constant expressions. */ +int lb[] = + { + LDBL_MANT_DIG, LDBL_MIN_EXP, LDBL_MAX_EXP, + LDBL_DIG, LDBL_MIN_10_EXP, LDBL_MAX_10_EXP + }; +long double lc1 = LDBL_EPSILON; +long double lc2 = LDBL_MIN; +#if 0 /* LDBL_MAX is not a constant expression on some platforms. */ +long double lc3 = LDBL_MAX; +#endif + +/* ------------------------------------------------------------------------- */ + +#include "fpucw.h" +#include "macros.h" + #if FLT_RADIX == 2 /* Return 2^n. */ @@ -117,14 +153,6 @@ pow2l (int n) /* ----------------------- Check macros for 'float' ----------------------- */ -/* Check that the FLT_* macros expand to constant expressions. */ -int fb[] = - { - FLT_MANT_DIG, FLT_MIN_EXP, FLT_MAX_EXP, - FLT_DIG, FLT_MIN_10_EXP, FLT_MAX_10_EXP - }; -float fc[] = { FLT_EPSILON, FLT_MIN, FLT_MAX }; - static void test_float (void) { @@ -196,14 +224,6 @@ test_float (void) /* ----------------------- Check macros for 'double' ----------------------- */ -/* Check that the DBL_* macros expand to constant expressions. */ -int db[] = - { - DBL_MANT_DIG, DBL_MIN_EXP, DBL_MAX_EXP, - DBL_DIG, DBL_MIN_10_EXP, DBL_MAX_10_EXP - }; -double dc[] = { DBL_EPSILON, DBL_MIN, DBL_MAX }; - static void test_double (void) { @@ -275,18 +295,6 @@ test_double (void) /* -------------------- Check macros for 'long double' -------------------- */ -/* Check that the LDBL_* macros expand to constant expressions. */ -int lb[] = - { - LDBL_MANT_DIG, LDBL_MIN_EXP, LDBL_MAX_EXP, - LDBL_DIG, LDBL_MIN_10_EXP, LDBL_MAX_10_EXP - }; -long double lc1 = LDBL_EPSILON; -long double lc2 = LDBL_MIN; -#if 0 /* LDBL_MAX is not a constant expression on some platforms. */ -long double lc3 = LDBL_MAX; -#endif - static void test_long_double (void) { diff --git a/tests/test-glob-h.c b/tests/test-glob-h.c index 90d542693e..0abd07321e 100644 --- a/tests/test-glob-h.c +++ b/tests/test-glob-h.c @@ -20,8 +20,6 @@ #include <glob.h> -#include "macros.h" - /* Check that size_t is defined. */ size_t a; @@ -43,6 +41,8 @@ int ret[] = GLOB_NOSPACE }; +#include "macros.h" + int main () { diff --git a/tests/test-inttypes.c b/tests/test-inttypes.c index 464436a53b..6c27275643 100644 --- a/tests/test-inttypes.c +++ b/tests/test-inttypes.c @@ -20,8 +20,6 @@ #include <inttypes.h> -#include <stddef.h> - /* Tests for macros supposed to be defined in inttypes.h. */ const char *k = /* implicit string concatenation */ diff --git a/tests/test-math.c b/tests/test-math.c index 874c5969f7..8de6931630 100644 --- a/tests/test-math.c +++ b/tests/test-math.c @@ -50,15 +50,15 @@ choke me choke me #endif -#include <limits.h> - -#include "macros.h" - #if 0 /* Check that NAN expands into a constant expression. */ static float n = NAN; #endif +#include <limits.h> + +#include "macros.h" + /* Compare two numbers with ==. This is a separate function because IRIX 6.5 "cc -O" miscompiles an 'x == x' test. */ diff --git a/tests/test-stddef.c b/tests/test-stddef.c index 3ec00a8f25..359fece6c2 100644 --- a/tests/test-stddef.c +++ b/tests/test-stddef.c @@ -19,7 +19,6 @@ #include <config.h> #include <stddef.h> -#include <limits.h> /* Check that appropriate types are defined. */ wchar_t a = 'c'; @@ -44,10 +43,6 @@ struct d static_assert (sizeof (offsetof (struct d, e)) == sizeof (size_t)); static_assert (offsetof (struct d, f) == 1); -/* offsetof promotes to an unsigned integer if and only if sizes do - not fit in int. */ -static_assert ((offsetof (struct d, e) < -1) == (INT_MAX < (size_t) -1)); - /* Check max_align_t's alignment. */ static_assert (alignof (double) <= alignof (max_align_t)); static_assert (alignof (int) <= alignof (max_align_t)); @@ -92,6 +87,12 @@ test_unreachable_noreturn (void) unreachable (); } +#include <limits.h> /* INT_MAX */ + +/* offsetof promotes to an unsigned integer if and only if sizes do + not fit in int. */ +static_assert ((offsetof (struct d, e) < -1) == (INT_MAX < (size_t) -1)); + int main (void) { diff --git a/tests/test-sys_select.c b/tests/test-sys_select.c index 701ccc0eed..f8905ce085 100644 --- a/tests/test-sys_select.c +++ b/tests/test-sys_select.c @@ -20,6 +20,16 @@ #include <sys/select.h> +/* Check that the 'struct timeval' type is defined. */ +struct timeval a; + +/* Check that a.tv_sec is wide enough to hold a time_t, ignoring + signedness issues. */ +typedef int verify_tv_sec_type[sizeof (time_t) <= sizeof (a.tv_sec) ? 1 : -1]; + +/* Check that sigset_t is defined. */ +sigset_t t2; + #include "signature.h" /* The following may be macros without underlying functions, so only @@ -37,16 +47,6 @@ SIGNATURE_CHECK (FD_SET, int, (int, fd_set *)); SIGNATURE_CHECK (FD_ZERO, void, (fd_set *)); #endif -/* Check that the 'struct timeval' type is defined. */ -struct timeval a; - -/* Check that a.tv_sec is wide enough to hold a time_t, ignoring - signedness issues. */ -typedef int verify_tv_sec_type[sizeof (time_t) <= sizeof (a.tv_sec) ? 1 : -1]; - -/* Check that sigset_t is defined. */ -sigset_t t2; - int main (void) { diff --git a/tests/test-sys_socket.c b/tests/test-sys_socket.c index b522c25a17..02ec6373ff 100644 --- a/tests/test-sys_socket.c +++ b/tests/test-sys_socket.c @@ -20,8 +20,6 @@ #include <sys/socket.h> -#include <errno.h> - #if HAVE_SHUTDOWN /* Check some integer constant expressions. */ int a[] = { SHUT_RD, SHUT_WR, SHUT_RDWR }; @@ -40,6 +38,8 @@ struct iovec io; /* Check that a minimal set of 'struct msghdr' is defined. */ struct msghdr msg; +#include <errno.h> + int main (void) { diff --git a/tests/test-wctype-h.c b/tests/test-wctype-h.c index e413d4bedd..ceee02bc7f 100644 --- a/tests/test-wctype-h.c +++ b/tests/test-wctype-h.c @@ -20,8 +20,6 @@ #include <wctype.h> -#include "macros.h" - /* Check that the type wint_t is defined. */ wint_t a = 'x'; /* Check that WEOF is defined. */ @@ -33,6 +31,8 @@ wctype_t p; /* Check that the type wctrans_t is defined. */ wctrans_t q; +#include "macros.h" + int main (void) {