1
/* Copyright (C) 2024 Free Software Foundation, Inc.
2
This file is part of the GNU LIBICONV Library.
4
The GNU LIBICONV Library is free software; you can redistribute it
5
and/or modify it under the terms of the GNU Lesser General Public
6
License as published by the Free Software Foundation; either version 2.1
7
of the License, or (at your option) any later version.
9
The GNU LIBICONV Library is distributed in the hope that it will be
10
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public
15
License along with the GNU LIBICONV Library; see the file COPYING.LIB.
16
If not, see <https://www.gnu.org/licenses/>. */
18
/* Comment out this line, to build against glibc instead of libiconv. */
26
/* This test checks the behaviour of iconv() with suffixes //IGNORE and
27
//NON_IDENTICAL_DISCARD, and also the equivalent options set through
30
static const char input1[7] = "3\xd4\xe2\x84\x83\xc3\x9f"; /* "3<D4>℃ß" */
31
static const char input2[7] = "3\xe2\x84\x83\xd4\xc3\x9f"; /* "3℃<D4>ß" */
33
static void test_default (iconv_t cd)
37
char *inbuf = (char *) input1;
38
size_t inbytesleft = sizeof (input1);
39
char *outbuf = output;
40
size_t outbytesleft = sizeof (output);
41
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
42
if (!(ret == (size_t)(-1) && errno == EILSEQ && sizeof (input1) - inbytesleft == 1))
44
if (!(sizeof (output) - outbytesleft == 1
50
char *inbuf = (char *) input2;
51
size_t inbytesleft = sizeof (input2);
52
char *outbuf = output;
53
size_t outbytesleft = sizeof (output);
54
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
55
if (!(ret == (size_t)(-1) && errno == EILSEQ && sizeof (input2) - inbytesleft == 1))
57
if (!(sizeof (output) - outbytesleft == 1
61
#ifdef _LIBICONV_VERSION
63
if (iconvctl (cd, ICONV_GET_TRANSLITERATE, &x) != 0)
67
if (iconvctl (cd, ICONV_GET_DISCARD_INVALID, &x) != 0)
71
if (iconvctl (cd, ICONV_GET_DISCARD_NON_IDENTICAL, &x) != 0)
75
if (iconvctl (cd, ICONV_GET_DISCARD_ILSEQ, &x) != 0)
82
static void test_translit (iconv_t cd)
86
char *inbuf = (char *) input1;
87
size_t inbytesleft = sizeof (input1);
88
char *outbuf = output;
89
size_t outbytesleft = sizeof (output);
90
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
91
if (!(ret == (size_t)(-1) && errno == EILSEQ && sizeof (input1) - inbytesleft == 1))
93
if (!(sizeof (output) - outbytesleft == 1
99
char *inbuf = (char *) input2;
100
size_t inbytesleft = sizeof (input2);
101
char *outbuf = output;
102
size_t outbytesleft = sizeof (output);
103
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
104
if (!(ret == (size_t)(-1) && errno == EILSEQ && sizeof (input2) - inbytesleft == 4))
106
if (!(sizeof (output) - outbytesleft == 3
107
&& output[0] == '3' && output[1] == '\xb0' && output[2] == 'C'))
110
#ifdef _LIBICONV_VERSION
112
if (iconvctl (cd, ICONV_GET_TRANSLITERATE, &x) != 0)
116
if (iconvctl (cd, ICONV_GET_DISCARD_INVALID, &x) != 0)
120
if (iconvctl (cd, ICONV_GET_DISCARD_NON_IDENTICAL, &x) != 0)
124
if (iconvctl (cd, ICONV_GET_DISCARD_ILSEQ, &x) != 0)
131
static void test_ignore (iconv_t cd)
135
char *inbuf = (char *) input1;
136
size_t inbytesleft = sizeof (input1);
137
char *outbuf = output;
138
size_t outbytesleft = sizeof (output);
139
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
140
#ifdef _LIBICONV_VERSION
141
if (!(ret == 1 && inbytesleft == 0))
144
if (!(ret == (size_t)(-1) && errno == EILSEQ && inbytesleft == 0))
147
if (!(sizeof (output) - outbytesleft == 2
148
&& output[0] == '3' && output[1] == '\xdf'))
153
char *inbuf = (char *) input2;
154
size_t inbytesleft = sizeof (input2);
155
char *outbuf = output;
156
size_t outbytesleft = sizeof (output);
157
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
158
#ifdef _LIBICONV_VERSION
159
if (!(ret == 1 && inbytesleft == 0))
162
if (!(ret == (size_t)(-1) && errno == EILSEQ && inbytesleft == 0))
165
if (!(sizeof (output) - outbytesleft == 2
166
&& output[0] == '3' && output[1] == '\xdf'))
169
#ifdef _LIBICONV_VERSION
171
if (iconvctl (cd, ICONV_GET_TRANSLITERATE, &x) != 0)
175
if (iconvctl (cd, ICONV_GET_DISCARD_INVALID, &x) != 0)
179
if (iconvctl (cd, ICONV_GET_DISCARD_NON_IDENTICAL, &x) != 0)
183
if (iconvctl (cd, ICONV_GET_DISCARD_ILSEQ, &x) != 0)
190
static void test_ignore_translit (iconv_t cd)
194
char *inbuf = (char *) input1;
195
size_t inbytesleft = sizeof (input1);
196
char *outbuf = output;
197
size_t outbytesleft = sizeof (output);
198
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
199
if (!(ret == 1 && inbytesleft == 0))
201
if (!(sizeof (output) - outbytesleft == 4
202
&& output[0] == '3' && output[1] == '\xb0' && output[2] == 'C' && output[3] == '\xdf'))
207
char *inbuf = (char *) input2;
208
size_t inbytesleft = sizeof (input2);
209
char *outbuf = output;
210
size_t outbytesleft = sizeof (output);
211
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
212
if (!(ret == 1 && inbytesleft == 0))
214
if (!(sizeof (output) - outbytesleft == 4
215
&& output[0] == '3' && output[1] == '\xb0' && output[2] == 'C' && output[3] == '\xdf'))
218
#ifdef _LIBICONV_VERSION
220
if (iconvctl (cd, ICONV_GET_TRANSLITERATE, &x) != 0)
224
if (iconvctl (cd, ICONV_GET_DISCARD_INVALID, &x) != 0)
228
if (iconvctl (cd, ICONV_GET_DISCARD_NON_IDENTICAL, &x) != 0)
232
if (iconvctl (cd, ICONV_GET_DISCARD_ILSEQ, &x) != 0)
239
static void test_nid (iconv_t cd)
243
char *inbuf = (char *) input1;
244
size_t inbytesleft = sizeof (input1);
245
char *outbuf = output;
246
size_t outbytesleft = sizeof (output);
247
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
248
if (!(ret == (size_t)(-1) && errno == EILSEQ && sizeof (input1) - inbytesleft == 1))
250
if (!(sizeof (output) - outbytesleft == 1
251
&& output[0] == '3'))
256
char *inbuf = (char *) input2;
257
size_t inbytesleft = sizeof (input2);
258
char *outbuf = output;
259
size_t outbytesleft = sizeof (output);
260
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
261
if (!(ret == (size_t)(-1) && errno == EILSEQ && sizeof (input2) - inbytesleft == 4))
263
if (!(sizeof (output) - outbytesleft == 1
264
&& output[0] == '3'))
267
#ifdef _LIBICONV_VERSION
269
if (iconvctl (cd, ICONV_GET_TRANSLITERATE, &x) != 0)
273
if (iconvctl (cd, ICONV_GET_DISCARD_INVALID, &x) != 0)
277
if (iconvctl (cd, ICONV_GET_DISCARD_NON_IDENTICAL, &x) != 0)
281
if (iconvctl (cd, ICONV_GET_DISCARD_ILSEQ, &x) != 0)
288
static void test_nid_translit (iconv_t cd)
292
char *inbuf = (char *) input1;
293
size_t inbytesleft = sizeof (input1);
294
char *outbuf = output;
295
size_t outbytesleft = sizeof (output);
296
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
297
if (!(ret == (size_t)(-1) && errno == EILSEQ && sizeof (input1) - inbytesleft == 1))
299
if (!(sizeof (output) - outbytesleft == 1
300
&& output[0] == '3'))
305
char *inbuf = (char *) input2;
306
size_t inbytesleft = sizeof (input2);
307
char *outbuf = output;
308
size_t outbytesleft = sizeof (output);
309
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
310
if (!(ret == (size_t)(-1) && errno == EILSEQ && sizeof (input2) - inbytesleft == 4))
312
if (!(sizeof (output) - outbytesleft == 3
313
&& output[0] == '3' && output[1] == '\xb0' && output[2] == 'C'))
316
#ifdef _LIBICONV_VERSION
318
if (iconvctl (cd, ICONV_GET_TRANSLITERATE, &x) != 0)
322
if (iconvctl (cd, ICONV_GET_DISCARD_INVALID, &x) != 0)
326
if (iconvctl (cd, ICONV_GET_DISCARD_NON_IDENTICAL, &x) != 0)
330
if (iconvctl (cd, ICONV_GET_DISCARD_ILSEQ, &x) != 0)
337
static void test_invd (iconv_t cd)
341
char *inbuf = (char *) input1;
342
size_t inbytesleft = sizeof (input1);
343
char *outbuf = output;
344
size_t outbytesleft = sizeof (output);
345
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
346
if (!(ret == (size_t)(-1) && errno == EILSEQ && sizeof (input1) - inbytesleft == 2))
348
if (!(sizeof (output) - outbytesleft == 1
349
&& output[0] == '3'))
354
char *inbuf = (char *) input2;
355
size_t inbytesleft = sizeof (input2);
356
char *outbuf = output;
357
size_t outbytesleft = sizeof (output);
358
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
359
if (!(ret == (size_t)(-1) && errno == EILSEQ && sizeof (input1) - inbytesleft == 1))
361
if (!(sizeof (output) - outbytesleft == 1
362
&& output[0] == '3'))
365
#ifdef _LIBICONV_VERSION
367
if (iconvctl (cd, ICONV_GET_TRANSLITERATE, &x) != 0)
371
if (iconvctl (cd, ICONV_GET_DISCARD_INVALID, &x) != 0)
375
if (iconvctl (cd, ICONV_GET_DISCARD_NON_IDENTICAL, &x) != 0)
379
if (iconvctl (cd, ICONV_GET_DISCARD_ILSEQ, &x) != 0)
386
static void test_invd_translit (iconv_t cd)
390
char *inbuf = (char *) input1;
391
size_t inbytesleft = sizeof (input1);
392
char *outbuf = output;
393
size_t outbytesleft = sizeof (output);
394
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
395
if (!(ret == 1 && inbytesleft == 0))
397
if (!(sizeof (output) - outbytesleft == 4
398
&& output[0] == '3' && output[1] == '\xb0' && output[2] == 'C' && output[3] == '\xdf'))
403
char *inbuf = (char *) input2;
404
size_t inbytesleft = sizeof (input2);
405
char *outbuf = output;
406
size_t outbytesleft = sizeof (output);
407
size_t ret = iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
408
if (!(ret == 1 && inbytesleft == 0))
410
if (!(sizeof (output) - outbytesleft == 4
411
&& output[0] == '3' && output[1] == '\xb0' && output[2] == 'C' && output[3] == '\xdf'))
414
#ifdef _LIBICONV_VERSION
416
if (iconvctl (cd, ICONV_GET_TRANSLITERATE, &x) != 0)
420
if (iconvctl (cd, ICONV_GET_DISCARD_INVALID, &x) != 0)
424
if (iconvctl (cd, ICONV_GET_DISCARD_NON_IDENTICAL, &x) != 0)
428
if (iconvctl (cd, ICONV_GET_DISCARD_ILSEQ, &x) != 0)
437
#ifndef _LIBICONV_VERSION
438
/* For glibc: Enable locale-dependent transliterations. */
439
setlocale (LC_ALL, "en_US.UTF-8");
443
iconv_t cd = iconv_open ("ISO-8859-1", "UTF-8");
449
iconv_t cd = iconv_open ("ISO-8859-1//TRANSLIT", "UTF-8");
453
#ifdef _LIBICONV_VERSION
455
iconv_t cd = iconv_open ("ISO-8859-1", "UTF-8");
456
{ int x = 1; iconvctl (cd, ICONV_SET_TRANSLITERATE, &x); }
463
iconv_t cd = iconv_open ("ISO-8859-1//IGNORE", "UTF-8");
467
#ifdef _LIBICONV_VERSION
469
iconv_t cd = iconv_open ("ISO-8859-1", "UTF-8");
470
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_ILSEQ, &x); }
476
iconv_t cd = iconv_open ("ISO-8859-1//NON_IDENTICAL_DISCARD//IGNORE", "UTF-8");
481
iconv_t cd = iconv_open ("ISO-8859-1//IGNORE//NON_IDENTICAL_DISCARD", "UTF-8");
485
#ifdef _LIBICONV_VERSION
487
iconv_t cd = iconv_open ("ISO-8859-1", "UTF-8");
488
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_ILSEQ, &x); }
489
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_NON_IDENTICAL, &x); }
494
#ifdef _LIBICONV_VERSION
496
iconv_t cd = iconv_open ("ISO-8859-1", "UTF-8");
497
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_INVALID, &x); }
498
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_NON_IDENTICAL, &x); }
505
iconv_t cd = iconv_open ("ISO-8859-1//IGNORE//TRANSLIT", "UTF-8");
506
test_ignore_translit (cd);
510
iconv_t cd = iconv_open ("ISO-8859-1//TRANSLIT//IGNORE", "UTF-8");
511
test_ignore_translit (cd);
514
#ifdef _LIBICONV_VERSION
516
iconv_t cd = iconv_open ("ISO-8859-1", "UTF-8");
517
{ int x = 1; iconvctl (cd, ICONV_SET_TRANSLITERATE, &x); }
518
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_ILSEQ, &x); }
519
test_ignore_translit (cd);
524
iconv_t cd = iconv_open ("ISO-8859-1//NON_IDENTICAL_DISCARD//IGNORE//TRANSLIT", "UTF-8");
525
test_ignore_translit (cd);
529
iconv_t cd = iconv_open ("ISO-8859-1//IGNORE//NON_IDENTICAL_DISCARD//TRANSLIT", "UTF-8");
530
test_ignore_translit (cd);
534
iconv_t cd = iconv_open ("ISO-8859-1//NON_IDENTICAL_DISCARD//TRANSLIT//IGNORE", "UTF-8");
535
test_ignore_translit (cd);
539
iconv_t cd = iconv_open ("ISO-8859-1//IGNORE//TRANSLIT//NON_IDENTICAL_DISCARD", "UTF-8");
540
test_ignore_translit (cd);
544
iconv_t cd = iconv_open ("ISO-8859-1//TRANSLIT//NON_IDENTICAL_DISCARD//IGNORE", "UTF-8");
545
test_ignore_translit (cd);
549
iconv_t cd = iconv_open ("ISO-8859-1//TRANSLIT//IGNORE//NON_IDENTICAL_DISCARD", "UTF-8");
550
test_ignore_translit (cd);
553
#ifdef _LIBICONV_VERSION
555
iconv_t cd = iconv_open ("ISO-8859-1", "UTF-8");
556
{ int x = 1; iconvctl (cd, ICONV_SET_TRANSLITERATE, &x); }
557
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_INVALID, &x); }
558
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_NON_IDENTICAL, &x); }
559
test_ignore_translit (cd);
565
iconv_t cd = iconv_open ("ISO-8859-1//NON_IDENTICAL_DISCARD", "UTF-8");
569
#ifdef _LIBICONV_VERSION
571
iconv_t cd = iconv_open ("ISO-8859-1", "UTF-8");
572
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_NON_IDENTICAL, &x); }
579
iconv_t cd = iconv_open ("ISO-8859-1//NON_IDENTICAL_DISCARD//TRANSLIT", "UTF-8");
580
test_nid_translit (cd);
584
iconv_t cd = iconv_open ("ISO-8859-1//TRANSLIT//NON_IDENTICAL_DISCARD", "UTF-8");
585
test_nid_translit (cd);
588
#ifdef _LIBICONV_VERSION
590
iconv_t cd = iconv_open ("ISO-8859-1", "UTF-8");
591
{ int x = 1; iconvctl (cd, ICONV_SET_TRANSLITERATE, &x); }
592
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_NON_IDENTICAL, &x); }
593
test_nid_translit (cd);
598
#ifdef _LIBICONV_VERSION
600
iconv_t cd = iconv_open ("ISO-8859-1", "UTF-8");
601
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_INVALID, &x); }
607
#ifdef _LIBICONV_VERSION
609
iconv_t cd = iconv_open ("ISO-8859-1", "UTF-8");
610
{ int x = 1; iconvctl (cd, ICONV_SET_TRANSLITERATE, &x); }
611
{ int x = 1; iconvctl (cd, ICONV_SET_DISCARD_INVALID, &x); }
612
test_invd_translit (cd);