1570af302Sopenharmony_ci#include <stdio.h>
2570af302Sopenharmony_ci#include <stdlib.h>
3570af302Sopenharmony_ci#include <string.h>
4570af302Sopenharmony_ci#include <errno.h>
5570af302Sopenharmony_ci#include <wchar.h>
6570af302Sopenharmony_ci#include "test.h"
7570af302Sopenharmony_ci
8570af302Sopenharmony_ci#define TEST(r, f, x, m) ( \
9570af302Sopenharmony_ci	errno = 0, msg = #f, ((r) = (f)) == (x) || \
10570af302Sopenharmony_ci	(t_error("%s failed (" m ")\n", #f, r, x), 0) )
11570af302Sopenharmony_ci
12570af302Sopenharmony_ci#define TEST2(r, f, x, m) ( \
13570af302Sopenharmony_ci	((r) = (f)) == (x) || \
14570af302Sopenharmony_ci	(t_error("%s failed (" m ")\n", msg, r, x), 0) )
15570af302Sopenharmony_ci
16570af302Sopenharmony_ciint main(void)
17570af302Sopenharmony_ci{
18570af302Sopenharmony_ci	int i;
19570af302Sopenharmony_ci	long l;
20570af302Sopenharmony_ci	unsigned long ul;
21570af302Sopenharmony_ci	char *msg="";
22570af302Sopenharmony_ci	wchar_t *s, *c;
23570af302Sopenharmony_ci
24570af302Sopenharmony_ci	TEST(l, wcstol(L"2147483647", 0, 0), 2147483647L, "max 32bit signed %ld != %ld");
25570af302Sopenharmony_ci	TEST(ul, wcstoul(L"4294967295", 0, 0), 4294967295UL, "max 32bit unsigned %lu != %lu");
26570af302Sopenharmony_ci
27570af302Sopenharmony_ci	if (sizeof(long) == 4) {
28570af302Sopenharmony_ci		TEST(l, wcstol(s=L"2147483648", &c, 0), 2147483647L, "uncaught overflow %ld != %ld");
29570af302Sopenharmony_ci		TEST2(i, c-s, 10, "wrong final position %d != %d");
30570af302Sopenharmony_ci		TEST2(i, errno, ERANGE, "missing errno %d != %d");
31570af302Sopenharmony_ci		TEST(l, wcstol(s=L"-2147483649", &c, 0), -2147483647L-1, "uncaught overflow %ld != %ld");
32570af302Sopenharmony_ci		TEST2(i, c-s, 11, "wrong final position %d != %d");
33570af302Sopenharmony_ci		TEST2(i, errno, ERANGE, "missing errno %d != %d");
34570af302Sopenharmony_ci		TEST(ul, wcstoul(s=L"4294967296", &c, 0), 4294967295UL, "uncaught overflow %lu != %lu");
35570af302Sopenharmony_ci		TEST2(i, c-s, 10, "wrong final position %d != %d");
36570af302Sopenharmony_ci		TEST2(i, errno, ERANGE, "missing errno %d != %d");
37570af302Sopenharmony_ci		TEST(ul, wcstoul(s=L"-1", &c, 0), -1UL, "rejected negative %lu != %lu");
38570af302Sopenharmony_ci		TEST2(i, c-s, 2, "wrong final position %d != %d");
39570af302Sopenharmony_ci		TEST2(i, errno, 0, "spurious errno %d != %d");
40570af302Sopenharmony_ci		TEST(ul, wcstoul(s=L"-2", &c, 0), -2UL, "rejected negative %lu != %lu");
41570af302Sopenharmony_ci		TEST2(i, c-s, 2, "wrong final position %d != %d");
42570af302Sopenharmony_ci		TEST2(i, errno, 0, "spurious errno %d != %d");
43570af302Sopenharmony_ci		TEST(ul, wcstoul(s=L"-2147483648", &c, 0), -2147483648UL, "rejected negative %lu != %lu");
44570af302Sopenharmony_ci		TEST2(i, c-s, 11, "wrong final position %d != %d");
45570af302Sopenharmony_ci		TEST2(i, errno, 0, "spurious errno %d != %d");
46570af302Sopenharmony_ci		TEST(ul, wcstoul(s=L"-2147483649", &c, 0), -2147483649UL, "rejected negative %lu != %lu");
47570af302Sopenharmony_ci		TEST2(i, c-s, 11, "wrong final position %d != %d");
48570af302Sopenharmony_ci		TEST2(i, errno, 0, "spurious errno %d != %d");
49570af302Sopenharmony_ci	} else if (sizeof(long) == 8) {
50570af302Sopenharmony_ci		TEST(l, wcstol(s=L"9223372036854775808", &c, 0), 9223372036854775807L, "uncaught overflow %ld != %ld");
51570af302Sopenharmony_ci		TEST2(i, c-s, 19, "wrong final position %d != %d");
52570af302Sopenharmony_ci		TEST2(i, errno, ERANGE, "missing errno %d != %d");
53570af302Sopenharmony_ci		TEST(l, wcstol(s=L"-9223372036854775809", &c, 0), -9223372036854775807L-1, "uncaught overflow %ld != %ld");
54570af302Sopenharmony_ci		TEST2(i, c-s, 20, "wrong final position %d != %d");
55570af302Sopenharmony_ci		TEST2(i, errno, ERANGE, "missing errno %d != %d");
56570af302Sopenharmony_ci		TEST(ul, wcstoul(s=L"18446744073709551616", &c, 0), 18446744073709551615UL, "uncaught overflow %lu != %lu");
57570af302Sopenharmony_ci		TEST2(i, c-s, 20, "wrong final position %d != %d");
58570af302Sopenharmony_ci		TEST2(i, errno, ERANGE, "missing errno %d != %d");
59570af302Sopenharmony_ci		TEST(ul, wcstoul(s=L"-1", &c, 0), -1UL, "rejected negative %lu != %lu");
60570af302Sopenharmony_ci		TEST2(i, c-s, 2, "wrong final position %d != %d");
61570af302Sopenharmony_ci		TEST2(i, errno, 0, "spurious errno %d != %d");
62570af302Sopenharmony_ci		TEST(ul, wcstoul(s=L"-2", &c, 0), -2UL, "rejected negative %lu != %lu");
63570af302Sopenharmony_ci		TEST2(i, c-s, 2, "wrong final position %d != %d");
64570af302Sopenharmony_ci		TEST2(i, errno, 0, "spurious errno %d != %d");
65570af302Sopenharmony_ci		TEST(ul, wcstoul(s=L"-9223372036854775808", &c, 0), -9223372036854775808UL, "rejected negative %lu != %lu");
66570af302Sopenharmony_ci		TEST2(i, c-s, 20, "wrong final position %d != %d");
67570af302Sopenharmony_ci		TEST2(i, errno, 0, "spurious errno %d != %d");
68570af302Sopenharmony_ci		TEST(ul, wcstoul(s=L"-9223372036854775809", &c, 0), -9223372036854775809UL, "rejected negative %lu != %lu");
69570af302Sopenharmony_ci		TEST2(i, c-s, 20, "wrong final position %d != %d");
70570af302Sopenharmony_ci		TEST2(i, errno, 0, "spurious errno %d != %d");
71570af302Sopenharmony_ci	} else {
72570af302Sopenharmony_ci		t_error("sizeof(long) == %d, not implemented\n", (int)sizeof(long));
73570af302Sopenharmony_ci	}
74570af302Sopenharmony_ci
75570af302Sopenharmony_ci	TEST(l, wcstol(L"z", 0, 36), 35, "%ld != %ld");
76570af302Sopenharmony_ci	TEST(l, wcstol(L"00010010001101000101011001111000", 0, 2), 0x12345678, "%ld != %ld");
77570af302Sopenharmony_ci
78570af302Sopenharmony_ci	TEST(l, wcstol(s=L"0xz", &c, 16), 0, "%ld != %ld");
79570af302Sopenharmony_ci	TEST2(i, c-s, 1, "wrong final position %ld != %ld");
80570af302Sopenharmony_ci
81570af302Sopenharmony_ci	TEST(l, wcstol(s=L"0x1234", &c, 16), 0x1234, "%ld != %ld");
82570af302Sopenharmony_ci	TEST2(i, c-s, 6, "wrong final position %ld != %ld");
83570af302Sopenharmony_ci
84570af302Sopenharmony_ci	c = NULL;
85570af302Sopenharmony_ci	TEST(l, wcstol(s=L"123", &c, 37), 0, "%ld != %ld");
86570af302Sopenharmony_ci	TEST2(i, c-s, 0, "wrong final position %d != %d");
87570af302Sopenharmony_ci	TEST2(i, errno, EINVAL, "%d != %d");
88570af302Sopenharmony_ci	return t_status;
89570af302Sopenharmony_ci}
90