1 | /* Test integer width macros. |
2 | Copyright (C) 2016-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <limits.h> |
20 | #include <stdio.h> |
21 | |
22 | #define CHECK_WIDTH(TYPE, MAX, WIDTH) \ |
23 | do \ |
24 | { \ |
25 | if ((MAX >> ((TYPE) -1 < 0 ? (WIDTH - 2) : (WIDTH - 1))) != 1) \ |
26 | { \ |
27 | puts ("bad width of " #TYPE); \ |
28 | result = 1; \ |
29 | } \ |
30 | else \ |
31 | puts ("width of " #TYPE " OK"); \ |
32 | } \ |
33 | while (0) |
34 | |
35 | static int |
36 | do_test (void) |
37 | { |
38 | int result = 0; |
39 | #ifndef CHAR_WIDTH |
40 | # error "missing CHAR_WIDTH" |
41 | #endif |
42 | CHECK_WIDTH (char, CHAR_MAX, CHAR_WIDTH); |
43 | #ifndef SCHAR_WIDTH |
44 | # error "missing SCHAR_WIDTH" |
45 | #endif |
46 | CHECK_WIDTH (signed char, SCHAR_MAX, SCHAR_WIDTH); |
47 | #ifndef UCHAR_WIDTH |
48 | # error "missing UCHAR_WIDTH" |
49 | #endif |
50 | CHECK_WIDTH (unsigned char, UCHAR_MAX, UCHAR_WIDTH); |
51 | #ifndef SHRT_WIDTH |
52 | # error "missing SHRT_WIDTH" |
53 | #endif |
54 | CHECK_WIDTH (signed short, SHRT_MAX, SHRT_WIDTH); |
55 | #ifndef USHRT_WIDTH |
56 | # error "missing USHRT_WIDTH" |
57 | #endif |
58 | CHECK_WIDTH (unsigned short, USHRT_MAX, USHRT_WIDTH); |
59 | #ifndef INT_WIDTH |
60 | # error "missing INT_WIDTH" |
61 | #endif |
62 | CHECK_WIDTH (signed int, INT_MAX, INT_WIDTH); |
63 | #ifndef UINT_WIDTH |
64 | # error "missing UINT_WIDTH" |
65 | #endif |
66 | CHECK_WIDTH (unsigned int, UINT_MAX, UINT_WIDTH); |
67 | #ifndef LONG_WIDTH |
68 | # error "missing LONG_WIDTH" |
69 | #endif |
70 | CHECK_WIDTH (signed long, LONG_MAX, LONG_WIDTH); |
71 | #ifndef ULONG_WIDTH |
72 | # error "missing ULONG_WIDTH" |
73 | #endif |
74 | CHECK_WIDTH (unsigned long, ULONG_MAX, ULONG_WIDTH); |
75 | #ifndef LLONG_WIDTH |
76 | # error "missing LLONG_WIDTH" |
77 | #endif |
78 | CHECK_WIDTH (signed long long, LLONG_MAX, LLONG_WIDTH); |
79 | #ifndef ULLONG_WIDTH |
80 | # error "missing ULLONG_WIDTH" |
81 | #endif |
82 | CHECK_WIDTH (unsigned long long, ULLONG_MAX, ULLONG_WIDTH); |
83 | return result; |
84 | } |
85 | |
86 | #define TEST_FUNCTION do_test () |
87 | #include "../test-skeleton.c" |
88 | |