1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // test <stdint.h> 10 11 #include <stdint.h> 12 #include <cstddef> 13 #include <csignal> 14 #include <climits> 15 #include <type_traits> 16 #include <limits> 17 #include <cassert> 18 19 #include "test_macros.h" 20 21 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 22 # include <cwctype> 23 #endif 24 25 int main(int, char**) { 26 // typedef int8_t 27 static_assert(sizeof(int8_t)*CHAR_BIT == 8, 28 "sizeof(int8_t)*CHAR_BIT == 8"); 29 static_assert(std::is_signed<int8_t>::value, 30 "std::is_signed<int8_t>::value"); 31 // typedef int16_t 32 static_assert(sizeof(int16_t)*CHAR_BIT == 16, 33 "sizeof(int16_t)*CHAR_BIT == 16"); 34 static_assert(std::is_signed<int16_t>::value, 35 "std::is_signed<int16_t>::value"); 36 // typedef int32_t 37 static_assert(sizeof(int32_t)*CHAR_BIT == 32, 38 "sizeof(int32_t)*CHAR_BIT == 32"); 39 static_assert(std::is_signed<int32_t>::value, 40 "std::is_signed<int32_t>::value"); 41 // typedef int64_t 42 static_assert(sizeof(int64_t)*CHAR_BIT == 64, 43 "sizeof(int64_t)*CHAR_BIT == 64"); 44 static_assert(std::is_signed<int64_t>::value, 45 "std::is_signed<int64_t>::value"); 46 47 // typedef uint8_t 48 static_assert(sizeof(uint8_t)*CHAR_BIT == 8, 49 "sizeof(uint8_t)*CHAR_BIT == 8"); 50 static_assert(std::is_unsigned<uint8_t>::value, 51 "std::is_unsigned<uint8_t>::value"); 52 // typedef uint16_t 53 static_assert(sizeof(uint16_t)*CHAR_BIT == 16, 54 "sizeof(uint16_t)*CHAR_BIT == 16"); 55 static_assert(std::is_unsigned<uint16_t>::value, 56 "std::is_unsigned<uint16_t>::value"); 57 // typedef uint32_t 58 static_assert(sizeof(uint32_t)*CHAR_BIT == 32, 59 "sizeof(uint32_t)*CHAR_BIT == 32"); 60 static_assert(std::is_unsigned<uint32_t>::value, 61 "std::is_unsigned<uint32_t>::value"); 62 // typedef uint64_t 63 static_assert(sizeof(uint64_t)*CHAR_BIT == 64, 64 "sizeof(uint64_t)*CHAR_BIT == 64"); 65 static_assert(std::is_unsigned<uint64_t>::value, 66 "std::is_unsigned<uint64_t>::value"); 67 68 // typedef int_least8_t 69 static_assert(sizeof(int_least8_t)*CHAR_BIT >= 8, 70 "sizeof(int_least8_t)*CHAR_BIT >= 8"); 71 static_assert(std::is_signed<int_least8_t>::value, 72 "std::is_signed<int_least8_t>::value"); 73 // typedef int_least16_t 74 static_assert(sizeof(int_least16_t)*CHAR_BIT >= 16, 75 "sizeof(int_least16_t)*CHAR_BIT >= 16"); 76 static_assert(std::is_signed<int_least16_t>::value, 77 "std::is_signed<int_least16_t>::value"); 78 // typedef int_least32_t 79 static_assert(sizeof(int_least32_t)*CHAR_BIT >= 32, 80 "sizeof(int_least32_t)*CHAR_BIT >= 32"); 81 static_assert(std::is_signed<int_least32_t>::value, 82 "std::is_signed<int_least32_t>::value"); 83 // typedef int_least64_t 84 static_assert(sizeof(int_least64_t)*CHAR_BIT >= 64, 85 "sizeof(int_least64_t)*CHAR_BIT >= 64"); 86 static_assert(std::is_signed<int_least64_t>::value, 87 "std::is_signed<int_least64_t>::value"); 88 89 // typedef uint_least8_t 90 static_assert(sizeof(uint_least8_t)*CHAR_BIT >= 8, 91 "sizeof(uint_least8_t)*CHAR_BIT >= 8"); 92 static_assert(std::is_unsigned<uint_least8_t>::value, 93 "std::is_unsigned<uint_least8_t>::value"); 94 // typedef uint_least16_t 95 static_assert(sizeof(uint_least16_t)*CHAR_BIT >= 16, 96 "sizeof(uint_least16_t)*CHAR_BIT >= 16"); 97 static_assert(std::is_unsigned<uint_least16_t>::value, 98 "std::is_unsigned<uint_least16_t>::value"); 99 // typedef uint_least32_t 100 static_assert(sizeof(uint_least32_t)*CHAR_BIT >= 32, 101 "sizeof(uint_least32_t)*CHAR_BIT >= 32"); 102 static_assert(std::is_unsigned<uint_least32_t>::value, 103 "std::is_unsigned<uint_least32_t>::value"); 104 // typedef uint_least64_t 105 static_assert(sizeof(uint_least64_t)*CHAR_BIT >= 64, 106 "sizeof(uint_least64_t)*CHAR_BIT >= 64"); 107 static_assert(std::is_unsigned<uint_least64_t>::value, 108 "std::is_unsigned<uint_least64_t>::value"); 109 110 // typedef int_fast8_t 111 static_assert(sizeof(int_fast8_t)*CHAR_BIT >= 8, 112 "sizeof(int_fast8_t)*CHAR_BIT >= 8"); 113 static_assert(std::is_signed<int_fast8_t>::value, 114 "std::is_signed<int_fast8_t>::value"); 115 // typedef int_fast16_t 116 static_assert(sizeof(int_fast16_t)*CHAR_BIT >= 16, 117 "sizeof(int_fast16_t)*CHAR_BIT >= 16"); 118 static_assert(std::is_signed<int_fast16_t>::value, 119 "std::is_signed<int_fast16_t>::value"); 120 // typedef int_fast32_t 121 static_assert(sizeof(int_fast32_t)*CHAR_BIT >= 32, 122 "sizeof(int_fast32_t)*CHAR_BIT >= 32"); 123 static_assert(std::is_signed<int_fast32_t>::value, 124 "std::is_signed<int_fast32_t>::value"); 125 // typedef int_fast64_t 126 static_assert(sizeof(int_fast64_t)*CHAR_BIT >= 64, 127 "sizeof(int_fast64_t)*CHAR_BIT >= 64"); 128 static_assert(std::is_signed<int_fast64_t>::value, 129 "std::is_signed<int_fast64_t>::value"); 130 131 // typedef uint_fast8_t 132 static_assert(sizeof(uint_fast8_t)*CHAR_BIT >= 8, 133 "sizeof(uint_fast8_t)*CHAR_BIT >= 8"); 134 static_assert(std::is_unsigned<uint_fast8_t>::value, 135 "std::is_unsigned<uint_fast8_t>::value"); 136 // typedef uint_fast16_t 137 static_assert(sizeof(uint_fast16_t)*CHAR_BIT >= 16, 138 "sizeof(uint_fast16_t)*CHAR_BIT >= 16"); 139 static_assert(std::is_unsigned<uint_fast16_t>::value, 140 "std::is_unsigned<uint_fast16_t>::value"); 141 // typedef uint_fast32_t 142 static_assert(sizeof(uint_fast32_t)*CHAR_BIT >= 32, 143 "sizeof(uint_fast32_t)*CHAR_BIT >= 32"); 144 static_assert(std::is_unsigned<uint_fast32_t>::value, 145 "std::is_unsigned<uint_fast32_t>::value"); 146 // typedef uint_fast64_t 147 static_assert(sizeof(uint_fast64_t)*CHAR_BIT >= 64, 148 "sizeof(uint_fast64_t)*CHAR_BIT >= 64"); 149 static_assert(std::is_unsigned<uint_fast64_t>::value, 150 "std::is_unsigned<uint_fast64_t>::value"); 151 152 // typedef intptr_t 153 static_assert(sizeof(intptr_t) >= sizeof(void*), 154 "sizeof(intptr_t) >= sizeof(void*)"); 155 static_assert(std::is_signed<intptr_t>::value, 156 "std::is_signed<intptr_t>::value"); 157 // typedef uintptr_t 158 static_assert(sizeof(uintptr_t) >= sizeof(void*), 159 "sizeof(uintptr_t) >= sizeof(void*)"); 160 static_assert(std::is_unsigned<uintptr_t>::value, 161 "std::is_unsigned<uintptr_t>::value"); 162 163 // typedef intmax_t 164 static_assert(sizeof(intmax_t) >= sizeof(long long), 165 "sizeof(intmax_t) >= sizeof(long long)"); 166 static_assert(std::is_signed<intmax_t>::value, 167 "std::is_signed<intmax_t>::value"); 168 // typedef uintmax_t 169 static_assert(sizeof(uintmax_t) >= sizeof(unsigned long long), 170 "sizeof(uintmax_t) >= sizeof(unsigned long long)"); 171 static_assert(std::is_unsigned<uintmax_t>::value, 172 "std::is_unsigned<uintmax_t>::value"); 173 174 // INTN_MIN 175 static_assert(INT8_MIN == -128, "INT8_MIN == -128"); 176 static_assert(INT16_MIN == -32768, "INT16_MIN == -32768"); 177 static_assert(INT32_MIN == -2147483647 - 1, "INT32_MIN == -2147483648"); 178 static_assert(INT64_MIN == -9223372036854775807LL - 1, "INT64_MIN == -9223372036854775808LL"); 179 180 // INTN_MAX 181 static_assert(INT8_MAX == 127, "INT8_MAX == 127"); 182 static_assert(INT16_MAX == 32767, "INT16_MAX == 32767"); 183 static_assert(INT32_MAX == 2147483647, "INT32_MAX == 2147483647"); 184 static_assert(INT64_MAX == 9223372036854775807LL, "INT64_MAX == 9223372036854775807LL"); 185 186 // UINTN_MAX 187 static_assert(UINT8_MAX == 255, "UINT8_MAX == 255"); 188 static_assert(UINT16_MAX == 65535, "UINT16_MAX == 65535"); 189 static_assert(UINT32_MAX == 4294967295U, "UINT32_MAX == 4294967295"); 190 static_assert(UINT64_MAX == 18446744073709551615ULL, "UINT64_MAX == 18446744073709551615ULL"); 191 192 // INT_FASTN_MIN 193 static_assert(INT_FAST8_MIN <= -128, "INT_FAST8_MIN <= -128"); 194 static_assert(INT_FAST16_MIN <= -32768, "INT_FAST16_MIN <= -32768"); 195 static_assert(INT_FAST32_MIN <= -2147483647 - 1, "INT_FAST32_MIN <= -2147483648"); 196 static_assert(INT_FAST64_MIN <= -9223372036854775807LL - 1, "INT_FAST64_MIN <= -9223372036854775808LL"); 197 198 // INT_FASTN_MAX 199 static_assert(INT_FAST8_MAX >= 127, "INT_FAST8_MAX >= 127"); 200 static_assert(INT_FAST16_MAX >= 32767, "INT_FAST16_MAX >= 32767"); 201 static_assert(INT_FAST32_MAX >= 2147483647, "INT_FAST32_MAX >= 2147483647"); 202 static_assert(INT_FAST64_MAX >= 9223372036854775807LL, "INT_FAST64_MAX >= 9223372036854775807LL"); 203 204 // UINT_FASTN_MAX 205 static_assert(UINT_FAST8_MAX >= 255, "UINT_FAST8_MAX >= 255"); 206 static_assert(UINT_FAST16_MAX >= 65535, "UINT_FAST16_MAX >= 65535"); 207 static_assert(UINT_FAST32_MAX >= 4294967295U, "UINT_FAST32_MAX >= 4294967295"); 208 static_assert(UINT_FAST64_MAX >= 18446744073709551615ULL, "UINT_FAST64_MAX >= 18446744073709551615ULL"); 209 210 // INTPTR_MIN 211 assert(INTPTR_MIN == std::numeric_limits<intptr_t>::min()); 212 213 // INTPTR_MAX 214 assert(INTPTR_MAX == std::numeric_limits<intptr_t>::max()); 215 216 // UINTPTR_MAX 217 assert(UINTPTR_MAX == std::numeric_limits<uintptr_t>::max()); 218 219 // INTMAX_MIN 220 assert(INTMAX_MIN == std::numeric_limits<intmax_t>::min()); 221 222 // INTMAX_MAX 223 assert(INTMAX_MAX == std::numeric_limits<intmax_t>::max()); 224 225 // UINTMAX_MAX 226 assert(UINTMAX_MAX == std::numeric_limits<uintmax_t>::max()); 227 228 // PTRDIFF_MIN 229 assert(PTRDIFF_MIN == std::numeric_limits<std::ptrdiff_t>::min()); 230 231 // PTRDIFF_MAX 232 assert(PTRDIFF_MAX == std::numeric_limits<std::ptrdiff_t>::max()); 233 234 // SIG_ATOMIC_MIN 235 assert(SIG_ATOMIC_MIN == std::numeric_limits<sig_atomic_t>::min()); 236 237 // SIG_ATOMIC_MAX 238 assert(SIG_ATOMIC_MAX == std::numeric_limits<sig_atomic_t>::max()); 239 240 // SIZE_MAX 241 assert(SIZE_MAX == std::numeric_limits<size_t>::max()); 242 243 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 244 // WCHAR_MIN 245 assert(WCHAR_MIN == std::numeric_limits<wchar_t>::min()); 246 247 // WCHAR_MAX 248 assert(WCHAR_MAX == std::numeric_limits<wchar_t>::max()); 249 250 // WINT_MIN 251 assert(WINT_MIN == std::numeric_limits<wint_t>::min()); 252 253 // WINT_MAX 254 assert(WINT_MAX == std::numeric_limits<wint_t>::max()); 255 #endif 256 257 #ifndef INT8_C 258 #error INT8_C not defined 259 #endif 260 261 #ifndef INT16_C 262 #error INT16_C not defined 263 #endif 264 265 #ifndef INT32_C 266 #error INT32_C not defined 267 #endif 268 269 #ifndef INT64_C 270 #error INT64_C not defined 271 #endif 272 273 #ifndef UINT8_C 274 #error UINT8_C not defined 275 #endif 276 277 #ifndef UINT16_C 278 #error UINT16_C not defined 279 #endif 280 281 #ifndef UINT32_C 282 #error UINT32_C not defined 283 #endif 284 285 #ifndef UINT64_C 286 #error UINT64_C not defined 287 #endif 288 289 #ifndef INTMAX_C 290 #error INTMAX_C not defined 291 #endif 292 293 #ifndef UINTMAX_C 294 #error UINTMAX_C not defined 295 #endif 296 297 return 0; 298 } 299