scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

0006-limits.c (4279B)


      1 #include <limits.h>
      2 #include <stdio.h>
      3 
      4 /*
      5  * This test assumes that CHAR_BIT is the size of every
      6  * unit returned by sizeof. It also assumes 2 complement.
      7  */
      8 
      9 /*
     10 output:
     11 test1
     12 test2
     13 test3
     14 end:
     15 */
     16 
     17 void
     18 test1()
     19 {
     20 	puts("test1");
     21 
     22 	if (CHAR_BIT < 8 ||
     23 	    CHAR_MAX < 127 || CHAR_MIN > 0 ||
     24 	    CHAR_MAX != SCHAR_MAX && CHAR_MAX != UCHAR_MAX)
     25 		puts("wrong char definition");
     26 
     27 	if (SCHAR_MAX < 127 || CHAR_MIN > -127)
     28 		puts("wrong signed char definition");
     29 
     30 	if (UCHAR_MAX < 255 || UCHAR_MAX <= 0)
     31 		puts("wrong unsigned char definition");
     32 
     33 	if (SHRT_MAX < 32767 ||
     34 	    SHRT_MIN > -32767 ||
     35 	    USHRT_MAX < 65535 || USHRT_MAX <= 0)
     36 		puts("wrong short definition");
     37 
     38 	if (INT_MAX < 32767 ||
     39 	    INT_MIN > -32767 ||
     40 	    UINT_MAX < 65535 || UINT_MAX <= 0 ||
     41 	    INT_MAX < SCHAR_MAX || INT_MIN > SCHAR_MIN ||
     42 	    UINT_MAX < UCHAR_MAX ||
     43 	    INT_MAX < SHRT_MAX || INT_MIN > SHRT_MIN ||
     44 	    UINT_MAX < USHRT_MAX)
     45 		puts("wrong int definition");
     46 
     47 	if (LONG_MAX < 2147483647 ||
     48 	    LONG_MIN > -2147483647 ||
     49 	    ULONG_MAX < 4294967295 || ULONG_MAX <= 0 ||
     50 	    LONG_MAX < SCHAR_MAX || LONG_MIN > SCHAR_MIN ||
     51 	    ULONG_MAX < UCHAR_MAX ||
     52 	    LONG_MAX < SHRT_MAX || LONG_MIN > SHRT_MIN ||
     53 	    ULONG_MAX < USHRT_MAX ||
     54 	    LONG_MAX < INT_MAX || LONG_MIN > INT_MIN ||
     55 	    ULONG_MAX < UINT_MAX)
     56 		puts("wrong long definition");
     57 
     58 	if (LONG_MAX < 9223372036854775807 ||
     59 	    LONG_MIN > -9223372036854775807 ||
     60 	    ULONG_MAX < 18446744073709551615 || ULONG_MAX <= 0 ||
     61 	    LONG_MAX < SCHAR_MAX || LONG_MIN > SCHAR_MIN ||
     62 	    ULONG_MAX < UCHAR_MAX ||
     63 	    LONG_MAX < SHRT_MAX || LONG_MIN > SHRT_MIN ||
     64 	    ULONG_MAX < USHRT_MAX ||
     65 	    LONG_MAX < LONG_MAX || LONG_MIN > LONG_MIN ||
     66 	    ULONG_MAX < ULONG_MAX)
     67 		puts("wrong long definition");
     68 
     69 	if (LLONG_MAX < 9223372036854775807 ||
     70 	    LLONG_MIN > -9223372036854775807 ||
     71 	    ULLONG_MAX < 18446744073709551615 || ULLONG_MAX <= 0 ||
     72 	    LLONG_MAX < SCHAR_MAX || LLONG_MIN > SCHAR_MIN ||
     73 	    ULLONG_MAX < UCHAR_MAX ||
     74 	    LLONG_MAX < SHRT_MAX || LLONG_MIN > SHRT_MIN ||
     75 	    ULLONG_MAX < USHRT_MAX ||
     76 	    LLONG_MAX < LONG_MAX || LLONG_MIN > LONG_MIN ||
     77 	    ULLONG_MAX < ULONG_MAX)
     78 		puts("wrong long long definition");
     79 
     80 	if (MB_LEN_MAX < sizeof(char))
     81 		puts("wrong value for MB_LEN_MAX");
     82 }
     83 
     84 
     85 void
     86 test2()
     87 {
     88 	char c;
     89 	int i;
     90 
     91 	puts("test2");
     92 	if ('\xff' > 0) {
     93 		for (c = i = 0; i < CHAR_BIT; i++) {
     94 			c <<= 1;
     95 			c |= 1;
     96 		}
     97 		if (c != CHAR_MAX)
     98 			printf("wrong char max %d-%d", c, CHAR_MAX);
     99 		if (CHAR_MIN != 0)
    100 			printf("wrong char min %d-%d", c, CHAR_MIN);
    101 	} else {
    102 		for (c = i = 0; i < CHAR_BIT -1; i++) {
    103 			c <<= 1;
    104 			c |= 1;
    105 		}
    106 		if (c != CHAR_MAX)
    107 			printf("wrong char max %d-%d", c, CHAR_MAX);
    108 		c = -c - 1;
    109 		if (c != CHAR_MIN)
    110 			printf("wrong char min %d-%d", c, CHAR_MIN);
    111 	}
    112 }
    113 
    114 #define SMAX(t) for (t = n = 0; n < sizeof(t)*CHAR_BIT -1; n++) {t <<= 1; t |= 1;}
    115 #define UMAX(t) for (t = n = 0; n < sizeof(t)*CHAR_BIT; n++) {t <<= 1; t |= 1;}
    116 
    117 void
    118 test3()
    119 {
    120 	signed char sc;
    121 	unsigned char uc;
    122 	int i, n;
    123 	unsigned u;
    124 	long l;
    125 	unsigned long ul;
    126 	long long ll;
    127 	unsigned long long ull;
    128 
    129 	puts("test3");
    130 	SMAX(sc);
    131 	if (sc != SCHAR_MAX)
    132 		printf("wrong signed char max %d %d\n", sc, SCHAR_MAX);
    133 	sc = -sc - 1;
    134 	if (sc != SCHAR_MIN)
    135 		printf("wrong signed char min %d %d\n", sc, SCHAR_MIN);
    136 
    137 	UMAX(uc);
    138 	if (uc != UCHAR_MAX)
    139 		printf("wrong unsigned char max %u %u", uc, UCHAR_MAX);
    140 
    141 	SMAX(i);
    142 	if (i != INT_MAX)
    143 		printf("wrong int max %d %d\n", i, INT_MAX);
    144 	i = -i - 1;
    145 	if (i != INT_MIN)
    146 		printf("wrong int min %d %d\n", i, INT_MIN);
    147 
    148 	UMAX(u);
    149 	if (u != UINT_MAX)
    150 		printf("wrong unsigned int max %u %u\n", u, UINT_MAX);
    151 
    152 	SMAX(l);
    153 	if (l != LONG_MAX)
    154 		printf("wrong long max %ld %ld\n", l, (long) LONG_MAX);
    155 	l = -l - 1;
    156 	if (l != LONG_MIN)
    157 		printf("wrong long max %ld %ld\n", l, (long) LONG_MIN);
    158 
    159 	UMAX(ul);
    160 	if (ul != ULONG_MAX)
    161 		printf("wrong int max %lu %lu\n", ul, (unsigned long) ULONG_MAX);
    162 
    163 	SMAX(ll);
    164 	if (ll != LLONG_MAX)
    165 		printf("wrong llong max %lld %lld\n", ll, (long long) LLONG_MAX);
    166 	ll = -ll - 1;
    167 	if (ll != LLONG_MIN)
    168 		printf("wrong llong min %lld %lld\n", ll, (long long) LLONG_MIN);
    169 
    170 	UMAX(ull);
    171 	if (ull != ULLONG_MAX)
    172 		printf("wrong ullong max %llu %llu\n", ull, (unsigned long long) ULLONG_MAX);
    173 }
    174 
    175 int
    176 main()
    177 {
    178 	test1();
    179 	test2();
    180 	test3();
    181 
    182 	return 0;
    183 }