1 /* A small test for strtuol. Assumes twos complement */
10 #define outfile stderr
19 static unsigned int Failures = 0;
23 static void IncStr (char* Buf)
24 /* Increment a number represented as a string by one. The string MUST not
25 ** start with a '9', we cannot handle overflow in this case.
28 int Len = strlen (Buf);
45 static void CheckStrToUL (const char* Str, int Base, unsigned long Val, unsigned char Ok)
48 unsigned long Res = strtoul (Str, &EndPtr, Base);
52 "strtol error in \"%s\":\n"
53 " result = %lu, should be %lu, chars = %d\n",
54 Str, Res, Val, EndPtr - Str);
58 if (errno != ERANGE) {
60 "strtol error in \"%s\":\n"
61 " should not convert, but errno = %d\n",
67 "strtol error in \"%s\":\n"
68 " result = %lu, should be %lu, chars = %d\n",
69 Str, Res, Val, EndPtr - Str);
81 /* Prefixed allowed if base = 0 */
82 CheckStrToUL ("\t 0x10G ", 0, 16UL, OK);
83 CheckStrToUL ("\t 0X10G ", 0, 16UL, OK);
84 CheckStrToUL (" \t0377\t", 0, 255UL, OK);
85 CheckStrToUL (" 377", 0, 377UL, OK);
87 CheckStrToUL ("\t -0x10G ", 0, (unsigned long) -16L, OK);
88 CheckStrToUL ("\t -0X10G ", 0, (unsigned long) -16L, OK);
89 CheckStrToUL (" \t-0377\t", 0, (unsigned long) -255L, OK);
90 CheckStrToUL (" -377", 0, (unsigned long) -377L, OK);
92 /* No prefixes if base = 10 */
93 CheckStrToUL ("\t 1234 ", 10, 1234UL, OK);
94 CheckStrToUL ("\t -1234 ", 10, (unsigned long) -1234L, OK);
95 CheckStrToUL ("\t -0x10G ", 10, 0UL, OK);
96 CheckStrToUL ("\t -0X10G ", 10, 0UL, OK);
97 CheckStrToUL (" \t-0377\t", 10, (unsigned long) -377L, OK);
98 CheckStrToUL (" 0377", 10, 377UL, OK);
100 /* 0x prefix is allowed if base = 16 */
101 CheckStrToUL ("\t 0x1234 ", 16, 0x1234UL, OK);
102 CheckStrToUL ("\t -0x1234 ", 16, (unsigned long) -0x1234L, OK);
103 CheckStrToUL ("\t -010G ", 16, (unsigned long) -16L, OK);
104 CheckStrToUL ("\t 10G ", 16, 16UL, OK);
106 /* Check ULONG_MAX */
107 sprintf (Buf, "%lu", ULONG_MAX);
108 CheckStrToUL (Buf, 0, ULONG_MAX, OK);
110 /* Check value one larger */
111 sprintf (Buf+1, "%lu", ULONG_MAX);
117 CheckStrToUL (Buf, 0, ULONG_MAX, ERROR);
119 /* Check numbers that are much too large or small */
120 CheckStrToUL ("-999999999999999999999999999999999999999999999999999999999", 0, ULONG_MAX, ERROR);
121 CheckStrToUL ("+999999999999999999999999999999999999999999999999999999999", 0, ULONG_MAX, ERROR);
122 CheckStrToUL (" 999999999999999999999999999999999999999999999999999999999", 0, ULONG_MAX, ERROR);
124 /* Check a few other bases */
125 CheckStrToUL ("aBcD", 36, 481261UL, OK);
126 CheckStrToUL ("zyaB", 35, 0UL, ERROR);
127 CheckStrToUL ("zyaB", 36, 1677395UL, ERROR);
129 fprintf (outfile, "Failures: %u\n", Failures);
130 return (Failures != 0);