* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / test / ruby / test_io_m17n.rb
blob0a3c403c754e90eab5d0068013495a59fb105da9
1 require 'test/unit'
2 require 'tmpdir'
3 require 'timeout'
4 require_relative 'envutil'
6 class TestIO_M17N < Test::Unit::TestCase
7   ENCS = [
8     Encoding::ASCII_8BIT,
9     Encoding::EUC_JP,
10     Encoding::Shift_JIS,
11     Encoding::UTF_8
12   ]
14   def with_tmpdir
15     Dir.mktmpdir {|dir|
16       Dir.chdir(dir) {
17         yield dir
18       }
19     }
20   end
22   def with_pipe(enc=nil)
23     r, w = IO.pipe(enc)
24     begin
25       yield r, w
26     ensure
27       r.close if !r.closed?
28       w.close if !w.closed?
29     end
30   end
32   def generate_file(path, content)
33     open(path, "wb") {|f| f.write content }
34   end
36   def encdump(str)
37     "#{str.dump}.force_encoding(#{str.encoding.name.dump})"
38   end
40   def assert_str_equal(expected, actual, message=nil)
41     full_message = build_message(message, <<EOT)
42 #{encdump expected} expected but not equal to
43 #{encdump actual}.
44 EOT
45     assert_block(full_message) { expected == actual }
46   end
48   def test_open_r
49     with_tmpdir {
50       generate_file('tmp', "")
51       open("tmp", "r") {|f|
52         assert_equal(Encoding.default_external, f.external_encoding)
53         assert_equal(nil, f.internal_encoding)
54       }
55     }
56   end
58   def test_open_rb
59     with_tmpdir {
60       generate_file('tmp', "")
61       open("tmp", "rb") {|f|
62         assert_equal(Encoding.default_external, f.external_encoding)
63         assert_equal(nil, f.internal_encoding)
64       }
65     }
66   end
68   def test_open_r_enc
69     with_tmpdir {
70       generate_file('tmp', "")
71       open("tmp", "r:euc-jp") {|f|
72         assert_equal(Encoding::EUC_JP, f.external_encoding)
73         assert_equal(nil, f.internal_encoding)
74       }
75     }
76   end
78   def test_open_r_enc_enc
79     with_tmpdir {
80       generate_file('tmp', "")
81       open("tmp", "r:euc-jp:utf-8") {|f|
82         assert_equal(Encoding::EUC_JP, f.external_encoding)
83         assert_equal(Encoding::UTF_8, f.internal_encoding)
84       }
85     }
86   end
88   def test_open_w
89     with_tmpdir {
90       open("tmp", "w") {|f|
91         assert_equal(nil, f.external_encoding)
92         assert_equal(nil, f.internal_encoding)
93       }
94     }
95   end
97   def test_open_wb
98     with_tmpdir {
99       open("tmp", "wb") {|f|
100         assert_equal(nil, f.external_encoding)
101         assert_equal(nil, f.internal_encoding)
102       }
103     }
104   end
106   def test_open_w_enc
107     with_tmpdir {
108       open("tmp", "w:euc-jp") {|f|
109         assert_equal(Encoding::EUC_JP, f.external_encoding)
110         assert_equal(nil, f.internal_encoding)
111       }
112     }
113   end
115   def test_open_w_enc_enc
116     with_tmpdir {
117       open("tmp", "w:euc-jp:utf-8") {|f|
118         assert_equal(Encoding::EUC_JP, f.external_encoding)
119         assert_equal(Encoding::UTF_8, f.internal_encoding)
120       }
121     }
122   end
124   def test_open_w_enc_enc_perm
125     with_tmpdir {
126       open("tmp", "w:euc-jp:utf-8", 0600) {|f|
127         assert_equal(Encoding::EUC_JP, f.external_encoding)
128         assert_equal(Encoding::UTF_8, f.internal_encoding)
129       }
130     }
131   end
133   def test_stdin
134     assert_equal(Encoding.default_external, STDIN.external_encoding)
135     assert_equal(nil, STDIN.internal_encoding)
136   end
138   def test_stdout
139     assert_equal(nil, STDOUT.external_encoding)
140     assert_equal(nil, STDOUT.internal_encoding)
141   end
143   def test_stderr
144     assert_equal(nil, STDERR.external_encoding)
145     assert_equal(nil, STDERR.internal_encoding)
146   end
148   def test_terminator_conversion
149     with_tmpdir {
150       generate_file('tmp', "before \u00FF after")
151       s = open("tmp", "r:utf-8:iso-8859-1") {|f|
152         f.gets("\xFF".force_encoding("iso-8859-1"))
153       }
154       assert_equal(Encoding.find("iso-8859-1"), s.encoding)
155       assert_str_equal("before \xFF".force_encoding("iso-8859-1"), s, '[ruby-core:14288]')
156     }
157   end
159   def test_terminator_conversion2
160     with_tmpdir {
161       generate_file('tmp', "before \xA1\xA2\xA2\xA3 after")
162       s = open("tmp", "r:euc-jp:utf-8") {|f|
163         f.gets("\xA2\xA2".force_encoding("euc-jp").encode("utf-8"))
164       }
165       assert_equal(Encoding.find("utf-8"), s.encoding)
166       assert_str_equal("before \xA1\xA2\xA2\xA3 after".force_encoding("euc-jp").encode("utf-8"), s, '[ruby-core:14319]')
167     }
168   end
170   def test_terminator_stateful_conversion
171     with_tmpdir {
172       src = "before \e$B\x23\x30\x23\x31\e(B after".force_encoding("iso-2022-jp")
173       generate_file('tmp', src)
174       s = open("tmp", "r:iso-2022-jp:euc-jp") {|f|
175         f.gets("0".force_encoding("euc-jp"))
176       }
177       assert_equal(Encoding.find("euc-jp"), s.encoding)
178       assert_str_equal(src.encode("euc-jp"), s)
179     }
180   end
182   def test_nonascii_terminator
183     with_tmpdir {
184       generate_file('tmp', "before \xA2\xA2 after")
185       open("tmp", "r:euc-jp") {|f|
186         assert_raise(ArgumentError) {
187           f.gets("\xA2\xA2".force_encoding("utf-8"))
188         }
189       }
190     }
191   end
193   def test_pipe_terminator_conversion
194     with_pipe("euc-jp:utf-8") {|r, w|
195       w.write "before \xa2\xa2 after"
196       rs = "\xA2\xA2".encode("utf-8", "euc-jp")
197       w.close
198       timeout(1) {
199         assert_equal("before \xa2\xa2".encode("utf-8", "euc-jp"),
200                      r.gets(rs))
201       }
202     }
203   end
205   def test_pipe_conversion
206     with_pipe("euc-jp:utf-8") {|r, w|
207       w.write "\xa1\xa1"
208       assert_equal("\xa1\xa1".encode("utf-8", "euc-jp"), r.getc)
209     }
210   end
212   def test_pipe_convert_partial_read
213     with_pipe("euc-jp:utf-8") {|r, w|
214       begin
215         t = Thread.new {
216           w.write "\xa1"
217           sleep 0.1
218           w.write "\xa1"
219         }
220         assert_equal("\xa1\xa1".encode("utf-8", "euc-jp"), r.getc)
221       ensure
222         t.join if t
223       end
224     }
225   end
227   def test_getc_invalid
228     with_pipe("euc-jp:utf-8") {|r, w|
229       w << "\xa1xyz"
230       w.close
231       err = assert_raise(Encoding::InvalidByteSequence) { r.getc }
232       assert_equal("\xA1".force_encoding("ascii-8bit"), err.error_bytes)
233       assert_equal("xyz", r.read(10))
234     }
235   end
237   def test_getc_stateful_conversion
238     with_tmpdir {
239       src = "\e$B\x23\x30\x23\x31\e(B".force_encoding("iso-2022-jp")
240       generate_file('tmp', src)
241       open("tmp", "r:iso-2022-jp:euc-jp") {|f|
242         assert_equal("\xa3\xb0".force_encoding("euc-jp"), f.getc)
243         assert_equal("\xa3\xb1".force_encoding("euc-jp"), f.getc)
244       }
245     }
246   end
248   def test_ungetc_stateful_conversion
249     with_tmpdir {
250       src = "before \e$B\x23\x30\x23\x31\e(B after".force_encoding("iso-2022-jp")
251       generate_file('tmp', src)
252       s = open("tmp", "r:iso-2022-jp:euc-jp") {|f|
253         f.ungetc("0".force_encoding("euc-jp"))
254         f.read
255       }
256       assert_equal(Encoding.find("euc-jp"), s.encoding)
257       assert_str_equal("0" + src.encode("euc-jp"), s)
258     }
259   end
261   def test_ungetc_stateful_conversion2
262     with_tmpdir {
263       src =    "before \e$B\x23\x30\x23\x31\e(B after".force_encoding("iso-2022-jp")
264       former = "before \e$B\x23\x30\e(B".force_encoding("iso-2022-jp")
265       rs =            "\e$B\x23\x30\e(B".force_encoding("iso-2022-jp")
266       latter =                "\e$B\x23\x31\e(B after".force_encoding("iso-2022-jp")
267       generate_file('tmp', src)
268       s = open("tmp", "r:iso-2022-jp:euc-jp") {|f|
269         assert_equal(former.encode("euc-jp", "iso-2022-jp"),
270                      f.gets(rs.encode("euc-jp", "iso-2022-jp")))
271         f.ungetc("0")
272         f.read
273       }
274       assert_equal(Encoding.find("euc-jp"), s.encoding)
275       assert_str_equal("0" + latter.encode("euc-jp"), s)
276     }
277   end
279   def test_open_ascii
280     with_tmpdir {
281       src = "abc\n"
282       generate_file('tmp', "abc\n")
283       ENCS.each {|enc|
284         s = open('tmp', "r:#{enc}") {|f| f.gets }
285         assert_equal(enc, s.encoding)
286         assert_str_equal(src, s)
287       }
288     }
289   end
291   def test_open_nonascii
292     with_tmpdir {
293       src = "\xc2\xa1\n"
294       generate_file('tmp', src)
295       ENCS.each {|enc|
296         content = src.dup.force_encoding(enc)
297         s = open('tmp', "r:#{enc}") {|f| f.gets }
298         assert_equal(enc, s.encoding)
299         assert_str_equal(content, s)
300       }
301     }
302   end
304   def test_read_encoding
305     with_tmpdir {
306       src = "\xc2\xa1\n".force_encoding("ASCII-8BIT")
307       generate_file('tmp', "\xc2\xa1\n")
308       ENCS.each {|enc|
309         content = src.dup.force_encoding(enc)
310         open('tmp', "r:#{enc}") {|f|
311           s = f.getc
312           assert_equal(enc, s.encoding)
313           assert_str_equal(content[0], s)
314         }
315         open('tmp', "r:#{enc}") {|f|
316           s = f.readchar
317           assert_equal(enc, s.encoding)
318           assert_str_equal(content[0], s)
319         }
320         open('tmp', "r:#{enc}") {|f|
321           s = f.gets
322           assert_equal(enc, s.encoding)
323           assert_str_equal(content, s)
324         }
325         open('tmp', "r:#{enc}") {|f|
326           s = f.readline
327           assert_equal(enc, s.encoding)
328           assert_str_equal(content, s)
329         }
330         open('tmp', "r:#{enc}") {|f|
331           lines = f.readlines
332           assert_equal(1, lines.length)
333           s = lines[0]
334           assert_equal(enc, s.encoding)
335           assert_str_equal(content, s)
336         }
337         open('tmp', "r:#{enc}") {|f|
338           f.each_line {|s|
339             assert_equal(enc, s.encoding)
340             assert_str_equal(content, s)
341           }
342         }
343         open('tmp', "r:#{enc}") {|f|
344           s = f.read
345           assert_equal(enc, s.encoding)
346           assert_str_equal(content, s)
347         }
348         open('tmp', "r:#{enc}") {|f|
349           s = f.read(1)
350           assert_equal(Encoding::ASCII_8BIT, s.encoding)
351           assert_str_equal(src[0], s)
352         }
353         open('tmp', "r:#{enc}") {|f|
354           s = f.readpartial(1)
355           assert_equal(Encoding::ASCII_8BIT, s.encoding)
356           assert_str_equal(src[0], s)
357         }
358         open('tmp', "r:#{enc}") {|f|
359           s = f.sysread(1)
360           assert_equal(Encoding::ASCII_8BIT, s.encoding)
361           assert_str_equal(src[0], s)
362         }
363       }
364     }
365   end
367   def test_write_noenc
368     src = "\xc2\xa1\n".force_encoding("ascii-8bit")
369     with_tmpdir {
370       open('tmp', "w") {|f|
371         ENCS.each {|enc|
372           f.write src.dup.force_encoding(enc)
373         }
374       }
375       open('tmp', 'r:ascii-8bit') {|f|
376         assert_equal(src*ENCS.length, f.read)
377       }
378     }
379   end
381   def test_write_conversion
382     utf8 = "\u6666"
383     eucjp = "\xb3\xa2".force_encoding("EUC-JP")
384     with_tmpdir {
385       open('tmp', "w:EUC-JP") {|f|
386         assert_equal(Encoding::EUC_JP, f.external_encoding)
387         assert_equal(nil, f.internal_encoding)
388         f.print utf8
389       }
390       assert_equal(eucjp, File.read('tmp').force_encoding("EUC-JP"))
391       open('tmp', 'r:EUC-JP:UTF-8') {|f|
392         assert_equal(Encoding::EUC_JP, f.external_encoding)
393         assert_equal(Encoding::UTF_8, f.internal_encoding)
394         assert_equal(utf8, f.read)
395       }
396     }
397   end
399   def test_pipe
400     utf8 = "\u6666"
401     eucjp = "\xb3\xa2".force_encoding("EUC-JP")
403     with_pipe {|r,w|
404       assert_equal(Encoding.default_external, r.external_encoding)
405       assert_equal(nil, r.internal_encoding)
406       w << utf8
407       w.close
408       s = r.read
409       assert_equal(Encoding.default_external, s.encoding)
410       assert_str_equal(utf8.dup.force_encoding(Encoding.default_external), s)
411     }
413     with_pipe("EUC-JP") {|r,w|
414       assert_equal(Encoding::EUC_JP, r.external_encoding)
415       assert_equal(nil, r.internal_encoding)
416       w << eucjp
417       w.close
418       assert_equal(eucjp, r.read)
419     }
421     with_pipe("UTF-8:EUC-JP") {|r,w|
422       assert_equal(Encoding::UTF_8, r.external_encoding)
423       assert_equal(Encoding::EUC_JP, r.internal_encoding)
424       w << utf8
425       w.close
426       assert_equal(eucjp, r.read)
427     }
429     ENCS.each {|enc|
430       with_pipe(enc) {|r, w|
431         w << "\xc2\xa1"
432         w.close
433         s = r.getc 
434         assert_equal(enc, s.encoding)
435       }
436     }
438     ENCS.each {|enc|
439       next if enc == Encoding::ASCII_8BIT
440       next if enc == Encoding::UTF_8
441       with_pipe("#{enc}:UTF-8") {|r, w|
442         w << "\xc2\xa1"
443         w.close
444         s = r.read
445         assert_equal(Encoding::UTF_8, s.encoding)
446         assert_equal(s.encode("UTF-8"), s)
447       }
448     }
450   end
452   def test_marshal
453     with_pipe("EUC-JP") {|r, w|
454       data = 56225
455       Marshal.dump(data, w)
456       w.close
457       result = nil
458       assert_nothing_raised("[ruby-dev:33264]") { result = Marshal.load(r) }
459       assert_equal(data, result)
460     }
461   end
463   def test_gets_nil
464     with_pipe("UTF-8:EUC-JP") {|r, w|
465       w << "\u{3042}"
466       w.close
467       result = r.gets(nil)
468       assert_equal("\u{3042}".encode("euc-jp"), result)
469     }
470   end
472   def test_gets_limit
473     with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
474       assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.gets(1))
475     }
476     with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
477       assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.gets(2))
478     }
479     with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
480       assert_equal("\xa4\xa2\xa4\xa4".force_encoding("euc-jp"), r.gets(3))
481     }
482     with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
483       assert_equal("\xa4\xa2\xa4\xa4".force_encoding("euc-jp"), r.gets(4))
484     }
485     with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
486       assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6".force_encoding("euc-jp"), r.gets(5))
487     }
488     with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
489       assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6".force_encoding("euc-jp"), r.gets(6))
490     }
491     with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
492       assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6\n".force_encoding("euc-jp"), r.gets(7))
493     }
494     with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
495       assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6\n".force_encoding("euc-jp"), r.gets(8))
496     }
497     with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
498       assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6\n".force_encoding("euc-jp"), r.gets(9))
499     }
500   end
502   def test_gets_invalid
503     with_pipe("utf-8:euc-jp") {|r, w|
504       before = "\u{3042}\u{3044}"
505       invalid = "\x80".force_encoding("utf-8")
506       after = "\u{3046}\u{3048}"
507       w << before + invalid + after
508       w.close
509       err = assert_raise(Encoding::InvalidByteSequence) { r.gets }
510       assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
511       assert_equal(after.encode("euc-jp"), r.gets)
512     }
513   end
515   def test_getc_invalid
516     with_pipe("utf-8:euc-jp") {|r, w|
517       before1 = "\u{3042}"
518       before2 = "\u{3044}"
519       invalid = "\x80".force_encoding("utf-8")
520       after1 = "\u{3046}"
521       after2 = "\u{3048}"
522       w << before1 + before2 + invalid + after1 + after2
523       w.close
524       assert_equal(before1.encode("euc-jp"), r.getc)
525       assert_equal(before2.encode("euc-jp"), r.getc)
526       err = assert_raise(Encoding::InvalidByteSequence) { r.getc }
527       assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
528       assert_equal(after1.encode("euc-jp"), r.getc)
529       assert_equal(after2.encode("euc-jp"), r.getc)
530     }
531   end
533   def test_getc_invalid2
534     with_pipe("utf-16le:euc-jp") {|r, w|
535       before1 = "\x42\x30".force_encoding("utf-16le")
536       before2 = "\x44\x30".force_encoding("utf-16le")
537       invalid = "\x00\xd8".force_encoding("utf-16le")
538       after1 = "\x46\x30".force_encoding("utf-16le")
539       after2 = "\x48\x30".force_encoding("utf-16le")
540       w << before1 + before2 + invalid + after1 + after2
541       w.close
542       assert_equal(before1.encode("euc-jp"), r.getc)
543       assert_equal(before2.encode("euc-jp"), r.getc)
544       err = assert_raise(Encoding::InvalidByteSequence) { r.getc }
545       assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
546       assert_equal(after1.encode("euc-jp"), r.getc)
547       assert_equal(after2.encode("euc-jp"), r.getc)
548     }
549   end
551   def test_read_all
552     with_pipe("utf-8:euc-jp") {|r, w|
553       str = "\u3042\u3044"
554       w << str
555       w.close
556       assert_equal(str.encode("euc-jp"), r.read)
557     }
558   end
560   def test_read_all_invalid
561     with_pipe("utf-8:euc-jp") {|r, w|
562       before = "\u{3042}\u{3044}"
563       invalid = "\x80".force_encoding("utf-8")
564       after = "\u{3046}\u{3048}"
565       w << before + invalid + after
566       w.close
567       err = assert_raise(Encoding::InvalidByteSequence) { r.read }
568       assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
569       assert_equal(after.encode("euc-jp"), r.read)
570     }
571   end
573   def test_file_foreach
574     with_tmpdir {
575       generate_file('tst', 'a' * 8191 + "\xa1\xa1")
576       assert_nothing_raised {
577         File.foreach('tst', :encoding=>"euc-jp") {|line| line.inspect }
578       }
579     }
580   end
582   def test_set_encoding
583     with_pipe("utf-8:euc-jp") {|r, w|
584       s = "\u3042".force_encoding("ascii-8bit")
585       s << "\x82\xa0".force_encoding("ascii-8bit")
586       w << s
587       w.close
588       assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
589       r.set_encoding("shift_jis:euc-jp")
590       assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
591     }
592   end
594   def test_set_encoding2
595     with_pipe("utf-8:euc-jp") {|r, w|
596       s = "\u3042".force_encoding("ascii-8bit")
597       s << "\x82\xa0".force_encoding("ascii-8bit")
598       w << s
599       w.close
600       assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
601       r.set_encoding("shift_jis", "euc-jp")
602       assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
603     }
604   end
606   def test_set_encoding_nil
607     with_pipe("utf-8:euc-jp") {|r, w|
608       s = "\u3042".force_encoding("ascii-8bit")
609       s << "\x82\xa0".force_encoding("ascii-8bit")
610       w << s
611       w.close
612       assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
613       r.set_encoding(nil)
614       assert_equal("\x82\xa0".force_encoding(Encoding.default_external), r.read)
615     }
616   end
618   def test_set_encoding_enc
619     with_pipe("utf-8:euc-jp") {|r, w|
620       s = "\u3042".force_encoding("ascii-8bit")
621       s << "\x82\xa0".force_encoding("ascii-8bit")
622       w << s
623       w.close
624       assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
625       r.set_encoding(Encoding::Shift_JIS)
626       assert_equal("\x82\xa0".force_encoding(Encoding::Shift_JIS), r.getc)
627     }
628   end
630   def test_write_conversion_fixenc
631     with_pipe {|r, w|
632       w.set_encoding("iso-2022-jp:utf-8")
633       t = Thread.new { r.read.force_encoding("ascii-8bit") }
634       w << "\u3042"
635       w << "\u3044"
636       w.close
637       assert_equal("\e$B$\"$$\e(B".force_encoding("ascii-8bit"), t.value)
638     }
639   end
641   def test_write_conversion_anyenc_stateful
642     with_pipe {|r, w|
643       w.set_encoding("iso-2022-jp")
644       t = Thread.new { r.read.force_encoding("ascii-8bit") }
645       w << "\u3042"
646       w << "\x82\xa2".force_encoding("sjis")
647       w.close
648       assert_equal("\e$B$\"$$\e(B".force_encoding("ascii-8bit"), t.value)
649     }
650   end
652   def test_write_conversion_anyenc_stateless
653     with_pipe {|r, w|
654       w.set_encoding("euc-jp")
655       t = Thread.new { r.read.force_encoding("ascii-8bit") }
656       w << "\u3042"
657       w << "\x82\xa2".force_encoding("sjis")
658       w.close
659       assert_equal("\xa4\xa2\xa4\xa4".force_encoding("ascii-8bit"), t.value)
660     }
661   end
663   def test_write_conversion_anyenc_stateful_nosync
664     with_pipe {|r, w|
665       w.sync = false
666       w.set_encoding("iso-2022-jp")
667       t = Thread.new { r.read.force_encoding("ascii-8bit") }
668       w << "\u3042"
669       w << "\x82\xa2".force_encoding("sjis")
670       w.close
671       assert_equal("\e$B$\"$$\e(B".force_encoding("ascii-8bit"), t.value)
672     }
673   end
675   def test_stdin_external_encoding_with_reopen
676     with_tmpdir {
677       open("tst", "w+") {|f|
678         pid = spawn(EnvUtil.rubybin, '-e', <<-'End', 10=>f)
679           io = IO.new(10, "r+")
680           STDIN.reopen(io)
681           STDIN.external_encoding
682           STDIN.write "\u3042"
683           STDIN.flush 
684         End
685         Process.wait pid
686         f.rewind
687         result = f.read.force_encoding("ascii-8bit")
688         assert_equal("\u3042".force_encoding("ascii-8bit"), result)
689       }
690     }
691   end