1 /* My bet is this was written by Chris Torek.
2 I reformatted and ansidecl-ized it, and tweaked it a little. */
13 CONST
char *str
; /* Convert this. */
14 unsigned long int expect
; /* To get this. */
15 int base
; /* Use this base. */
16 char left
; /* With this left over. */
17 int err
; /* And this in errno. */
19 static CONST
struct ltest tests
[] =
21 #if ~0UL == 0xffffffff
22 /* First, signed numbers. */
23 { " -17", -17, 0, 0, 0 },
24 { " +0x123fg", 0x123f, 0, 'g', 0 },
25 { "2147483647", 2147483647, 0, 0, 0 },
26 { "2147483648", 2147483647, 0, 0, ERANGE
},
27 { "214748364888", 2147483647, 0, 0, ERANGE
},
28 { "2147483650", 2147483647, 0, 0, ERANGE
},
29 { "-2147483649", -2147483648, 0, 0, ERANGE
},
30 { "-2147483648", -2147483648, 0, 0, 0 },
31 { "0123", 0123, 0, 0, 0 },
32 { "0x1122334455z", 2147483647, 16, 'z', ERANGE
},
33 { "0x0xc", 0, 0, 'x', 0 },
34 { "yz!", 34*36+35, 36, '!', 0 },
39 { "0xffffffffg", 0xffffffff, 0, 'g', 0 },
40 { "0xf1f2f3f4f5", 0xffffffff, 0, 0, ERANGE
},
41 { "-0x123456789", 0xffffffff, 0, 0, ERANGE
},
42 { "-0xfedcba98", -0xfedcba98, 0, 0, 0 },
45 /* assume 64 bit long... */
47 /* First, signed numbers. */
48 { " -17", -17, 0, 0, 0 },
49 { " +0x123fg", 0x123f, 0, 'g', 0 },
50 { "2147483647", 2147483647, 0, 0, 0 },
51 { "9223372036854775807", 9223372036854775807, 0, 0, 0 },
52 { "9223372036854775808", 9223372036854775807, 0, 0, ERANGE
},
53 { "922337203685477580777", 9223372036854775807, 0, 0, ERANGE
},
54 { "9223372036854775810", 9223372036854775807, 0, 0, ERANGE
},
55 { "-2147483648", -2147483648, 0, 0, 0 },
56 { "-9223372036854775808", -9223372036854775808, 0, 0, 0 },
57 { "-9223372036854775809", -9223372036854775808, 0, 0, ERANGE
},
58 { "0123", 0123, 0, 0, 0 },
59 { "0x112233445566778899z", 9223372036854775807, 16, 'z', ERANGE
},
60 { "0x0xc", 0, 0, 'x', 0 },
61 { "yz!", 34*36+35, 36, '!', 0 },
66 { "0xffffffffg", 0xffffffff, 0, 'g', 0 },
67 { "0xffffffffffffffffg", 0xffffffffffffffff, 0, 'g', 0 },
68 { "0xf1f2f3f4f5f6f7f8f9", 0xffffffffffffffff, 0, 0, ERANGE
},
69 { "-0x123456789abcdef01", 0xffffffffffffffff, 0, 0, ERANGE
},
70 { "-0xfedcba987654321", -0xfedcba987654321, 0, 0, 0 },
75 static void EXFUN(expand
, (char *dst
, int c
));
80 register CONST
struct ltest
*lt
;
84 for (lt
= tests
; lt
->str
!= NULL
; ++lt
)
89 l
= strtol(lt
->str
, &ep
, lt
->base
);
90 printf("strtol(\"%s\", , %d) test %u",
91 lt
->str
, lt
->base
, (unsigned int) (lt
- tests
));
92 if (l
== (long int) lt
->expect
&& *ep
== lt
->left
&& errno
== lt
->err
)
97 if (l
!= (long int) lt
->expect
)
98 printf(" returns %ld, expected %ld\n",
99 l
, (long int) lt
->expect
);
102 char exp1
[5], exp2
[5];
104 expand(exp2
, lt
->left
);
105 printf(" leaves '%s', expected '%s'\n", exp1
, exp2
);
107 if (errno
!= lt
->err
)
108 printf(" errno %d (%s) instead of %d (%s)\n",
109 errno
, strerror(errno
), lt
->err
, strerror(lt
->err
));
114 for (++lt
; lt
->str
!= NULL
; lt
++)
116 register unsigned long int ul
;
119 ul
= strtoul(lt
->str
, &ep
, lt
->base
);
120 printf("strtoul(\"%s\", , %d) test %u",
121 lt
->str
, lt
->base
, (unsigned int) (lt
- tests
));
122 if (ul
== lt
->expect
&& *ep
== lt
->left
&& errno
== lt
->err
)
127 if (ul
!= lt
->expect
)
128 printf(" returns %lu, expected %lu\n",
132 char exp1
[5], exp2
[5];
134 expand(exp2
, lt
->left
);
135 printf(" leaves '%s', expected '%s'\n", exp1
, exp2
);
137 if (errno
!= lt
->err
)
138 printf(" errno %d (%s) instead of %d (%s)\n",
139 errno
, strerror(errno
), lt
->err
, strerror(lt
->err
));
144 exit(status
? EXIT_FAILURE
: EXIT_SUCCESS
);
148 DEFUN(expand
, (dst
, c
), register char *dst AND
register int c
)
156 (void) sprintf(dst
, "%#.3o", (unsigned int) c
);