2 !!DESCRIPTION!! A small test for strtuol. Assumes twos complement
17 static unsigned int Failures = 0;
19 static void IncStr (char* Buf)
20 /* Increment a number represented as a string by one. The string MUST not
21 * start with a '9', we cannot handle overflow in this case.
24 int Len = strlen (Buf);
39 static void CheckStrToUL (const char* Str, int Base, unsigned long Val, unsigned char Ok)
42 unsigned long Res = strtoul (Str, &EndPtr, Base);
45 printf ("strtol error in \"%s\":\n"
46 " result = %lu, should be %lu, chars = %d\n",
47 Str, Res, Val, EndPtr - Str);
51 if (errno != ERANGE) {
52 printf ("strtol error in \"%s\":\n"
53 " should not convert, but errno = %d\n",
58 printf ("strtol error in \"%s\":\n"
59 " result = %lu, should be %lu, chars = %d\n",
60 Str, Res, Val, EndPtr - Str);
70 /* Prefixed allowed if base = 0 */
71 CheckStrToUL ("\t 0x10G ", 0, 16UL, OK);
72 CheckStrToUL ("\t 0X10G ", 0, 16UL, OK);
73 CheckStrToUL (" \t0377\t", 0, 255UL, OK);
74 CheckStrToUL (" 377", 0, 377UL, OK);
76 CheckStrToUL ("\t -0x10G ", 0, (unsigned long) -16L, OK);
77 CheckStrToUL ("\t -0X10G ", 0, (unsigned long) -16L, OK);
78 CheckStrToUL (" \t-0377\t", 0, (unsigned long) -255L, OK);
79 CheckStrToUL (" -377", 0, (unsigned long) -377L, OK);
81 /* No prefixes if base = 10 */
82 CheckStrToUL ("\t 1234 ", 10, 1234UL, OK);
83 CheckStrToUL ("\t -1234 ", 10, (unsigned long) -1234L, OK);
84 CheckStrToUL ("\t -0x10G ", 10, 0UL, OK);
85 CheckStrToUL ("\t -0X10G ", 10, 0UL, OK);
86 CheckStrToUL (" \t-0377\t", 10, (unsigned long) -377L, OK);
87 CheckStrToUL (" 0377", 10, 377UL, OK);
89 /* 0x prefix is allowed if base = 16 */
90 CheckStrToUL ("\t 0x1234 ", 16, 0x1234UL, OK);
91 CheckStrToUL ("\t -0x1234 ", 16, (unsigned long) -0x1234L, OK);
92 CheckStrToUL ("\t -010G ", 16, (unsigned long) -16L, OK);
93 CheckStrToUL ("\t 10G ", 16, 16UL, OK);
96 sprintf (Buf, "%lu", ULONG_MAX);
97 CheckStrToUL (Buf, 0, ULONG_MAX, OK);
99 /* Check value one larger */
100 sprintf (Buf+1, "%lu", ULONG_MAX);
106 CheckStrToUL (Buf, 0, ULONG_MAX, ERROR);
108 /* Check numbers that are much too large or small */
109 CheckStrToUL ("-999999999999999999999999999999999999999999999999999999999", 0, ULONG_MAX, ERROR);
110 CheckStrToUL ("+999999999999999999999999999999999999999999999999999999999", 0, ULONG_MAX, ERROR);
111 CheckStrToUL (" 999999999999999999999999999999999999999999999999999999999", 0, ULONG_MAX, ERROR);
113 /* Check a few other bases */
114 CheckStrToUL ("aBcD", 36, 481261UL, OK);
115 CheckStrToUL ("zyaB", 35, 0UL, ERROR);
116 CheckStrToUL ("zyaB", 36, 1677395UL, ERROR);
118 printf ("Failures: %u\n", Failures);