Back to index

glibc  2.9
tst-strtol.c
Go to the documentation of this file.
00001 /* My bet is this was written by Chris Torek.
00002    I reformatted and ansidecl-ized it, and tweaked it a little.  */
00003 
00004 #include <ctype.h>
00005 #include <stdio.h>
00006 #include <errno.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 #include <limits.h>
00010 
00011 struct ltest
00012   {
00013     const char *str;        /* Convert this.  */
00014     unsigned long int expect;      /* To get this.  */
00015     int base;               /* Use this base.  */
00016     char left;                     /* With this left over.  */
00017     int err;                /* And this in errno.  */
00018   };
00019 static const struct ltest tests[] =
00020   {
00021   /* First, signed numbers:  */
00022   /* simple... */
00023   {"123", 123, 0, 0, 0},
00024   {"+123", 123, 0, 0, 0},
00025   {"  123", 123, 0, 0, 0},
00026   {" 123 ", 123, 0, ' ', 0},
00027   {"   -17", -17, 0, 0, 0},
00028 
00029   /* implicit base... */
00030   {"0123", 0123, 0, 0, 0},
00031   {"0123a", 0123, 0, 'a', 0},
00032   {"01239", 0123, 0, '9', 0},
00033   {"0x123", 0x123, 0, 0, 0},
00034   {"-0x123", -0x123, 0, 0, 0},
00035   {"0x0xc", 0, 0, 'x', 0},
00036   {" +0x123fg", 0x123f, 0, 'g', 0},
00037 
00038   /* explicit base... */
00039   {"123", 0x123, 16, 0, 0},
00040   {"0x123", 0x123, 16, 0, 0},
00041   {"123", 0123, 8, 0, 0},
00042   {"0123", 0123, 8, 0, 0},
00043   {"0123", 123, 10, 0, 0},
00044   {"0x123", 0, 10, 'x', 0},
00045 
00046   /* case insensitivity... */
00047   {"abcd", 0xabcd, 16, 0, 0},
00048   {"AbCd", 0xabcd, 16, 0, 0},
00049   {"0xABCD", 0xabcd, 16, 0, 0},
00050   {"0Xabcd", 0xabcd, 16, 0, 0},
00051 
00052   /* odd bases... */
00053   {"0xyz", 33 * 35 + 34, 35, 'z', 0},
00054   {"yz!", 34 * 36 + 35, 36, '!', 0},
00055   {"-yz", -(34*36 + 35), 36, 0, 0},
00056   {"GhI4", ((16*20 + 17)*20 + 18)*20 + 4, 20, 0, 0},
00057 
00058   /* extremes... */
00059 #if LONG_MAX == 0x7fffffff
00060   {"2147483647", 2147483647, 0, 0, 0},
00061   {"2147483648", 2147483647, 0, 0, ERANGE},
00062   {"214748364888", 2147483647, 0, 0, ERANGE},
00063   {"2147483650", 2147483647, 0, 0, ERANGE},
00064   {"-2147483648", 0x80000000, 0, 0, 0},
00065   {"-2147483649", 0x80000000, 0, 0, ERANGE},
00066   {"0x1122334455z", 2147483647, 16, 'z', ERANGE},
00067 #else
00068   {"9223372036854775807", 9223372036854775807, 0, 0, 0},
00069   {"9223372036854775808", 9223372036854775807, 0, 0, ERANGE},
00070   {"922337203685477580777", 9223372036854775807, 0, 0, ERANGE},
00071   {"9223372036854775810", 9223372036854775807, 0, 0, ERANGE},
00072   {"-2147483648", -2147483648, 0, 0, 0},
00073   {"-9223372036854775808", 0x8000000000000000, 0, 0, 0},
00074   {"-9223372036854775809", 0x8000000000000000, 0, 0, ERANGE},
00075   {"0x112233445566778899z", 9223372036854775807, 16, 'z', ERANGE},
00076   {"0xFFFFFFFFFFFF00FF" , 9223372036854775807, 0, 0, ERANGE},
00077 #endif
00078   {NULL, 0, 0, 0, 0},
00079 
00080   /* Then unsigned.  */
00081   {"  0", 0, 0, 0, 0},
00082   {"0xffffffffg", 0xffffffff, 0, 'g', 0},
00083 #if LONG_MAX == 0x7fffffff
00084   {"-0xfedcba98", 0x01234568, 0, 0, 0},
00085   {"0xf1f2f3f4f5", 0xffffffff, 0, 0, ERANGE},
00086   {"-0x123456789", 0xffffffff, 0, 0, ERANGE},
00087 #else
00088   {"0xffffffffffffffffg", 0xffffffffffffffff, 0, 'g', 0},
00089   {"-0xfedcba987654321", 0xf0123456789abcdf, 0, 0, 0},
00090   {"0xf1f2f3f4f5f6f7f8f9", 0xffffffffffffffff, 0, 0, ERANGE},
00091   {"-0x123456789abcdef01", 0xffffffffffffffff, 0, 0, ERANGE},
00092 #endif
00093   {NULL, 0, 0, 0, 0},
00094   };
00095 
00096 /* Prototypes for local functions.  */
00097 static void expand (char *dst, int c);
00098 
00099 int
00100 main (void)
00101 {
00102   register const struct ltest *lt;
00103   char *ep;
00104   int status = 0;
00105   int save_errno;
00106 
00107   for (lt = tests; lt->str != NULL; ++lt)
00108     {
00109       register long int l;
00110 
00111       errno = 0;
00112       l = strtol (lt->str, &ep, lt->base);
00113       save_errno = errno;
00114       printf ("strtol(\"%s\", , %d) test %u",
00115              lt->str, lt->base, (unsigned int) (lt - tests));
00116       if (l == (long int) lt->expect && *ep == lt->left
00117          && save_errno == lt->err)
00118        puts("\tOK");
00119       else
00120        {
00121          puts("\tBAD");
00122          if (l != (long int) lt->expect)
00123            printf("  returns %ld, expected %ld\n",
00124                  l, (long int) lt->expect);
00125          if (lt->left != *ep)
00126            {
00127              char exp1[5], exp2[5];
00128              expand (exp1, *ep);
00129              expand (exp2, lt->left);
00130              printf ("  leaves '%s', expected '%s'\n", exp1, exp2);
00131            }
00132          if (save_errno != lt->err)
00133            printf ("  errno %d (%s)  instead of %d (%s)\n",
00134                   save_errno, strerror (save_errno),
00135                   lt->err, strerror (lt->err));
00136          status = 1;
00137        }
00138     }
00139 
00140   for (++lt; lt->str != NULL; lt++)
00141     {
00142       register unsigned long int ul;
00143 
00144       errno = 0;
00145       ul = strtoul (lt->str, &ep, lt->base);
00146       save_errno = errno;
00147       printf ("strtoul(\"%s\", , %d) test %u",
00148              lt->str, lt->base, (unsigned int) (lt - tests));
00149       if (ul == lt->expect && *ep == lt->left && save_errno == lt->err)
00150        puts("\tOK");
00151       else
00152        {
00153          puts ("\tBAD");
00154          if (ul != lt->expect)
00155            printf ("  returns %lu, expected %lu\n",
00156                   ul, lt->expect);
00157          if (lt->left != *ep)
00158            {
00159              char exp1[5], exp2[5];
00160              expand (exp1, *ep);
00161              expand (exp2, lt->left);
00162              printf ("  leaves '%s', expected '%s'\n", exp1, exp2);
00163            }
00164          if (save_errno != lt->err)
00165            printf ("  errno %d (%s) instead of %d (%s)\n",
00166                   save_errno, strerror (save_errno),
00167                   lt->err, strerror (lt->err));
00168          status = 1;
00169        }
00170     }
00171 
00172   return status ? EXIT_FAILURE : EXIT_SUCCESS;
00173 }
00174 
00175 static void
00176 expand (dst, c)
00177      char *dst;
00178      int c;
00179 {
00180   if (isprint (c))
00181     {
00182       dst[0] = c;
00183       dst[1] = '\0';
00184     }
00185   else
00186     (void) sprintf (dst, "%#.3o", (unsigned int) c);
00187 }