lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* It is important that this comes first to not hide effects introduced |
| 2 | by other headers. */ |
| 3 | #include <limits.h> |
| 4 | |
| 5 | #include <inttypes.h> |
| 6 | #include <stdio.h> |
| 7 | |
| 8 | |
| 9 | static long long int |
| 10 | bitval (int bits) |
| 11 | { |
| 12 | long long int val = 0; |
| 13 | while (bits-- > 0) |
| 14 | val |= 1ll << bits; |
| 15 | return val; |
| 16 | } |
| 17 | |
| 18 | |
| 19 | static int |
| 20 | do_test (void) |
| 21 | { |
| 22 | int result = 0; |
| 23 | |
| 24 | #define TEST(name, format, expected) \ |
| 25 | printf ("%-12s expected = %-20" format " actual = %" format "\n", \ |
| 26 | #name ":", expected, name); \ |
| 27 | result |= name != expected |
| 28 | |
| 29 | /* The limits from ISO C99. */ |
| 30 | |
| 31 | /* We cannot support anything but 8-bit chars. */ |
| 32 | TEST (CHAR_BIT, "d", 8); |
| 33 | TEST (SCHAR_MIN, "d", -128); |
| 34 | TEST (SCHAR_MAX, "d", 127); |
| 35 | TEST (UCHAR_MAX, "d", 255); |
| 36 | |
| 37 | TEST (SHRT_MIN, "d", -(1 << (sizeof (short int) * CHAR_BIT - 1))); |
| 38 | TEST (SHRT_MAX, "d", (1 << (sizeof (short int) * CHAR_BIT - 1)) - 1); |
| 39 | TEST (USHRT_MAX, "d", (1 << sizeof (short int) * CHAR_BIT) - 1); |
| 40 | |
| 41 | TEST (INT_MIN, "d", (int) -bitval (sizeof (int) * CHAR_BIT - 1) - 1); |
| 42 | TEST (INT_MAX, "d", (int) bitval (sizeof (int) * CHAR_BIT - 1)); |
| 43 | TEST (UINT_MAX, "u", |
| 44 | (unsigned int) bitval (sizeof (unsigned int) * CHAR_BIT)); |
| 45 | |
| 46 | TEST (LONG_MIN, "ld", |
| 47 | (long int) -bitval (sizeof (long int) * CHAR_BIT - 1) - 1); |
| 48 | TEST (LONG_MAX, "ld", (long int) bitval (sizeof (long int) * CHAR_BIT - 1)); |
| 49 | TEST (ULONG_MAX, "lu", |
| 50 | (unsigned long int) bitval (sizeof (unsigned long int) * CHAR_BIT)); |
| 51 | |
| 52 | TEST (LLONG_MIN, "lld", -bitval (sizeof (long long int) * CHAR_BIT - 1) - 1); |
| 53 | TEST (LLONG_MAX, "lld", bitval (sizeof (long long int) * CHAR_BIT - 1)); |
| 54 | TEST (ULLONG_MAX, "llu", |
| 55 | (unsigned long long int) bitval (sizeof (unsigned long long int) |
| 56 | * CHAR_BIT)); |
| 57 | |
| 58 | /* Values from POSIX and Unix. */ |
| 59 | #ifdef PAGESIZE |
| 60 | TEST (PAGESIZE, "d", getpagesize ()); |
| 61 | #elif defined (PAGE_SIZE) |
| 62 | TEST (PAGE_SIZE, "d", getpagesize ()); |
| 63 | #endif |
| 64 | |
| 65 | TEST (WORD_BIT, "d", (int) sizeof (int) * CHAR_BIT); |
| 66 | TEST (LONG_BIT, "d", (int) sizeof (long int) * CHAR_BIT); |
| 67 | |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | #define TEST_FUNCTION do_test () |
| 72 | #include "../test-skeleton.c" |