Blame iconvdata/tst-e2big.c

Packit 6c4009
/* Test for a tricky E2BIG situation.
Packit 6c4009
   Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Bruno Haible <bruno@clisp.org>, 2002.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <alloca.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <iconv.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
/* In EUC-JISX0213 and TSCII, a single input character can convert to
Packit 6c4009
   a sequence of two or more Unicode characters.  When the output buffer
Packit 6c4009
   has room for less Unicode characters than would be produced with an
Packit 6c4009
   unconstrained output buffer, the conversion must give errno = E2BIG.  */
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
test (const char *encoding, char *inbuf, size_t inbufsize, size_t outbufsize)
Packit 6c4009
{
Packit 6c4009
  char *outbuf = alloca (outbufsize);
Packit 6c4009
  iconv_t cd;
Packit 6c4009
  char *inptr;
Packit 6c4009
  size_t inlen;
Packit 6c4009
  char *outptr;
Packit 6c4009
  size_t outlen;
Packit 6c4009
  int result;
Packit 6c4009
  bool empty_input;
Packit 6c4009
  bool empty_output;
Packit 6c4009
Packit 6c4009
  cd = iconv_open ("UTF-8", encoding);
Packit 6c4009
  if (cd == (iconv_t) -1)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "cannot convert from %s\n", encoding);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  inptr = inbuf;
Packit 6c4009
  inlen = inbufsize;
Packit 6c4009
  outptr = outbuf;
Packit 6c4009
  outlen = outbufsize;
Packit 6c4009
Packit 6c4009
  result = iconv (cd, &inptr, &inlen, &outptr, &outlen);
Packit 6c4009
  if (!(result == -1 && errno == E2BIG))
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s: wrong iconv result: %d/%d (%m)\n",
Packit 6c4009
	       encoding, result, errno);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  empty_input = (inptr == inbuf && inlen == inbufsize);
Packit 6c4009
  empty_output = (outptr == outbuf && outlen == outbufsize);
Packit 6c4009
Packit 6c4009
  if (!empty_input && empty_output)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s: ate %td input bytes\n", encoding, inptr - inbuf);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
  if (empty_input && !empty_output)
Packit 6c4009
    {
Packit 6c4009
      fprintf (stderr, "%s: produced %td output bytes\n",
Packit 6c4009
	       encoding, outptr - outbuf);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  iconv_close (cd);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
test_euc_jisx0213 (void)
Packit 6c4009
{
Packit 6c4009
  char inbuf[2] = { 0xa4, 0xf7 };
Packit 6c4009
  test ("EUC-JISX0213", inbuf, sizeof (inbuf), 3);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
test_tscii (void)
Packit 6c4009
{
Packit 6c4009
  char inbuf[1] = { 0x82 };
Packit 6c4009
  test ("TSCII", inbuf, sizeof (inbuf), 3);
Packit 6c4009
  test ("TSCII", inbuf, sizeof (inbuf), 6);
Packit 6c4009
  test ("TSCII", inbuf, sizeof (inbuf), 9);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  test_euc_jisx0213 ();
Packit 6c4009
  test_tscii ();
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"