1 /* Verify the BIG5HKSCS outputs that generate 2 wchar_t's (Bug 25734).
2 Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
23 #include <support/check.h>
24 #include <support/support.h>
26 /* A few BIG5-HKSCS characters map in two unicode code points.
28 /x88/x62 => <U00CA><U0304>
29 /x88/x64 => <U00CA><U030C>
30 /x88/xa3 => <U00EA><U0304>
31 /x88/xa5 => <U00EA><U030C>
32 Each of these is special cased in iconvdata/big5hkscs.c.
33 This test ensures that we correctly reset the shift state after
34 outputting any of these characters. We do this by converting
35 each them followed by converting an ASCII character. If we fail
36 to reset the shift state (bug 25734) then we'll see the last
37 character in the queue output again. */
39 /* Each test has name, input bytes, and expected wide character
47 /* In BIG5-HKSCS (2008) there are 4 characters that generate multiple
49 struct testdata tests
[4] = {
50 /* <H-8862>X => <U+00CA><U+0304>X */
51 { "<H-8862>", "\x88\x62\x58", { 0x00CA, 0x0304, 0x0058 } },
52 /* <H-8864>X => <U+00CA><U+030C>X */
53 { "<H-8864>", "\x88\x64\x58", { 0x00CA, 0x030C, 0x0058 } },
54 /* <H-88A3>X => <U+00EA><U+0304>X */
55 { "<H-88A3>", "\x88\xa3\x58", { 0x00EA, 0x0304, 0x0058 } },
56 /* <H-88A5>X => <U+00EA><U+030C>X */
57 { "<H-88A5>", "\x88\xa5\x58", { 0x00EA, 0x030C, 0x0058 } }
60 /* Each test is of the form:
61 - Translate first code sequence (two bytes)
62 - Translate second (zero bytes)
63 - Translate the third (one byte). */
65 check_conversion (struct testdata test
)
71 const char *mbs
= test
.input
;
73 /* Input is always 3 bytes long. */
76 memset (&st
, 0, sizeof (st
));
77 /* First conversion: Consumes first 2 bytes. */
78 ret
= mbrtowc (&wc
, mbs
, inlen
- consumed
, &st
);
81 printf ("error: First conversion consumed only %zd bytes.\n", ret
);
84 /* Advance the two consumed bytes. */
87 if (wc
!= test
.expected
[0])
89 printf ("error: Result of first conversion was wrong.\n");
92 /* Second conversion: Consumes 0 bytes. */
93 ret
= mbrtowc (&wc
, mbs
, inlen
- consumed
, &st
);
96 printf ("error: Second conversion consumed only %zd bytes.\n", ret
);
99 /* Advance the zero consumed bytes. */
102 if (wc
!= test
.expected
[1])
104 printf ("error: Result of second conversion was wrong.\n");
107 /* After the second conversion the state of the converter should be
108 in the initial state. It is in the initial state because the two
109 input BIG5-HKSCS bytes have been consumed and the 2 wchar_t's have
111 if (mbsinit (&st
) == 0)
113 printf ("error: Converter not in initial state.\n");
116 /* Third conversion: Consumes 1 byte (it's an ASCII character). */
117 ret
= mbrtowc (&wc
, mbs
, inlen
- consumed
, &st
);
120 printf ("error: Third conversion consumed only %zd bytes.\n", ret
);
123 /* Advance the one byte. */
126 if (wc
!= test
.expected
[2])
128 printf ("error: Result of third conversion was wrong.\n");
132 /* Now perform the same test as above consuming one byte at a time. */
134 memset (&st
, 0, sizeof (st
));
136 /* Consume the first byte; expect an incomplete multibyte character. */
137 ret
= mbrtowc (&wc
, mbs
, 1, &st
);
140 printf ("error: First byte conversion returned %zd.\n", ret
);
143 /* Advance past the first consumed byte. */
145 /* Consume the second byte; expect the first wchar_t. */
146 ret
= mbrtowc (&wc
, mbs
, 1, &st
);
149 printf ("error: Second byte conversion returned %zd.\n", ret
);
152 /* Advance past the second consumed byte. */
154 if (wc
!= test
.expected
[0])
156 printf ("error: Result of first wchar_t conversion was wrong.\n");
159 /* Consume no bytes; expect the second wchar_t. */
160 ret
= mbrtowc (&wc
, mbs
, 1, &st
);
163 printf ("error: First attempt of third byte conversion returned %zd.\n", ret
);
166 /* Do not advance past the third byte. */
168 if (wc
!= test
.expected
[1])
170 printf ("error: Result of second wchar_t conversion was wrong.\n");
173 /* After the second wchar_t conversion, the converter should be in
174 the initial state since the two input BIG5-HKSCS bytes have been
175 consumed and the two wchar_t's have been output. */
176 if (mbsinit (&st
) == 0)
178 printf ("error: Converter not in initial state.\n");
181 /* Consume the third byte; expect the third wchar_t. */
182 ret
= mbrtowc (&wc
, mbs
, 1, &st
);
185 printf ("error: Third byte conversion returned %zd.\n", ret
);
188 /* Advance past the third consumed byte. */
190 if (wc
!= test
.expected
[2])
192 printf ("error: Result of third wchar_t conversion was wrong.\n");
196 /* Return 0 if we saw no errors. */
205 /* Testing BIG5-HKSCS. */
206 xsetlocale (LC_ALL
, "zh_HK.BIG5-HKSCS");
208 /* Run all the special conversions. */
209 for (int i
= 0; i
< (sizeof (tests
) / sizeof (struct testdata
)); i
++)
211 printf ("Running test for %s\n", tests
[i
].name
);
212 ret
= check_conversion (tests
[i
]);
214 printf ("Test %s failed.\n", tests
[i
].name
);
218 /* Fail if any conversion had an error. */
220 FAIL_EXIT1 ("One or more conversions failed.");
225 #include <support/test-driver.c>