tst-iconv2.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (C) 2001-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <iconv.h>
  16. #include <mcheck.h>
  17. #include <stddef.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. static int
  22. do_test (void)
  23. {
  24. char buf[3];
  25. const wchar_t wc[1] = L"a";
  26. iconv_t cd;
  27. char *inptr;
  28. size_t inlen;
  29. char *outptr;
  30. size_t outlen;
  31. size_t n;
  32. int e;
  33. int result = 0;
  34. mtrace ();
  35. cd = iconv_open ("UCS4", "WCHAR_T");
  36. if (cd == (iconv_t) -1)
  37. {
  38. printf ("cannot convert from wchar_t to UCS4: %m\n");
  39. exit (1);
  40. }
  41. inptr = (char *) wc;
  42. inlen = sizeof (wchar_t);
  43. outptr = buf;
  44. outlen = 3;
  45. n = iconv (cd, &inptr, &inlen, &outptr, &outlen);
  46. e = errno;
  47. if (n != (size_t) -1)
  48. {
  49. printf ("incorrect iconv() return value: %zd, expected -1\n", n);
  50. result = 1;
  51. }
  52. if (e != E2BIG)
  53. {
  54. printf ("incorrect error value: %s, expected %s\n",
  55. strerror (e), strerror (E2BIG));
  56. result = 1;
  57. }
  58. if (inptr != (char *) wc)
  59. {
  60. puts ("inptr changed");
  61. result = 1;
  62. }
  63. if (inlen != sizeof (wchar_t))
  64. {
  65. puts ("inlen changed");
  66. result = 1;
  67. }
  68. if (outptr != buf)
  69. {
  70. puts ("outptr changed");
  71. result = 1;
  72. }
  73. if (outlen != 3)
  74. {
  75. puts ("outlen changed");
  76. result = 1;
  77. }
  78. iconv_close (cd);
  79. return result;
  80. }
  81. #define TEST_FUNCTION do_test ()
  82. #include "../test-skeleton.c"