Back to index

glibc  2.9
tst-swscanf.c
Go to the documentation of this file.
00001 #include <locale.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <wchar.h>
00005 
00006 
00007 static int do_test (const char *loc);
00008 
00009 
00010 int
00011 main (void)
00012 {
00013   int result;
00014 
00015   result = do_test ("C");
00016   result |= do_test ("de_DE.ISO-8859-1");
00017   result |= do_test ("de_DE.UTF-8");
00018   result |= do_test ("ja_JP.EUC-JP");
00019 
00020   return result;
00021 }
00022 
00023 
00024 static const struct
00025 {
00026   const wchar_t *fmt;
00027   const wchar_t *wfmt;
00028   const wchar_t *arg;
00029   int retval;
00030   const char *res;
00031   const wchar_t *wres;
00032   int only_C_locale;
00033 } tests[] =
00034   {
00035     { L"%[abc]", L"%l[abc]", L"aabbccddaabb", 1 ,"aabbcc", L"aabbcc", 0 },
00036     { L"%[^def]", L"%l[^def]", L"aabbccddaabb", 1, "aabbcc", L"aabbcc", 0 },
00037     { L"%[^abc]", L"%l[^abc]", L"aabbccddaabb", 0, "", L"", 0 },
00038     { L"%[a-c]", L"%l[a-c]", L"aabbccddaabb", 1, "aabbcc", L"aabbcc", 1 },
00039     { L"%[^d-f]", L"%l[^d-f]", L"aabbccddaabb", 1, "aabbcc", L"aabbcc", 1 },
00040     { L"%[^a-c]", L"%l[^a-c]", L"aabbccddaabb", 0, "", L"", 1 },
00041     { L"%[^a-c]", L"%l[^a-c]", L"bbccddaabb", 0, "", L"", 1 }
00042   };
00043 
00044 
00045 static int
00046 do_test (const char *loc)
00047 {
00048   size_t n;
00049   int result = 0;
00050 
00051   if (setlocale (LC_ALL, loc) == NULL)
00052     {
00053       printf ("cannot set locale \"%s\": %m\n", loc);
00054       return 1;
00055     }
00056 
00057   printf ("\nnew locale: \"%s\"\n", loc);
00058 
00059   for (n = 0; n < sizeof (tests) / sizeof (tests[0]); ++n)
00060     {
00061       char buf[100];
00062       wchar_t wbuf[100];
00063 
00064       if (tests[n].only_C_locale && strcmp (loc, "C") != 0)
00065        continue;
00066 
00067       if (swscanf (tests[n].arg, tests[n].fmt, buf) != tests[n].retval)
00068        {
00069          printf ("swscanf (\"%S\", \"%S\", ...) failed\n",
00070                 tests[n].arg, tests[n].fmt);
00071          result = 1;
00072        }
00073       else if (tests[n].retval != 0 && strcmp (buf, tests[n].res) != 0)
00074        {
00075          printf ("swscanf (\"%S\", \"%S\", ...) return \"%s\", expected \"%s\"\n",
00076                 tests[n].arg, tests[n].fmt, buf, tests[n].res);
00077          result = 1;
00078        }
00079       else
00080        printf ("swscanf (\"%S\", \"%S\", ...) OK\n",
00081               tests[n].arg, tests[n].fmt);
00082 
00083       if (swscanf (tests[n].arg, tests[n].wfmt, wbuf) != tests[n].retval)
00084        {
00085          printf ("swscanf (\"%S\", \"%S\", ...) failed\n",
00086                 tests[n].arg, tests[n].wfmt);
00087          result = 1;
00088        }
00089       else if (tests[n].retval != 0 && wcscmp (wbuf, tests[n].wres) != 0)
00090        {
00091          printf ("swscanf (\"%S\", \"%S\", ...) return \"%S\", expected \"%S\"\n",
00092                 tests[n].arg, tests[n].wfmt, wbuf, tests[n].wres);
00093          result = 1;
00094        }
00095       else
00096        printf ("swscanf (\"%S\", \"%S\", ...) OK\n",
00097               tests[n].arg, tests[n].wfmt);
00098     }
00099 
00100   return result;
00101 }