tst-mbswcs3.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Test restarting behaviour of wcsrtombs.
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <wchar.h>
  20. #include <locale.h>
  21. #define show(expr, nexp, srcexp, bufexp) \
  22. { \
  23. size_t res = expr; \
  24. printf (#expr " -> %Zd", res); \
  25. dst += res; \
  26. printf (", src = srcbuf+%td, dst = buf+%td", \
  27. src - srcbuf, dst - (char *) buf); \
  28. if (res != nexp || src != (srcexp) || dst != (char *) (bufexp)) \
  29. { \
  30. printf (", expected %Zd and srcbuf+%td and buf+%td", nexp, \
  31. (srcexp) - srcbuf, (bufexp) - (unsigned char *) buf); \
  32. result = 1; \
  33. } \
  34. putc ('\n', stdout); \
  35. }
  36. int
  37. main (void)
  38. {
  39. unsigned char buf[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  40. const unsigned char bufcheck[6] = { 0x25, 0xe2, 0x82, 0xac, 0xce, 0xbb };
  41. const wchar_t srcbuf[4] = { 0x25, 0x20ac, 0x03bb, 0 };
  42. mbstate_t state;
  43. const wchar_t *src;
  44. char *dst;
  45. int result = 0;
  46. const char *used_locale;
  47. setlocale (LC_CTYPE, "de_DE.UTF-8");
  48. /* Double check. */
  49. used_locale = setlocale (LC_CTYPE, NULL);
  50. printf ("used locale: \"%s\"\n", used_locale);
  51. result = strcmp (used_locale, "de_DE.UTF-8");
  52. memset (&state, '\0', sizeof (state));
  53. src = srcbuf;
  54. dst = (char *) buf;
  55. show (wcsrtombs (dst, &src, 1, &state), 1, srcbuf + 1, buf + 1);
  56. show (wcsrtombs (dst, &src, 1, &state), 0, srcbuf + 1, buf + 1);
  57. show (wcsrtombs (dst, &src, 4, &state), 3, srcbuf + 2, buf + 4);
  58. show (wcsrtombs (dst, &src, 2, &state), 2, srcbuf + 3, buf + 6);
  59. if (memcmp (buf, bufcheck, 6))
  60. {
  61. puts ("wrong results");
  62. result = 1;
  63. }
  64. return result;
  65. }