root/maint/gnulib/tests/uniconv/test-u16-conv-from-enc.c

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

DEFINITIONS

This source file includes following definitions.
  1. new_offsets
  2. main

   1 /* Test of conversion to UTF-16 from legacy encodings.
   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 #include "uniconv.h"
  22 
  23 #include <stdlib.h>
  24 #include <string.h>
  25 
  26 #include "unistr.h"
  27 #include "macros.h"
  28 extern int iconv_supports_encoding (const char *encoding);
  29 
  30 /* Magic number for detecting bounds violations.  */
  31 #define MAGIC 0x1983EFF1
  32 
  33 static size_t *
  34 new_offsets (size_t n)
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36   size_t *offsets = (size_t *) malloc ((n + 1) * sizeof (size_t));
  37   offsets[n] = MAGIC;
  38   return offsets;
  39 }
  40 
  41 int
  42 main ()
     /* [previous][next][first][last][top][bottom][index][help] */
  43 {
  44 #if HAVE_ICONV
  45   static enum iconv_ilseq_handler handlers[] =
  46     { iconveh_error, iconveh_question_mark, iconveh_escape_sequence };
  47   size_t h;
  48   size_t o;
  49   size_t i;
  50 
  51   /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
  52      ISO-8859-2, and UTF-8.  */
  53 
  54   /* Test conversion from ISO-8859-1 to UTF-16 with no errors.  */
  55   for (h = 0; h < SIZEOF (handlers); h++)
  56     {
  57       enum iconv_ilseq_handler handler = handlers[h];
  58       static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
  59       static const uint16_t expected[] = /* Ärger mit bösen Bübchen ohne Augenmaß */
  60         {
  61           0xC4, 'r', 'g', 'e', 'r', ' ', 'm', 'i', 't', ' ', 'b', 0xF6, 's',
  62           'e', 'n', ' ', 'B', 0xFC, 'b', 'c', 'h', 'e', 'n', ' ', 'o', 'h',
  63           'n', 'e', ' ', 'A', 'u', 'g', 'e', 'n', 'm', 'a', 0xDF
  64         };
  65       for (o = 0; o < 2; o++)
  66         {
  67           size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
  68           size_t length;
  69           uint16_t *result = u16_conv_from_encoding ("ISO-8859-1", handler,
  70                                                      input, strlen (input),
  71                                                      offsets,
  72                                                      NULL, &length);
  73           ASSERT (result != NULL);
  74           ASSERT (length == SIZEOF (expected));
  75           ASSERT (u16_cmp (result, expected, SIZEOF (expected)) == 0);
  76           if (o)
  77             {
  78               for (i = 0; i < 37; i++)
  79                 ASSERT (offsets[i] == i);
  80               ASSERT (offsets[37] == MAGIC);
  81               free (offsets);
  82             }
  83           free (result);
  84         }
  85     }
  86 
  87   /* Test conversion from ISO-8859-2 to UTF-16 with no errors.  */
  88   for (h = 0; h < SIZEOF (handlers); h++)
  89     {
  90       enum iconv_ilseq_handler handler = handlers[h];
  91       static const char input[] = "Rafa\263 Maszkowski"; /* Rafał Maszkowski */
  92       static const uint16_t expected[] =
  93         {
  94           'R', 'a', 'f', 'a', 0x0142, ' ', 'M', 'a', 's', 'z', 'k', 'o', 'w',
  95           's', 'k', 'i'
  96         };
  97       for (o = 0; o < 2; o++)
  98         {
  99           size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
 100           size_t length;
 101           uint16_t *result = u16_conv_from_encoding ("ISO-8859-2", handler,
 102                                                      input, strlen (input),
 103                                                      offsets,
 104                                                      NULL, &length);
 105           ASSERT (result != NULL);
 106           ASSERT (length == SIZEOF (expected));
 107           ASSERT (u16_cmp (result, expected, SIZEOF (expected)) == 0);
 108           if (o)
 109             {
 110               for (i = 0; i < 16; i++)
 111                 ASSERT (offsets[i] == i);
 112               ASSERT (offsets[16] == MAGIC);
 113               free (offsets);
 114             }
 115           free (result);
 116         }
 117     }
 118 
 119   /* autodetect_jp is only supported when iconv() support ISO-2022-JP-2.  */
 120 # if defined _LIBICONV_VERSION || !(defined _AIX || defined __sgi || defined __hpux || defined __osf__ || defined __sun)
 121   if (iconv_supports_encoding ("ISO-2022-JP-2"))
 122     {
 123       /* Test conversions from autodetect_jp to UTF-16.  */
 124       for (h = 0; h < SIZEOF (handlers); h++)
 125         {
 126           enum iconv_ilseq_handler handler = handlers[h];
 127           static const char input[] = "\244\263\244\363\244\313\244\301\244\317"; /* こんにちは in EUC-JP */
 128           static const uint16_t expected[] = /* こんにちは */
 129             {
 130               0x3053, 0x3093, 0x306B, 0x3061, 0x306F
 131             };
 132           for (o = 0; o < 2; o++)
 133             {
 134               size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
 135               size_t length;
 136               uint16_t *result = u16_conv_from_encoding ("autodetect_jp", handler,
 137                                                          input, strlen (input),
 138                                                          offsets,
 139                                                          NULL, &length);
 140               ASSERT (result != NULL);
 141               ASSERT (length == SIZEOF (expected));
 142               ASSERT (u16_cmp (result, expected, SIZEOF (expected)) == 0);
 143               if (o)
 144                 {
 145                   for (i = 0; i < 10; i++)
 146                     ASSERT (offsets[i] == ((i % 2) == 0 ? i / 2 : (size_t)(-1)));
 147                   ASSERT (offsets[10] == MAGIC);
 148                   free (offsets);
 149                 }
 150               free (result);
 151             }
 152         }
 153       for (h = 0; h < SIZEOF (handlers); h++)
 154         {
 155           enum iconv_ilseq_handler handler = handlers[h];
 156           static const char input[] = "\202\261\202\361\202\311\202\277\202\315"; /* こんにちは in Shift_JIS */
 157           static const uint16_t expected[] = /* こんにちは */
 158             {
 159               0x3053, 0x3093, 0x306B, 0x3061, 0x306F
 160             };
 161           for (o = 0; o < 2; o++)
 162             {
 163               size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
 164               size_t length;
 165               uint16_t *result = u16_conv_from_encoding ("autodetect_jp", handler,
 166                                                          input, strlen (input),
 167                                                          offsets,
 168                                                          NULL, &length);
 169               ASSERT (result != NULL);
 170               ASSERT (length == SIZEOF (expected));
 171               ASSERT (u16_cmp (result, expected, SIZEOF (expected)) == 0);
 172               if (o)
 173                 {
 174                   for (i = 0; i < 10; i++)
 175                     ASSERT (offsets[i] == ((i % 2) == 0 ? i / 2 : (size_t)(-1)));
 176                   ASSERT (offsets[10] == MAGIC);
 177                   free (offsets);
 178                 }
 179               free (result);
 180             }
 181         }
 182       for (h = 0; h < SIZEOF (handlers); h++)
 183         {
 184           enum iconv_ilseq_handler handler = handlers[h];
 185           static const char input[] = "\033$B$3$s$K$A$O\033(B"; /* こんにちは in ISO-2022-JP-2 */
 186           static const uint16_t expected[] = /* こんにちは */
 187             {
 188               0x3053, 0x3093, 0x306B, 0x3061, 0x306F
 189             };
 190           for (o = 0; o < 2; o++)
 191             {
 192               size_t *offsets = (o ? new_offsets (strlen (input)) : NULL);
 193               size_t length;
 194               uint16_t *result = u16_conv_from_encoding ("autodetect_jp", handler,
 195                                                          input, strlen (input),
 196                                                          offsets,
 197                                                          NULL, &length);
 198               ASSERT (result != NULL);
 199               ASSERT (length == SIZEOF (expected));
 200               ASSERT (u16_cmp (result, expected, SIZEOF (expected)) == 0);
 201               if (o)
 202                 {
 203                   for (i = 0; i < 16; i++)
 204                     ASSERT (offsets[i] == (i == 0 ? 0 :
 205                                            i == 5 ? 1 :
 206                                            i == 7 ? 2 :
 207                                            i == 9 ? 3 :
 208                                            i == 11 ? 4 :
 209                                            i == 13 ? 5 :
 210                                            (size_t)(-1)));
 211                   ASSERT (offsets[16] == MAGIC);
 212                   free (offsets);
 213                 }
 214               free (result);
 215             }
 216         }
 217     }
 218 # endif
 219 
 220 #endif
 221 
 222   return 0;
 223 }

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