test-stdint.c

Go to the documentation of this file.
00001 /* Test of <stdint.h> substitute.
00002    Copyright (C) 2006-2008 Free Software Foundation, Inc.
00003 
00004    This program is free software: you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00016 
00017 /* Written by Bruno Haible <bruno@clisp.org>, 2006.  */
00018 
00019 #include <config.h>
00020 
00021 /* Whether to enable pedantic checks. */
00022 #define DO_PEDANTIC 0
00023 
00024 #define __STDC_LIMIT_MACROS 1 /* to make it work also in C++ mode */
00025 #include <stdint.h>
00026 
00027 #include "verify.h"
00028 #include "intprops.h"
00029 
00030 #if __GNUC__ >= 2 && DO_PEDANTIC
00031 # define verify_same_types(expr1,expr2)  \
00032     extern void _verify_func(__LINE__) (__typeof__ (expr1) *); \
00033     extern void _verify_func(__LINE__) (__typeof__ (expr2) *);
00034 # define _verify_func(line) _verify_func2(line)
00035 # define _verify_func2(line) verify_func_ ## line
00036 #else
00037 # define verify_same_types(expr1,expr2) extern void verify_func (int)
00038 #endif
00039 
00040 /* 7.18.1.1. Exact-width integer types */
00041 /* 7.18.2.1. Limits of exact-width integer types */
00042 
00043 int8_t a1[3] = { INT8_C (17), INT8_MIN, INT8_MAX };
00044 verify (TYPE_MINIMUM (int8_t) == INT8_MIN);
00045 verify (TYPE_MAXIMUM (int8_t) == INT8_MAX);
00046 verify_same_types (INT8_MIN, (int8_t) 0 + 0);
00047 verify_same_types (INT8_MAX, (int8_t) 0 + 0);
00048 
00049 int16_t a2[3] = { INT16_C (17), INT16_MIN, INT16_MAX };
00050 verify (TYPE_MINIMUM (int16_t) == INT16_MIN);
00051 verify (TYPE_MAXIMUM (int16_t) == INT16_MAX);
00052 verify_same_types (INT16_MIN, (int16_t) 0 + 0);
00053 verify_same_types (INT16_MAX, (int16_t) 0 + 0);
00054 
00055 int32_t a3[3] = { INT32_C (17), INT32_MIN, INT32_MAX };
00056 verify (TYPE_MINIMUM (int32_t) == INT32_MIN);
00057 verify (TYPE_MAXIMUM (int32_t) == INT32_MAX);
00058 verify_same_types (INT32_MIN, (int32_t) 0 + 0);
00059 verify_same_types (INT32_MAX, (int32_t) 0 + 0);
00060 
00061 #ifdef INT64_MAX
00062 int64_t a4[3] = { INT64_C (17), INT64_MIN, INT64_MAX };
00063 verify (TYPE_MINIMUM (int64_t) == INT64_MIN);
00064 verify (TYPE_MAXIMUM (int64_t) == INT64_MAX);
00065 verify_same_types (INT64_MIN, (int64_t) 0 + 0);
00066 verify_same_types (INT64_MAX, (int64_t) 0 + 0);
00067 #endif
00068 
00069 uint8_t b1[2] = { UINT8_C (17), UINT8_MAX };
00070 verify (TYPE_MAXIMUM (uint8_t) == UINT8_MAX);
00071 verify_same_types (UINT8_MAX, (uint8_t) 0 + 0);
00072 
00073 uint16_t b2[2] = { UINT16_C (17), UINT16_MAX };
00074 verify (TYPE_MAXIMUM (uint16_t) == UINT16_MAX);
00075 verify_same_types (UINT16_MAX, (uint16_t) 0 + 0);
00076 
00077 uint32_t b3[2] = { UINT32_C (17), UINT32_MAX };
00078 verify (TYPE_MAXIMUM (uint32_t) == UINT32_MAX);
00079 verify_same_types (UINT32_MAX, (uint32_t) 0 + 0);
00080 
00081 #ifdef UINT64_MAX
00082 uint64_t b4[2] = { UINT64_C (17), UINT64_MAX };
00083 verify (TYPE_MAXIMUM (uint64_t) == UINT64_MAX);
00084 verify_same_types (UINT64_MAX, (uint64_t) 0 + 0);
00085 #endif
00086 
00087 #if INT8_MIN && INT8_MAX && INT16_MIN && INT16_MAX && INT32_MIN && INT32_MAX
00088 /* ok */
00089 #else
00090 err or;
00091 #endif
00092 
00093 #if UINT8_MAX && UINT16_MAX && UINT32_MAX
00094 /* ok */
00095 #else
00096 err or;
00097 #endif
00098 
00099 /* 7.18.1.2. Minimum-width integer types */
00100 /* 7.18.2.2. Limits of minimum-width integer types */
00101 
00102 int_least8_t c1[3] = { 17, INT_LEAST8_MIN, INT_LEAST8_MAX };
00103 verify (TYPE_MINIMUM (int_least8_t) == INT_LEAST8_MIN);
00104 verify (TYPE_MAXIMUM (int_least8_t) == INT_LEAST8_MAX);
00105 verify_same_types (INT_LEAST8_MIN, (int_least8_t) 0 + 0);
00106 verify_same_types (INT_LEAST8_MAX, (int_least8_t) 0 + 0);
00107 
00108 int_least16_t c2[3] = { 17, INT_LEAST16_MIN, INT_LEAST16_MAX };
00109 verify (TYPE_MINIMUM (int_least16_t) == INT_LEAST16_MIN);
00110 verify (TYPE_MAXIMUM (int_least16_t) == INT_LEAST16_MAX);
00111 verify_same_types (INT_LEAST16_MIN, (int_least16_t) 0 + 0);
00112 verify_same_types (INT_LEAST16_MAX, (int_least16_t) 0 + 0);
00113 
00114 int_least32_t c3[3] = { 17, INT_LEAST32_MIN, INT_LEAST32_MAX };
00115 verify (TYPE_MINIMUM (int_least32_t) == INT_LEAST32_MIN);
00116 verify (TYPE_MAXIMUM (int_least32_t) == INT_LEAST32_MAX);
00117 verify_same_types (INT_LEAST32_MIN, (int_least32_t) 0 + 0);
00118 verify_same_types (INT_LEAST32_MAX, (int_least32_t) 0 + 0);
00119 
00120 #ifdef INT_LEAST64_MAX
00121 int_least64_t c4[3] = { 17, INT_LEAST64_MIN, INT_LEAST64_MAX };
00122 verify (TYPE_MINIMUM (int_least64_t) == INT_LEAST64_MIN);
00123 verify (TYPE_MAXIMUM (int_least64_t) == INT_LEAST64_MAX);
00124 verify_same_types (INT_LEAST64_MIN, (int_least64_t) 0 + 0);
00125 verify_same_types (INT_LEAST64_MAX, (int_least64_t) 0 + 0);
00126 #endif
00127 
00128 uint_least8_t d1[2] = { 17, UINT_LEAST8_MAX };
00129 verify (TYPE_MAXIMUM (uint_least8_t) == UINT_LEAST8_MAX);
00130 verify_same_types (UINT_LEAST8_MAX, (uint_least8_t) 0 + 0);
00131 
00132 uint_least16_t d2[2] = { 17, UINT_LEAST16_MAX };
00133 verify (TYPE_MAXIMUM (uint_least16_t) == UINT_LEAST16_MAX);
00134 verify_same_types (UINT_LEAST16_MAX, (uint_least16_t) 0 + 0);
00135 
00136 uint_least32_t d3[2] = { 17, UINT_LEAST32_MAX };
00137 verify (TYPE_MAXIMUM (uint_least32_t) == UINT_LEAST32_MAX);
00138 verify_same_types (UINT_LEAST32_MAX, (uint_least32_t) 0 + 0);
00139 
00140 #ifdef UINT_LEAST64_MAX
00141 uint_least64_t d4[2] = { 17, UINT_LEAST64_MAX };
00142 verify (TYPE_MAXIMUM (uint_least64_t) == UINT_LEAST64_MAX);
00143 verify_same_types (UINT_LEAST64_MAX, (uint_least64_t) 0 + 0);
00144 #endif
00145 
00146 #if INT_LEAST8_MIN && INT_LEAST8_MAX && INT_LEAST16_MIN && INT_LEAST16_MAX && INT_LEAST32_MIN && INT_LEAST32_MAX
00147 /* ok */
00148 #else
00149 err or;
00150 #endif
00151 
00152 #if UINT_LEAST8_MAX && UINT_LEAST16_MAX && UINT_LEAST32_MAX
00153 /* ok */
00154 #else
00155 err or;
00156 #endif
00157 
00158 /* 7.18.1.3. Fastest minimum-width integer types */
00159 /* 7.18.2.3. Limits of fastest minimum-width integer types */
00160 
00161 int_fast8_t e1[3] = { 17, INT_FAST8_MIN, INT_FAST8_MAX };
00162 verify (TYPE_MINIMUM (int_fast8_t) == INT_FAST8_MIN);
00163 verify (TYPE_MAXIMUM (int_fast8_t) == INT_FAST8_MAX);
00164 verify_same_types (INT_FAST8_MIN, (int_fast8_t) 0 + 0);
00165 verify_same_types (INT_FAST8_MAX, (int_fast8_t) 0 + 0);
00166 
00167 int_fast16_t e2[3] = { 17, INT_FAST16_MIN, INT_FAST16_MAX };
00168 verify (TYPE_MINIMUM (int_fast16_t) == INT_FAST16_MIN);
00169 verify (TYPE_MAXIMUM (int_fast16_t) == INT_FAST16_MAX);
00170 verify_same_types (INT_FAST16_MIN, (int_fast16_t) 0 + 0);
00171 verify_same_types (INT_FAST16_MAX, (int_fast16_t) 0 + 0);
00172 
00173 int_fast32_t e3[3] = { 17, INT_FAST32_MIN, INT_FAST32_MAX };
00174 verify (TYPE_MINIMUM (int_fast32_t) == INT_FAST32_MIN);
00175 verify (TYPE_MAXIMUM (int_fast32_t) == INT_FAST32_MAX);
00176 verify_same_types (INT_FAST32_MIN, (int_fast32_t) 0 + 0);
00177 verify_same_types (INT_FAST32_MAX, (int_fast32_t) 0 + 0);
00178 
00179 #ifdef INT_FAST64_MAX
00180 int_fast64_t e4[3] = { 17, INT_FAST64_MIN, INT_FAST64_MAX };
00181 verify (TYPE_MINIMUM (int_fast64_t) == INT_FAST64_MIN);
00182 verify (TYPE_MAXIMUM (int_fast64_t) == INT_FAST64_MAX);
00183 verify_same_types (INT_FAST64_MIN, (int_fast64_t) 0 + 0);
00184 verify_same_types (INT_FAST64_MAX, (int_fast64_t) 0 + 0);
00185 #endif
00186 
00187 uint_fast8_t f1[2] = { 17, UINT_FAST8_MAX };
00188 verify (TYPE_MAXIMUM (uint_fast8_t) == UINT_FAST8_MAX);
00189 verify_same_types (UINT_FAST8_MAX, (uint_fast8_t) 0 + 0);
00190 
00191 uint_fast16_t f2[2] = { 17, UINT_FAST16_MAX };
00192 verify (TYPE_MAXIMUM (uint_fast16_t) == UINT_FAST16_MAX);
00193 verify_same_types (UINT_FAST16_MAX, (uint_fast16_t) 0 + 0);
00194 
00195 uint_fast32_t f3[2] = { 17, UINT_FAST32_MAX };
00196 verify (TYPE_MAXIMUM (uint_fast32_t) == UINT_FAST32_MAX);
00197 verify_same_types (UINT_FAST32_MAX, (uint_fast32_t) 0 + 0);
00198 
00199 #ifdef UINT_FAST64_MAX
00200 uint_fast64_t f4[2] = { 17, UINT_FAST64_MAX };
00201 verify (TYPE_MAXIMUM (uint_fast64_t) == UINT_FAST64_MAX);
00202 verify_same_types (UINT_FAST64_MAX, (uint_fast64_t) 0 + 0);
00203 #endif
00204 
00205 #if INT_FAST8_MIN && INT_FAST8_MAX && INT_FAST16_MIN && INT_FAST16_MAX && INT_FAST32_MIN && INT_FAST32_MAX
00206 /* ok */
00207 #else
00208 err or;
00209 #endif
00210 
00211 #if UINT_FAST8_MAX && UINT_FAST16_MAX && UINT_FAST32_MAX
00212 /* ok */
00213 #else
00214 err or;
00215 #endif
00216 
00217 /* 7.18.1.4. Integer types capable of holding object pointers */
00218 /* 7.18.2.4. Limits of integer types capable of holding object pointers */
00219 
00220 intptr_t g[3] = { 17, INTPTR_MIN, INTPTR_MAX };
00221 verify (TYPE_MINIMUM (intptr_t) == INTPTR_MIN);
00222 verify (TYPE_MAXIMUM (intptr_t) == INTPTR_MAX);
00223 verify_same_types (INTPTR_MIN, (intptr_t) 0 + 0);
00224 verify_same_types (INTPTR_MAX, (intptr_t) 0 + 0);
00225 
00226 uintptr_t h[2] = { 17, UINTPTR_MAX };
00227 verify (TYPE_MAXIMUM (uintptr_t) == UINTPTR_MAX);
00228 verify_same_types (UINTPTR_MAX, (uintptr_t) 0 + 0);
00229 
00230 #if INTPTR_MIN && INTPTR_MAX && UINTPTR_MAX
00231 /* ok */
00232 #else
00233 err or;
00234 #endif
00235 
00236 /* 7.18.1.5. Greatest-width integer types */
00237 /* 7.18.2.5. Limits of greatest-width integer types */
00238 
00239 intmax_t i[3] = { INTMAX_C (17), INTMAX_MIN, INTMAX_MAX };
00240 verify (TYPE_MINIMUM (intmax_t) == INTMAX_MIN);
00241 verify (TYPE_MAXIMUM (intmax_t) == INTMAX_MAX);
00242 verify_same_types (INTMAX_MIN, (intmax_t) 0 + 0);
00243 verify_same_types (INTMAX_MAX, (intmax_t) 0 + 0);
00244 
00245 uintmax_t j[2] = { UINTMAX_C (17), UINTMAX_MAX };
00246 verify (TYPE_MAXIMUM (uintmax_t) == UINTMAX_MAX);
00247 verify_same_types (UINTMAX_MAX, (uintmax_t) 0 + 0);
00248 
00249 /* As of 2007, Sun C and HP-UX 10.20 cc don't support 'long long' constants in
00250    the preprocessor.  */
00251 #if !(defined __SUNPRO_C || (defined __hpux && !defined __GNUC__))
00252 #if INTMAX_MIN && INTMAX_MAX && UINTMAX_MAX
00253 /* ok */
00254 #else
00255 err or;
00256 #endif
00257 #endif
00258 
00259 /* 7.18.3. Limits of other integer types */
00260 
00261 #include <stddef.h>
00262 
00263 verify (TYPE_MINIMUM (ptrdiff_t) == PTRDIFF_MIN);
00264 verify (TYPE_MAXIMUM (ptrdiff_t) == PTRDIFF_MAX);
00265 verify_same_types (PTRDIFF_MIN, (ptrdiff_t) 0 + 0);
00266 verify_same_types (PTRDIFF_MAX, (ptrdiff_t) 0 + 0);
00267 
00268 #if PTRDIFF_MIN && PTRDIFF_MAX
00269 /* ok */
00270 #else
00271 err or;
00272 #endif
00273 
00274 #include <signal.h>
00275 
00276 verify (TYPE_MINIMUM (sig_atomic_t) == SIG_ATOMIC_MIN);
00277 verify (TYPE_MAXIMUM (sig_atomic_t) == SIG_ATOMIC_MAX);
00278 verify_same_types (SIG_ATOMIC_MIN, (sig_atomic_t) 0 + 0);
00279 verify_same_types (SIG_ATOMIC_MAX, (sig_atomic_t) 0 + 0);
00280 
00281 #if SIG_ATOMIC_MIN != 17 && SIG_ATOMIC_MAX
00282 /* ok */
00283 #else
00284 err or;
00285 #endif
00286 
00287 verify (TYPE_MAXIMUM (size_t) == SIZE_MAX);
00288 verify_same_types (SIZE_MAX, (size_t) 0 + 0);
00289 
00290 #if SIZE_MAX
00291 /* ok */
00292 #else
00293 err or;
00294 #endif
00295 
00296 #if HAVE_WCHAR_T
00297 verify (TYPE_MINIMUM (wchar_t) == WCHAR_MIN);
00298 verify (TYPE_MAXIMUM (wchar_t) == WCHAR_MAX);
00299 verify_same_types (WCHAR_MIN, (wchar_t) 0 + 0);
00300 verify_same_types (WCHAR_MAX, (wchar_t) 0 + 0);
00301 
00302 # if WCHAR_MIN != 17 && WCHAR_MAX
00303 /* ok */
00304 # else
00305 err or;
00306 # endif
00307 #endif
00308 
00309 #if HAVE_WINT_T
00310 # include <wchar.h>
00311 
00312 verify (TYPE_MINIMUM (wint_t) == WINT_MIN);
00313 verify (TYPE_MAXIMUM (wint_t) == WINT_MAX);
00314 verify_same_types (WINT_MIN, (wint_t) 0 + 0);
00315 verify_same_types (WINT_MAX, (wint_t) 0 + 0);
00316 
00317 # if WINT_MIN != 17 && WINT_MAX
00318 /* ok */
00319 # else
00320 err or;
00321 # endif
00322 #endif
00323 
00324 /* 7.18.4. Macros for integer constants */
00325 
00326 verify (INT8_C (17) == 17);
00327 verify_same_types (INT8_C (17), (int_least8_t)0 + 0);
00328 verify (UINT8_C (17) == 17);
00329 verify_same_types (UINT8_C (17), (uint_least8_t)0 + 0);
00330 
00331 verify (INT16_C (17) == 17);
00332 verify_same_types (INT16_C (17), (int_least16_t)0 + 0);
00333 verify (UINT16_C (17) == 17);
00334 verify_same_types (UINT16_C (17), (uint_least16_t)0 + 0);
00335 
00336 verify (INT32_C (17) == 17);
00337 verify_same_types (INT32_C (17), (int_least32_t)0 + 0);
00338 verify (UINT32_C (17) == 17);
00339 verify_same_types (UINT32_C (17), (uint_least32_t)0 + 0);
00340 
00341 #ifdef INT64_C
00342 verify (INT64_C (17) == 17);
00343 verify_same_types (INT64_C (17), (int_least64_t)0 + 0);
00344 #endif
00345 #ifdef UINT64_C
00346 verify (UINT64_C (17) == 17);
00347 verify_same_types (UINT64_C (17), (uint_least64_t)0 + 0);
00348 #endif
00349 
00350 verify (INTMAX_C (17) == 17);
00351 verify_same_types (INTMAX_C (17), (intmax_t)0 + 0);
00352 verify (UINTMAX_C (17) == 17);
00353 verify_same_types (UINTMAX_C (17), (uintmax_t)0 + 0);
00354 
00355 
00356 int
00357 main ()
00358 {
00359   return 0;
00360 }

Generated on Tue Oct 21 18:28:19 2008 for gsasl by  doxygen 1.5.6