tst-iconv9.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Verify that using C.UTF-8 works.
  2. Copyright (C) 2021-2026 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. <https://www.gnu.org/licenses/>. */
  15. #include <iconv.h>
  16. #include <stddef.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <support/support.h>
  20. #include <support/check.h>
  21. /* This test does two things:
  22. (1) Verify that we have likely included translit_combining in C.UTF-8.
  23. (2) Verify default_missing is '?' as expected. */
  24. /* ISO-8859-1 encoding of "für". */
  25. char iso88591_in[] = { 0x66, 0xfc, 0x72, 0x0 };
  26. /* ASCII transliteration is "fur" with C.UTF-8 translit_combining. */
  27. char ascii_exp[] = { 0x66, 0x75, 0x72, 0x0 };
  28. /* First 3-byte UTF-8 code point. */
  29. char utf8_in[] = { 0xe0, 0xa0, 0x80, 0x0 };
  30. /* There is no ASCII transliteration for SAMARITAN LETTER ALAF
  31. so we get default_missing used which is '?'. */
  32. char default_missing_exp[] = { 0x3f, 0x0 };
  33. static int
  34. do_test (void)
  35. {
  36. char ascii_out[5];
  37. iconv_t cd;
  38. char *inbuf;
  39. char *outbuf;
  40. size_t inbytes;
  41. size_t outbytes;
  42. size_t n;
  43. /* The C.UTF-8 locale should include translit_combining, which provides
  44. the transliteration for "LATIN SMALL LETTER U WITH DIAERESIS" which
  45. is not provided by locale/C-translit.h.in. */
  46. xsetlocale (LC_ALL, "C.UTF-8");
  47. /* From ISO-8859-1 to ASCII. */
  48. cd = iconv_open ("ASCII//TRANSLIT,IGNORE", "ISO-8859-1");
  49. TEST_VERIFY (cd != (iconv_t) -1);
  50. inbuf = iso88591_in;
  51. inbytes = 3;
  52. outbuf = ascii_out;
  53. outbytes = 3;
  54. n = iconv (cd, &inbuf, &inbytes, &outbuf, &outbytes);
  55. TEST_VERIFY (n != -1);
  56. *outbuf = '\0';
  57. TEST_COMPARE_BLOB (ascii_out, 3, ascii_exp, 3);
  58. TEST_VERIFY (iconv_close (cd) == 0);
  59. /* From UTF-8 to ASCII. */
  60. cd = iconv_open ("ASCII//TRANSLIT,IGNORE", "UTF-8");
  61. TEST_VERIFY (cd != (iconv_t) -1);
  62. inbuf = utf8_in;
  63. inbytes = 3;
  64. outbuf = ascii_out;
  65. outbytes = 3;
  66. n = iconv (cd, &inbuf, &inbytes, &outbuf, &outbytes);
  67. TEST_VERIFY (n != -1);
  68. *outbuf = '\0';
  69. TEST_COMPARE_BLOB (ascii_out, 1, default_missing_exp, 1);
  70. TEST_VERIFY (iconv_close (cd) == 0);
  71. return 0;
  72. }
  73. #include <support/test-driver.c>