summary refs log tree commit diff
path: root/iconvdata/bug-iconv9.c
blob: 28f88d22e93277103bf7cec0bf5678d69761184b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// BZ 12814
#include <errno.h>
#include <iconv.h>
#include <stdio.h>
#include <string.h>

static int
do_test (void)
{
  iconv_t h = iconv_open ("ISO-2022-JP-2", "UTF-8");
  if (h == (iconv_t) -1)
    {
      printf ("cannot load iconv module: %m\n");
      return 1;
    }

  // Euro sign
  static const char inbuf[] = "\xe2\x82\xac";
  char *in = (char *) inbuf;
  size_t inlen = sizeof (inbuf) - 1;

  char outbuf[100];
  char *out = outbuf;
  size_t outlen = sizeof (outbuf);

  int res = 0;
  size_t n = iconv (h, &in, &inlen, &out, &outlen);
  if (n == (size_t) -1)
    {
      printf ("iconv failed with %d: %m\n", errno);
      return 1;
    }
  if (n != 0)
    {
      printf ("iconv returned %zu, expected zero\n", n);
      res = 1;
    }
  if (in != inbuf + sizeof (inbuf) - 1)
    {
      printf ("in advanced by %td, expected %zu\n",
	      in - inbuf, sizeof (inbuf) - 1);
      res = 1;
    }
  static const char expected[] = "\x1b\x2e\x46\x1b\x4e\x24";
  if (out - outbuf != sizeof (expected) - 1
      || memcmp (outbuf, expected, sizeof (expected) - 1) != 0)
    {
      fputs ("generated sequence is: \"", stdout);
      for (size_t i = 0; i < out - outbuf; ++i)
	printf ("\\x%02hhx", outbuf[i]);
      fputs ("\", expected \"", stdout);
      for (size_t i = 0; i < sizeof (expected) - 1; ++i)
	printf ("\\x%02hhx", expected[i]);
      puts ("\"");
      res = 1;
    }

  if (iconv_close (h) != 0)
    {
      puts ("failed closing iconv module");
      res = 1;
    }

  return res;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"