1 | // BZ 12814 |
2 | #include <errno.h> |
3 | #include <iconv.h> |
4 | #include <stdio.h> |
5 | #include <string.h> |
6 | |
7 | static int |
8 | do_test (void) |
9 | { |
10 | iconv_t h = iconv_open (tocode: "ISO-2022-JP-2" , fromcode: "UTF-8" ); |
11 | if (h == (iconv_t) -1) |
12 | { |
13 | printf (format: "cannot load iconv module: %m\n" ); |
14 | return 1; |
15 | } |
16 | |
17 | // Euro sign |
18 | static const char inbuf[] = "\xe2\x82\xac" ; |
19 | char *in = (char *) inbuf; |
20 | size_t inlen = sizeof (inbuf) - 1; |
21 | |
22 | char outbuf[100]; |
23 | char *out = outbuf; |
24 | size_t outlen = sizeof (outbuf); |
25 | |
26 | int res = 0; |
27 | size_t n = iconv (cd: h, inbuf: &in, inbytesleft: &inlen, outbuf: &out, outbytesleft: &outlen); |
28 | if (n == (size_t) -1) |
29 | { |
30 | printf (format: "iconv failed with %d: %m\n" , errno); |
31 | return 1; |
32 | } |
33 | if (n != 0) |
34 | { |
35 | printf (format: "iconv returned %zu, expected zero\n" , n); |
36 | res = 1; |
37 | } |
38 | if (in != inbuf + sizeof (inbuf) - 1) |
39 | { |
40 | printf (format: "in advanced by %td, expected %zu\n" , |
41 | in - inbuf, sizeof (inbuf) - 1); |
42 | res = 1; |
43 | } |
44 | static const char expected[] = "\x1b\x2e\x46\x1b\x4e\x24" ; |
45 | if (out - outbuf != sizeof (expected) - 1 |
46 | || memcmp (s1: outbuf, s2: expected, n: sizeof (expected) - 1) != 0) |
47 | { |
48 | fputs (s: "generated sequence is: \"" , stdout); |
49 | for (size_t i = 0; i < out - outbuf; ++i) |
50 | printf (format: "\\x%02hhx" , outbuf[i]); |
51 | fputs (s: "\", expected \"" , stdout); |
52 | for (size_t i = 0; i < sizeof (expected) - 1; ++i) |
53 | printf (format: "\\x%02hhx" , expected[i]); |
54 | puts (s: "\"" ); |
55 | res = 1; |
56 | } |
57 | |
58 | if (iconv_close (cd: h) != 0) |
59 | { |
60 | puts (s: "failed closing iconv module" ); |
61 | res = 1; |
62 | } |
63 | |
64 | return res; |
65 | } |
66 | |
67 | #define TEST_FUNCTION do_test () |
68 | #include "../test-skeleton.c" |
69 | |