]> git.sur5r.net Git - cc65/blob - testcode/lib/strtol-test.c
Add translation from PETSCII to screen codes.
[cc65] / testcode / lib / strtol-test.c
1 /* A small test for strtol. 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 CheckStrToL (const char* Str, int Base, long Val, unsigned char Ok)
46 {
47     char* EndPtr;
48     long Res = strtol (Str, &EndPtr, Base);
49     if (Ok) {
50         if (Res != Val) {
51             fprintf (outfile,
52                      "strtol error in \"%s\":\n"
53                      "  result = %ld, should be %ld, 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 = %ld, should be %ld, 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     CheckStrToL ("\t 0x10G ", 0, 16L, OK);
83     CheckStrToL ("\t 0X10G ", 0, 16L, OK);
84     CheckStrToL (" \t0377\t", 0, 255L, OK);
85     CheckStrToL (" 377", 0, 377L, OK);
86
87     CheckStrToL ("\t -0x10G ", 0, -16L, OK);
88     CheckStrToL ("\t -0X10G ", 0, -16L, OK);
89     CheckStrToL (" \t-0377\t", 0, -255L, OK);
90     CheckStrToL (" -377", 0, -377L, OK);
91
92     /* No prefixes if base = 10 */
93     CheckStrToL ("\t 1234 ", 10, 1234L, OK);
94     CheckStrToL ("\t -1234 ", 10, -1234L, OK);
95     CheckStrToL ("\t -0x10G ", 10, 0L, OK);
96     CheckStrToL ("\t -0X10G ", 10, 0L, OK);
97     CheckStrToL (" \t-0377\t", 10, -377L, OK);
98     CheckStrToL (" 0377", 10, 377L, OK);
99
100     /* 0x prefix is allowed if base = 16 */
101     CheckStrToL ("\t 0x1234 ", 16, 0x1234L, OK);
102     CheckStrToL ("\t -0x1234 ", 16, -0x1234L, OK);
103     CheckStrToL ("\t -010G ", 16, -16L, OK);
104     CheckStrToL ("\t 10G ", 16, 16L, OK);
105
106     /* Check LONG_MIN and LONG_MAX */
107     sprintf (Buf, "%ld", LONG_MIN);
108     CheckStrToL (Buf, 0, LONG_MIN, OK);
109     sprintf (Buf, "%ld", LONG_MAX);
110     CheckStrToL (Buf, 0, LONG_MAX, OK);
111
112     /* Check value one smaller */
113     sprintf (Buf+1, "%ld", LONG_MIN);
114     Buf[1] = '0';       /* Overwrite '-' */
115     IncStr (Buf+1);
116     if (Buf[1] == '0') {
117         Buf[1] = '-';
118         Buf[0] = ' ';
119     } else {
120         Buf[0] = '-';
121     }
122     CheckStrToL (Buf, 0, LONG_MIN, ERROR);
123
124     /* Check value one larger */
125     sprintf (Buf+1, "%ld", LONG_MAX);
126     Buf[0] = '0';
127     IncStr (Buf);
128     if (Buf[0] == '0') {
129         Buf[0] = ' ';
130     }
131     CheckStrToL (Buf, 0, LONG_MAX, ERROR);
132
133     /* Check numbers that are much too large or small */
134     CheckStrToL ("-999999999999999999999999999999999999999999999999999999999", 0, LONG_MIN, ERROR);
135     CheckStrToL ("+999999999999999999999999999999999999999999999999999999999", 0, LONG_MAX, ERROR);
136     CheckStrToL (" 999999999999999999999999999999999999999999999999999999999", 0, LONG_MAX, ERROR);
137
138     /* Check a few other bases */
139     CheckStrToL ("aBcD", 36, 481261L, OK);
140     CheckStrToL ("zyaB", 35, 0L, ERROR);
141     CheckStrToL ("zyaB", 36, 1677395L, ERROR);
142
143     fprintf (outfile, "Failures: %u\n", Failures);
144     return (Failures != 0);
145 }