LCOV - code coverage report
Current view: top level - gltests - test-mbrtowc.c (source / functions) Hit Total Coverage
Test: GNU SASL Lines: 76 190 40.0 %
Date: 2010-09-27 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 42 185 22.7 %

           Branch data     Line data    Source code
       1                 :            : /* Test of conversion of multibyte character to wide character.
       2                 :            :    Copyright (C) 2008, 2009, 2010 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 <http://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 (mbrtowc, size_t, (wchar_t *, char const *, size_t,
      25                 :            :                                    mbstate_t *));
      26                 :            : 
      27                 :            : #include <locale.h>
      28                 :            : #include <stdio.h>
      29                 :            : #include <string.h>
      30                 :            : 
      31                 :            : #include "macros.h"
      32                 :            : 
      33                 :            : int
      34                 :          1 : main (int argc, char *argv[])
      35                 :            : {
      36                 :            :   mbstate_t state;
      37                 :            :   wchar_t wc;
      38                 :            :   size_t ret;
      39                 :            : 
      40                 :            :   /* configure should already have checked that the locale is supported.  */
      41         [ -  + ]:          1 :   if (setlocale (LC_ALL, "") == NULL)
      42                 :          0 :     return 1;
      43                 :            : 
      44                 :            :   /* Test zero-length input.  */
      45                 :            :   {
      46                 :          1 :     memset (&state, '\0', sizeof (mbstate_t));
      47                 :          1 :     wc = (wchar_t) 0xBADFACE;
      48                 :          1 :     ret = mbrtowc (&wc, "x", 0, &state);
      49                 :            :     /* gnulib's implementation returns (size_t)(-2).
      50                 :            :        The AIX 5.1 implementation returns (size_t)(-1).
      51                 :            :        glibc's implementation returns 0.  */
      52   [ +  -  +  - ]:          1 :     ASSERT (ret == (size_t)(-2) || ret == (size_t)(-1) || ret == 0);
                 [ -  + ]
      53         [ -  + ]:          1 :     ASSERT (mbsinit (&state));
      54                 :            :   }
      55                 :            : 
      56                 :            :   /* Test NUL byte input.  */
      57                 :            :   {
      58                 :          1 :     memset (&state, '\0', sizeof (mbstate_t));
      59                 :          1 :     wc = (wchar_t) 0xBADFACE;
      60                 :          1 :     ret = mbrtowc (&wc, "", 1, &state);
      61         [ -  + ]:          1 :     ASSERT (ret == 0);
      62         [ -  + ]:          1 :     ASSERT (wc == 0);
      63         [ -  + ]:          1 :     ASSERT (mbsinit (&state));
      64                 :          1 :     ret = mbrtowc (NULL, "", 1, &state);
      65         [ -  + ]:          1 :     ASSERT (ret == 0);
      66         [ -  + ]:          1 :     ASSERT (mbsinit (&state));
      67                 :            :   }
      68                 :            : 
      69                 :            :   /* Test single-byte input.  */
      70                 :            :   {
      71                 :            :     int c;
      72                 :            :     char buf[1];
      73                 :            : 
      74                 :          1 :     memset (&state, '\0', sizeof (mbstate_t));
      75         [ +  + ]:        257 :     for (c = 0; c < 0x100; c++)
      76         [ +  + ]:        256 :       switch (c)
      77                 :            :         {
      78                 :            :         case '\t': case '\v': case '\f':
      79                 :            :         case ' ': case '!': case '"': case '#': case '%':
      80                 :            :         case '&': case '\'': case '(': case ')': case '*':
      81                 :            :         case '+': case ',': case '-': case '.': case '/':
      82                 :            :         case '0': case '1': case '2': case '3': case '4':
      83                 :            :         case '5': case '6': case '7': case '8': case '9':
      84                 :            :         case ':': case ';': case '<': case '=': case '>':
      85                 :            :         case '?':
      86                 :            :         case 'A': case 'B': case 'C': case 'D': case 'E':
      87                 :            :         case 'F': case 'G': case 'H': case 'I': case 'J':
      88                 :            :         case 'K': case 'L': case 'M': case 'N': case 'O':
      89                 :            :         case 'P': case 'Q': case 'R': case 'S': case 'T':
      90                 :            :         case 'U': case 'V': case 'W': case 'X': case 'Y':
      91                 :            :         case 'Z':
      92                 :            :         case '[': case '\\': case ']': case '^': case '_':
      93                 :            :         case 'a': case 'b': case 'c': case 'd': case 'e':
      94                 :            :         case 'f': case 'g': case 'h': case 'i': case 'j':
      95                 :            :         case 'k': case 'l': case 'm': case 'n': case 'o':
      96                 :            :         case 'p': case 'q': case 'r': case 's': case 't':
      97                 :            :         case 'u': case 'v': case 'w': case 'x': case 'y':
      98                 :            :         case 'z': case '{': case '|': case '}': case '~':
      99                 :            :           /* c is in the ISO C "basic character set".  */
     100                 :         95 :           buf[0] = c;
     101                 :         95 :           wc = (wchar_t) 0xBADFACE;
     102                 :         95 :           ret = mbrtowc (&wc, buf, 1, &state);
     103         [ -  + ]:         95 :           ASSERT (ret == 1);
     104         [ -  + ]:         95 :           ASSERT (wc == c);
     105         [ -  + ]:         95 :           ASSERT (mbsinit (&state));
     106                 :         95 :           ret = mbrtowc (NULL, buf, 1, &state);
     107         [ -  + ]:         95 :           ASSERT (ret == 1);
     108         [ -  + ]:         95 :           ASSERT (mbsinit (&state));
     109                 :            :           break;
     110                 :            :         }
     111                 :            :   }
     112                 :            : 
     113                 :            :   /* Test special calling convention, passing a NULL pointer.  */
     114                 :            :   {
     115                 :          1 :     memset (&state, '\0', sizeof (mbstate_t));
     116                 :          1 :     wc = (wchar_t) 0xBADFACE;
     117                 :          1 :     ret = mbrtowc (&wc, NULL, 5, &state);
     118         [ -  + ]:          1 :     ASSERT (ret == 0);
     119         [ -  + ]:          1 :     ASSERT (wc == (wchar_t) 0xBADFACE);
     120         [ -  + ]:          1 :     ASSERT (mbsinit (&state));
     121                 :            :   }
     122                 :            : 
     123         [ +  - ]:          1 :   if (argc > 1)
     124   [ -  +  -  -  :          1 :     switch (argv[1][0])
                      - ]
     125                 :            :       {
     126                 :            :       case '1':
     127                 :            :         /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
     128                 :            :         {
     129                 :          0 :           char input[] = "B\374\337er"; /* "Büßer" */
     130                 :          0 :           memset (&state, '\0', sizeof (mbstate_t));
     131                 :            : 
     132                 :          0 :           wc = (wchar_t) 0xBADFACE;
     133                 :          0 :           ret = mbrtowc (&wc, input, 1, &state);
     134         [ #  # ]:          0 :           ASSERT (ret == 1);
     135         [ #  # ]:          0 :           ASSERT (wc == 'B');
     136         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     137                 :          0 :           input[0] = '\0';
     138                 :            : 
     139                 :          0 :           wc = (wchar_t) 0xBADFACE;
     140                 :          0 :           ret = mbrtowc (&wc, input + 1, 1, &state);
     141         [ #  # ]:          0 :           ASSERT (ret == 1);
     142         [ #  # ]:          0 :           ASSERT (wctob (wc) == (unsigned char) '\374');
     143         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     144                 :          0 :           input[1] = '\0';
     145                 :            : 
     146                 :          0 :           wc = (wchar_t) 0xBADFACE;
     147                 :          0 :           ret = mbrtowc (&wc, input + 2, 3, &state);
     148         [ #  # ]:          0 :           ASSERT (ret == 1);
     149         [ #  # ]:          0 :           ASSERT (wctob (wc) == (unsigned char) '\337');
     150         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     151                 :          0 :           input[2] = '\0';
     152                 :            : 
     153                 :          0 :           wc = (wchar_t) 0xBADFACE;
     154                 :          0 :           ret = mbrtowc (&wc, input + 3, 2, &state);
     155         [ #  # ]:          0 :           ASSERT (ret == 1);
     156         [ #  # ]:          0 :           ASSERT (wc == 'e');
     157         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     158                 :          0 :           input[3] = '\0';
     159                 :            : 
     160                 :          0 :           wc = (wchar_t) 0xBADFACE;
     161                 :          0 :           ret = mbrtowc (&wc, input + 4, 1, &state);
     162         [ #  # ]:          0 :           ASSERT (ret == 1);
     163         [ #  # ]:          0 :           ASSERT (wc == 'r');
     164         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     165                 :            :         }
     166                 :          0 :         return 0;
     167                 :            : 
     168                 :            :       case '2':
     169                 :            :         /* Locale encoding is UTF-8.  */
     170                 :            :         {
     171                 :          1 :           char input[] = "B\303\274\303\237er"; /* "Büßer" */
     172                 :          1 :           memset (&state, '\0', sizeof (mbstate_t));
     173                 :            : 
     174                 :          1 :           wc = (wchar_t) 0xBADFACE;
     175                 :          1 :           ret = mbrtowc (&wc, input, 1, &state);
     176         [ -  + ]:          1 :           ASSERT (ret == 1);
     177         [ -  + ]:          1 :           ASSERT (wc == 'B');
     178         [ -  + ]:          1 :           ASSERT (mbsinit (&state));
     179                 :          1 :           input[0] = '\0';
     180                 :            : 
     181                 :          1 :           wc = (wchar_t) 0xBADFACE;
     182                 :          1 :           ret = mbrtowc (&wc, input + 1, 1, &state);
     183         [ -  + ]:          1 :           ASSERT (ret == (size_t)(-2));
     184         [ -  + ]:          1 :           ASSERT (wc == (wchar_t) 0xBADFACE);
     185         [ -  + ]:          1 :           ASSERT (!mbsinit (&state));
     186                 :          1 :           input[1] = '\0';
     187                 :            : 
     188                 :          1 :           wc = (wchar_t) 0xBADFACE;
     189                 :          1 :           ret = mbrtowc (&wc, input + 2, 5, &state);
     190         [ -  + ]:          1 :           ASSERT (ret == 1);
     191         [ -  + ]:          1 :           ASSERT (wctob (wc) == EOF);
     192         [ -  + ]:          1 :           ASSERT (mbsinit (&state));
     193                 :          1 :           input[2] = '\0';
     194                 :            : 
     195                 :          1 :           wc = (wchar_t) 0xBADFACE;
     196                 :          1 :           ret = mbrtowc (&wc, input + 3, 4, &state);
     197         [ -  + ]:          1 :           ASSERT (ret == 2);
     198         [ -  + ]:          1 :           ASSERT (wctob (wc) == EOF);
     199         [ -  + ]:          1 :           ASSERT (mbsinit (&state));
     200                 :          1 :           input[3] = '\0';
     201                 :          1 :           input[4] = '\0';
     202                 :            : 
     203                 :          1 :           wc = (wchar_t) 0xBADFACE;
     204                 :          1 :           ret = mbrtowc (&wc, input + 5, 2, &state);
     205         [ -  + ]:          1 :           ASSERT (ret == 1);
     206         [ -  + ]:          1 :           ASSERT (wc == 'e');
     207         [ -  + ]:          1 :           ASSERT (mbsinit (&state));
     208                 :          1 :           input[5] = '\0';
     209                 :            : 
     210                 :          1 :           wc = (wchar_t) 0xBADFACE;
     211                 :          1 :           ret = mbrtowc (&wc, input + 6, 1, &state);
     212         [ -  + ]:          1 :           ASSERT (ret == 1);
     213         [ -  + ]:          1 :           ASSERT (wc == 'r');
     214         [ -  + ]:          1 :           ASSERT (mbsinit (&state));
     215                 :            :         }
     216                 :          1 :         return 0;
     217                 :            : 
     218                 :            :       case '3':
     219                 :            :         /* Locale encoding is EUC-JP.  */
     220                 :            :         {
     221                 :          0 :           char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
     222                 :          0 :           memset (&state, '\0', sizeof (mbstate_t));
     223                 :            : 
     224                 :          0 :           wc = (wchar_t) 0xBADFACE;
     225                 :          0 :           ret = mbrtowc (&wc, input, 1, &state);
     226         [ #  # ]:          0 :           ASSERT (ret == 1);
     227         [ #  # ]:          0 :           ASSERT (wc == '<');
     228         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     229                 :          0 :           input[0] = '\0';
     230                 :            : 
     231                 :          0 :           wc = (wchar_t) 0xBADFACE;
     232                 :          0 :           ret = mbrtowc (&wc, input + 1, 2, &state);
     233         [ #  # ]:          0 :           ASSERT (ret == 2);
     234         [ #  # ]:          0 :           ASSERT (wctob (wc) == EOF);
     235         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     236                 :          0 :           input[1] = '\0';
     237                 :          0 :           input[2] = '\0';
     238                 :            : 
     239                 :          0 :           wc = (wchar_t) 0xBADFACE;
     240                 :          0 :           ret = mbrtowc (&wc, input + 3, 1, &state);
     241         [ #  # ]:          0 :           ASSERT (ret == (size_t)(-2));
     242         [ #  # ]:          0 :           ASSERT (wc == (wchar_t) 0xBADFACE);
     243         [ #  # ]:          0 :           ASSERT (!mbsinit (&state));
     244                 :          0 :           input[3] = '\0';
     245                 :            : 
     246                 :          0 :           wc = (wchar_t) 0xBADFACE;
     247                 :          0 :           ret = mbrtowc (&wc, input + 4, 4, &state);
     248         [ #  # ]:          0 :           ASSERT (ret == 1);
     249         [ #  # ]:          0 :           ASSERT (wctob (wc) == EOF);
     250         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     251                 :          0 :           input[4] = '\0';
     252                 :            : 
     253                 :          0 :           wc = (wchar_t) 0xBADFACE;
     254                 :          0 :           ret = mbrtowc (&wc, input + 5, 3, &state);
     255         [ #  # ]:          0 :           ASSERT (ret == 2);
     256         [ #  # ]:          0 :           ASSERT (wctob (wc) == EOF);
     257         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     258                 :          0 :           input[5] = '\0';
     259                 :          0 :           input[6] = '\0';
     260                 :            : 
     261                 :          0 :           wc = (wchar_t) 0xBADFACE;
     262                 :          0 :           ret = mbrtowc (&wc, input + 7, 1, &state);
     263         [ #  # ]:          0 :           ASSERT (ret == 1);
     264         [ #  # ]:          0 :           ASSERT (wc == '>');
     265         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     266                 :            :         }
     267                 :          0 :         return 0;
     268                 :            : 
     269                 :            :       case '4':
     270                 :            :         /* Locale encoding is GB18030.  */
     271                 :            :         {
     272                 :          0 :           char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
     273                 :          0 :           memset (&state, '\0', sizeof (mbstate_t));
     274                 :            : 
     275                 :          0 :           wc = (wchar_t) 0xBADFACE;
     276                 :          0 :           ret = mbrtowc (&wc, input, 1, &state);
     277         [ #  # ]:          0 :           ASSERT (ret == 1);
     278         [ #  # ]:          0 :           ASSERT (wc == 'B');
     279         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     280                 :          0 :           input[0] = '\0';
     281                 :            : 
     282                 :          0 :           wc = (wchar_t) 0xBADFACE;
     283                 :          0 :           ret = mbrtowc (&wc, input + 1, 1, &state);
     284         [ #  # ]:          0 :           ASSERT (ret == (size_t)(-2));
     285         [ #  # ]:          0 :           ASSERT (wc == (wchar_t) 0xBADFACE);
     286         [ #  # ]:          0 :           ASSERT (!mbsinit (&state));
     287                 :          0 :           input[1] = '\0';
     288                 :            : 
     289                 :          0 :           wc = (wchar_t) 0xBADFACE;
     290                 :          0 :           ret = mbrtowc (&wc, input + 2, 7, &state);
     291         [ #  # ]:          0 :           ASSERT (ret == 1);
     292         [ #  # ]:          0 :           ASSERT (wctob (wc) == EOF);
     293         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     294                 :          0 :           input[2] = '\0';
     295                 :            : 
     296                 :          0 :           wc = (wchar_t) 0xBADFACE;
     297                 :          0 :           ret = mbrtowc (&wc, input + 3, 6, &state);
     298         [ #  # ]:          0 :           ASSERT (ret == 4);
     299         [ #  # ]:          0 :           ASSERT (wctob (wc) == EOF);
     300         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     301                 :          0 :           input[3] = '\0';
     302                 :          0 :           input[4] = '\0';
     303                 :          0 :           input[5] = '\0';
     304                 :          0 :           input[6] = '\0';
     305                 :            : 
     306                 :          0 :           wc = (wchar_t) 0xBADFACE;
     307                 :          0 :           ret = mbrtowc (&wc, input + 7, 2, &state);
     308         [ #  # ]:          0 :           ASSERT (ret == 1);
     309         [ #  # ]:          0 :           ASSERT (wc == 'e');
     310         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     311                 :          0 :           input[5] = '\0';
     312                 :            : 
     313                 :          0 :           wc = (wchar_t) 0xBADFACE;
     314                 :          0 :           ret = mbrtowc (&wc, input + 8, 1, &state);
     315         [ #  # ]:          0 :           ASSERT (ret == 1);
     316         [ #  # ]:          0 :           ASSERT (wc == 'r');
     317         [ #  # ]:          0 :           ASSERT (mbsinit (&state));
     318                 :            :         }
     319                 :          0 :         return 0;
     320                 :            :       }
     321                 :            : 
     322                 :          1 :   return 1;
     323                 :            : }

Generated by: LCOV version 1.8