root/maint/gnulib/tests/test-iconv-utf.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test of character set conversion.
   2    Copyright (C) 2007-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3 of the License, or
   7    (at your option) any later version.
   8 
   9    This program 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
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
  18 
  19 #include <config.h>
  20 
  21 #if HAVE_ICONV
  22 # include <iconv.h>
  23 #endif
  24 
  25 #include <errno.h>
  26 #include <string.h>
  27 
  28 #include "macros.h"
  29 
  30 /* If compiling on an EBCDIC system, keep the test strings in ASCII.  */
  31 #if defined __IBMC__ && 'A' != 0x41
  32 # pragma convert("ISO8859-1")
  33 # define CONVERT_ENABLED
  34 #endif
  35 
  36 /* The text is "Japanese (日本語) [\U0001D50D\U0001D51E\U0001D52D]".  */
  37 
  38 const char test_utf8_string[] = "Japanese (\346\227\245\346\234\254\350\252\236) [\360\235\224\215\360\235\224\236\360\235\224\255]";
  39 
  40 const char test_utf16be_string[] = "\000J\000a\000p\000a\000n\000e\000s\000e\000 \000(\145\345\147\054\212\236\000)\000 \000[\330\065\335\015\330\065\335\036\330\065\335\055\000]";
  41 
  42 const char test_utf16le_string[] = "J\000a\000p\000a\000n\000e\000s\000e\000 \000(\000\345\145\054\147\236\212)\000 \000[\000\065\330\015\335\065\330\036\335\065\330\055\335]\000";
  43 
  44 const char test_utf32be_string[] = "\000\000\000J\000\000\000a\000\000\000p\000\000\000a\000\000\000n\000\000\000e\000\000\000s\000\000\000e\000\000\000 \000\000\000(\000\000\145\345\000\000\147\054\000\000\212\236\000\000\000)\000\000\000 \000\000\000[\000\001\325\015\000\001\325\036\000\001\325\055\000\000\000]";
  45 
  46 const char test_utf32le_string[] = "J\000\000\000a\000\000\000p\000\000\000a\000\000\000n\000\000\000e\000\000\000s\000\000\000e\000\000\000 \000\000\000(\000\000\000\345\145\000\000\054\147\000\000\236\212\000\000)\000\000\000 \000\000\000[\000\000\000\015\325\001\000\036\325\001\000\055\325\001\000]\000\000\000";
  47 
  48 #ifdef CONVERT_ENABLED
  49 # pragma convert(pop)
  50 #endif
  51 
  52 int
  53 main ()
     /* [previous][next][first][last][top][bottom][index][help] */
  54 {
  55 #if HAVE_ICONV
  56   /* Assume that iconv() supports at least the encoding UTF-8.  */
  57 
  58   /* Test conversion from UTF-8 to UTF-16BE with no errors.  */
  59   {
  60 #define input    test_utf8_string
  61 #define expected test_utf16be_string
  62     iconv_t cd;
  63     char buf[100];
  64     const char *inptr;
  65     size_t inbytesleft;
  66     char *outptr;
  67     size_t outbytesleft;
  68     size_t res;
  69 
  70     cd = iconv_open ("UTF-16BE", "UTF-8");
  71     ASSERT (cd != (iconv_t)(-1));
  72 
  73     inptr = input;
  74     inbytesleft = sizeof (input) - 1;
  75     outptr = buf;
  76     outbytesleft = sizeof (buf);
  77     res = iconv (cd,
  78                  (ICONV_CONST char **) &inptr, &inbytesleft,
  79                  &outptr, &outbytesleft);
  80     ASSERT (res == 0 && inbytesleft == 0);
  81     ASSERT (outptr == buf + (sizeof (expected) - 1));
  82     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
  83 
  84     ASSERT (iconv_close (cd) == 0);
  85 
  86 #undef input
  87 #undef expected
  88   }
  89 
  90   /* Test conversion from UTF-8 to UTF-16LE with no errors.  */
  91   {
  92 #define input    test_utf8_string
  93 #define expected test_utf16le_string
  94     iconv_t cd;
  95     char buf[100];
  96     const char *inptr;
  97     size_t inbytesleft;
  98     char *outptr;
  99     size_t outbytesleft;
 100     size_t res;
 101 
 102     cd = iconv_open ("UTF-16LE", "UTF-8");
 103     ASSERT (cd != (iconv_t)(-1));
 104 
 105     inptr = input;
 106     inbytesleft = sizeof (input) - 1;
 107     outptr = buf;
 108     outbytesleft = sizeof (buf);
 109     res = iconv (cd,
 110                  (ICONV_CONST char **) &inptr, &inbytesleft,
 111                  &outptr, &outbytesleft);
 112     ASSERT (res == 0 && inbytesleft == 0);
 113     ASSERT (outptr == buf + (sizeof (expected) - 1));
 114     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
 115 
 116     ASSERT (iconv_close (cd) == 0);
 117 
 118 #undef input
 119 #undef expected
 120   }
 121 
 122   /* Test conversion from UTF-8 to UTF-32BE with no errors.  */
 123   {
 124 #define input    test_utf8_string
 125 #define expected test_utf32be_string
 126     iconv_t cd;
 127     char buf[100];
 128     const char *inptr;
 129     size_t inbytesleft;
 130     char *outptr;
 131     size_t outbytesleft;
 132     size_t res;
 133 
 134     cd = iconv_open ("UTF-32BE", "UTF-8");
 135     ASSERT (cd != (iconv_t)(-1));
 136 
 137     inptr = input;
 138     inbytesleft = sizeof (input) - 1;
 139     outptr = buf;
 140     outbytesleft = sizeof (buf);
 141     res = iconv (cd,
 142                  (ICONV_CONST char **) &inptr, &inbytesleft,
 143                  &outptr, &outbytesleft);
 144     ASSERT (res == 0 && inbytesleft == 0);
 145     ASSERT (outptr == buf + (sizeof (expected) - 1));
 146     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
 147 
 148     ASSERT (iconv_close (cd) == 0);
 149 
 150 #undef input
 151 #undef expected
 152   }
 153 
 154   /* Test conversion from UTF-8 to UTF-32LE with no errors.  */
 155   {
 156 #define input    test_utf8_string
 157 #define expected test_utf32le_string
 158     iconv_t cd;
 159     char buf[100];
 160     const char *inptr;
 161     size_t inbytesleft;
 162     char *outptr;
 163     size_t outbytesleft;
 164     size_t res;
 165 
 166     cd = iconv_open ("UTF-32LE", "UTF-8");
 167     ASSERT (cd != (iconv_t)(-1));
 168 
 169     inptr = input;
 170     inbytesleft = sizeof (input) - 1;
 171     outptr = buf;
 172     outbytesleft = sizeof (buf);
 173     res = iconv (cd,
 174                  (ICONV_CONST char **) &inptr, &inbytesleft,
 175                  &outptr, &outbytesleft);
 176     ASSERT (res == 0 && inbytesleft == 0);
 177     ASSERT (outptr == buf + (sizeof (expected) - 1));
 178     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
 179 
 180     ASSERT (iconv_close (cd) == 0);
 181 
 182 #undef input
 183 #undef expected
 184   }
 185 
 186   /* Test conversion from UTF-16BE to UTF-8 with no errors.  */
 187   {
 188 #define input    test_utf16be_string
 189 #define expected test_utf8_string
 190     iconv_t cd;
 191     char buf[100];
 192     const char *inptr;
 193     size_t inbytesleft;
 194     char *outptr;
 195     size_t outbytesleft;
 196     size_t res;
 197 
 198     cd = iconv_open ("UTF-8", "UTF-16BE");
 199     ASSERT (cd != (iconv_t)(-1));
 200 
 201     inptr = input;
 202     inbytesleft = sizeof (input) - 1;
 203     outptr = buf;
 204     outbytesleft = sizeof (buf);
 205     res = iconv (cd,
 206                  (ICONV_CONST char **) &inptr, &inbytesleft,
 207                  &outptr, &outbytesleft);
 208     ASSERT (res == 0 && inbytesleft == 0);
 209     ASSERT (outptr == buf + (sizeof (expected) - 1));
 210     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
 211 
 212     ASSERT (iconv_close (cd) == 0);
 213 
 214 #undef input
 215 #undef expected
 216   }
 217 
 218   /* Test conversion from UTF-16LE to UTF-8 with no errors.  */
 219   {
 220 #define input    test_utf16le_string
 221 #define expected test_utf8_string
 222     iconv_t cd;
 223     char buf[100];
 224     const char *inptr;
 225     size_t inbytesleft;
 226     char *outptr;
 227     size_t outbytesleft;
 228     size_t res;
 229 
 230     cd = iconv_open ("UTF-8", "UTF-16LE");
 231     ASSERT (cd != (iconv_t)(-1));
 232 
 233     inptr = input;
 234     inbytesleft = sizeof (input) - 1;
 235     outptr = buf;
 236     outbytesleft = sizeof (buf);
 237     res = iconv (cd,
 238                  (ICONV_CONST char **) &inptr, &inbytesleft,
 239                  &outptr, &outbytesleft);
 240     ASSERT (res == 0 && inbytesleft == 0);
 241     ASSERT (outptr == buf + (sizeof (expected) - 1));
 242     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
 243 
 244     ASSERT (iconv_close (cd) == 0);
 245 
 246 #undef input
 247 #undef expected
 248   }
 249 
 250   /* Test conversion from UTF-32BE to UTF-8 with no errors.  */
 251   {
 252 #define input    test_utf32be_string
 253 #define expected test_utf8_string
 254     iconv_t cd;
 255     char buf[100];
 256     const char *inptr;
 257     size_t inbytesleft;
 258     char *outptr;
 259     size_t outbytesleft;
 260     size_t res;
 261 
 262     cd = iconv_open ("UTF-8", "UTF-32BE");
 263     ASSERT (cd != (iconv_t)(-1));
 264 
 265     inptr = input;
 266     inbytesleft = sizeof (input) - 1;
 267     outptr = buf;
 268     outbytesleft = sizeof (buf);
 269     res = iconv (cd,
 270                  (ICONV_CONST char **) &inptr, &inbytesleft,
 271                  &outptr, &outbytesleft);
 272     ASSERT (res == 0 && inbytesleft == 0);
 273     ASSERT (outptr == buf + (sizeof (expected) - 1));
 274     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
 275 
 276     ASSERT (iconv_close (cd) == 0);
 277 
 278 #undef input
 279 #undef expected
 280   }
 281 
 282   /* Test conversion from UTF-32LE to UTF-8 with no errors.  */
 283   {
 284 #define input    test_utf32le_string
 285 #define expected test_utf8_string
 286     iconv_t cd;
 287     char buf[100];
 288     const char *inptr;
 289     size_t inbytesleft;
 290     char *outptr;
 291     size_t outbytesleft;
 292     size_t res;
 293 
 294     cd = iconv_open ("UTF-8", "UTF-32LE");
 295     ASSERT (cd != (iconv_t)(-1));
 296 
 297     inptr = input;
 298     inbytesleft = sizeof (input) - 1;
 299     outptr = buf;
 300     outbytesleft = sizeof (buf);
 301     res = iconv (cd,
 302                  (ICONV_CONST char **) &inptr, &inbytesleft,
 303                  &outptr, &outbytesleft);
 304     ASSERT (res == 0 && inbytesleft == 0);
 305     ASSERT (outptr == buf + (sizeof (expected) - 1));
 306     ASSERT (memcmp (buf, expected, sizeof (expected) - 1) == 0);
 307 
 308     ASSERT (iconv_close (cd) == 0);
 309 
 310 #undef input
 311 #undef expected
 312   }
 313 #endif
 314 
 315   return 0;
 316 }

/* [previous][next][first][last][top][bottom][index][help] */