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

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

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test of conversion of 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 <wchar.h>
  22 
  23 #include "signature.h"
  24 SIGNATURE_CHECK (wcsrtombs, size_t, (char *, wchar_t const **, size_t,
  25                                      mbstate_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       wchar_t input[10];
  43       size_t n;
  44       const wchar_t *src;
  45       #define BUFSIZE 20
  46       char buf[BUFSIZE];
  47       size_t ret;
  48 
  49       {
  50         size_t i;
  51         for (i = 0; i < BUFSIZE; i++)
  52           buf[i] = '_';
  53       }
  54 
  55       switch (argv[1][0])
  56         {
  57         case '1':
  58           /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
  59           {
  60             const char original[] = "B\374\337er"; /* "Büßer" */
  61 
  62             ret = mbstowcs (input, original, 10);
  63             ASSERT (ret == 5);
  64 
  65             for (n = 0; n < 10; n++)
  66               {
  67                 src = input;
  68                 ret = wcsrtombs (NULL, &src, n, NULL);
  69                 ASSERT (ret == 5);
  70                 ASSERT (src == input);
  71 
  72                 src = input;
  73                 ret = wcsrtombs (buf, &src, n, NULL);
  74                 ASSERT (ret == (n <= 5 ? n : 5));
  75                 ASSERT (src == (n <= 5 ? input + n : NULL));
  76                 ASSERT (memcmp (buf, original, ret) == 0);
  77                 if (src == NULL)
  78                   ASSERT (buf[ret] == '\0');
  79                 ASSERT (buf[ret + (src == NULL) + 0] == '_');
  80                 ASSERT (buf[ret + (src == NULL) + 1] == '_');
  81                 ASSERT (buf[ret + (src == NULL) + 2] == '_');
  82               }
  83           }
  84           break;
  85 
  86         case '2':
  87           /* Locale encoding is UTF-8.  */
  88           {
  89             const char original[] = "B\303\274\303\237er"; /* "Büßer" */
  90 
  91             ret = mbstowcs (input, original, 10);
  92             ASSERT (ret == 5);
  93 
  94             for (n = 0; n < 10; n++)
  95               {
  96                 src = input;
  97                 ret = wcsrtombs (NULL, &src, n, NULL);
  98                 ASSERT (ret == 7);
  99                 ASSERT (src == input);
 100 
 101                 src = input;
 102                 ret = wcsrtombs (buf, &src, n, NULL);
 103                 ASSERT (ret == (n < 1 ? n :
 104                                 n < 3 ? 1 :
 105                                 n < 5 ? 3 :
 106                                 n <= 7 ? n : 7));
 107                 ASSERT (src == (n < 1 ? input + n :
 108                                 n < 3 ? input + 1 :
 109                                 n < 5 ? input + 2 :
 110                                 n <= 7 ? input + (n - 2) : NULL));
 111                 ASSERT (memcmp (buf, original, ret) == 0);
 112                 if (src == NULL)
 113                   ASSERT (buf[ret] == '\0');
 114                 ASSERT (buf[ret + (src == NULL) + 0] == '_');
 115                 ASSERT (buf[ret + (src == NULL) + 1] == '_');
 116                 ASSERT (buf[ret + (src == NULL) + 2] == '_');
 117               }
 118           }
 119           break;
 120 
 121         case '3':
 122           /* Locale encoding is EUC-JP.  */
 123           {
 124             const char original[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
 125 
 126             ret = mbstowcs (input, original, 10);
 127             ASSERT (ret == 5);
 128 
 129             for (n = 0; n < 10; n++)
 130               {
 131                 src = input;
 132                 ret = wcsrtombs (NULL, &src, n, NULL);
 133                 ASSERT (ret == 8);
 134                 ASSERT (src == input);
 135 
 136                 src = input;
 137                 ret = wcsrtombs (buf, &src, n, NULL);
 138                 ASSERT (ret == (n < 1 ? n :
 139                                 n < 3 ? 1 :
 140                                 n < 5 ? 3 :
 141                                 n < 7 ? 5 :
 142                                 n <= 8 ? n : 8));
 143                 ASSERT (src == (n < 1 ? input + n :
 144                                 n < 3 ? input + 1 :
 145                                 n < 5 ? input + 2 :
 146                                 n < 7 ? input + 3 :
 147                                 n <= 8 ? input + (n - 3) : NULL));
 148                 ASSERT (memcmp (buf, original, ret) == 0);
 149                 if (src == NULL)
 150                   ASSERT (buf[ret] == '\0');
 151                 ASSERT (buf[ret + (src == NULL) + 0] == '_');
 152                 ASSERT (buf[ret + (src == NULL) + 1] == '_');
 153                 ASSERT (buf[ret + (src == NULL) + 2] == '_');
 154               }
 155           }
 156           break;
 157 
 158 
 159         case '4':
 160           /* Locale encoding is GB18030.  */
 161           {
 162             const char original[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
 163 
 164             ret = mbstowcs (input, original, 10);
 165             ASSERT (ret == 5);
 166 
 167             for (n = 0; n < 10; n++)
 168               {
 169                 src = input;
 170                 ret = wcsrtombs (NULL, &src, n, NULL);
 171                 ASSERT (ret == 9);
 172                 ASSERT (src == input);
 173 
 174                 src = input;
 175                 ret = wcsrtombs (buf, &src, n, NULL);
 176                 ASSERT (ret == (n < 1 ? n :
 177                                 n < 3 ? 1 :
 178                                 n < 7 ? 3 :
 179                                 n <= 9 ? n : 9));
 180                 ASSERT (src == (n < 1 ? input + n :
 181                                 n < 3 ? input + 1 :
 182                                 n < 7 ? input + 2 :
 183                                 n <= 9 ? input + (n - 4) : NULL));
 184                 ASSERT (memcmp (buf, original, ret) == 0);
 185                 if (src == NULL)
 186                   ASSERT (buf[ret] == '\0');
 187                 ASSERT (buf[ret + (src == NULL) + 0] == '_');
 188                 ASSERT (buf[ret + (src == NULL) + 1] == '_');
 189                 ASSERT (buf[ret + (src == NULL) + 2] == '_');
 190               }
 191           }
 192           break;
 193 
 194         default:
 195           return 1;
 196         }
 197 
 198       return 0;
 199     }
 200 
 201   return 1;
 202 }

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