]> git.sur5r.net Git - cc65/blob - testcode/lib/strtoul-test.c
C90 param, void
[cc65] / testcode / lib / strtoul-test.c
1 /* A small test for strtuol. Assumes twos complement */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <limits.h>
6 #include <errno.h>
7
8
9
10 #define outfile stderr
11
12
13
14 #define ERROR   0
15 #define OK      1
16
17
18
19 static unsigned int Failures = 0;
20
21
22
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.
26 */
27 {
28     int Len = strlen (Buf);
29
30     while (--Len >= 0) {
31         switch (Buf[Len]) {
32             case '9':
33                 Buf[Len] = '0';
34                 break;
35
36             default:
37                 ++(Buf[Len]);
38                 return;
39         }
40     }
41 }
42
43
44
45 static void CheckStrToUL (const char* Str, int Base, unsigned long Val, unsigned char Ok)
46 {
47     char* EndPtr;
48     unsigned long Res = strtoul (Str, &EndPtr, Base);
49     if (Ok) {
50         if (Res != Val) {
51             fprintf (outfile,
52                      "strtol error in \"%s\":\n"
53                      "  result = %lu, should be %lu, chars = %d\n",
54                      Str, Res, Val, EndPtr - Str);
55             ++Failures;
56         }
57     } else {
58         if (errno != ERANGE) {
59             fprintf (outfile,
60                      "strtol error in \"%s\":\n"
61                      "  should not convert, but errno = %d\n",
62                      Str, errno);
63             ++Failures;
64         }
65         if (Res != Val) {
66             fprintf (outfile,
67                      "strtol error in \"%s\":\n"
68                      "  result = %lu, should be %lu, chars = %d\n",
69                      Str, Res, Val, EndPtr - Str);
70             ++Failures;
71         }
72     }
73 }
74
75
76
77 int main (void)
78 {
79     char Buf[80];
80
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);
86
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);
91
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);
99
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);
105
106     /* Check ULONG_MAX */
107     sprintf (Buf, "%lu", ULONG_MAX);
108     CheckStrToUL (Buf, 0, ULONG_MAX, OK);
109
110     /* Check value one larger */
111     sprintf (Buf+1, "%lu", ULONG_MAX);
112     Buf[0] = '0';
113     IncStr (Buf);
114     if (Buf[0] == '0') {
115         Buf[0] = ' ';
116     }
117     CheckStrToUL (Buf, 0, ULONG_MAX, ERROR);
118
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);
123
124     /* Check a few other bases */
125     CheckStrToUL ("aBcD", 36, 481261UL, OK);
126     CheckStrToUL ("zyaB", 35, 0UL, ERROR);
127     CheckStrToUL ("zyaB", 36, 1677395UL, ERROR);
128
129     fprintf (outfile, "Failures: %u\n", Failures);
130     return (Failures != 0);
131 }
132