summaryrefslogtreecommitdiff
path: root/tests/test-intprops.c
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff.email>2022-01-08 11:51:07 +0100
committerJörg Frings-Fürst <debian@jff.email>2022-01-08 11:51:07 +0100
commitbe8efac78d067c138ad8dda03df4336e73f94887 (patch)
tree5f5254a628ba0ef72065b93d949d1c985742ea8e /tests/test-intprops.c
parent7b65dbd4ebade81d504cfe5e681292a58ad1fdf0 (diff)
New upstream version 1.0upstream/1.0
Diffstat (limited to 'tests/test-intprops.c')
-rw-r--r--tests/test-intprops.c120
1 files changed, 85 insertions, 35 deletions
diff --git a/tests/test-intprops.c b/tests/test-intprops.c
index 78c6975..dd7e7d3 100644
--- a/tests/test-intprops.c
+++ b/tests/test-intprops.c
@@ -1,5 +1,5 @@
/* Test intprops.h.
- Copyright (C) 2011-2018 Free Software Foundation, Inc.
+ Copyright (C) 2011-2022 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -39,19 +39,26 @@
#include "macros.h"
+/* Compile-time verification of expression X.
+ In this file, we need it as a statement, rather than as a declaration. */
+#define verify_stmt(x) do { verify (x); } while (0)
+
/* VERIFY (X) uses a static assertion for compilers that are known to work,
and falls back on a dynamic assertion for other compilers.
These tests should be checkable via 'verify' rather than 'ASSERT', but
using 'verify' would run into a bug with HP-UX 11.23 cc; see
<https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
-#if __GNUC__ || __SUNPRO_C
-# define VERIFY(x) do { verify (x); } while (0)
+#if __GNUC__ || __clang__ || __SUNPRO_C
+# define VERIFY(x) verify_stmt (x)
#else
# define VERIFY(x) ASSERT (x)
#endif
#define DONTCARE __LINE__
+int int_minus_2 = -2;
+int int_1 = 1;
+
int
main (void)
{
@@ -114,8 +121,10 @@ main (void)
VERIFY (TYPE_MINIMUM (unsigned long int) == 0);
VERIFY (TYPE_MAXIMUM (unsigned long int) == ULONG_MAX);
#ifdef LLONG_MAX
- verify (TYPE_MINIMUM (long long int) == LLONG_MIN);
- verify (TYPE_MAXIMUM (long long int) == LLONG_MAX);
+ verify_stmt (TYPE_MINIMUM (long long int) == LLONG_MIN);
+ verify_stmt (TYPE_MAXIMUM (long long int) == LLONG_MAX);
+ verify_stmt (TYPE_MINIMUM (unsigned long long int) == 0);
+ verify_stmt (TYPE_MAXIMUM (unsigned long long int) == ULLONG_MAX);
#endif
VERIFY (TYPE_MINIMUM (intmax_t) == INTMAX_MIN);
VERIFY (TYPE_MAXIMUM (intmax_t) == INTMAX_MAX);
@@ -124,18 +133,18 @@ main (void)
/* TYPE_WIDTH. */
#ifdef CHAR_WIDTH
- verify (TYPE_WIDTH (char) == CHAR_WIDTH);
- verify (TYPE_WIDTH (signed char) == SCHAR_WIDTH);
- verify (TYPE_WIDTH (unsigned char) == UCHAR_WIDTH);
- verify (TYPE_WIDTH (short int) == SHRT_WIDTH);
- verify (TYPE_WIDTH (unsigned short int) == USHRT_WIDTH);
- verify (TYPE_WIDTH (int) == INT_WIDTH);
- verify (TYPE_WIDTH (unsigned int) == UINT_WIDTH);
- verify (TYPE_WIDTH (long int) == LONG_WIDTH);
- verify (TYPE_WIDTH (unsigned long int) == ULONG_WIDTH);
+ verify_stmt (TYPE_WIDTH (char) == CHAR_WIDTH);
+ verify_stmt (TYPE_WIDTH (signed char) == SCHAR_WIDTH);
+ verify_stmt (TYPE_WIDTH (unsigned char) == UCHAR_WIDTH);
+ verify_stmt (TYPE_WIDTH (short int) == SHRT_WIDTH);
+ verify_stmt (TYPE_WIDTH (unsigned short int) == USHRT_WIDTH);
+ verify_stmt (TYPE_WIDTH (int) == INT_WIDTH);
+ verify_stmt (TYPE_WIDTH (unsigned int) == UINT_WIDTH);
+ verify_stmt (TYPE_WIDTH (long int) == LONG_WIDTH);
+ verify_stmt (TYPE_WIDTH (unsigned long int) == ULONG_WIDTH);
#ifdef LLONG_WIDTH
- verify (TYPE_WIDTH (long long int) == LLONG_WIDTH);
- verify (TYPE_WIDTH (unsigned long long int) == ULLONG_WIDTH);
+ verify_stmt (TYPE_WIDTH (long long int) == LLONG_WIDTH);
+ verify_stmt (TYPE_WIDTH (unsigned long long int) == ULLONG_WIDTH);
#endif
#endif
@@ -209,8 +218,10 @@ main (void)
CHECK_SBINOP (*, MULTIPLY, INT_MIN, INT_MIN, int, true, 0);
CHECK_SBINOP (*, MULTIPLY, -1, INT_MIN, int,
INT_NEGATE_OVERFLOW (INT_MIN), INT_MIN);
+#if !defined __HP_cc
CHECK_SBINOP (*, MULTIPLY, LONG_MIN / INT_MAX, (long int) INT_MAX,
long int, false, LONG_MIN - LONG_MIN % INT_MAX);
+#endif
CHECK_BINOP (/, DIVIDE, INT_MIN, -1, int,
INT_NEGATE_OVERFLOW (INT_MIN), INT_MIN);
@@ -231,33 +242,48 @@ main (void)
/* INT_<op>_OVERFLOW and INT_<op>_WRAPV with mixed types. */
#define CHECK_SUM(a, b, t, v, vres) \
- CHECK_SUM1(a, b, t, v, vres); \
- CHECK_SUM1(b, a, t, v, vres)
- #define CHECK_SSUM(a, b, t, v, vres) \
- CHECK_SSUM1(a, b, t, v, vres); \
- CHECK_SSUM1(b, a, t, v, vres)
+ CHECK_SUM1 (a, b, t, v, vres); \
+ CHECK_SUM1 (b, a, t, v, vres)
+ #define CHECK_SUM_WRAPV(a, b, t, v, vres, okres) \
+ CHECK_SUM_WRAPV1 (a, b, t, v, vres, okres); \
+ CHECK_SUM_WRAPV1 (b, a, t, v, vres, okres)
#define CHECK_SUM1(a, b, t, v, vres) \
- VERIFY (INT_ADD_OVERFLOW (a, b) == (v))
- #define CHECK_SSUM1(a, b, t, v, vres) \
- CHECK_SUM1(a, b, t, v, vres); \
+ VERIFY (INT_ADD_OVERFLOW (a, b) == (v)); \
+ CHECK_SUM_WRAPV1 (a, b, t, v, vres, (a) + (b))
+ #define CHECK_SUM_WRAPV1(a, b, t, v, vres, okres) \
{ \
t result; \
ASSERT (INT_ADD_WRAPV (a, b, &result) == (v)); \
- ASSERT (result == ((v) ? (vres) : ((a) + (b)))); \
+ ASSERT (result == ((v) ? (vres) : (okres))); \
}
- CHECK_SSUM (-1, LONG_MIN, long int, true, LONG_MAX);
+ CHECK_SUM (-1, LONG_MIN, long int, true, LONG_MAX);
CHECK_SUM (-1, UINT_MAX, unsigned int, false, DONTCARE);
- CHECK_SSUM (-1L, INT_MIN, long int, INT_MIN == LONG_MIN,
+ CHECK_SUM (-1L, INT_MIN, long int, INT_MIN == LONG_MIN,
INT_MIN == LONG_MIN ? INT_MAX : DONTCARE);
CHECK_SUM (0u, -1, unsigned int, true, 0u + -1);
CHECK_SUM (0u, 0, unsigned int, false, DONTCARE);
CHECK_SUM (0u, 1, unsigned int, false, DONTCARE);
- CHECK_SSUM (1, LONG_MAX, long int, true, LONG_MIN);
+ CHECK_SUM (1, LONG_MAX, long int, true, LONG_MIN);
CHECK_SUM (1, UINT_MAX, unsigned int, true, 0u);
- CHECK_SSUM (1L, INT_MAX, long int, INT_MAX == LONG_MAX,
+ CHECK_SUM (1L, INT_MAX, long int, INT_MAX == LONG_MAX,
INT_MAX == LONG_MAX ? INT_MIN : DONTCARE);
CHECK_SUM (1u, INT_MAX, unsigned int, INT_MAX == UINT_MAX, 1u + INT_MAX);
CHECK_SUM (1u, INT_MIN, unsigned int, true, 1u + INT_MIN);
+ CHECK_SUM_WRAPV (-1, 1u, int, false, DONTCARE, 0);
+ CHECK_SUM_WRAPV (-1, 1ul, int, false, DONTCARE, 0);
+ CHECK_SUM_WRAPV (-1l, 1u, int, false, DONTCARE, 0);
+ CHECK_SUM_WRAPV (-100, 1000u, int, false, DONTCARE, 900);
+ CHECK_SUM_WRAPV (INT_MIN, UINT_MAX, int, false, DONTCARE, INT_MAX);
+ CHECK_SUM_WRAPV (1u, INT_MAX, int, true, INT_MIN, DONTCARE);
+ CHECK_SUM_WRAPV (INT_MAX, 1, long int, LONG_MAX <= INT_MAX, INT_MIN,
+ INT_MAX + 1L);
+ CHECK_SUM_WRAPV (UINT_MAX, 1, long int, LONG_MAX <= UINT_MAX, 0,
+ UINT_MAX + 1L);
+ CHECK_SUM_WRAPV (INT_MAX, 1, unsigned long int, ULONG_MAX <= INT_MAX, 0,
+ INT_MAX + 1uL);
+ CHECK_SUM_WRAPV (UINT_MAX, 1, unsigned long int, ULONG_MAX <= UINT_MAX, 0,
+ UINT_MAX + 1uL);
+
{
long int result;
ASSERT (INT_ADD_WRAPV (1, INT_MAX, &result) == (INT_MAX == LONG_MAX));
@@ -267,7 +293,9 @@ main (void)
#define CHECK_DIFFERENCE(a, b, t, v, vres) \
VERIFY (INT_SUBTRACT_OVERFLOW (a, b) == (v))
#define CHECK_SDIFFERENCE(a, b, t, v, vres) \
- CHECK_DIFFERENCE(a, b, t, v, vres); \
+ CHECK_DIFFERENCE (a, b, t, v, vres); \
+ CHECK_SDIFFERENCE_WRAPV (a, b, t, v, vres)
+ #define CHECK_SDIFFERENCE_WRAPV(a, b, t, v, vres) \
{ \
t result; \
ASSERT (INT_SUBTRACT_WRAPV (a, b, &result) == (v)); \
@@ -284,6 +312,11 @@ main (void)
CHECK_SDIFFERENCE (-1, INT_MAX, int, false, -1 - INT_MAX);
CHECK_SDIFFERENCE (0, INT_MIN, int, INT_MIN < -INT_MAX, INT_MIN);
CHECK_SDIFFERENCE (0, INT_MAX, int, false, 0 - INT_MAX);
+ CHECK_SDIFFERENCE_WRAPV (-1, 1u, int, false, DONTCARE);
+ CHECK_SDIFFERENCE_WRAPV (-1, 1ul, int, false, DONTCARE);
+ CHECK_SDIFFERENCE_WRAPV (-1l, 1u, int, false, DONTCARE);
+ CHECK_SDIFFERENCE_WRAPV (0u, INT_MAX, int, false, DONTCARE);
+ CHECK_SDIFFERENCE_WRAPV (1u, INT_MIN, int, true, 1u - INT_MIN);
{
long int result;
ASSERT (INT_SUBTRACT_WRAPV (INT_MAX, -1, &result) == (INT_MAX == LONG_MAX));
@@ -291,15 +324,20 @@ main (void)
}
#define CHECK_PRODUCT(a, b, t, v, vres) \
- CHECK_PRODUCT1(a, b, t, v, vres); \
- CHECK_PRODUCT1(b, a, t, v, vres)
+ CHECK_PRODUCT1 (a, b, t, v, vres); \
+ CHECK_PRODUCT1 (b, a, t, v, vres)
#define CHECK_SPRODUCT(a, b, t, v, vres) \
- CHECK_SPRODUCT1(a, b, t, v, vres); \
- CHECK_SPRODUCT1(b, a, t, v, vres)
+ CHECK_SPRODUCT1 (a, b, t, v, vres); \
+ CHECK_SPRODUCT1 (b, a, t, v, vres)
+ #define CHECK_SPRODUCT_WRAPV(a, b, t, v, vres) \
+ CHECK_SPRODUCT_WRAPV1 (a, b, t, v, vres); \
+ CHECK_SPRODUCT_WRAPV1 (b, a, t, v, vres)
#define CHECK_PRODUCT1(a, b, t, v, vres) \
VERIFY (INT_MULTIPLY_OVERFLOW (a, b) == (v))
#define CHECK_SPRODUCT1(a, b, t, v, vres) \
- CHECK_PRODUCT1(a, b, t, v, vres); \
+ CHECK_PRODUCT1 (a, b, t, v, vres); \
+ CHECK_SPRODUCT_WRAPV1 (a, b, t, v, vres)
+ #define CHECK_SPRODUCT_WRAPV1(a, b, t, v, vres) \
{ \
t result; \
ASSERT (INT_MULTIPLY_WRAPV (a, b, &result) == (v)); \
@@ -332,11 +370,17 @@ main (void)
CHECK_PRODUCT (INT_MAX, UINT_MAX, unsigned int, true, INT_MAX * UINT_MAX);
CHECK_PRODUCT (INT_MAX, ULONG_MAX, unsigned long int, true,
INT_MAX * ULONG_MAX);
+#if !defined __HP_cc
CHECK_SPRODUCT (INT_MIN, LONG_MAX / INT_MIN - 1, long int, true, LONG_MIN);
CHECK_SPRODUCT (INT_MIN, LONG_MAX / INT_MIN, long int, false, DONTCARE);
+#endif
CHECK_PRODUCT (INT_MIN, UINT_MAX, unsigned int, true, INT_MIN * UINT_MAX);
CHECK_PRODUCT (INT_MIN, ULONG_MAX, unsigned long int, true,
INT_MIN * ULONG_MAX);
+ CHECK_SPRODUCT_WRAPV (-1, INT_MAX + 1u, int, false, DONTCARE);
+ CHECK_SPRODUCT_WRAPV (-1, 1u, int, false, DONTCARE);
+ CHECK_SPRODUCT (0, ULONG_MAX, int, false, DONTCARE);
+ CHECK_SPRODUCT (0u, LONG_MIN, int, false, DONTCARE);
{
long int result;
ASSERT (INT_MULTIPLY_WRAPV (INT_MAX, INT_MAX, &result)
@@ -359,6 +403,12 @@ main (void)
}
# endif
+ /* Check for GCC bug 91450. */
+ {
+ unsigned long long result;
+ ASSERT (INT_MULTIPLY_WRAPV (int_minus_2, int_1, &result) && result == -2);
+ }
+
#define CHECK_QUOTIENT(a, b, v) VERIFY (INT_DIVIDE_OVERFLOW (a, b) == (v))
CHECK_QUOTIENT (INT_MIN, -1L, INT_MIN == LONG_MIN);