tst-iconv6.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Testing ucs4le_internal_loop() in gconv_simple.c.
  2. Copyright (C) 2016 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <inttypes.h>
  19. #include <byteswap.h>
  20. #if defined(__GLIBC__) && !defined(__UCLIBC__)
  21. #include <iconv.h>
  22. #endif
  23. static int
  24. do_test (void)
  25. {
  26. #if defined(__GLIBC__) && !defined(__UCLIBC__)
  27. iconv_t cd;
  28. char *inptr;
  29. size_t inlen;
  30. char *outptr;
  31. size_t outlen;
  32. size_t n;
  33. int e;
  34. int result = 0;
  35. #if __BYTE_ORDER == __BIG_ENDIAN
  36. /* On big-endian machines, ucs4le_internal_loop() swaps the bytes before
  37. error checking. Thus the input values has to be swapped. */
  38. # define VALUE(val) bswap_32 (val)
  39. #else
  40. # define VALUE(val) val
  41. #endif
  42. uint32_t inbuf[3] = { VALUE (0x41), VALUE (0x80000000), VALUE (0x42) };
  43. uint32_t outbuf[3] = { 0, 0, 0 };
  44. cd = iconv_open ("WCHAR_T", "UCS-4LE");
  45. if (cd == (iconv_t) -1)
  46. {
  47. printf ("cannot convert from UCS4LE to wchar_t: %m\n");
  48. return 1;
  49. }
  50. inptr = (char *) inbuf;
  51. inlen = sizeof (inbuf);
  52. outptr = (char *) outbuf;
  53. outlen = sizeof (outbuf);
  54. n = iconv (cd, &inptr, &inlen, &outptr, &outlen);
  55. e = errno;
  56. if (n != (size_t) -1)
  57. {
  58. printf ("incorrect iconv() return value: %zd, expected -1\n", n);
  59. result = 1;
  60. }
  61. if (e != EILSEQ)
  62. {
  63. printf ("incorrect error value: %s, expected %s\n",
  64. strerror (e), strerror (EILSEQ));
  65. result = 1;
  66. }
  67. if (inptr != (char *) &inbuf[1])
  68. {
  69. printf ("inptr=0x%p does not point to invalid character! Expected=0x%p\n"
  70. , inptr, &inbuf[1]);
  71. result = 1;
  72. }
  73. if (inlen != sizeof (inbuf) - sizeof (uint32_t))
  74. {
  75. printf ("inlen=%zd != %zd\n"
  76. , inlen, sizeof (inbuf) - sizeof (uint32_t));
  77. result = 1;
  78. }
  79. if (outptr != (char *) &outbuf[1])
  80. {
  81. printf ("outptr=0x%p does not point to invalid character in inbuf! "
  82. "Expected=0x%p\n"
  83. , outptr, &outbuf[1]);
  84. result = 1;
  85. }
  86. if (outlen != sizeof (inbuf) - sizeof (uint32_t))
  87. {
  88. printf ("outlen=%zd != %zd\n"
  89. , outlen, sizeof (outbuf) - sizeof (uint32_t));
  90. result = 1;
  91. }
  92. if (outbuf[0] != 0x41 || outbuf[1] != 0 || outbuf[2] != 0)
  93. {
  94. puts ("Characters conversion is incorrect!");
  95. result = 1;
  96. }
  97. iconv_close (cd);
  98. return result;
  99. #else
  100. return 23;
  101. #endif
  102. }
  103. #define TEST_FUNCTION do_test ()
  104. #include "../test-skeleton.c"