Blame gnulib-tests/test-iconv.c

Packit Service fdd496
/* Test of character set conversion.
Packit Service fdd496
   Copyright (C) 2007-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
Packit Service fdd496
Packit Service fdd496
#include <config.h>
Packit Service fdd496
Packit Service fdd496
#if HAVE_ICONV
Packit Service fdd496
# include <iconv.h>
Packit Service fdd496
Packit Service fdd496
# ifndef ICONV_CONST
Packit Service fdd496
#  define ICONV_CONST /* empty */
Packit Service fdd496
# endif
Packit Service fdd496
Packit Service fdd496
#include "signature.h"
Packit Service fdd496
SIGNATURE_CHECK (iconv, size_t, (iconv_t, ICONV_CONST char **, size_t *,
Packit Service fdd496
                                 char **, size_t *));
Packit Service fdd496
SIGNATURE_CHECK (iconv_close, int, (iconv_t x));
Packit Service fdd496
SIGNATURE_CHECK (iconv_open, iconv_t, (char const *, char const *));
Packit Service fdd496
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#include <errno.h>
Packit Service fdd496
#include <string.h>
Packit Service fdd496
Packit Service fdd496
#include "macros.h"
Packit Service fdd496
Packit Service fdd496
int
Packit Service fdd496
main ()
Packit Service fdd496
{
Packit Service fdd496
#if HAVE_ICONV
Packit Service fdd496
  /* Assume that iconv() supports at least the encodings ASCII, ISO-8859-1,
Packit Service fdd496
     and UTF-8.  */
Packit Service fdd496
  iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO8859-1");
Packit Service fdd496
  iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
Packit Service fdd496
Packit Service fdd496
#if defined __MVS__ && defined __IBMC__
Packit Service fdd496
  /* String literals below are in ASCII, not EBCDIC.  */
Packit Service fdd496
# pragma convert("ISO8859-1")
Packit Service fdd496
# define CONVERT_ENABLED
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
  ASSERT (cd_88591_to_utf8 != (iconv_t)(-1));
Packit Service fdd496
  ASSERT (cd_utf8_to_88591 != (iconv_t)(-1));
Packit Service fdd496
Packit Service fdd496
  /* Test conversion from ISO-8859-1 to UTF-8 with no errors.  */
Packit Service fdd496
  {
Packit Service fdd496
    static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
Packit Service fdd496
    static const char expected[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
Packit Service fdd496
    char buf[50];
Packit Service fdd496
    const char *inptr = input;
Packit Service fdd496
    size_t inbytesleft = strlen (input);
Packit Service fdd496
    char *outptr = buf;
Packit Service fdd496
    size_t outbytesleft = sizeof (buf);
Packit Service fdd496
    size_t res = iconv (cd_88591_to_utf8,
Packit Service fdd496
                        (ICONV_CONST char **) &inptr, &inbytesleft,
Packit Service fdd496
                        &outptr, &outbytesleft);
Packit Service fdd496
    ASSERT (res == 0 && inbytesleft == 0);
Packit Service fdd496
    ASSERT (outptr == buf + strlen (expected));
Packit Service fdd496
    ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  /* Test conversion from ISO-8859-1 to UTF-8 with E2BIG.  */
Packit Service fdd496
  {
Packit Service fdd496
    static const char input[] = "\304";
Packit Service fdd496
    static char buf[2] = { (char)0xDE, (char)0xAD };
Packit Service fdd496
    const char *inptr = input;
Packit Service fdd496
    size_t inbytesleft = 1;
Packit Service fdd496
    char *outptr = buf;
Packit Service fdd496
    size_t outbytesleft = 1;
Packit Service fdd496
    size_t res = iconv (cd_88591_to_utf8,
Packit Service fdd496
                        (ICONV_CONST char **) &inptr, &inbytesleft,
Packit Service fdd496
                        &outptr, &outbytesleft);
Packit Service fdd496
    ASSERT (res == (size_t)(-1) && errno == E2BIG);
Packit Service fdd496
    ASSERT (inbytesleft == 1);
Packit Service fdd496
    ASSERT (outbytesleft == 1);
Packit Service fdd496
    ASSERT ((unsigned char) buf[1] == 0xAD);
Packit Service fdd496
    ASSERT ((unsigned char) buf[0] == 0xDE);
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  /* Test conversion from UTF-8 to ISO-8859-1 with no errors.  */
Packit Service fdd496
  {
Packit Service fdd496
    static const char input[] = "\303\204rger mit b\303\266sen B\303\274bchen ohne Augenma\303\237";
Packit Service fdd496
    static const char expected[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
Packit Service fdd496
    char buf[50];
Packit Service fdd496
    const char *inptr = input;
Packit Service fdd496
    size_t inbytesleft = strlen (input);
Packit Service fdd496
    char *outptr = buf;
Packit Service fdd496
    size_t outbytesleft = sizeof (buf);
Packit Service fdd496
    size_t res = iconv (cd_utf8_to_88591,
Packit Service fdd496
                        (ICONV_CONST char **) &inptr, &inbytesleft,
Packit Service fdd496
                        &outptr, &outbytesleft);
Packit Service fdd496
    ASSERT (res == 0 && inbytesleft == 0);
Packit Service fdd496
    ASSERT (outptr == buf + strlen (expected));
Packit Service fdd496
    ASSERT (memcmp (buf, expected, strlen (expected)) == 0);
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  /* Test conversion from UTF-8 to ISO-8859-1 with EILSEQ.  */
Packit Service fdd496
  {
Packit Service fdd496
    static const char input[] = "\342\202\254"; /* EURO SIGN */
Packit Service fdd496
    char buf[10];
Packit Service fdd496
    const char *inptr = input;
Packit Service fdd496
    size_t inbytesleft = strlen (input);
Packit Service fdd496
    char *outptr = buf;
Packit Service fdd496
    size_t outbytesleft = sizeof (buf);
Packit Service fdd496
    size_t res = iconv (cd_utf8_to_88591,
Packit Service fdd496
                        (ICONV_CONST char **) &inptr, &inbytesleft,
Packit Service fdd496
                        &outptr, &outbytesleft);
Packit Service fdd496
    if (res == (size_t)(-1))
Packit Service fdd496
      {
Packit Service fdd496
        ASSERT (errno == EILSEQ);
Packit Service fdd496
        ASSERT (inbytesleft == strlen (input) && outptr == buf);
Packit Service fdd496
      }
Packit Service fdd496
    else
Packit Service fdd496
      {
Packit Service fdd496
        ASSERT (res == 1);
Packit Service fdd496
        ASSERT (inbytesleft == 0);
Packit Service fdd496
      }
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  /* Test conversion from UTF-8 to ISO-8859-1 with EINVAL.  */
Packit Service fdd496
  {
Packit Service fdd496
    static const char input[] = "\342";
Packit Service fdd496
    char buf[10];
Packit Service fdd496
    const char *inptr = input;
Packit Service fdd496
    size_t inbytesleft = 1;
Packit Service fdd496
    char *outptr = buf;
Packit Service fdd496
    size_t outbytesleft = sizeof (buf);
Packit Service fdd496
    size_t res = iconv (cd_utf8_to_88591,
Packit Service fdd496
                        (ICONV_CONST char **) &inptr, &inbytesleft,
Packit Service fdd496
                        &outptr, &outbytesleft);
Packit Service fdd496
    ASSERT (res == (size_t)(-1) && errno == EINVAL);
Packit Service fdd496
    ASSERT (inbytesleft == 1 && outptr == buf);
Packit Service fdd496
  }
Packit Service fdd496
Packit Service fdd496
  iconv_close (cd_88591_to_utf8);
Packit Service fdd496
  iconv_close (cd_utf8_to_88591);
Packit Service fdd496
Packit Service fdd496
#ifdef CONVERT_ENABLED
Packit Service fdd496
# pragma convert(pop)
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#endif /* HAVE_ICONV */
Packit Service fdd496
Packit Service fdd496
  return 0;
Packit Service fdd496
}