headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / system / libroot / posix / tst-swscanf.c
blobbbbdf9379b2da49ef23685748e76f1ae1f238ec5
1 #include <locale.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <wchar.h>
6 static int do_test(const char *loc);
9 int
10 main(void)
12 int result;
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");
19 return result;
22 static 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 }
41 static int
42 do_test(const char *loc)
44 size_t n;
45 int result = 0;
47 if (setlocale(LC_ALL, loc) == NULL) {
48 printf("cannot set locale \"%s\": %m\n", loc);
49 return 1;
52 printf("\nnew locale: \"%s\"\n", loc);
54 for (n = 0; n < sizeof(tests) / sizeof(tests[0]); ++n) {
55 char buf[100];
56 wchar_t wbuf[100];
58 if (tests[n].only_C_locale && strcmp(loc, "C") != 0)
59 continue;
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);
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);
88 return result;