1/* Testing ucs4le_internal_loop() in gconv_simple.c.
2 Copyright (C) 2016-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <stdio.h>
20#include <errno.h>
21#include <string.h>
22#include <inttypes.h>
23#include <iconv.h>
24#include <byteswap.h>
25#include <endian.h>
26
27static int
28do_test (void)
29{
30 iconv_t cd;
31 char *inptr;
32 size_t inlen;
33 char *outptr;
34 size_t outlen;
35 size_t n;
36 int e;
37 int result = 0;
38
39#if __BYTE_ORDER == __BIG_ENDIAN
40 /* On big-endian machines, ucs4le_internal_loop() swaps the bytes before
41 error checking. Thus the input values has to be swapped. */
42# define VALUE(val) bswap_32 (val)
43#else
44# define VALUE(val) val
45#endif
46 uint32_t inbuf[3] = { VALUE (0x41), VALUE (0x80000000), VALUE (0x42) };
47 uint32_t outbuf[3] = { 0, 0, 0 };
48
49 cd = iconv_open (tocode: "WCHAR_T", fromcode: "UCS-4LE");
50 if (cd == (iconv_t) -1)
51 {
52 printf (format: "cannot convert from UCS4LE to wchar_t: %m\n");
53 return 1;
54 }
55
56 inptr = (char *) inbuf;
57 inlen = sizeof (inbuf);
58 outptr = (char *) outbuf;
59 outlen = sizeof (outbuf);
60
61 n = iconv (cd: cd, inbuf: &inptr, inbytesleft: &inlen, outbuf: &outptr, outbytesleft: &outlen);
62 e = errno;
63
64 if (n != (size_t) -1)
65 {
66 printf (format: "incorrect iconv() return value: %zd, expected -1\n", n);
67 result = 1;
68 }
69
70 if (e != EILSEQ)
71 {
72 printf (format: "incorrect error value: %s, expected %s\n",
73 strerror (errnum: e), strerror (EILSEQ));
74 result = 1;
75 }
76
77 if (inptr != (char *) &inbuf[1])
78 {
79 printf (format: "inptr=0x%p does not point to invalid character! Expected=0x%p\n"
80 , inptr, &inbuf[1]);
81 result = 1;
82 }
83
84 if (inlen != sizeof (inbuf) - sizeof (uint32_t))
85 {
86 printf (format: "inlen=%zd != %zd\n"
87 , inlen, sizeof (inbuf) - sizeof (uint32_t));
88 result = 1;
89 }
90
91 if (outptr != (char *) &outbuf[1])
92 {
93 printf (format: "outptr=0x%p does not point to invalid character in inbuf! "
94 "Expected=0x%p\n"
95 , outptr, &outbuf[1]);
96 result = 1;
97 }
98
99 if (outlen != sizeof (inbuf) - sizeof (uint32_t))
100 {
101 printf (format: "outlen=%zd != %zd\n"
102 , outlen, sizeof (outbuf) - sizeof (uint32_t));
103 result = 1;
104 }
105
106 if (outbuf[0] != 0x41 || outbuf[1] != 0 || outbuf[2] != 0)
107 {
108 puts (s: "Characters conversion is incorrect!");
109 result = 1;
110 }
111
112 iconv_close (cd: cd);
113
114 return result;
115}
116
117#define TEST_FUNCTION do_test ()
118#include "../test-skeleton.c"
119

source code of glibc/iconv/tst-iconv6.c