summaryrefslogtreecommitdiff
path: root/test/stringio/test_stringio.rb
blob: 34c47481853a94bf08602b41cf349fb002636d24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
# frozen_string_literal: false
require 'test/unit'
require 'stringio'
require "rbconfig/sizeof"
require_relative '../ruby/ut_eof'

class TestStringIO < Test::Unit::TestCase
  include TestEOF
  def open_file(content)
    f = StringIO.new(content)
    yield f
  end
  alias open_file_rw open_file

  include TestEOF::Seek

  def test_initialize
    assert_kind_of StringIO, StringIO.new
    assert_kind_of StringIO, StringIO.new('str')
    assert_kind_of StringIO, StringIO.new('str', 'r+')
    assert_raise(ArgumentError) { StringIO.new('', 'x') }
    assert_raise(ArgumentError) { StringIO.new('', 'rx') }
    assert_raise(ArgumentError) { StringIO.new('', 'rbt') }
    assert_raise(TypeError) { StringIO.new(nil) }

    o = Object.new
    def o.to_str
      nil
    end
    assert_raise(TypeError) { StringIO.new(o) }

    o = Object.new
    def o.to_str
      'str'
    end
    assert_kind_of StringIO, StringIO.new(o)
  end

  def test_truncate
    io = StringIO.new("")
    io.puts "abc"
    assert_equal(0, io.truncate(0))
    io.puts "def"
    assert_equal("\0\0\0\0def\n", io.string, "[ruby-dev:24190]")
    assert_raise(Errno::EINVAL) { io.truncate(-1) }
    assert_equal(0, io.truncate(10))
    assert_equal("\0\0\0\0def\n\0\0", io.string)
  end

  def test_seek_beyond_eof
    io = StringIO.new
    n = 100
    io.seek(n)
    io.print "last"
    assert_equal("\0" * n + "last", io.string, "[ruby-dev:24194]")
  end

  def test_overwrite
    stringio = StringIO.new
    responses = ['', 'just another ruby', 'hacker']
    responses.each do |resp|
      stringio.puts(resp)
      stringio.rewind
    end
    assert_equal("hacker\nother ruby\n", stringio.string, "[ruby-core:3836]")
  end

  def test_gets
    assert_equal(nil, StringIO.new("").gets)
    assert_equal("\n", StringIO.new("\n").gets)
    assert_equal("a\n", StringIO.new("a\n").gets)
    assert_equal("a\n", StringIO.new("a\nb\n").gets)
    assert_equal("a", StringIO.new("a").gets)
    assert_equal("a\n", StringIO.new("a\nb").gets)
    assert_equal("abc\n", StringIO.new("abc\n\ndef\n").gets)
    assert_equal("abc\n\ndef\n", StringIO.new("abc\n\ndef\n").gets(nil))
    assert_equal("abc\n\n", StringIO.new("abc\n\ndef\n").gets(""))
    stringio = StringIO.new("abc\n\ndef\n")
    assert_equal("abc\n\n", stringio.gets(""))
    assert_equal("def\n", stringio.gets(""))
    assert_raise(TypeError){StringIO.new("").gets(1, 1)}
    assert_nothing_raised {StringIO.new("").gets(nil, nil)}

    assert_string("", Encoding::UTF_8, StringIO.new("foo").gets(0))
  end

  def test_gets_chomp
    assert_equal(nil, StringIO.new("").gets(chomp: true))
    assert_equal("", StringIO.new("\n").gets(chomp: true))
    assert_equal("a", StringIO.new("a\n").gets(chomp: true))
    assert_equal("a", StringIO.new("a\nb\n").gets(chomp: true))
    assert_equal("a", StringIO.new("a").gets(chomp: true))
    assert_equal("a", StringIO.new("a\nb").gets(chomp: true))
    assert_equal("abc", StringIO.new("abc\n\ndef\n").gets(chomp: true))
    assert_equal("abc\n\ndef\n", StringIO.new("abc\n\ndef\n").gets(nil, chomp: true))
    assert_equal("abc", StringIO.new("abc\n\ndef\n").gets("", chomp: true))
    stringio = StringIO.new("abc\n\ndef\n")
    assert_equal("abc", stringio.gets("", chomp: true))
    assert_equal("def\n", stringio.gets("", chomp: true))

    assert_string("", Encoding::UTF_8, StringIO.new("\n").gets(chomp: true))
  end

  def test_gets_chomp_eol
    assert_equal(nil, StringIO.new("").gets(chomp: true))
    assert_equal("", StringIO.new("\r\n").gets(chomp: true))
    assert_equal("a", StringIO.new("a\r\n").gets(chomp: true))
    assert_equal("a", StringIO.new("a\r\nb\r\n").gets(chomp: true))
    assert_equal("a", StringIO.new("a").gets(chomp: true))
    assert_equal("a", StringIO.new("a\r\nb").gets(chomp: true))
    assert_equal("abc", StringIO.new("abc\r\n\r\ndef\r\n").gets(chomp: true))
    assert_equal("abc\r\n\r\ndef\r\n", StringIO.new("abc\r\n\r\ndef\r\n").gets(nil, chomp: true))
    assert_equal("abc", StringIO.new("abc\r\n\r\ndef\r\n").gets("", chomp: true))
    stringio = StringIO.new("abc\r\n\r\ndef\r\n")
    assert_equal("abc", stringio.gets("", chomp: true))
    assert_equal("def\r\n", stringio.gets("", chomp: true))
  end

  def test_readlines
    assert_equal([], StringIO.new("").readlines)
    assert_equal(["\n"], StringIO.new("\n").readlines)
    assert_equal(["a\n"], StringIO.new("a\n").readlines)
    assert_equal(["a\n", "b\n"], StringIO.new("a\nb\n").readlines)
    assert_equal(["a"], StringIO.new("a").readlines)
    assert_equal(["a\n", "b"], StringIO.new("a\nb").readlines)
    assert_equal(["abc\n", "\n", "def\n"], StringIO.new("abc\n\ndef\n").readlines)
    assert_equal(["abc\n\ndef\n"], StringIO.new("abc\n\ndef\n").readlines(nil), "[ruby-dev:34591]")
    assert_equal(["abc\n\n", "def\n"], StringIO.new("abc\n\ndef\n").readlines(""))
  end

  def test_write
    s = ""
    f = StringIO.new(s, "w")
    f.print("foo")
    f.close
    assert_equal("foo", s)

    f = StringIO.new(s, File::WRONLY)
    f.print("bar")
    f.close
    assert_equal("bar", s)

    f = StringIO.new(s, "a")
    o = Object.new
    def o.to_s; "baz"; end
    f.print(o)
    f.close
    assert_equal("barbaz", s)
  ensure
    f.close unless f.closed?
  end

  def test_write_nonblock_no_exceptions
    s = ""
    f = StringIO.new(s, "w")
    f.write_nonblock("foo", exception: false)
    f.close
    assert_equal("foo", s)
  end

  def test_write_nonblock
    s = ""
    f = StringIO.new(s, "w")
    f.write_nonblock("foo")
    f.close
    assert_equal("foo", s)

    f = StringIO.new(s, File::WRONLY)
    f.write_nonblock("bar")
    f.close
    assert_equal("bar", s)

    f = StringIO.new(s, "a")
    o = Object.new
    def o.to_s; "baz"; end
    f.write_nonblock(o)
    f.close
    assert_equal("barbaz", s)
  ensure
    f.close unless f.closed?
  end

  def test_write_encoding
    s = "".force_encoding(Encoding::UTF_8)
    f = StringIO.new(s)
    f.print("\u{3053 3093 306b 3061 306f ff01}".b)
    assert_equal(Encoding::UTF_8, s.encoding, "honor the original encoding over ASCII-8BIT")
  end

  def test_write_encoding_conversion
    convertible = "\u{3042}"
    inconvertible = "\u{1f363}"
    conversion_encoding = Encoding::Windows_31J

    s = StringIO.new.set_encoding(conversion_encoding)
    s.write(convertible)
    assert_equal(conversion_encoding, s.string.encoding)

    s = StringIO.new.set_encoding(Encoding::UTF_8)
    s.write("foo".force_encoding("ISO-8859-1"), convertible)
    assert_equal(Encoding::UTF_8, s.string.encoding)

    s = StringIO.new.set_encoding(Encoding::US_ASCII)
    s.write("foo".force_encoding("US-ASCII"), convertible)
    assert_equal(Encoding::UTF_8, s.string.encoding)

    all_assertions do |a|
      [
        inconvertible,
        convertible + inconvertible,
        [convertible, inconvertible],
        ["a", inconvertible],
      ].each do |data|
        a.for(data.inspect) do
          s = StringIO.new.set_encoding(conversion_encoding)
          assert_raise(Encoding::CompatibilityError) do
            s.write(*data)
          end
        end
      end
    end
  end

  def test_write_integer_overflow
    f = StringIO.new
    f.pos = RbConfig::LIMITS["LONG_MAX"]
    assert_raise(ArgumentError) {
      f.write("pos + len overflows")
    }
  end

  def test_write_with_multiple_arguments
    s = ""
    f = StringIO.new(s, "w")
    f.write("foo", "bar")
    f.close
    assert_equal("foobar", s)
  ensure
    f.close unless f.closed?
  end

  def test_set_encoding
    bug10285 = '[ruby-core:65240] [Bug #10285]'
    f = StringIO.new()
    f.set_encoding(Encoding::ASCII_8BIT)
    f.write("quz \x83 mat".b)
    s = "foo \x97 bar".force_encoding(Encoding::WINDOWS_1252)
    assert_nothing_raised(Encoding::CompatibilityError, bug10285) {
      f.write(s)
    }
    assert_equal(Encoding::ASCII_8BIT, f.string.encoding, bug10285)

    bug11827 = '[ruby-core:72189] [Bug #11827]'
    f = StringIO.new("foo\x83".freeze)
    assert_nothing_raised(RuntimeError, bug11827) {
      f.set_encoding(Encoding::ASCII_8BIT)
    }
    assert_equal("foo\x83".b, f.gets)

    f = StringIO.new()
    f.set_encoding("ISO-8859-16:ISO-8859-1")
    assert_equal(Encoding::ISO_8859_16, f.external_encoding)
    assert_equal(Encoding::ISO_8859_16, f.string.encoding)
    assert_nil(f.internal_encoding)
  end

  def test_mode_error
    f = StringIO.new("", "r")
    assert_raise(IOError) { f.write("foo") }

    f = StringIO.new("", "w")
    assert_raise(IOError) { f.read }

    assert_raise(Errno::EACCES) { StringIO.new("".freeze, "w") }
    s = ""
    f = StringIO.new(s, "w")
    s.freeze
    assert_raise(IOError) { f.write("foo") }

    assert_raise(IOError) { StringIO.allocate.read }
  ensure
    f.close unless f.closed?
  end

  def test_open
    s = ""
    StringIO.open("foo") {|f| s = f.read }
    assert_equal("foo", s)
  end

  def test_isatty
    assert_equal(false, StringIO.new("").isatty)
  end

  def test_fsync
    assert_equal(0, StringIO.new("").fsync)
  end

  def test_sync
    assert_equal(true, StringIO.new("").sync)
    assert_equal(false, StringIO.new("").sync = false)
  end

  def test_set_fcntl
    assert_raise(NotImplementedError) { StringIO.new("").fcntl }
  end

  def test_close
    f = StringIO.new("")
    f.close
    assert_nil(f.close)

    f = StringIO.new("")
    f.close_read
    f.close_write
    assert_nil(f.close)
  ensure
    f.close unless f.closed?
  end

  def test_close_read
    f = StringIO.new("")
    f.close_read
    assert_raise(IOError) { f.read }
    assert_nothing_raised(IOError) {f.close_read}
    f.close

    f = StringIO.new("", "w")
    assert_raise(IOError) { f.close_read }
    f.close
  ensure
    f.close unless f.closed?
  end

  def test_close_write
    f = StringIO.new("")
    f.close_write
    assert_raise(IOError) { f.write("foo") }
    assert_nothing_raised(IOError) {f.close_write}
    f.close

    f = StringIO.new("", "r")
    assert_raise(IOError) { f.close_write }
    f.close
  ensure
    f.close unless f.closed?
  end

  def test_closed
    f = StringIO.new("")
    assert_equal(false, f.closed?)
    f.close
    assert_equal(true, f.closed?)
  ensure
    f.close unless f.closed?
  end

  def test_closed_read
    f = StringIO.new("")
    assert_equal(false, f.closed_read?)
    f.close_write
    assert_equal(false, f.closed_read?)
    f.close_read
    assert_equal(true, f.closed_read?)
  ensure
    f.close unless f.closed?
  end

  def test_closed_write
    f = StringIO.new("")
    assert_equal(false, f.closed_write?)
    f.close_read
    assert_equal(false, f.closed_write?)
    f.close_write
    assert_equal(true, f.closed_write?)
  ensure
    f.close unless f.closed?
  end

  def test_dup
    f1 = StringIO.new("1234")
    assert_equal("1", f1.getc)
    f2 = f1.dup
    assert_equal("2", f2.getc)
    assert_equal("3", f1.getc)
    assert_equal("4", f2.getc)
    assert_equal(nil, f1.getc)
    assert_equal(true, f2.eof?)
    f1.close
    assert_equal(false, f2.closed?, '[ruby-core:48443]')
  ensure
    f1.close unless f1.closed?
    f2.close unless f2.closed?
  end

  def test_lineno
    f = StringIO.new("foo\nbar\nbaz\n")
    assert_equal([0, "foo\n"], [f.lineno, f.gets])
    assert_equal([1, "bar\n"], [f.lineno, f.gets])
    f.lineno = 1000
    assert_equal([1000, "baz\n"], [f.lineno, f.gets])
    assert_equal([1001, nil], [f.lineno, f.gets])
  ensure
    f.close unless f.closed?
  end

  def test_pos
    f = StringIO.new("foo\nbar\nbaz\n")
    assert_equal([0, "foo\n"], [f.pos, f.gets])
    assert_equal([4, "bar\n"], [f.pos, f.gets])
    assert_raise(Errno::EINVAL) { f.pos = -1 }
    f.pos = 1
    assert_equal([1, "oo\n"], [f.pos, f.gets])
    assert_equal([4, "bar\n"], [f.pos, f.gets])
    assert_equal([8, "baz\n"], [f.pos, f.gets])
    assert_equal([12, nil], [f.pos, f.gets])
  ensure
    f.close unless f.closed?
  end

  def test_reopen
    f = StringIO.new("foo\nbar\nbaz\n")
    assert_equal("foo\n", f.gets)
    f.reopen("qux\nquux\nquuux\n")
    assert_equal("qux\n", f.gets)

    f2 = StringIO.new("")
    f2.reopen(f)
    assert_equal("quux\n", f2.gets)
  ensure
    f.close unless f.closed?
  end

  def test_seek
    f = StringIO.new("1234")
    assert_raise(Errno::EINVAL) { f.seek(-1) }
    f.seek(-1, 2)
    assert_equal("4", f.getc)
    assert_raise(Errno::EINVAL) { f.seek(1, 3) }
    f.close
    assert_raise(IOError) { f.seek(0) }
  ensure
    f.close unless f.closed?
  end

  def test_each_byte
    f = StringIO.new("1234")
    a = []
    f.each_byte {|c| a << c }
    assert_equal(%w(1 2 3 4).map {|c| c.ord }, a)
  ensure
    f.close unless f.closed?
  end

  def test_each_byte_closed
    f = StringIO.new("1234")
    assert_equal("1".ord, f.each_byte {|c| f.close; break c })
    f = StringIO.new("1234")
    assert_raise(IOError) do
      f.each_byte { f.close }
    end
  end

  def test_getbyte
    f = StringIO.new("1234")
    assert_equal("1".ord, f.getbyte)
    assert_equal("2".ord, f.getbyte)
    assert_equal("3".ord, f.getbyte)
    assert_equal("4".ord, f.getbyte)
    assert_equal(nil, f.getbyte)
  ensure
    f.close unless f.closed?
  end

  def test_ungetbyte
    s = "foo\nbar\n"
    t = StringIO.new(s, "r")
    t.ungetbyte(0x41)
    assert_equal(0x41, t.getbyte)
    t.ungetbyte("qux")
    assert_equal("quxfoo\n", t.gets)
    t.set_encoding("utf-8")
    t.ungetbyte(0x89)
    t.ungetbyte(0x8e)
    t.ungetbyte("\xe7")
    t.ungetbyte("\xe7\xb4\x85")
    assert_equal("\u7d05\u7389bar\n", t.gets)
    assert_equal("q\u7d05\u7389bar\n", s)
    t.pos = 1
    t.ungetbyte("\u{30eb 30d3 30fc}")
    assert_equal(0, t.pos)
    assert_equal("\u{30eb 30d3 30fc}\u7d05\u7389bar\n", s)

    assert_nothing_raised {t.ungetbyte(-1)}
    assert_nothing_raised {t.ungetbyte(256)}
    assert_nothing_raised {t.ungetbyte(1<<64)}
  end

  def test_ungetc
    s = "1234"
    f = StringIO.new(s, "r")
    assert_nothing_raised { f.ungetc("x") }
    assert_equal("x", f.getc) # bug? -> it's a feature from 1.9.
    assert_equal("1", f.getc)

    s = "1234"
    f = StringIO.new(s, "r")
    assert_equal("1", f.getc)
    f.ungetc("y".ord)
    assert_equal("y", f.getc)
    assert_equal("2", f.getc)

    assert_raise(RangeError) {f.ungetc(0x1ffffff)}
    assert_raise(RangeError) {f.ungetc(0xffffffffffffff)}
  ensure
    f.close unless f.closed?
  end

  def test_readchar
    f = StringIO.new("1234")
    a = ""
    assert_raise(EOFError) { loop { a << f.readchar } }
    assert_equal("1234", a)
  end

  def test_readbyte
    f = StringIO.new("1234")
    a = []
    assert_raise(EOFError) { loop { a << f.readbyte } }
    assert_equal("1234".unpack("C*"), a)
  end

  def test_each_char
    f = StringIO.new("1234")
    assert_equal(%w(1 2 3 4), f.each_char.to_a)
  end

  def test_each_char_closed
    f = StringIO.new("1234")
    assert_equal("1", f.each_char {|c| f.close; break c })
    f = StringIO.new("1234")
    assert_raise(IOError) do
      f.each_char { f.close }
    end
  end

  def test_each_codepoint
    f = StringIO.new("1234")
    assert_equal([49, 50, 51, 52], f.each_codepoint.to_a)
  end

  def test_each_codepoint_closed
    f = StringIO.new("1234")
    assert_equal("1".ord, f.each_codepoint {|c| f.close; break c })
    f = StringIO.new("1234")
    assert_raise(IOError) do
      f.each_codepoint { f.close }
    end
  end

  def test_each_codepoint_enumerator
    io = StringIO.new('你好построить')

    chinese_part = io.each_codepoint.take(2).pack('U*')
    russian_part = io.read(40).force_encoding('UTF-8')

    assert_equal("你好", chinese_part)
    assert_equal("построить", russian_part)
  end

  def test_gets2
    f = StringIO.new("foo\nbar\nbaz\n")
    assert_equal("fo", f.gets(2))

    o = Object.new
    def o.to_str; "z"; end
    assert_equal("o\nbar\nbaz", f.gets(o))

    f = StringIO.new("foo\nbar\nbaz\n")
    assert_equal("foo\nbar\nbaz", f.gets("az"))
    f = StringIO.new("a" * 10000 + "zz!")
    assert_equal("a" * 10000 + "zz", f.gets("zz"))
    f = StringIO.new("a" * 10000 + "zz!")
    assert_equal("a" * 10000 + "zz!", f.gets("zzz"))

    bug4112 = '[ruby-dev:42674]'
    ["a".encode("utf-16be"), "\u3042"].each do |s|
      assert_equal(s, StringIO.new(s).gets(1), bug4112)
      assert_equal(s, StringIO.new(s).gets(nil, 1), bug4112)
    end
  end

  def test_each_string_sep
    f = StringIO.new('a||b||c')
    assert_equal(["a||", "b||", "c"], f.each("||").to_a)
    f.rewind
    assert_equal(["a", "b", "c"], f.each("||", chomp: true).to_a)
  end

  def test_each
    f = StringIO.new("foo\nbar\nbaz\n")
    assert_equal(["foo\n", "bar\n", "baz\n"], f.each.to_a)
    f.rewind
    assert_equal(["foo", "bar", "baz"], f.each(chomp: true).to_a)
    f = StringIO.new("foo\nbar\n\n\nbaz\n")
    assert_equal(["foo\nbar\n\n\n", "baz\n"], f.each("").to_a)
    f.rewind
    assert_equal(["foo\nbar", "baz\n"], f.each("", chomp: true).to_a)

    f = StringIO.new("foo\r\nbar\r\n\r\n\r\nbaz\r\n")
    assert_equal(["foo\r\nbar\r\n\r\n\r\n", "baz\r\n"], f.each("").to_a)
    f.rewind
    assert_equal(["foo\r\nbar", "baz\r\n"], f.each("", chomp: true).to_a)

    f = StringIO.new("abc\n\ndef\n")
    assert_equal(["ab", "c\n", "\nd", "ef", "\n"], f.each(nil, 2, chomp: true).to_a)
  end

  def test_putc
    s = ""
    f = StringIO.new(s, "w")
    f.putc("1")
    f.putc("2")
    f.putc("3")
    f.close
    assert_equal("123", s)

    s = "foo"
    f = StringIO.new(s, "a")
    f.putc("1")
    f.putc("2")
    f.putc("3")
    f.close
    assert_equal("foo123", s)
  end

  def test_putc_nonascii
    s = ""
    f = StringIO.new(s, "w")
    f.putc("\u{3042}")
    f.putc(0x3044)
    f.close
    assert_equal("\u{3042}D", s)

    s = "foo"
    f = StringIO.new(s, "a")
    f.putc("\u{3042}")
    f.putc(0x3044)
    f.close
    assert_equal("foo\u{3042}D", s)
  end

  def test_read
    f = StringIO.new("\u3042\u3044")
    assert_raise(ArgumentError) { f.read(-1) }
    assert_raise(ArgumentError) { f.read(1, 2, 3) }
    assert_equal("\u3042\u3044", f.read)
    assert_nil(f.read(1))
    f.rewind
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read(f.size))

    bug5207 = '[ruby-core:39026]'
    f.rewind
    assert_equal("\u3042\u3044", f.read(nil, nil), bug5207)
    f.rewind
    s = ""
    assert_same(s, f.read(nil, s))
    assert_equal("\u3042\u3044", s, bug5207)
    f.rewind
    # not empty buffer
    s = "0123456789"
    assert_same(s, f.read(nil, s))
    assert_equal("\u3042\u3044", s)

    bug13806 = '[ruby-core:82349] [Bug #13806]'
    assert_string("", Encoding::UTF_8, f.read, bug13806)
    assert_string("", Encoding::UTF_8, f.read(nil, nil), bug13806)
    s.force_encoding(Encoding::US_ASCII)
    assert_same(s, f.read(nil, s))
    assert_string("", Encoding::UTF_8, s, bug13806)
  end

  def test_readpartial
    f = StringIO.new("\u3042\u3044")
    assert_raise(ArgumentError) { f.readpartial(-1) }
    assert_raise(ArgumentError) { f.readpartial(1, 2, 3) }
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(100))
    f.rewind
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size))
    f.rewind
    # not empty buffer
    s = '0123456789'
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.readpartial(f.size, s))
  end

  def test_read_nonblock
    f = StringIO.new("\u3042\u3044")
    assert_raise(ArgumentError) { f.read_nonblock(-1) }
    assert_raise(ArgumentError) { f.read_nonblock(1, 2, 3) }
    assert_equal("\u3042\u3044".force_encoding("BINARY"), f.read_nonblock(100))
    assert_raise(EOFError) { f.read_nonblock(10) }
    f.rewind
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size))
  end

  def test_read_nonblock_no_exceptions
    f = StringIO.new("\u3042\u3044")
    assert_raise(ArgumentError) { f.read_nonblock(-1, exception: false) }
    assert_raise(ArgumentError) { f.read_nonblock(1, 2, 3, exception: false) }
    assert_raise(ArgumentError) { f.read_nonblock }
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(100, exception: false))
    assert_equal(nil, f.read_nonblock(10, exception: false))
    f.rewind
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size))
    f.rewind
    # not empty buffer
    s = '0123456789'
    assert_equal("\u3042\u3044".force_encoding(Encoding::ASCII_8BIT), f.read_nonblock(f.size, s))
  end

  def test_sysread
    f = StringIO.new("sysread \u{30c6 30b9 30c8}")
    assert_equal "sysread \u{30c6 30b9 30c8}", f.sysread
    assert_equal "", f.sysread
    assert_raise(EOFError) { f.sysread(1) }
    f.rewind
    assert_equal Encoding::ASCII_8BIT, f.sysread(3).encoding
  end

  def test_size
    f = StringIO.new("1234")
    assert_equal(4, f.size)
  end

  # This test is should in ruby/test_method.rb
  # However this test depends on stringio library,
  # we write it here.
  class C < StringIO
    alias old_init initialize
    attr_reader :foo
    def initialize
      @foo = :ok
      old_init
    end
  end

  def test_method
    assert_equal(:ok, C.new.foo, 'Bug #632 [ruby-core:19282]')
  end

  def test_ungetc_pos
    b = '\\b00010001 \\B00010001 \\b1 \\B1 \\b000100011'
    s = StringIO.new( b )
    expected_pos = 0
    while n = s.getc
      assert_equal( expected_pos + 1, s.pos )

      s.ungetc( n )
      assert_equal( expected_pos, s.pos )
      assert_equal( n, s.getc )

      expected_pos += 1
    end
  end

  def test_ungetc_padding
    s = StringIO.new()
    s.pos = 2
    s.ungetc("a")
    assert_equal("\0""a", s.string)
    s.pos = 0
    s.ungetc("b")
    assert_equal("b""\0""a", s.string)
  end

  def test_ungetc_fill
    count = 100
    s = StringIO.new
    s.print 'a' * count
    s.ungetc('b' * (count * 5))
    assert_equal((count * 5), s.string.size)
    assert_match(/\Ab+\z/, s.string)
  end

  def test_ungetbyte_pos
    b = '\\b00010001 \\B00010001 \\b1 \\B1 \\b000100011'
    s = StringIO.new( b )
    expected_pos = 0
    while n = s.getbyte
      assert_equal( expected_pos + 1, s.pos )

      s.ungetbyte( n )
      assert_equal( expected_pos, s.pos )
      assert_equal( n, s.getbyte )

      expected_pos += 1
    end
  end

  def test_ungetbyte_padding
    s = StringIO.new()
    s.pos = 2
    s.ungetbyte("a".ord)
    assert_equal("\0""a", s.string)
    s.pos = 0
    s.ungetbyte("b".ord)
    assert_equal("b""\0""a", s.string)
  end

  def test_ungetbyte_fill
    count = 100
    s = StringIO.new
    s.print 'a' * count
    s.ungetbyte('b' * (count * 5))
    assert_equal((count * 5), s.string.size)
    assert_match(/\Ab+\z/, s.string)
  end

  def test_frozen
    s = StringIO.new
    s.freeze
    bug = '[ruby-core:33648]'
    exception_class = defined?(FrozenError) ? FrozenError : RuntimeError
    assert_raise(exception_class, bug) {s.puts("foo")}
    assert_raise(exception_class, bug) {s.string = "foo"}
    assert_raise(exception_class, bug) {s.reopen("")}
  end

  def test_frozen_string
    s = StringIO.new("".freeze)
    bug = '[ruby-core:48530]'
    assert_raise(IOError, bug) {s.write("foo")}
    assert_raise(IOError, bug) {s.ungetc("a")}
    assert_raise(IOError, bug) {s.ungetbyte("a")}
  end

  def test_readlines_limit_0
    assert_raise(ArgumentError, "[ruby-dev:43392]") { StringIO.new.readlines(0) }
  end

  def test_each_line_limit_0
    assert_raise(ArgumentError, "[ruby-dev:43392]") { StringIO.new.each_line(0){} }
    assert_raise(ArgumentError, "[ruby-dev:43392]") { StringIO.new.each_line("a",0){} }
  end

  def test_binmode
    s = StringIO.new
    s.set_encoding('utf-8')
    assert_same s, s.binmode

    bug_11945 = '[ruby-core:72699] [Bug #11945]'
    assert_equal Encoding::ASCII_8BIT, s.external_encoding, bug_11945
  end

  def test_new_block_warning
    assert_warn(/does not take block/) do
      StringIO.new {}
    end
  end

  def test_overflow
    return if RbConfig::SIZEOF["void*"] > RbConfig::SIZEOF["long"]
    limit = RbConfig::LIMITS["INTPTR_MAX"] - 0x10
    assert_separately(%w[-rstringio], "#{<<-"begin;"}\n#{<<-"end;"}")
    begin;
      limit = #{limit}
      ary = []
      begin
        x = "a"*0x100000
        break if [x].pack("p").unpack("i!")[0] < 0
        ary << x
      end while ary.size <= 100
      s = StringIO.new(x)
      s.gets("xxx", limit)
      assert_equal(0x100000, s.pos)
    end;
  end

  def test_encoding_write
    s = StringIO.new("", "w:utf-32be")
    s.print "abc"
    assert_equal("abc".encode("utf-32be"), s.string)
  end

  def test_encoding_read
    s = StringIO.new("abc".encode("utf-32be"), "r:utf-8")
    assert_equal("\0\0\0a\0\0\0b\0\0\0c", s.read)
  end

  %w/UTF-8 UTF-16BE UTF-16LE UTF-32BE UTF-32LE/.each do |name|
    define_method("test_strip_bom:#{name}") do
      text = "\uFEFF\u0100a"
      content = text.encode(name)
      result = StringIO.new(content, mode: 'rb:BOM|UTF-8').read
      assert_equal(Encoding.find(name), result.encoding, name)
      assert_equal(content[1..-1].b, result.b, name)

      StringIO.open(content) {|f|
        assert_equal(Encoding.find(name), f.set_encoding_by_bom)
      }
    end
  end

  def test_binary_encoding_read_and_default_internal
    verbose, $VERBOSE = $VERBOSE, nil
    default_internal = Encoding.default_internal
    Encoding.default_internal = Encoding::UTF_8
    $VERBOSE = verbose
    assert_equal Encoding::BINARY, StringIO.new("Hello".b).read.encoding
  ensure
    $VERBOSE = nil
    Encoding.default_internal = default_internal
    $VERBOSE = verbose
  end

  def assert_string(content, encoding, str, mesg = nil)
    assert_equal([content, encoding], [str, str.encoding], mesg)
  end
end