source: trunk/src/sed/gnulib-tests/test-limits-h.c@ 3613

Last change on this file since 3613 was 3611, checked in by bird, 10 months ago

vendor/sed/current: GNU sed 4.9 (sed-4.9.tar.xz sha256:6e226b732e1cd739464ad6862bd1a1aba42d7982922da7a53519631d24975181)

File size: 4.2 KB
Line 
1/* Test of <limits.h> substitute.
2 Copyright 2016-2022 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 <https://www.gnu.org/licenses/>. */
16
17/* Written by Paul Eggert. */
18
19#include <config.h>
20
21#include <limits.h>
22
23#if 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
24# pragma GCC diagnostic ignored "-Woverlength-strings"
25#endif
26
27#define verify_width(width, min, max) \
28 static_assert ((max) >> ((width) - 1 - ((min) < 0)) == 1)
29
30/* Macros borrowed from intprops.h. */
31#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
32#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
33#define TYPE_MINIMUM(t) ((t) ~ TYPE_MAXIMUM (t))
34#define TYPE_MAXIMUM(t) \
35 ((t) (! TYPE_SIGNED (t) \
36 ? (t) -1 \
37 : ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
38
39/* Type width macros. */
40
41int type_bits[] =
42 {
43 CHAR_BIT,
44 WORD_BIT,
45 LONG_BIT
46 };
47verify_width (CHAR_BIT, CHAR_MIN, CHAR_MAX);
48verify_width (WORD_BIT, INT_MIN, INT_MAX);
49verify_width (LONG_BIT, LONG_MIN, LONG_MAX);
50
51/* Numerical limit macros. */
52
53char limits1[] = { CHAR_MIN, CHAR_MAX };
54static_assert (TYPE_MINIMUM (char) == CHAR_MIN);
55static_assert (TYPE_MAXIMUM (char) == CHAR_MAX);
56
57signed char limits2[] = { SCHAR_MIN, SCHAR_MAX };
58static_assert (TYPE_MINIMUM (signed char) == SCHAR_MIN);
59static_assert (TYPE_MAXIMUM (signed char) == SCHAR_MAX);
60
61unsigned char limits3[] = { UCHAR_MAX };
62static_assert (TYPE_MINIMUM (unsigned char) == 0);
63static_assert (TYPE_MAXIMUM (unsigned char) == UCHAR_MAX);
64
65short limits4[] = { SHRT_MIN, SHRT_MAX };
66static_assert (TYPE_MINIMUM (short int) == SHRT_MIN);
67static_assert (TYPE_MAXIMUM (short int) == SHRT_MAX);
68
69unsigned short limits5[] = { USHRT_MAX };
70static_assert (TYPE_MINIMUM (unsigned short int) == 0);
71static_assert (TYPE_MAXIMUM (unsigned short int) == USHRT_MAX);
72
73int limits6[] = { INT_MIN, INT_MAX };
74static_assert (TYPE_MINIMUM (int) == INT_MIN);
75static_assert (TYPE_MAXIMUM (int) == INT_MAX);
76
77unsigned int limits7[] = { UINT_MAX };
78static_assert (TYPE_MINIMUM (unsigned int) == 0);
79static_assert (TYPE_MAXIMUM (unsigned int) == UINT_MAX);
80
81long limits8[] = { LONG_MIN, LONG_MAX };
82static_assert (TYPE_MINIMUM (long int) == LONG_MIN);
83static_assert (TYPE_MAXIMUM (long int) == LONG_MAX);
84
85unsigned long limits9[] = { ULONG_MAX };
86static_assert (TYPE_MINIMUM (unsigned long int) == 0);
87static_assert (TYPE_MAXIMUM (unsigned long int) == ULONG_MAX);
88
89long long limits10[] = { LLONG_MIN, LLONG_MAX };
90static_assert (TYPE_MINIMUM (long long int) == LLONG_MIN);
91static_assert (TYPE_MAXIMUM (long long int) == LLONG_MAX);
92
93unsigned long long limits11[] = { ULLONG_MAX };
94static_assert (TYPE_MINIMUM (unsigned long long int) == 0);
95static_assert (TYPE_MAXIMUM (unsigned long long int) == ULLONG_MAX);
96
97/* Macros specified by ISO/IEC TS 18661-1:2014. */
98
99verify_width (CHAR_WIDTH, CHAR_MIN, CHAR_MAX);
100verify_width (SCHAR_WIDTH, SCHAR_MIN, SCHAR_MAX);
101verify_width (UCHAR_WIDTH, 0, UCHAR_MAX);
102verify_width (SHRT_WIDTH, SHRT_MIN, SHRT_MAX);
103verify_width (USHRT_WIDTH, 0, USHRT_MAX);
104verify_width (INT_WIDTH, INT_MIN, INT_MAX);
105verify_width (UINT_WIDTH, 0, UINT_MAX);
106verify_width (LONG_WIDTH, LONG_MIN, LONG_MAX);
107verify_width (ULONG_WIDTH, 0, ULONG_MAX);
108verify_width (LLONG_WIDTH, LLONG_MIN, LLONG_MAX);
109verify_width (ULLONG_WIDTH, 0, ULLONG_MAX);
110
111/* Macros specified by C2x. */
112
113int bool_attrs[] = { BOOL_MAX, BOOL_WIDTH };
114static_assert (BOOL_MAX == (((1U << (BOOL_WIDTH - 1)) - 1) * 2) + 1);
115
116int
117main (void)
118{
119 return 0;
120}
Note: See TracBrowser for help on using the repository browser.