2 !!DESCRIPTION!! A small test for atoi/strtol. 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 CheckStrToL (const char* Str, int Base, long Val, unsigned char Ok)
42 long Res = strtol (Str, &EndPtr, Base);
45 printf ("strtol error in \"%s\":\n"
46 " result = %ld, should be %ld, 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 = %ld, should be %ld, chars = %d\n",
60 Str, Res, Val, EndPtr - Str);
70 /* Prefixed allowed if base = 0 */
71 CheckStrToL ("\t 0x10G ", 0, 16L, OK);
72 CheckStrToL ("\t 0X10G ", 0, 16L, OK);
73 CheckStrToL (" \t0377\t", 0, 255L, OK);
74 CheckStrToL (" 377", 0, 377L, OK);
76 CheckStrToL ("\t -0x10G ", 0, -16L, OK);
77 CheckStrToL ("\t -0X10G ", 0, -16L, OK);
78 CheckStrToL (" \t-0377\t", 0, -255L, OK);
79 CheckStrToL (" -377", 0, -377L, OK);
81 /* No prefixes if base = 10 */
82 CheckStrToL ("\t 1234 ", 10, 1234L, OK);
83 CheckStrToL ("\t -1234 ", 10, -1234L, OK);
84 CheckStrToL ("\t -0x10G ", 10, 0L, OK);
85 CheckStrToL ("\t -0X10G ", 10, 0L, OK);
86 CheckStrToL (" \t-0377\t", 10, -377L, OK);
87 CheckStrToL (" 0377", 10, 377L, OK);
89 /* 0x prefix is allowed if base = 16 */
90 CheckStrToL ("\t 0x1234 ", 16, 0x1234L, OK);
91 CheckStrToL ("\t -0x1234 ", 16, -0x1234L, OK);
92 CheckStrToL ("\t -010G ", 16, -16L, OK);
93 CheckStrToL ("\t 10G ", 16, 16L, OK);
95 /* Check LONG_MIN and LONG_MAX */
96 sprintf (Buf, "%ld", LONG_MIN);
97 CheckStrToL (Buf, 0, LONG_MIN, OK);
98 sprintf (Buf, "%ld", LONG_MAX);
99 CheckStrToL (Buf, 0, LONG_MAX, OK);
101 /* Check value one smaller */
102 sprintf (Buf+1, "%ld", LONG_MIN);
103 Buf[1] = '0'; /* Overwrite '-' */
111 CheckStrToL (Buf, 0, LONG_MIN, ERROR);
113 /* Check value one larger */
114 sprintf (Buf+1, "%ld", LONG_MAX);
120 CheckStrToL (Buf, 0, LONG_MAX, ERROR);
122 /* Check numbers that are much too large or small */
123 CheckStrToL ("-999999999999999999999999999999999999999999999999999999999", 0, LONG_MIN, ERROR);
124 CheckStrToL ("+999999999999999999999999999999999999999999999999999999999", 0, LONG_MAX, ERROR);
125 CheckStrToL (" 999999999999999999999999999999999999999999999999999999999", 0, LONG_MAX, ERROR);
127 /* Check a few other bases */
128 CheckStrToL ("aBcD", 36, 481261L, OK);
129 CheckStrToL ("zyaB", 35, 0L, ERROR);
130 CheckStrToL ("zyaB", 36, 1677395L, ERROR);
132 printf ("Failures: %u\n", Failures);