tst-swscanf.c 2.4 KB

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