tst-mbswcs5.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Test restarting behaviour of wcrtomb.
  2. Copyright (C) 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Bruno Haible <haible@ilog.fr>.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <wchar.h>
  19. #include <locale.h>
  20. #define show(expr, nexp, bufexp) \
  21. { \
  22. size_t res = expr; \
  23. printf (#expr " -> %Zd", res); \
  24. dst += res; \
  25. printf (", dst = buf+%td", dst - (char *) buf); \
  26. if (res != nexp || dst != (char *) (bufexp)) \
  27. { \
  28. printf (", expected %Zd and buf+%td", nexp, \
  29. (bufexp) - (unsigned char *) buf); \
  30. result = 1; \
  31. } \
  32. putc ('\n', stdout); \
  33. }
  34. int
  35. main (void)
  36. {
  37. unsigned char buf[7] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  38. const unsigned char bufcheck[7] = { 0x25, 0xe2, 0x82, 0xac, 0xce, 0xbb, 0 };
  39. const wchar_t srcbuf[4] = { 0x25, 0x20ac, 0x03bb, 0 };
  40. mbstate_t state;
  41. const wchar_t *src;
  42. char *dst;
  43. int result = 0;
  44. const char *used_locale;
  45. setlocale (LC_CTYPE, "de_DE.UTF-8");
  46. /* Double check. */
  47. used_locale = setlocale (LC_CTYPE, NULL);
  48. printf ("used locale: \"%s\"\n", used_locale);
  49. result = strcmp (used_locale, "de_DE.UTF-8");
  50. memset (&state, '\0', sizeof (state));
  51. src = srcbuf;
  52. dst = (char *) buf;
  53. show (wcrtomb (dst, *src++, &state), 1, buf + 1);
  54. show (wcrtomb (dst, *src++, &state), 3, buf + 4);
  55. show (wcrtomb (dst, *src++, &state), 2, buf + 6);
  56. show (wcrtomb (dst, *src, &state), 1, buf + 7);
  57. if (memcmp (buf, bufcheck, 7))
  58. {
  59. puts ("wrong results");
  60. result = 1;
  61. }
  62. return result;
  63. }