• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <wchar.h>
6 #include "test.h"
7 
8 #define TEST(r, f, x, m) ( \
9 	errno = 0, msg = #f, ((r) = (f)) == (x) || \
10 	(t_error("%s failed (" m ")\n", #f, r, x), 0) )
11 
12 #define TEST2(r, f, x, m) ( \
13 	((r) = (f)) == (x) || \
14 	(t_error("%s failed (" m ")\n", msg, r, x), 0) )
15 
main(void)16 int main(void)
17 {
18 	int i;
19 	long l;
20 	unsigned long ul;
21 	char *msg="";
22 	wchar_t *s, *c;
23 
24 	TEST(l, wcstol(L"2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld");
25 	TEST(ul, wcstoul(L"4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu");
26 
27 	if (sizeof(long) == 4) {
28 		TEST(l, wcstol(s=L"2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld");
29 		TEST2(i, c-s, 10, "wrong final position %d != %d");
30 		TEST2(i, errno, ERANGE, "missing errno %d != %d");
31 		TEST(l, wcstol(s=L"-2147483649", &c, 0), -2147483647L-1, "uncaught overflow %ld != %ld");
32 		TEST2(i, c-s, 11, "wrong final position %d != %d");
33 		TEST2(i, errno, ERANGE, "missing errno %d != %d");
34 		TEST(ul, wcstoul(s=L"4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
35 		TEST2(i, c-s, 10, "wrong final position %d != %d");
36 		TEST2(i, errno, ERANGE, "missing errno %d != %d");
37 		TEST(ul, wcstoul(s=L"-1", &c, 0), -1UL, "rejected negative %lu != %lu");
38 		TEST2(i, c-s, 2, "wrong final position %d != %d");
39 		TEST2(i, errno, 0, "spurious errno %d != %d");
40 		TEST(ul, wcstoul(s=L"-2", &c, 0), -2UL, "rejected negative %lu != %lu");
41 		TEST2(i, c-s, 2, "wrong final position %d != %d");
42 		TEST2(i, errno, 0, "spurious errno %d != %d");
43 		TEST(ul, wcstoul(s=L"-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu");
44 		TEST2(i, c-s, 11, "wrong final position %d != %d");
45 		TEST2(i, errno, 0, "spurious errno %d != %d");
46 		TEST(ul, wcstoul(s=L"-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu");
47 		TEST2(i, c-s, 11, "wrong final position %d != %d");
48 		TEST2(i, errno, 0, "spurious errno %d != %d");
49 	} else if (sizeof(long) == 8) {
50 		TEST(l, wcstol(s=L"9223372036854775808", &c, 0), 9223372036854775807L, "uncaught overflow %ld != %ld");
51 		TEST2(i, c-s, 19, "wrong final position %d != %d");
52 		TEST2(i, errno, ERANGE, "missing errno %d != %d");
53 		TEST(l, wcstol(s=L"-9223372036854775809", &c, 0), -9223372036854775807L-1, "uncaught overflow %ld != %ld");
54 		TEST2(i, c-s, 20, "wrong final position %d != %d");
55 		TEST2(i, errno, ERANGE, "missing errno %d != %d");
56 		TEST(ul, wcstoul(s=L"18446744073709551616", &c, 0), 18446744073709551615UL, "uncaught overflow %lu != %lu");
57 		TEST2(i, c-s, 20, "wrong final position %d != %d");
58 		TEST2(i, errno, ERANGE, "missing errno %d != %d");
59 		TEST(ul, wcstoul(s=L"-1", &c, 0), -1UL, "rejected negative %lu != %lu");
60 		TEST2(i, c-s, 2, "wrong final position %d != %d");
61 		TEST2(i, errno, 0, "spurious errno %d != %d");
62 		TEST(ul, wcstoul(s=L"-2", &c, 0), -2UL, "rejected negative %lu != %lu");
63 		TEST2(i, c-s, 2, "wrong final position %d != %d");
64 		TEST2(i, errno, 0, "spurious errno %d != %d");
65 		TEST(ul, wcstoul(s=L"-9223372036854775808", &c, 0), -9223372036854775808UL, "rejected negative %lu != %lu");
66 		TEST2(i, c-s, 20, "wrong final position %d != %d");
67 		TEST2(i, errno, 0, "spurious errno %d != %d");
68 		TEST(ul, wcstoul(s=L"-9223372036854775809", &c, 0), -9223372036854775809UL, "rejected negative %lu != %lu");
69 		TEST2(i, c-s, 20, "wrong final position %d != %d");
70 		TEST2(i, errno, 0, "spurious errno %d != %d");
71 	} else {
72 		t_error("sizeof(long) == %d, not implemented\n", (int)sizeof(long));
73 	}
74 
75 	TEST(l, wcstol(L"z", 0, 36), 35, "%ld != %ld");
76 	TEST(l, wcstol(L"00010010001101000101011001111000", 0, 2), 0x12345678, "%ld != %ld");
77 
78 	TEST(l, wcstol(s=L"0xz", &c, 16), 0, "%ld != %ld");
79 	TEST2(i, c-s, 1, "wrong final position %ld != %ld");
80 
81 	TEST(l, wcstol(s=L"0x1234", &c, 16), 0x1234, "%ld != %ld");
82 	TEST2(i, c-s, 6, "wrong final position %ld != %ld");
83 
84 	c = NULL;
85 	TEST(l, wcstol(s=L"123", &c, 37), 0, "%ld != %ld");
86 	TEST2(i, c-s, 0, "wrong final position %d != %d");
87 	TEST2(i, errno, EINVAL, "%d != %d");
88 	return t_status;
89 }
90