LCOV - code coverage report
Current view: top level - lib/gltests - test-iconv.c (source / functions) Hit Total Coverage
Test: GNU Libidn Lines: 49 51 96.1 %
Date: 2013-07-10 Functions: 1 1 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 24 52 46.2 %

           Branch data     Line data    Source code
       1                 :            : /* Test of character set conversion.
       2                 :            :    Copyright (C) 2007-2013 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>, 2007.  */
      18                 :            : 
      19                 :            : #include <config.h>
      20                 :            : 
      21                 :            : #if HAVE_ICONV
      22                 :            : # include <iconv.h>
      23                 :            : 
      24                 :            : # ifndef ICONV_CONST
      25                 :            : #  define ICONV_CONST /* empty */
      26                 :            : # endif
      27                 :            : 
      28                 :            : #include "signature.h"
      29                 :            : SIGNATURE_CHECK (iconv, size_t, (iconv_t, ICONV_CONST char **, size_t *,
      30                 :            :                                  char **, size_t *));
      31                 :            : SIGNATURE_CHECK (iconv_close, int, (iconv_t x));
      32                 :            : SIGNATURE_CHECK (iconv_open, iconv_t, (char const *, char const *));
      33                 :            : 
      34                 :            : #endif
      35                 :            : 
      36                 :            : #include <errno.h>
      37                 :            : #include <string.h>
      38                 :            : 
      39                 :            : #include "macros.h"
      40                 :            : 
      41                 :            : int
      42                 :          1 : main ()
      43                 :            : {
      44                 :            : #if HAVE_ICONV
      45                 :            :   /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
      46                 :            :      and UTF-8.  */
      47                 :          1 :   iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
      48                 :          1 :   iconv_t cd_utf8_to_88591 = iconv_open ("ISO-8859-1", "UTF-8");
      49                 :            : 
      50         [ -  + ]:          1 :   ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
      51         [ -  + ]:          1 :   ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
      52                 :            : 
      53                 :            :   /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
      54                 :            :   {
      55                 :            :     static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
      56                 :            :     static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
      57                 :            :     char buf[50];
      58                 :          1 :     const char *inptr = input;
      59                 :          1 :     size_t inbytesleft = strlen (input);
      60                 :          1 :     char *outptr = buf;
      61                 :          1 :     size_t outbytesleft = sizeof (buf);
      62                 :          1 :     size_t res = iconv (cd_88591_to_utf8,
      63                 :            :                         (ICONV_CONST char **) &inptr, &inbytesleft,
      64                 :            :                         &outptr, &outbytesleft);
      65 [ +  - ][ -  + ]:          1 :     ASSERT (res == 0 && inbytesleft == 0);
      66         [ -  + ]:          1 :     ASSERT (outptr == buf + strlen (expected));
      67         [ -  + ]:          1 :     ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
      68                 :            :   }
      69                 :            : 
      70                 :            :   /* Test conversion from ISO-8859-1 to UTF-8 with E2BIG.  */
      71                 :            :   {
      72                 :            :     static const char input[] = "\304";
      73                 :            :     static char buf[2] = { (char)0xDE, (char)0xAD };
      74                 :          1 :     const char *inptr = input;
      75                 :          1 :     size_t inbytesleft = 1;
      76                 :          1 :     char *outptr = buf;
      77                 :          1 :     size_t outbytesleft = 1;
      78                 :          1 :     size_t res = iconv (cd_88591_to_utf8,
      79                 :            :                         (ICONV_CONST char **) &inptr, &inbytesleft,
      80                 :            :                         &outptr, &outbytesleft);
      81 [ +  - ][ -  + ]:          1 :     ASSERT (res == (size_t)(-1) && errno == E2BIG);
      82         [ -  + ]:          1 :     ASSERT (inbytesleft == 1);
      83         [ -  + ]:          1 :     ASSERT (outbytesleft == 1);
      84         [ -  + ]:          1 :     ASSERT ((unsigned char) buf[1] == 0xAD);
      85         [ -  + ]:          1 :     ASSERT ((unsigned char) buf[0] == 0xDE);
      86                 :            :   }
      87                 :            : 
      88                 :            :   /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
      89                 :            :   {
      90                 :            :     static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
      91                 :            :     static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
      92                 :            :     char buf[50];
      93                 :          1 :     const char *inptr = input;
      94                 :          1 :     size_t inbytesleft = strlen (input);
      95                 :          1 :     char *outptr = buf;
      96                 :          1 :     size_t outbytesleft = sizeof (buf);
      97                 :          1 :     size_t res = iconv (cd_utf8_to_88591,
      98                 :            :                         (ICONV_CONST char **) &inptr, &inbytesleft,
      99                 :            :                         &outptr, &outbytesleft);
     100 [ +  - ][ -  + ]:          1 :     ASSERT (res == 0 && inbytesleft == 0);
     101         [ -  + ]:          1 :     ASSERT (outptr == buf + strlen (expected));
     102         [ -  + ]:          1 :     ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
     103                 :            :   }
     104                 :            : 
     105                 :            :   /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
     106                 :            :   {
     107                 :            :     static const char input[] = "\342\202\254"; /* EURO SIGN */
     108                 :            :     char buf[10];
     109                 :          1 :     const char *inptr = input;
     110                 :          1 :     size_t inbytesleft = strlen (input);
     111                 :          1 :     char *outptr = buf;
     112                 :          1 :     size_t outbytesleft = sizeof (buf);
     113                 :          1 :     size_t res = iconv (cd_utf8_to_88591,
     114                 :            :                         (ICONV_CONST char **) &inptr, &inbytesleft,
     115                 :            :                         &outptr, &outbytesleft);
     116         [ +  - ]:          1 :     if (res == (size_t)(-1))
     117                 :            :       {
     118         [ -  + ]:          1 :         ASSERT (errno == EILSEQ);
     119 [ +  - ][ -  + ]:          1 :         ASSERT (inbytesleft == strlen (input) && outptr == buf);
     120                 :            :       }
     121                 :            :     else
     122                 :            :       {
     123         [ #  # ]:          0 :         ASSERT (res == 1);
     124         [ #  # ]:          0 :         ASSERT (inbytesleft == 0);
     125                 :            :       }
     126                 :            :   }
     127                 :            : 
     128                 :            :   /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
     129                 :            :   {
     130                 :            :     static const char input[] = "\342";
     131                 :            :     char buf[10];
     132                 :          1 :     const char *inptr = input;
     133                 :          1 :     size_t inbytesleft = 1;
     134                 :          1 :     char *outptr = buf;
     135                 :          1 :     size_t outbytesleft = sizeof (buf);
     136                 :          1 :     size_t res = iconv (cd_utf8_to_88591,
     137                 :            :                         (ICONV_CONST char **) &inptr, &inbytesleft,
     138                 :            :                         &outptr, &outbytesleft);
     139 [ +  - ][ -  + ]:          1 :     ASSERT (res == (size_t)(-1) && errno == EINVAL);
     140 [ +  - ][ -  + ]:          1 :     ASSERT (inbytesleft == 1 && outptr == buf);
     141                 :            :   }
     142                 :            : 
     143                 :          1 :   iconv_close (cd_88591_to_utf8);
     144                 :          1 :   iconv_close (cd_utf8_to_88591);
     145                 :            : #endif
     146                 :            : 
     147                 :          1 :   return 0;
     148                 :            : }

Generated by: LCOV version 1.9