From 23c665097c8cf504cfee4dfc3d5f151e2c60837f Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 9 Feb 2025 06:07:13 +0100 Subject: [PATCH] intprops tests: Strengthen INT_PROMOTE tests. * lib/intprops.h (INT_PROMOTE): Refine comment. * tests/test-intprops.c: Check the size and sign of INT_PROMOTE (x) on all compilers. (main): Check that INT_PROMOTE is a no-op on floats. --- ChangeLog | 8 ++++++++ lib/intprops.h | 2 +- tests/test-intprops.c | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 11912cd67a..710db41aa8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2025-02-09 Bruno Haible + + intprops tests: Strengthen INT_PROMOTE tests. + * lib/intprops.h (INT_PROMOTE): Refine comment. + * tests/test-intprops.c: Check the size and sign of INT_PROMOTE (x) on + all compilers. + (main): Check that INT_PROMOTE is a no-op on floats. + 2025-02-08 Collin Funk doc: Document free_aligned_sized and free_sized added in C23. diff --git a/lib/intprops.h b/lib/intprops.h index 83efe39910..2f9fa0a022 100644 --- a/lib/intprops.h +++ b/lib/intprops.h @@ -39,7 +39,7 @@ then 'switch (INT_PROMOTE (E))' pacifies gcc -Wswitch-enum if some enum values are deliberately omitted from the switch's cases. Here, unary + is safer than a cast or inline function, as unary + - does only integer promotions. */ + does only integer promotions and is disallowed on pointers. */ #define INT_PROMOTE(e) (+ (e)) diff --git a/tests/test-intprops.c b/tests/test-intprops.c index 026c615aba..6c06e47ab3 100644 --- a/tests/test-intprops.c +++ b/tests/test-intprops.c @@ -69,6 +69,11 @@ || defined __clang__)) int check_INT_PROMOTE = _Generic (INT_PROMOTE ((short int) 0), int: 0); #endif +/* For other compilers, check the size and sign of INT_PROMOTE (x). */ +int check_INT_PROMOTE_size + [2 * (sizeof (INT_PROMOTE ((short int) 0)) == sizeof (int)) - 1]; +int check_INT_PROMOTE_sign + [2 * (INT_PROMOTE ((short int) -1) < 0) - 1]; int int_minus_2 = -2; int int_1 = 1; @@ -79,6 +84,9 @@ main (void) /* Use VERIFY for tests that must be integer constant expressions, ASSERT otherwise. */ + /* Check that INT_PROMOTE is a no-op on floats. */ + ASSERT (INT_PROMOTE (2.71828) > 2); + #ifndef TEST_STDCKDINT /* TYPE_IS_INTEGER. */ ASSERT (TYPE_IS_INTEGER (bool)); -- 2.39.5