Update the testsuite for changed GB18030 converter.
[libiconv.git] / tests / table-from.c
blob4923897522a134217af77fd4bc30f611fb8793a1
1 /* Copyright (C) 2000-2001 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 Library General Public
6 License as published by the Free Software Foundation; either version 2
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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU LIBICONV Library; see the file COPYING.LIB.
16 If not, write to the Free Software Foundation, Inc., 59 Temple Place -
17 Suite 330, Boston, MA 02111-1307, USA. */
19 /* Create a table from CHARSET to Unicode. */
21 #include "config.h"
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <iconv.h>
27 #include <errno.h>
29 static const char* hexbuf (unsigned char buf[], unsigned int buflen)
31 static char msg[50];
32 switch (buflen) {
33 case 1: sprintf(msg,"0x%02X",buf[0]); break;
34 case 2: sprintf(msg,"0x%02X%02X",buf[0],buf[1]); break;
35 case 3: sprintf(msg,"0x%02X%02X%02X",buf[0],buf[1],buf[2]); break;
36 case 4: sprintf(msg,"0x%02X%02X%02X%02X",buf[0],buf[1],buf[2],buf[3]); break;
37 default: abort();
39 return msg;
42 static int try (iconv_t cd, unsigned char buf[], unsigned int buflen, unsigned int *out)
44 const char* inbuf = (const char*) buf;
45 size_t inbytesleft = buflen;
46 char* outbuf = (char*) out;
47 size_t outbytesleft = sizeof(unsigned int);
48 size_t result = iconv(cd,(ICONV_CONST char**)&inbuf,&inbytesleft,&outbuf,&outbytesleft);
49 if (result == (size_t)(-1)) {
50 if (errno == EILSEQ) {
51 return -1;
52 } else if (errno == EINVAL) {
53 return 0;
54 } else {
55 int saved_errno = errno;
56 fprintf(stderr,"%s: iconv error: ",hexbuf(buf,buflen));
57 errno = saved_errno;
58 perror("");
59 exit(1);
61 } else if (result > 0) /* ignore conversions with transliteration */ {
62 return -1;
63 } else {
64 if (inbytesleft != 0 || outbytesleft != 0) {
65 fprintf(stderr,"%s: inbytes = %ld, outbytes = %ld\n",hexbuf(buf,buflen),(long)(buflen-inbytesleft),(long)(sizeof(unsigned int)-outbytesleft));
66 exit(1);
68 return 1;
72 int main (int argc, char* argv[])
74 const char* charset;
75 iconv_t cd;
77 if (argc != 2) {
78 fprintf(stderr,"Usage: table-to charset\n");
79 exit(1);
81 charset = argv[1];
83 cd = iconv_open("UCS-4-INTERNAL",charset);
84 if (cd == (iconv_t)(-1)) {
85 perror("iconv_open");
86 exit(1);
90 unsigned int out;
91 unsigned char buf[4];
92 unsigned int i0, i1, i2, i3;
93 int result;
94 for (i0 = 0; i0 < 0x100; i0++) {
95 buf[0] = i0;
96 result = try(cd,buf,1,&out);
97 if (result < 0) {
98 } else if (result > 0) {
99 printf("0x%02X\t0x%04X\n",i0,out);
100 } else {
101 for (i1 = 0; i1 < 0x100; i1++) {
102 buf[1] = i1;
103 result = try(cd,buf,2,&out);
104 if (result < 0) {
105 } else if (result > 0) {
106 printf("0x%02X%02X\t0x%04X\n",i0,i1,out);
107 } else {
108 for (i2 = 0; i2 < 0x100; i2++) {
109 buf[2] = i2;
110 result = try(cd,buf,3,&out);
111 if (result < 0) {
112 } else if (result > 0) {
113 printf("0x%02X%02X%02X\t0x%04X\n",i0,i1,i2,out);
114 } else if (strcmp(charset,"UTF-8")) {
115 for (i3 = 0; i3 < 0x100; i3++) {
116 buf[3] = i3;
117 result = try(cd,buf,4,&out);
118 if (result < 0) {
119 } else if (result > 0) {
120 if (out < 0x10000 || strcmp(charset,"GB18030"))
121 printf("0x%02X%02X%02X%02X\t0x%04X\n",i0,i1,i2,i3,out);
122 } else {
123 fprintf(stderr,"%s: incomplete byte sequence\n",hexbuf(buf,4));
124 exit(1);
135 if (iconv_close(cd) < 0) {
136 perror("iconv_close");
137 exit(1);
140 if (ferror(stdin) || ferror(stdout)) {
141 fprintf(stderr,"I/O error\n");
142 exit(1);
145 exit(0);