Update.
[glibc.git] / libio / tst-swscanf.c
blob86472de16e2d6d716283ea5fecc6964dff00325c
1 #include <locale.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <wchar.h>
7 static int do_test (const char *loc);
10 int
11 main (void)
13 int result;
15 result = do_test ("C");
16 result |= do_test ("de_DE.ISO-8859-1");
17 result |= do_test ("de_DE.UTF-8");
18 result |= do_test ("ja_JP.EUC-JP");
20 return result;
24 static const struct
26 const wchar_t *fmt;
27 const wchar_t *wfmt;
28 const wchar_t *arg;
29 const char *res;
30 const wchar_t *wres;
31 int only_C_locale;
32 } tests[] =
34 { L"%[abc]", L"%l[abc]", L"aabbccddaabb", "aabbcc", L"aabbcc", 0 },
35 { L"%[^def]", L"%l[^def]", L"aabbccddaabb", "aabbcc", L"aabbcc", 0 },
36 { L"%[^abc]", L"%l[^abc]", L"aabbccddaabb", "", L"", 0 },
37 { L"%[a-c]", L"%l[a-c]", L"aabbccddaabb", "aabbcc", L"aabbcc", 1 },
38 { L"%[^d-f]", L"%l[^d-f]", L"aabbccddaabb", "aabbcc", L"aabbcc", 1 },
39 { L"%[^a-c]", L"%l[^a-c]", L"aabbccddaabb", "", L"", 1 },
40 { L"%[^a-c]", L"%l[^a-c]", L"bbccddaabb", "", L"", 1 }
44 static int
45 do_test (const char *loc)
47 size_t n;
48 int result = 0;
50 if (setlocale (LC_ALL, loc) == NULL)
52 printf ("cannot set locale \"%s\": %m\n", loc);
53 return 1;
56 printf ("\nnew locale: \"%s\"\n", loc);
58 for (n = 0; n < sizeof (tests) / sizeof (tests[0]); ++n)
60 char buf[100];
61 wchar_t wbuf[100];
63 if (tests[n].only_C_locale && strcmp (loc, "C") != 0)
64 continue;
66 if (swscanf (tests[n].arg, tests[n].fmt, buf) != 1)
68 printf ("swscanf (\"%S\", \"%S\", ...) failed\n",
69 tests[n].arg, tests[n].fmt);
70 result = 1;
72 else if (strcmp (buf, tests[n].res) != 0)
74 printf ("swscanf (\"%S\", \"%S\", ...) return \"%s\", expected \"%s\"\n",
75 tests[n].arg, tests[n].fmt, buf, tests[n].res);
76 result = 1;
78 else
79 printf ("swscanf (\"%S\", \"%S\", ...) OK\n",
80 tests[n].arg, tests[n].fmt);
82 if (swscanf (tests[n].arg, tests[n].wfmt, wbuf) != 1)
84 printf ("swscanf (\"%S\", \"%S\", ...) failed\n",
85 tests[n].arg, tests[n].wfmt);
86 result = 1;
88 else if (wcscmp (wbuf, tests[n].wres) != 0)
90 printf ("swscanf (\"%S\", \"%S\", ...) return \"%S\", expected \"%S\"\n",
91 tests[n].arg, tests[n].wfmt, wbuf, tests[n].wres);
92 result = 1;
94 else
95 printf ("swscanf (\"%S\", \"%S\", ...) OK\n",
96 tests[n].arg, tests[n].wfmt);
99 return result;