root/maint/gnulib/tests/test-c32stombs.c

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

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test of conversion of 32-bit wide string to string.
   2    Copyright (C) 2008-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>, 2008.  */
  18 
  19 #include <config.h>
  20 
  21 #include <uchar.h>
  22 
  23 #include "signature.h"
  24 SIGNATURE_CHECK (c32stombs, size_t,
  25                  (char *, const char32_t *, size_t));
  26 
  27 #include <locale.h>
  28 #include <stdlib.h>
  29 #include <string.h>
  30 
  31 #include "macros.h"
  32 
  33 int
  34 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  35 {
  36   /* configure should already have checked that the locale is supported.  */
  37   if (setlocale (LC_ALL, "") == NULL)
  38     return 1;
  39 
  40   if (argc > 1)
  41     {
  42       char32_t input[10];
  43       size_t n;
  44       #define BUFSIZE 20
  45       char buf[BUFSIZE];
  46       size_t ret;
  47 
  48       {
  49         size_t i;
  50         for (i = 0; i < BUFSIZE; i++)
  51           buf[i] = '_';
  52       }
  53 
  54       switch (argv[1][0])
  55         {
  56         case '1':
  57           /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
  58           {
  59             const char original[] = "B\374\337er"; /* "Büßer" */
  60 
  61             ret = mbstoc32s (input, original, 10);
  62             ASSERT (ret == 5);
  63 
  64             for (n = 0; n < 10; n++)
  65               {
  66                 ret = c32stombs (NULL, input, n);
  67                 ASSERT (ret == 5);
  68 
  69                 ret = c32stombs (buf, input, n);
  70                 ASSERT (ret == (n <= 5 ? n : 5));
  71                 ASSERT (memcmp (buf, original, ret) == 0);
  72                 if (n > 5)
  73                   ASSERT (buf[ret] == '\0');
  74                 ASSERT (buf[ret + (n > 5) + 0] == '_');
  75                 ASSERT (buf[ret + (n > 5) + 1] == '_');
  76                 ASSERT (buf[ret + (n > 5) + 2] == '_');
  77               }
  78           }
  79           break;
  80 
  81         case '2':
  82           /* Locale encoding is UTF-8.  */
  83           {
  84             const char original[] = "s\303\274\303\237\360\237\230\213!"; /* "süß😋!" */
  85 
  86             ret = mbstoc32s (input, original, 10);
  87             ASSERT (ret == 5);
  88 
  89             for (n = 0; n < 15; n++)
  90               {
  91                 ret = c32stombs (NULL, input, n);
  92                 ASSERT (ret == 10);
  93 
  94                 ret = c32stombs (buf, input, n);
  95                 ASSERT (ret == (n < 1 ? n :
  96                                 n < 3 ? 1 :
  97                                 n < 5 ? 3 :
  98                                 n < 9 ? 5 :
  99                                 n <= 10 ? n : 10));
 100                 ASSERT (memcmp (buf, original, ret) == 0);
 101                 if (n > 10)
 102                   ASSERT (buf[ret] == '\0');
 103                 ASSERT (buf[ret + (n > 10) + 0] == '_');
 104                 ASSERT (buf[ret + (n > 10) + 1] == '_');
 105                 ASSERT (buf[ret + (n > 10) + 2] == '_');
 106               }
 107           }
 108           break;
 109 
 110         case '3':
 111           /* Locale encoding is EUC-JP.  */
 112           {
 113             const char original[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
 114 
 115             ret = mbstoc32s (input, original, 10);
 116             ASSERT (ret == 5);
 117 
 118             for (n = 0; n < 10; n++)
 119               {
 120                 ret = c32stombs (NULL, input, n);
 121                 ASSERT (ret == 8);
 122 
 123                 ret = c32stombs (buf, input, n);
 124                 ASSERT (ret == (n < 1 ? n :
 125                                 n < 3 ? 1 :
 126                                 n < 5 ? 3 :
 127                                 n < 7 ? 5 :
 128                                 n <= 8 ? n : 8));
 129                 ASSERT (memcmp (buf, original, ret) == 0);
 130                 if (n > 8)
 131                   ASSERT (buf[ret] == '\0');
 132                 ASSERT (buf[ret + (n > 8) + 0] == '_');
 133                 ASSERT (buf[ret + (n > 8) + 1] == '_');
 134                 ASSERT (buf[ret + (n > 8) + 2] == '_');
 135               }
 136           }
 137           break;
 138 
 139 
 140         case '4':
 141           /* Locale encoding is GB18030.  */
 142           {
 143             const char original[] = "s\250\271\201\060\211\070\224\071\375\067!"; /* "süß😋!" */
 144 
 145             ret = mbstoc32s (input, original, 10);
 146             ASSERT (ret == 5);
 147 
 148             for (n = 0; n < 15; n++)
 149               {
 150                 ret = c32stombs (NULL, input, n);
 151                 ASSERT (ret == 12);
 152 
 153                 ret = c32stombs (buf, input, n);
 154                 ASSERT (ret == (n < 1 ? n :
 155                                 n < 3 ? 1 :
 156                                 n < 7 ? 3 :
 157                                 n < 11 ? 7 :
 158                                 n <= 12 ? n : 12));
 159                 ASSERT (memcmp (buf, original, ret) == 0);
 160                 if (n > 12)
 161                   ASSERT (buf[ret] == '\0');
 162                 ASSERT (buf[ret + (n > 12) + 0] == '_');
 163                 ASSERT (buf[ret + (n > 12) + 1] == '_');
 164                 ASSERT (buf[ret + (n > 12) + 2] == '_');
 165               }
 166           }
 167           break;
 168 
 169         default:
 170           return 1;
 171         }
 172 
 173       return 0;
 174     }
 175 
 176   return 1;
 177 }

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