1 /* Test of <limits.h> substitute.
2 Copyright 2016-2020 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Paul Eggert. */
25 #if 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
26 # pragma GCC diagnostic ignored "-Woverlength-strings"
29 #define verify_width(width, min, max) \
30 verify ((max) >> ((width) - 1 - ((min) < 0)) == 1)
32 /* Macros borrowed from intprops.h. */
33 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
34 #define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
35 #define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
36 #define TYPE_MAXIMUM(t) \
37 ((t) (! TYPE_SIGNED (t) \
39 : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
41 /* Type width macros. */
49 verify_width (CHAR_BIT
, CHAR_MIN
, CHAR_MAX
);
50 verify_width (WORD_BIT
, INT_MIN
, INT_MAX
);
51 verify_width (LONG_BIT
, LONG_MIN
, LONG_MAX
);
53 /* Numerical limit macros. */
55 char limits1
[] = { CHAR_MIN
, CHAR_MAX
};
56 verify (TYPE_MINIMUM (char) == CHAR_MIN
);
57 verify (TYPE_MAXIMUM (char) == CHAR_MAX
);
59 signed char limits2
[] = { SCHAR_MIN
, SCHAR_MAX
};
60 verify (TYPE_MINIMUM (signed char) == SCHAR_MIN
);
61 verify (TYPE_MAXIMUM (signed char) == SCHAR_MAX
);
63 unsigned char limits3
[] = { UCHAR_MAX
};
64 verify (TYPE_MINIMUM (unsigned char) == 0);
65 verify (TYPE_MAXIMUM (unsigned char) == UCHAR_MAX
);
67 short limits4
[] = { SHRT_MIN
, SHRT_MAX
};
68 verify (TYPE_MINIMUM (short int) == SHRT_MIN
);
69 verify (TYPE_MAXIMUM (short int) == SHRT_MAX
);
71 unsigned short limits5
[] = { USHRT_MAX
};
72 verify (TYPE_MINIMUM (unsigned short int) == 0);
73 verify (TYPE_MAXIMUM (unsigned short int) == USHRT_MAX
);
75 int limits6
[] = { INT_MIN
, INT_MAX
};
76 verify (TYPE_MINIMUM (int) == INT_MIN
);
77 verify (TYPE_MAXIMUM (int) == INT_MAX
);
79 unsigned int limits7
[] = { UINT_MAX
};
80 verify (TYPE_MINIMUM (unsigned int) == 0);
81 verify (TYPE_MAXIMUM (unsigned int) == UINT_MAX
);
83 long limits8
[] = { LONG_MIN
, LONG_MAX
};
84 verify (TYPE_MINIMUM (long int) == LONG_MIN
);
85 verify (TYPE_MAXIMUM (long int) == LONG_MAX
);
87 unsigned long limits9
[] = { ULONG_MAX
};
88 verify (TYPE_MINIMUM (unsigned long int) == 0);
89 verify (TYPE_MAXIMUM (unsigned long int) == ULONG_MAX
);
91 long long limits10
[] = { LLONG_MIN
, LLONG_MAX
};
92 verify (TYPE_MINIMUM (long long int) == LLONG_MIN
);
93 verify (TYPE_MAXIMUM (long long int) == LLONG_MAX
);
95 unsigned long long limits11
[] = { ULLONG_MAX
};
96 verify (TYPE_MINIMUM (unsigned long long int) == 0);
97 verify (TYPE_MAXIMUM (unsigned long long int) == ULLONG_MAX
);
99 /* Macros specified by ISO/IEC TS 18661-1:2014. */
101 verify_width (CHAR_WIDTH
, CHAR_MIN
, CHAR_MAX
);
102 verify_width (SCHAR_WIDTH
, SCHAR_MIN
, SCHAR_MAX
);
103 verify_width (UCHAR_WIDTH
, 0, UCHAR_MAX
);
104 verify_width (SHRT_WIDTH
, SHRT_MIN
, SHRT_MAX
);
105 verify_width (USHRT_WIDTH
, 0, USHRT_MAX
);
106 verify_width (INT_WIDTH
, INT_MIN
, INT_MAX
);
107 verify_width (UINT_WIDTH
, 0, UINT_MAX
);
108 verify_width (LONG_WIDTH
, LONG_MIN
, LONG_MAX
);
109 verify_width (ULONG_WIDTH
, 0, ULONG_MAX
);
110 verify_width (LLONG_WIDTH
, LLONG_MIN
, LLONG_MAX
);
111 verify_width (ULLONG_WIDTH
, 0, ULLONG_MAX
);