Back to index

glibc  2.9
tst-limits.c
Go to the documentation of this file.
00001 /* It is important that this comes first to not hide effects introduced
00002    by other headers.  */
00003 #include <limits.h>
00004 
00005 #include <inttypes.h>
00006 #include <stdio.h>
00007 
00008 
00009 static long long int
00010 bitval (int bits)
00011 {
00012   long long int val = 0;
00013   while (bits-- > 0)
00014     val |= 1ll << bits;
00015   return val;
00016 }
00017 
00018 
00019 int
00020 main (void)
00021 {
00022   int result = 0;
00023 
00024 #define TEST(name, format, expected) \
00025   printf ("%-12s expected = %-20" format "  actual = %" format "\n",        \
00026          #name ":", expected, name);                                        \
00027   result |= name != expected
00028 
00029   /* The limits from ISO C99.  */
00030 
00031   /* We cannot support anything but 8-bit chars.  */
00032   TEST (CHAR_BIT, "d", 8);
00033   TEST (SCHAR_MIN, "d", -128);
00034   TEST (SCHAR_MAX, "d", 127);
00035   TEST (UCHAR_MAX, "d", 255);
00036 
00037   TEST (SHRT_MIN, "d", -(1 << (sizeof (short int) * CHAR_BIT - 1)));
00038   TEST (SHRT_MAX, "d", (1 << (sizeof (short int) * CHAR_BIT - 1)) - 1);
00039   TEST (USHRT_MAX, "d", (1 << sizeof (short int) * CHAR_BIT) - 1);
00040 
00041   TEST (INT_MIN, "d", (int) -bitval (sizeof (int) * CHAR_BIT - 1) - 1);
00042   TEST (INT_MAX, "d", (int) bitval (sizeof (int) * CHAR_BIT - 1));
00043   TEST (UINT_MAX, "u",
00044        (unsigned int) bitval (sizeof (unsigned int) * CHAR_BIT));
00045 
00046   TEST (LONG_MIN, "ld",
00047        (long int) -bitval (sizeof (long int) * CHAR_BIT - 1) - 1);
00048   TEST (LONG_MAX, "ld", (long int) bitval (sizeof (long int) * CHAR_BIT - 1));
00049   TEST (ULONG_MAX, "lu",
00050        (unsigned long int) bitval (sizeof (unsigned long int) * CHAR_BIT));
00051 
00052   TEST (LLONG_MIN, "lld", -bitval (sizeof (long long int) * CHAR_BIT - 1) - 1);
00053   TEST (LLONG_MAX, "lld", bitval (sizeof (long long int) * CHAR_BIT - 1));
00054   TEST (ULLONG_MAX, "llu",
00055        (unsigned long long int) bitval (sizeof (unsigned long long int)
00056                                     * CHAR_BIT));
00057 
00058   /* Values from POSIX and Unix.  */
00059 #ifdef PAGESIZE
00060   TEST (PAGESIZE, "d", getpagesize ());
00061 #elif PAGE_SIZE
00062   TEST (PAGE_SIZE, "d", getpagesize ());
00063 #endif
00064 
00065   TEST (WORD_BIT, "d", (int) sizeof (int) * CHAR_BIT);
00066   TEST (LONG_BIT, "d", (int) sizeof (long int) * CHAR_BIT);
00067 
00068   return result;
00069 }