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

source code of glibc/libio/tst-swscanf.c