* transcode_data.h (TRANSCODE_ERROR): removed.
[ruby-svn.git] / test / ruby / test_transcode.rb
blob6ad1b09202cc097cb1448958bef6aa2d3a6d2d27
1 # -*- encoding: ASCII-8BIT -*-   # make sure this runs in binary mode
2 # some of the comments are in UTF-8
4 require 'test/unit'
5 class TestTranscode < Test::Unit::TestCase
6   def setup_really_needed? # trick to create all the necessary encodings
7     all_encodings = [ 'ISO-8859-1', 'ISO-8859-2',
8                       'ISO-8859-3', 'ISO-8859-4',
9                       'ISO-8859-5', 'ISO-8859-6',
10                       'ISO-8859-7', 'ISO-8859-8',
11                       'ISO-8859-9', 'ISO-8859-10',
12                       'ISO-8859-11', 'ISO-8859-13',
13                       'ISO-8859-14', 'ISO-8859-15',
14                       'UTF-16BE'
15                     ]
16     all_encodings.each do |enc|
17       'abc'.encode(enc, 'UTF-8')
18     end
19   end
21   def test_errors
22     assert_raise(ArgumentError) { 'abc'.encode }
23     assert_raise(ArgumentError) { 'abc'.encode! }
24     assert_raise(ArgumentError) { 'abc'.encode('foo', 'bar') }
25     assert_raise(ArgumentError) { 'abc'.encode!('foo', 'bar') }
26     assert_raise(ArgumentError) { 'abc'.force_encoding('utf-8').encode('foo') }
27     assert_raise(ArgumentError) { 'abc'.force_encoding('utf-8').encode!('foo') }
28     assert_raise(Encoding::ConversionUndefined) { "\x80".encode('utf-8','ASCII-8BIT') }
29     assert_raise(Encoding::InvalidByteSequence) { "\x80".encode('utf-8','US-ASCII') }
30     assert_raise(Encoding::ConversionUndefined) { "\xA5".encode('utf-8','iso-8859-3') }
31   end
33   def test_arguments
34     assert_equal('abc', 'abc'.force_encoding('utf-8').encode('iso-8859-1'))
35     # check that encoding is kept when no conversion is done
36     assert_equal('abc'.force_encoding('Shift_JIS'), 'abc'.force_encoding('Shift_JIS').encode('Shift_JIS'))
37     assert_equal('abc'.force_encoding('Shift_JIS'), 'abc'.force_encoding('Shift_JIS').encode!('Shift_JIS'))
38     # assert that encoding is correctly set
39     assert_equal("D\u00FCrst".encoding, "D\xFCrst".force_encoding('iso-8859-1').encode('utf-8').encoding)
40     # check that Encoding can be used as parameter
41     assert_equal("D\u00FCrst", "D\xFCrst".encode('utf-8', Encoding.find('ISO-8859-1')))
42     assert_equal("D\u00FCrst", "D\xFCrst".encode(Encoding.find('utf-8'), 'ISO-8859-1'))
43     assert_equal("D\u00FCrst", "D\xFCrst".encode(Encoding.find('utf-8'), Encoding.find('ISO-8859-1')))
44   end
46   def test_length
47     assert_equal("\u20AC"*20, ("\xA4"*20).encode('utf-8', 'iso-8859-15'))
48     assert_equal("\u20AC"*20, ("\xA4"*20).encode!('utf-8', 'iso-8859-15'))
49     assert_equal("\u20AC"*2000, ("\xA4"*2000).encode('utf-8', 'iso-8859-15'))
50     assert_equal("\u20AC"*2000, ("\xA4"*2000).encode!('utf-8', 'iso-8859-15'))
51     assert_equal("\u20AC"*200000, ("\xA4"*200000).encode('utf-8', 'iso-8859-15'))
52     assert_equal("\u20AC"*200000, ("\xA4"*200000).encode!('utf-8', 'iso-8859-15'))
53   end
55   def check_both_ways(utf8, raw, encoding)
56     assert_equal(utf8.force_encoding('utf-8'), raw.encode('utf-8', encoding))
57     assert_equal(raw.force_encoding(encoding), utf8.encode(encoding, 'utf-8'))
58   end
60   def test_encodings
61     check_both_ways("\u307E\u3064\u3082\u3068 \u3086\u304D\u3072\u308D",
62         "\x82\xdc\x82\xc2\x82\xe0\x82\xc6 \x82\xe4\x82\xab\x82\xd0\x82\xeb", 'shift_jis') # まつもと ゆきひろ
63     check_both_ways("\u307E\u3064\u3082\u3068 \u3086\u304D\u3072\u308D",
64         "\xa4\xde\xa4\xc4\xa4\xe2\xa4\xc8 \xa4\xe6\xa4\xad\xa4\xd2\xa4\xed", 'euc-jp')
65     check_both_ways("\u677E\u672C\u884C\u5F18", "\x8f\xbc\x96\x7b\x8d\x73\x8d\x4f", 'shift_jis') # 松本行弘
66     check_both_ways("\u677E\u672C\u884C\u5F18", "\xbe\xbe\xcb\xdc\xb9\xd4\xb9\xb0", 'euc-jp')
67     check_both_ways("D\u00FCrst", "D\xFCrst", 'iso-8859-1') # Dürst
68     check_both_ways("D\u00FCrst", "D\xFCrst", 'iso-8859-2')
69     check_both_ways("D\u00FCrst", "D\xFCrst", 'iso-8859-3')
70     check_both_ways("D\u00FCrst", "D\xFCrst", 'iso-8859-4')
71     check_both_ways("D\u00FCrst", "D\xFCrst", 'iso-8859-9')
72     check_both_ways("D\u00FCrst", "D\xFCrst", 'iso-8859-10')
73     check_both_ways("D\u00FCrst", "D\xFCrst", 'iso-8859-13')
74     check_both_ways("D\u00FCrst", "D\xFCrst", 'iso-8859-14')
75     check_both_ways("D\u00FCrst", "D\xFCrst", 'iso-8859-15')
76     check_both_ways("r\u00E9sum\u00E9", "r\xE9sum\xE9", 'iso-8859-1') # résumé
77     check_both_ways("\u0065\u006C\u0151\u00ED\u0072\u00E1\u0073", "el\xF5\xEDr\xE1s", 'iso-8859-2') # előírás
78     check_both_ways("\u043F\u0435\u0440\u0435\u0432\u043E\u0434",
79          "\xDF\xD5\xE0\xD5\xD2\xDE\xD4", 'iso-8859-5') # перевод
80     check_both_ways("\u0643\u062A\u0628", "\xE3\xCA\xC8", 'iso-8859-6') # كتب
81     check_both_ways("\u65E5\u8A18", "\x93\xFA\x8BL", 'shift_jis') # 日記
82     check_both_ways("\u65E5\u8A18", "\xC6\xFC\xB5\xAD", 'euc-jp')
83     check_both_ways("\uC560\uC778\uAD6C\uD568\u0020\u6734\uC9C0\uC778",
84          "\xBE\xD6\xC0\xCE\xB1\xB8\xC7\xD4\x20\xDA\xD3\xC1\xF6\xC0\xCE", 'euc-kr') # 애인구함 朴지인
85     check_both_ways("\uC544\uD58F\uD58F\u0020\uB620\uBC29\uD6BD\uB2D8\u0020\uC0AC\uB791\uD716",
86          "\xBE\xC6\xC1\x64\xC1\x64\x20\x8C\x63\xB9\xE6\xC4\x4F\xB4\xD4\x20\xBB\xE7\xB6\xFB\xC5\x42", 'cp949') # 아햏햏 똠방횽님 사랑휖
87   end
89   def test_twostep
90     assert_equal("D\xFCrst".force_encoding('iso-8859-2'), "D\xFCrst".encode('iso-8859-2', 'iso-8859-1'))
91   end
93   def test_ascii_range
94     encodings = [
95       'US-ASCII', 'ASCII-8BIT',
96       'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3',
97       'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-6',
98       'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9',
99       'ISO-8859-10', 'ISO-8859-11', 'ISO-8859-13',
100       'ISO-8859-14', 'ISO-8859-15',
101       'EUC-JP', 'SHIFT_JIS', 'EUC-KR'
102     ]
103     all_ascii = (0..127).to_a.pack 'C*'
104     encodings.each do |enc|
105       test_start = all_ascii
106       assert_equal(test_start, test_start.encode('UTF-8',enc).encode(enc).force_encoding('ASCII-8BIT')) 
107     end
108   end
110   def test_all_bytes
111     encodings_8859 = [
112       'ISO-8859-1', 'ISO-8859-2',
113       #'ISO-8859-3', # not all bytes used
114       'ISO-8859-4', 'ISO-8859-5',
115       #'ISO-8859-6', # not all bytes used
116       #'ISO-8859-7', # not all bytes used
117       #'ISO-8859-8', # not all bytes used
118       'ISO-8859-9', 'ISO-8859-10',
119       #'ISO-8859-11', # not all bytes used
120       #'ISO-8859-12', # not available
121       'ISO-8859-13','ISO-8859-14','ISO-8859-15',
122       #'ISO-8859-16', # not available
123     ]
124     all_bytes = (0..255).to_a.pack 'C*'
125     encodings_8859.each do |enc|
126       test_start = all_bytes
127       assert_equal(test_start, test_start.encode('UTF-8',enc).encode(enc).force_encoding('ASCII-8BIT')) 
128     end
129   end
131   def check_utf_16_both_ways(utf8, raw)
132     copy = raw.dup
133     0.step(copy.length-1, 2) { |i| copy[i+1], copy[i] = copy[i], copy[i+1] }
134     check_both_ways(utf8, raw, 'utf-16be')
135     check_both_ways(utf8, copy, 'utf-16le')
136   end
138   def test_utf_16
139     check_utf_16_both_ways("abc", "\x00a\x00b\x00c")
140     check_utf_16_both_ways("\u00E9", "\x00\xE9");
141     check_utf_16_both_ways("\u00E9\u0070\u00E9\u0065", "\x00\xE9\x00\x70\x00\xE9\x00\x65") # épée
142     check_utf_16_both_ways("\u677E\u672C\u884C\u5F18", "\x67\x7E\x67\x2C\x88\x4C\x5F\x18") # 松本行弘
143     check_utf_16_both_ways("\u9752\u5C71\u5B66\u9662\u5927\u5B66", "\x97\x52\x5C\x71\x5B\x66\x96\x62\x59\x27\x5B\x66") # 青山学院大学
144     check_utf_16_both_ways("Martin D\u00FCrst", "\x00M\x00a\x00r\x00t\x00i\x00n\x00 \x00D\x00\xFC\x00r\x00s\x00t") # Martin Dürst
145     # BMP
146     check_utf_16_both_ways("\u0000", "\x00\x00")
147     check_utf_16_both_ways("\u007F", "\x00\x7F")
148     check_utf_16_both_ways("\u0080", "\x00\x80")
149     check_utf_16_both_ways("\u0555", "\x05\x55")
150     check_utf_16_both_ways("\u04AA", "\x04\xAA")
151     check_utf_16_both_ways("\u0333", "\x03\x33")
152     check_utf_16_both_ways("\u04CC", "\x04\xCC")
153     check_utf_16_both_ways("\u00F0", "\x00\xF0")
154     check_utf_16_both_ways("\u070F", "\x07\x0F")
155     check_utf_16_both_ways("\u07FF", "\x07\xFF")
156     check_utf_16_both_ways("\u0800", "\x08\x00")
157     check_utf_16_both_ways("\uD7FF", "\xD7\xFF")
158     check_utf_16_both_ways("\uE000", "\xE0\x00")
159     check_utf_16_both_ways("\uFFFF", "\xFF\xFF")
160     check_utf_16_both_ways("\u5555", "\x55\x55")
161     check_utf_16_both_ways("\uAAAA", "\xAA\xAA")
162     check_utf_16_both_ways("\u3333", "\x33\x33")
163     check_utf_16_both_ways("\uCCCC", "\xCC\xCC")
164     check_utf_16_both_ways("\uF0F0", "\xF0\xF0")
165     check_utf_16_both_ways("\u0F0F", "\x0F\x0F")
166     check_utf_16_both_ways("\uFF00", "\xFF\x00")
167     check_utf_16_both_ways("\u00FF", "\x00\xFF")
168     # outer planes
169     check_utf_16_both_ways("\u{10000}", "\xD8\x00\xDC\x00")
170     check_utf_16_both_ways("\u{FFFFF}", "\xDB\xBF\xDF\xFF")
171     check_utf_16_both_ways("\u{100000}", "\xDB\xC0\xDC\x00")
172     check_utf_16_both_ways("\u{10FFFF}", "\xDB\xFF\xDF\xFF")
173     check_utf_16_both_ways("\u{105555}", "\xDB\xD5\xDD\x55")
174     check_utf_16_both_ways("\u{55555}", "\xD9\x15\xDD\x55")
175     check_utf_16_both_ways("\u{AAAAA}", "\xDA\x6A\xDE\xAA")
176     check_utf_16_both_ways("\u{33333}", "\xD8\x8C\xDF\x33")
177     check_utf_16_both_ways("\u{CCCCC}", "\xDA\xF3\xDC\xCC")
178     check_utf_16_both_ways("\u{8F0F0}", "\xD9\xFC\xDC\xF0")
179     check_utf_16_both_ways("\u{F0F0F}", "\xDB\x83\xDF\x0F")
180     check_utf_16_both_ways("\u{8FF00}", "\xD9\xFF\xDF\x00")
181     check_utf_16_both_ways("\u{F00FF}", "\xDB\x80\xDC\xFF")
182   end
184   def check_utf_32_both_ways(utf8, raw)
185     copy = raw.dup
186     0.step(copy.length-1, 4) do |i|
187       copy[i+3], copy[i+2], copy[i+1], copy[i] = copy[i], copy[i+1], copy[i+2], copy[i+3]
188     end
189     check_both_ways(utf8, raw, 'utf-32be')
190     #check_both_ways(utf8, copy, 'utf-32le')
191   end
193   def test_utf_32
194     check_utf_32_both_ways("abc", "\x00\x00\x00a\x00\x00\x00b\x00\x00\x00c")
195     check_utf_32_both_ways("\u00E9", "\x00\x00\x00\xE9");
196     check_utf_32_both_ways("\u00E9\u0070\u00E9\u0065",
197       "\x00\x00\x00\xE9\x00\x00\x00\x70\x00\x00\x00\xE9\x00\x00\x00\x65") # épée
198     check_utf_32_both_ways("\u677E\u672C\u884C\u5F18",
199       "\x00\x00\x67\x7E\x00\x00\x67\x2C\x00\x00\x88\x4C\x00\x00\x5F\x18") # 松本行弘
200     check_utf_32_both_ways("\u9752\u5C71\u5B66\u9662\u5927\u5B66",
201       "\x00\x00\x97\x52\x00\x00\x5C\x71\x00\x00\x5B\x66\x00\x00\x96\x62\x00\x00\x59\x27\x00\x00\x5B\x66") # 青山学院大学
202     check_utf_32_both_ways("Martin D\u00FCrst",
203       "\x00\x00\x00M\x00\x00\x00a\x00\x00\x00r\x00\x00\x00t\x00\x00\x00i\x00\x00\x00n\x00\x00\x00 \x00\x00\x00D\x00\x00\x00\xFC\x00\x00\x00r\x00\x00\x00s\x00\x00\x00t") # Martin Dürst
204     # BMP
205     check_utf_32_both_ways("\u0000", "\x00\x00\x00\x00")
206     check_utf_32_both_ways("\u007F", "\x00\x00\x00\x7F")
207     check_utf_32_both_ways("\u0080", "\x00\x00\x00\x80")
208     check_utf_32_both_ways("\u0555", "\x00\x00\x05\x55")
209     check_utf_32_both_ways("\u04AA", "\x00\x00\x04\xAA")
210     check_utf_32_both_ways("\u0333", "\x00\x00\x03\x33")
211     check_utf_32_both_ways("\u04CC", "\x00\x00\x04\xCC")
212     check_utf_32_both_ways("\u00F0", "\x00\x00\x00\xF0")
213     check_utf_32_both_ways("\u070F", "\x00\x00\x07\x0F")
214     check_utf_32_both_ways("\u07FF", "\x00\x00\x07\xFF")
215     check_utf_32_both_ways("\u0800", "\x00\x00\x08\x00")
216     check_utf_32_both_ways("\uD7FF", "\x00\x00\xD7\xFF")
217     check_utf_32_both_ways("\uE000", "\x00\x00\xE0\x00")
218     check_utf_32_both_ways("\uFFFF", "\x00\x00\xFF\xFF")
219     check_utf_32_both_ways("\u5555", "\x00\x00\x55\x55")
220     check_utf_32_both_ways("\uAAAA", "\x00\x00\xAA\xAA")
221     check_utf_32_both_ways("\u3333", "\x00\x00\x33\x33")
222     check_utf_32_both_ways("\uCCCC", "\x00\x00\xCC\xCC")
223     check_utf_32_both_ways("\uF0F0", "\x00\x00\xF0\xF0")
224     check_utf_32_both_ways("\u0F0F", "\x00\x00\x0F\x0F")
225     check_utf_32_both_ways("\uFF00", "\x00\x00\xFF\x00")
226     check_utf_32_both_ways("\u00FF", "\x00\x00\x00\xFF")
227     # outer planes
228     check_utf_32_both_ways("\u{10000}", "\x00\x01\x00\x00")
229     check_utf_32_both_ways("\u{FFFFF}", "\x00\x0F\xFF\xFF")
230     check_utf_32_both_ways("\u{100000}","\x00\x10\x00\x00")
231     check_utf_32_both_ways("\u{10FFFF}","\x00\x10\xFF\xFF")
232     check_utf_32_both_ways("\u{105555}","\x00\x10\x55\x55")
233     check_utf_32_both_ways("\u{55555}", "\x00\x05\x55\x55")
234     check_utf_32_both_ways("\u{AAAAA}", "\x00\x0A\xAA\xAA")
235     check_utf_32_both_ways("\u{33333}", "\x00\x03\x33\x33")
236     check_utf_32_both_ways("\u{CCCCC}", "\x00\x0C\xCC\xCC")
237     check_utf_32_both_ways("\u{8F0F0}", "\x00\x08\xF0\xF0")
238     check_utf_32_both_ways("\u{F0F0F}", "\x00\x0F\x0F\x0F")
239     check_utf_32_both_ways("\u{8FF00}", "\x00\x08\xFF\x00")
240     check_utf_32_both_ways("\u{F00FF}", "\x00\x0F\x00\xFF")
241   end
242   
243   def test_invalid_ignore
244     # arguments only
245     assert_nothing_raised { 'abc'.encode('utf-8', invalid: :ignore) }
246     # check handling of UTF-8 ill-formed subsequences
247     assert_equal("\x00\x41\x00\x3E\x00\x42".force_encoding('UTF-16BE'),
248       "\x41\xC2\x3E\x42".encode('UTF-16BE', 'UTF-8', invalid: :ignore))
249     assert_equal("\x00\x41\x00\xF1\x00\x42".force_encoding('UTF-16BE'),
250       "\x41\xC2\xC3\xB1\x42".encode('UTF-16BE', 'UTF-8', invalid: :ignore))
251     assert_equal("\x00\x42".force_encoding('UTF-16BE'),
252       "\xF0\x80\x80\x42".encode('UTF-16BE', 'UTF-8', invalid: :ignore))
253     assert_equal(''.force_encoding('UTF-16BE'),
254       "\x82\xAB".encode('UTF-16BE', 'UTF-8', invalid: :ignore))
256     assert_equal("\e$B!!\e(B".force_encoding("ISO-2022-JP"),
257       "\xA1\xA1\xFF".encode("ISO-2022-JP", "EUC-JP", invalid: :ignore))
258     assert_equal("\e$B\x24\x22\x24\x24\e(B".force_encoding("ISO-2022-JP"),
259       "\xA4\xA2\xFF\xA4\xA4".encode("ISO-2022-JP", "EUC-JP", invalid: :ignore))
260     assert_equal("\e$B\x24\x22\x24\x24\e(B".force_encoding("ISO-2022-JP"),
261       "\xA4\xA2\xFF\xFF\xA4\xA4".encode("ISO-2022-JP", "EUC-JP", invalid: :ignore))
262   end
264   def test_invalid_replace
265     # arguments only
266     assert_nothing_raised { 'abc'.encode('UTF-8', invalid: :replace) }
267     assert_equal("\xEF\xBF\xBD".force_encoding("UTF-8"),
268       "\x80".encode("UTF-8", "UTF-16BE", invalid: :replace))
269     assert_equal("\xFF\xFD".force_encoding("UTF-16BE"),
270       "\x80".encode("UTF-16BE", "UTF-8", invalid: :replace))
271     assert_equal("\xFD\xFF".force_encoding("UTF-16LE"),
272       "\x80".encode("UTF-16LE", "UTF-8", invalid: :replace))
273     assert_equal("\x00\x00\xFF\xFD".force_encoding("UTF-32BE"),
274       "\x80".encode("UTF-32BE", "UTF-8", invalid: :replace))
275     assert_equal("\xFD\xFF\x00\x00".force_encoding("UTF-32LE"),
276       "\x80".encode("UTF-32LE", "UTF-8", invalid: :replace))
278     assert_equal("\uFFFD!",
279       "\xdc\x00\x00!".encode("utf-8", "utf-16be", :invalid=>:replace))
280     assert_equal("\uFFFD!",
281       "\xd8\x00\x00!".encode("utf-8", "utf-16be", :invalid=>:replace))
283     assert_equal("\uFFFD!",
284       "\x00\xdc!\x00".encode("utf-8", "utf-16le", :invalid=>:replace))
285     assert_equal("\uFFFD!",
286       "\x00\xd8!\x00".encode("utf-8", "utf-16le", :invalid=>:replace))
288     assert_equal("\uFFFD!",
289       "\x01\x00\x00\x00\x00\x00\x00!".encode("utf-8", "utf-32be", :invalid=>:replace), "[ruby-dev:35726]")
290     assert_equal("\uFFFD!",
291       "\x00\xff\x00\x00\x00\x00\x00!".encode("utf-8", "utf-32be", :invalid=>:replace))
292     assert_equal("\uFFFD!",
293       "\x00\x00\xd8\x00\x00\x00\x00!".encode("utf-8", "utf-32be", :invalid=>:replace))
295     assert_equal("\uFFFD!",
296       "\x00\x00\x00\xff!\x00\x00\x00".encode("utf-8", "utf-32le", :invalid=>:replace))
297     assert_equal("\uFFFD!",
298       "\x00\x00\xff\x00!\x00\x00\x00".encode("utf-8", "utf-32le", :invalid=>:replace))
299     assert_equal("\uFFFD!",
300       "\x00\xd8\x00\x00!\x00\x00\x00".encode("utf-8", "utf-32le", :invalid=>:replace))
302     assert_equal("\uFFFD!",
303       "\xff!".encode("utf-8", "euc-jp", :invalid=>:replace))
304     assert_equal("\uFFFD!",
305       "\xa1!".encode("utf-8", "euc-jp", :invalid=>:replace))
306     assert_equal("\uFFFD!",
307       "\x8f\xa1!".encode("utf-8", "euc-jp", :invalid=>:replace))
309     assert_equal("?",
310       "\xdc\x00".encode("EUC-JP", "UTF-16BE", :invalid=>:replace), "[ruby-dev:35776]")
311     assert_equal("ab?cd?ef",
312       "\0a\0b\xdc\x00\0c\0d\xdf\x00\0e\0f".encode("EUC-JP", "UTF-16BE", :invalid=>:replace))
314     assert_equal("\e$B!!\e(B?".force_encoding("ISO-2022-JP"),
315       "\xA1\xA1\xFF".encode("ISO-2022-JP", "EUC-JP", invalid: :replace))
316     assert_equal("\e$B\x24\x22\e(B?\e$B\x24\x24\e(B".force_encoding("ISO-2022-JP"),
317       "\xA4\xA2\xFF\xA4\xA4".encode("ISO-2022-JP", "EUC-JP", invalid: :replace))
318     assert_equal("\e$B\x24\x22\e(B??\e$B\x24\x24\e(B".force_encoding("ISO-2022-JP"),
319       "\xA4\xA2\xFF\xFF\xA4\xA4".encode("ISO-2022-JP", "EUC-JP", invalid: :replace))
320   end
322   def test_undef_replace
323     assert_equal("?", "\u20AC".encode("EUC-JP", :undef=>:replace), "[ruby-dev:35709]")
324   end
326   def test_shift_jis
327     check_both_ways("\u3000", "\x81\x40", 'shift_jis') # full-width space
328     check_both_ways("\u00D7", "\x81\x7E", 'shift_jis') # ~
329     check_both_ways("\u00F7", "\x81\x80", 'shift_jis') # 
330     check_both_ways("\u25C7", "\x81\x9E", 'shift_jis') # 
331     check_both_ways("\u25C6", "\x81\x9F", 'shift_jis') # 
332     check_both_ways("\u25EF", "\x81\xFC", 'shift_jis') # 
333     check_both_ways("\u6A97", "\x9F\x40", 'shift_jis') # @
334     check_both_ways("\u6BEF", "\x9F\x7E", 'shift_jis') # ~
335     check_both_ways("\u9EBE", "\x9F\x80", 'shift_jis') # 
336     check_both_ways("\u6CBE", "\x9F\x9E", 'shift_jis') # 
337     check_both_ways("\u6CBA", "\x9F\x9F", 'shift_jis') # 
338     check_both_ways("\u6ECC", "\x9F\xFC", 'shift_jis') # 
339     check_both_ways("\u6F3E", "\xE0\x40", 'shift_jis') # @
340     check_both_ways("\u70DD", "\xE0\x7E", 'shift_jis') # ~
341     check_both_ways("\u70D9", "\xE0\x80", 'shift_jis') # 
342     check_both_ways("\u71FC", "\xE0\x9E", 'shift_jis') # 
343     check_both_ways("\u71F9", "\xE0\x9F", 'shift_jis') # 
344     check_both_ways("\u73F1", "\xE0\xFC", 'shift_jis') # 
345     assert_raise(Encoding::ConversionUndefined) { "\xEF\x40".encode("utf-8", 'shift_jis') }
346     assert_raise(Encoding::ConversionUndefined) { "\xEF\x7E".encode("utf-8", 'shift_jis') }
347     assert_raise(Encoding::ConversionUndefined) { "\xEF\x80".encode("utf-8", 'shift_jis') }
348     assert_raise(Encoding::ConversionUndefined) { "\xEF\x9E".encode("utf-8", 'shift_jis') }
349     assert_raise(Encoding::ConversionUndefined) { "\xEF\x9F".encode("utf-8", 'shift_jis') }
350     assert_raise(Encoding::ConversionUndefined) { "\xEF\xFC".encode("utf-8", 'shift_jis') }
351     assert_raise(Encoding::ConversionUndefined) { "\xF0\x40".encode("utf-8", 'shift_jis') }
352     assert_raise(Encoding::ConversionUndefined) { "\xF0\x7E".encode("utf-8", 'shift_jis') }
353     assert_raise(Encoding::ConversionUndefined) { "\xF0\x80".encode("utf-8", 'shift_jis') }
354     assert_raise(Encoding::ConversionUndefined) { "\xF0\x9E".encode("utf-8", 'shift_jis') }
355     assert_raise(Encoding::ConversionUndefined) { "\xF0\x9F".encode("utf-8", 'shift_jis') }
356     assert_raise(Encoding::ConversionUndefined) { "\xF0\xFC".encode("utf-8", 'shift_jis') }
357     check_both_ways("\u9ADC", "\xFC\x40", 'shift_jis') # @
358     assert_raise(Encoding::ConversionUndefined) { "\xFC\x7E".encode("utf-8", 'shift_jis') }
359     assert_raise(Encoding::ConversionUndefined) { "\xFC\x80".encode("utf-8", 'shift_jis') }
360     assert_raise(Encoding::ConversionUndefined) { "\xFC\x9E".encode("utf-8", 'shift_jis') }
361     assert_raise(Encoding::ConversionUndefined) { "\xFC\x9F".encode("utf-8", 'shift_jis') }
362     assert_raise(Encoding::ConversionUndefined) { "\xFC\xFC".encode("utf-8", 'shift_jis') }
363     check_both_ways("\u677E\u672C\u884C\u5F18", "\x8f\xbc\x96\x7b\x8d\x73\x8d\x4f", 'shift_jis') # {sO
364     check_both_ways("\u9752\u5C71\u5B66\u9662\u5927\u5B66", "\x90\xC2\x8E\x52\x8A\x77\x89\x40\x91\xE5\x8A\x77", 'shift_jis') # ŽRw@w
365     check_both_ways("\u795E\u6797\u7FA9\u535A", "\x90\x5F\x97\xD1\x8B\x60\x94\x8E", 'shift_jis') # _ы`
366   end
368   def test_iso_2022_jp
369     assert_raise(Encoding::InvalidByteSequence) { "\x1b(A".encode("utf-8", "iso-2022-jp") }
370     assert_raise(Encoding::InvalidByteSequence) { "\x1b$(A".encode("utf-8", "iso-2022-jp") }
371     assert_raise(Encoding::InvalidByteSequence) { "\x1b$C".encode("utf-8", "iso-2022-jp") }
372     assert_raise(Encoding::InvalidByteSequence) { "\x0e".encode("utf-8", "iso-2022-jp") }
373     assert_raise(Encoding::InvalidByteSequence) { "\x80".encode("utf-8", "iso-2022-jp") }
374     assert_raise(Encoding::InvalidByteSequence) { "\x1b$(Dd!\x1b(B".encode("utf-8", "iso-2022-jp") }
375     assert_raise(Encoding::ConversionUndefined) { "\u9299".encode("iso-2022-jp") }
376     assert_raise(Encoding::ConversionUndefined) { "\uff71\uff72\uff73\uff74\uff75".encode("iso-2022-jp") }
377     assert_raise(Encoding::InvalidByteSequence) { "\x1b(I12345\x1b(B".encode("utf-8", "iso-2022-jp") }
378     assert_equal("\xA1\xA1".force_encoding("euc-jp"),
379                  "\e$B!!\e(B".encode("EUC-JP", "ISO-2022-JP"))
380     assert_equal("\e$B!!\e(B".force_encoding("ISO-2022-JP"),
381                  "\xA1\xA1".encode("ISO-2022-JP", "EUC-JP"))
382   end
383   
384   def test_iso_2022_jp_1
385     # check_both_ways("\u9299", "\x1b$(Dd!\x1b(B", "iso-2022-jp-1") # JIS X 0212 区68 点01 銙
386   end
387   
388   def test_unicode_public_review_issue_121 # see http://www.unicode.org/review/pr-121.html
389     # assert_equal("\x00\x61\xFF\xFD\x00\x62".force_encoding('UTF-16BE'),
390     #   "\x61\xF1\x80\x80\xE1\x80\xC2\x62".encode('UTF-16BE', 'UTF-8', invalid: :replace)) # option 1
391     assert_equal("\x00\x61\xFF\xFD\xFF\xFD\xFF\xFD\x00\x62".force_encoding('UTF-16BE'),
392       "\x61\xF1\x80\x80\xE1\x80\xC2\x62".encode('UTF-16BE', 'UTF-8', invalid: :replace)) # option 2
393     assert_equal("\x61\x00\xFD\xFF\xFD\xFF\xFD\xFF\x62\x00".force_encoding('UTF-16LE'),
394       "\x61\xF1\x80\x80\xE1\x80\xC2\x62".encode('UTF-16LE', 'UTF-8', invalid: :replace)) # option 2
395     # assert_equal("\x00\x61\xFF\xFD\xFF\xFD\xFF\xFD\xFF\xFD\xFF\xFD\xFF\xFD\x00\x62".force_encoding('UTF-16BE'),
396     # "\x61\xF1\x80\x80\xE1\x80\xC2\x62".encode('UTF-16BE', 'UTF-8', invalid: :replace)) # option 3
397   end