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

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

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /* Test of conversion of string to 32-bit wide 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 (mbstoc32s, size_t, (char32_t *, const char *, size_t));
  25 
  26 #include <locale.h>
  27 #include <stdio.h>
  28 #include <string.h>
  29 
  30 #include "macros.h"
  31 
  32 int
  33 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  34 {
  35   char32_t wc;
  36   size_t ret;
  37 
  38   /* configure should already have checked that the locale is supported.  */
  39   if (setlocale (LC_ALL, "") == NULL)
  40     return 1;
  41 
  42   /* Test NUL byte input.  */
  43   {
  44     const char *src;
  45 
  46     src = "";
  47     ret = mbstoc32s (NULL, src, 0);
  48     ASSERT (ret == 0);
  49 
  50     src = "";
  51     ret = mbstoc32s (NULL, src, 1);
  52     ASSERT (ret == 0);
  53 
  54     wc = (char32_t) 0xBADFACE;
  55     src = "";
  56     ret = mbstoc32s (&wc, src, 0);
  57     ASSERT (ret == 0);
  58     ASSERT (wc == (char32_t) 0xBADFACE);
  59 
  60     wc = (char32_t) 0xBADFACE;
  61     src = "";
  62     ret = mbstoc32s (&wc, src, 1);
  63     ASSERT (ret == 0);
  64     ASSERT (wc == 0);
  65   }
  66 
  67   if (argc > 1)
  68     {
  69       int unlimited;
  70 
  71       for (unlimited = 0; unlimited < 2; unlimited++)
  72         {
  73           #define BUFSIZE 10
  74           char32_t buf[BUFSIZE];
  75           const char *src;
  76 
  77           {
  78             size_t i;
  79             for (i = 0; i < BUFSIZE; i++)
  80               buf[i] = (char32_t) 0xBADFACE;
  81           }
  82 
  83           switch (argv[1][0])
  84             {
  85             case '1':
  86               /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
  87               {
  88                 char input[] = "B\374\337er"; /* "Büßer" */
  89 
  90                 wc = (char32_t) 0xBADFACE;
  91                 ret = mbstoc32s (&wc, input, 1);
  92                 ASSERT (ret == 1);
  93                 ASSERT (wc == 'B');
  94                 input[0] = '\0';
  95 
  96                 wc = (char32_t) 0xBADFACE;
  97                 ret = mbstoc32s (&wc, input + 1, 1);
  98                 ASSERT (ret == 1);
  99                 ASSERT (c32tob (wc) == (unsigned char) '\374');
 100                 input[1] = '\0';
 101 
 102                 src = input + 2;
 103                 ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 1);
 104                 ASSERT (ret == 3);
 105 
 106                 src = input + 2;
 107                 ret = mbstoc32s (buf, src, unlimited ? BUFSIZE : 1);
 108                 ASSERT (ret == (unlimited ? 3 : 1));
 109                 ASSERT (c32tob (buf[0]) == (unsigned char) '\337');
 110                 if (unlimited)
 111                   {
 112                     ASSERT (buf[1] == 'e');
 113                     ASSERT (buf[2] == 'r');
 114                     ASSERT (buf[3] == 0);
 115                     ASSERT (buf[4] == (char32_t) 0xBADFACE);
 116                   }
 117                 else
 118                   ASSERT (buf[1] == (char32_t) 0xBADFACE);
 119               }
 120               break;
 121 
 122             case '2':
 123               /* Locale encoding is UTF-8.  */
 124               {
 125                 char input[] = "s\303\274\303\237\360\237\230\213!"; /* "süß😋!" */
 126 
 127                 wc = (char32_t) 0xBADFACE;
 128                 ret = mbstoc32s (&wc, input, 1);
 129                 ASSERT (ret == 1);
 130                 ASSERT (wc == 's');
 131                 input[0] = '\0';
 132 
 133                 wc = (char32_t) 0xBADFACE;
 134                 ret = mbstoc32s (&wc, input + 1, 1);
 135                 ASSERT (ret == 1);
 136                 ASSERT (wc == 0x00FC); /* expect Unicode encoding */
 137                 input[1] = '\0';
 138                 input[2] = '\0';
 139 
 140                 src = input + 3;
 141                 ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 2);
 142                 ASSERT (ret == 3);
 143 
 144                 src = input + 3;
 145                 ret = mbstoc32s (buf, src, unlimited ? BUFSIZE : 2);
 146                 ASSERT (ret == (unlimited ? 3 : 2));
 147                 ASSERT (buf[0] == 0x00DF); /* expect Unicode encoding */
 148                 ASSERT (buf[1] == 0x1F60B); /* expect Unicode encoding */
 149                 if (unlimited)
 150                   {
 151                     ASSERT (buf[2] == '!');
 152                     ASSERT (buf[3] == 0);
 153                     ASSERT (buf[4] == (char32_t) 0xBADFACE);
 154                   }
 155                 else
 156                   ASSERT (buf[2] == (char32_t) 0xBADFACE);
 157               }
 158               break;
 159 
 160             case '3':
 161               /* Locale encoding is EUC-JP.  */
 162               {
 163                 char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
 164 
 165                 wc = (char32_t) 0xBADFACE;
 166                 ret = mbstoc32s (&wc, input, 1);
 167                 ASSERT (ret == 1);
 168                 ASSERT (wc == '<');
 169                 input[0] = '\0';
 170 
 171                 wc = (char32_t) 0xBADFACE;
 172                 ret = mbstoc32s (&wc, input + 1, 1);
 173                 ASSERT (ret == 1);
 174                 ASSERT (c32tob (wc) == EOF);
 175                 input[1] = '\0';
 176                 input[2] = '\0';
 177 
 178                 src = input + 3;
 179                 ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 2);
 180                 ASSERT (ret == 3);
 181 
 182                 src = input + 3;
 183                 ret = mbstoc32s (buf, src, unlimited ? BUFSIZE : 2);
 184                 ASSERT (ret == (unlimited ? 3 : 2));
 185                 ASSERT (c32tob (buf[0]) == EOF);
 186                 ASSERT (c32tob (buf[1]) == EOF);
 187                 if (unlimited)
 188                   {
 189                     ASSERT (buf[2] == '>');
 190                     ASSERT (buf[3] == 0);
 191                     ASSERT (buf[4] == (char32_t) 0xBADFACE);
 192                   }
 193                 else
 194                   ASSERT (buf[2] == (char32_t) 0xBADFACE);
 195               }
 196               break;
 197 
 198             case '4':
 199               /* Locale encoding is GB18030.  */
 200               {
 201                 char input[] = "s\250\271\201\060\211\070\224\071\375\067!"; /* "süß😋!" */
 202 
 203                 wc = (char32_t) 0xBADFACE;
 204                 ret = mbstoc32s (&wc, input, 1);
 205                 ASSERT (ret == 1);
 206                 ASSERT (wc == 's');
 207                 input[0] = '\0';
 208 
 209                 wc = (char32_t) 0xBADFACE;
 210                 ret = mbstoc32s (&wc, input + 1, 1);
 211                 ASSERT (ret == 1);
 212                 ASSERT (c32tob (wc) == EOF);
 213                 input[1] = '\0';
 214 
 215                 src = input + 3;
 216                 ret = mbstoc32s (NULL, src, unlimited ? BUFSIZE : 2);
 217                 ASSERT (ret == 3);
 218 
 219                 src = input + 3;
 220                 ret = mbstoc32s (buf, src, unlimited ? BUFSIZE : 2);
 221                 ASSERT (ret == (unlimited ? 3 : 2));
 222                 ASSERT (c32tob (buf[0]) == EOF);
 223                 ASSERT (c32tob (buf[1]) == EOF);
 224                 if (unlimited)
 225                   {
 226                     ASSERT (buf[2] == '!');
 227                     ASSERT (buf[3] == 0);
 228                     ASSERT (buf[4] == (char32_t) 0xBADFACE);
 229                   }
 230                 else
 231                   ASSERT (buf[2] == (char32_t) 0xBADFACE);
 232               }
 233               break;
 234 
 235             default:
 236               return 1;
 237             }
 238         }
 239 
 240       return 0;
 241     }
 242 
 243   return 1;
 244 }

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