1#!/bin/sh
2# Check of --unicode-subst, --byte-subst, --widechar-subst options.
3set -e
4iconv=../src/iconv_no_i18n
5
6options_ascii='--unicode-subst=<U+%04X> --byte-subst=<0x%02x> --widechar-subst=<%08x>'
7options_utf8='--unicode-subst=��U+%04X�� --byte-subst=��0x%02x�� --widechar-subst=��%08x��'
8
9# Test of --byte-subst with an ASCII substitution.
10
11cat > tmp-in <<\EOF
12B��se B��bchen
13EOF
14$iconv $options_ascii -f ASCII -t ASCII < tmp-in > tmp-out
15cat > tmp-ok <<\EOF
16B<0xc3><0xb6>se B<0xc3><0xbc>bchen
17EOF
18cmp tmp-out tmp-ok
19
20# Test of --byte-subst with a non-ASCII substitution.
21
22if test "`(locale charmap) 2>/dev/null`" = UTF-8; then
23  cat > tmp-in <<\EOF
24B��se B��bchen
25EOF
26  $iconv $options_utf8 -f ASCII -t UTF-8 2>/dev/null < tmp-in > tmp-out
27  cat > tmp-ok <<\EOF
28B��0xc3����0xb6��se B��0xc3����0xbc��bchen
29EOF
30  cmp tmp-out tmp-ok
31fi
32
33if test "`(locale charmap) 2>/dev/null`" = UTF-8; then
34  cat > tmp-in <<\EOF
35B��se B��bchen
36EOF
37  $iconv $options_utf8 -f ASCII -t ISO-8859-1 2>/dev/null < tmp-in > tmp-out
38  $iconv -f ISO-8859-1 -t UTF-8 < tmp-out > tmp-out2
39  cat > tmp-ok <<\EOF
40B��0xc3����0xb6��se B��0xc3����0xbc��bchen
41EOF
42  cmp tmp-out2 tmp-ok
43fi
44
45# Test of --byte-subst with a very long substitution.
46
47cat > tmp-in <<\EOF
48B��se B��bchen
49EOF
50$iconv --byte-subst='<0x%010000x>' -f ASCII -t ASCII < tmp-in > tmp-out
51printf 'B<0x%010000x><0x%010000x>se B<0x%010000x><0x%010000x>bchen\n' 0xC3 0xB6 0xC3 0xBC > tmp-ok
52cmp tmp-out tmp-ok
53
54# Test of --unicode-subst with an ASCII substitution.
55
56cat > tmp-in <<\EOF
57B��se B��bchen
58EOF
59$iconv $options_ascii -f UTF-8 -t ASCII < tmp-in > tmp-out
60cat > tmp-ok <<\EOF
61B<U+00F6>se B<U+00FC>bchen
62EOF
63cmp tmp-out tmp-ok
64
65cat > tmp-in <<\EOF
66Russian (��������������)
67EOF
68$iconv $options_ascii -f UTF-8 -t ISO-8859-1 2>/dev/null < tmp-in | $iconv -f ISO-8859-1 -t UTF-8 > tmp-out
69cat > tmp-ok <<\EOF
70Russian (<U+0420><U+0443><U+0441><U+0441><U+043A><U+0438><U+0439>)
71EOF
72cmp tmp-out tmp-ok
73
74# Test of --unicode-subst with a non-ASCII substitution.
75
76if test "`(locale charmap) 2>/dev/null`" = UTF-8; then
77  cat > tmp-in <<\EOF
78Russian (��������������)
79EOF
80  $iconv $options_utf8 -f UTF-8 -t ISO-8859-1 2>/dev/null < tmp-in > tmp-out
81  $iconv -f ISO-8859-1 -t UTF-8 < tmp-out > tmp-out2
82  cat > tmp-ok <<\EOF
83Russian (��U+0420����U+0443����U+0441����U+0441����U+043A����U+0438����U+0439��)
84EOF
85  cmp tmp-out2 tmp-ok
86fi
87
88# Test of --unicode-subst with a very long substitution.
89
90cat > tmp-in <<\EOF
91B��se B��bchen
92EOF
93$iconv --unicode-subst='<U+%010000X>' -f UTF-8 -t ASCII < tmp-in > tmp-out
94printf 'B<U+%010000X>se B<U+%010000X>bchen\n' 0x00F6 0x00FC > tmp-ok
95cmp tmp-out tmp-ok
96
97cat > tmp-in <<\EOF
98B��se B��bchen
99EOF
100$iconv --byte-subst='<0x%010000x>' -f ASCII -t ASCII < tmp-in > tmp-out
101printf 'B<0x%010000x><0x%010000x>se B<0x%010000x><0x%010000x>bchen\n' 0xC3 0xB6 0xC3 0xBC > tmp-ok
102cmp tmp-out tmp-ok
103
104# Test of --widechar-subst:
105# wcrtomb() doesn't exist on FreeBSD 4.0 and is broken on MacOS X 10.3.
106# So far this has been tested only on a glibc system with !__STDC_ISO_10646__.
107
108if false && test "`(locale charmap) 2>/dev/null`" = UTF-8; then
109  cat > tmp-in <<\EOF
110Russian (��������������)
111EOF
112  $iconv -f char -t wchar_t < tmp-in > tmp-inw
113  LC_ALL=C                $iconv $options_ascii -f wchar_t -t ASCII < tmp-inw > tmp-out1
114  LC_ALL=de_DE.ISO-8859-1 $iconv $options_ascii -f wchar_t -t ASCII < tmp-inw > tmp-out2
115  cat > tmp-ok <<\EOF
116Russian (<00000420><00000443><00000441><00000441><0000043a><00000438><00000439>)
117EOF
118  cmp tmp-out1 tmp-ok
119  cmp tmp-out2 tmp-ok
120  if test "`(LC_ALL=de_DE.ISO-8859-1 locale charmap) 2>/dev/null`" = ISO-8859-1; then
121    options_latin1=`echo " $options_utf8" | $iconv -f UTF-8 -t ISO-8859-1`
122    LC_ALL=de_DE.ISO-8859-1 $iconv $options_latin1 -f wchar_t -t UTF-8 < tmp-inw > tmp-out1
123    cat > tmp-ok <<\EOF
124Russian (��00000420����00000443����00000441����00000441����0000043a����00000438����00000439��)
125EOF
126    cmp tmp-out1 tmp-ok
127  fi
128fi
129
130rm -f tmp-in* tmp-out* tmp-ok
131exit 0
132