iconv regression test
[libc-test.git] / src / regression / iconv-roundtrips.c
blob279d25ba2cd19d6fc0a3f9ffa9432a36c1b25ef4
1 // commit: b7bfb5c3a8330002250f304cb5deb522fa054eae
2 // fix iconv conversions for iso88592-iso885916
3 #include <iconv.h>
4 #include <string.h>
5 #include "test.h"
7 int main(void)
9 static char *test_charsets[] = {
10 "iso-8859-1",
11 "iso-8859-2",
12 "iso-8859-4",
13 "iso-8859-5",
14 "iso-8859-9",
15 "iso-8859-10",
16 "iso-8859-13",
17 "iso-8859-14",
18 "iso-8859-15",
19 "iso-8859-16",
22 char all_codepoints[256];
23 int i;
25 for (i=0; i<256; i++)
26 all_codepoints[i] = 255-i;
28 for (i=0; test_charsets[i]; i++) {
29 iconv_t there = iconv_open("UTF-8", test_charsets[i]);
30 if (there == (iconv_t)-1) continue;
31 iconv_t andback = iconv_open(test_charsets[i], "UTF-8");
32 if (andback == (iconv_t)-1) {
33 iconv_close(there);
34 continue;
36 char u8buf[1024];
37 char buf[256];
38 size_t u8rem = sizeof u8buf;
39 int r1 = iconv(there,
40 &(char *){all_codepoints}, &(size_t){sizeof all_codepoints},
41 &(char *){u8buf}, &u8rem);
42 size_t u8len = sizeof u8buf - u8rem;
43 int r2 = iconv(andback,
44 &(char *){u8buf}, &(size_t){u8len},
45 &(char *){buf}, &(size_t){sizeof buf});
47 if (r1) t_error("got %d converting from %s\n", r1, test_charsets[i]);
48 if (r2) t_error("got %d converting back to %s\n", r2, test_charsets[i]);
50 if (memcmp(all_codepoints, buf, sizeof buf)) {
51 t_error("round trip corrupted %s characters\n", test_charsets[i]);
54 iconv_close(there);
55 iconv_close(andback);
58 return t_status;