1#include <locale.h>
2#include <stdio.h>
3#include <string.h>
4#include <wchar.h>
5
6static int do_test(const char *loc);
7
8
9int
10main(void)
11{
12	int result;
13
14	result = do_test("C");
15	result |= do_test("de_DE.ISO-8859-1");
16	result |= do_test("de_DE.UTF-8");
17	result |= do_test("ja_JP.EUC-JP");
18
19	return result;
20}
21
22static const struct {
23	const wchar_t *fmt;
24	const wchar_t *wfmt;
25	const wchar_t *arg;
26	int retval;
27	const char *res;
28	const wchar_t *wres;
29	int only_C_locale;
30} tests[] = {
31	{ L"%[abc]", L"%l[abc]", L"aabbccddaabb", 1 ,"aabbcc", L"aabbcc", 0 },
32	{ L"%[^def]", L"%l[^def]", L"aabbccddaabb", 1, "aabbcc", L"aabbcc", 0 },
33	{ L"%[^abc]", L"%l[^abc]", L"aabbccddaabb", 0, "", L"", 0 },
34	{ L"%[a-c]", L"%l[a-c]", L"aabbccddaabb", 1, "aabbcc", L"aabbcc", 1 },
35	{ L"%[^d-f]", L"%l[^d-f]", L"aabbccddaabb", 1, "aabbcc", L"aabbcc", 1 },
36	{ L"%[^a-c]", L"%l[^a-c]", L"aabbccddaabb", 0, "", L"", 1 },
37	{ L"%[^a-c]", L"%l[^a-c]", L"bbccddaabb", 0, "", L"", 1 }
38};
39
40
41static int
42do_test(const char *loc)
43{
44	size_t n;
45	int result = 0;
46
47	if (setlocale(LC_ALL, loc) == NULL) {
48		printf("cannot set locale \"%s\": %m\n", loc);
49		return 1;
50	}
51
52	printf("\nnew locale: \"%s\"\n", loc);
53
54	for (n = 0; n < sizeof(tests) / sizeof(tests[0]); ++n) {
55		char buf[100];
56		wchar_t wbuf[100];
57
58		if (tests[n].only_C_locale && strcmp(loc, "C") != 0)
59			continue;
60
61		if (swscanf(tests[n].arg, tests[n].fmt, buf) != tests[n].retval) {
62			printf("swscanf (\"%S\", \"%S\", ...) failed\n", tests[n].arg,
63				tests[n].fmt);
64			result = 1;
65		} else if (tests[n].retval != 0 && strcmp(buf, tests[n].res) != 0) {
66			printf(
67				"swscanf (\"%S\", \"%S\", ...) return \"%s\", expected \"%s\"\n",
68				tests[n].arg, tests[n].fmt, buf, tests[n].res);
69			result = 1;
70		} else
71			printf("swscanf (\"%S\", \"%S\", ...) OK\n", tests[n].arg,
72				tests[n].fmt);
73
74		if (swscanf(tests[n].arg, tests[n].wfmt, wbuf) != tests[n].retval) {
75			printf("swscanf (\"%S\", \"%S\", ...) failed\n", tests[n].arg,
76				tests[n].wfmt);
77			result = 1;
78		} else if (tests[n].retval != 0 && wcscmp(wbuf, tests[n].wres) != 0) {
79			printf(
80				"swscanf (\"%S\", \"%S\", ...) return \"%S\", expected \"%S\"\n",
81				tests[n].arg, tests[n].wfmt, wbuf, tests[n].wres);
82			result = 1;
83		} else
84			printf("swscanf (\"%S\", \"%S\", ...) OK\n", tests[n].arg,
85				tests[n].wfmt);
86	}
87
88	return result;
89}
90