From b7d153699153629f037a059b930d8e928c42a4a1 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 18 Dec 2015 03:09:16 +0000 Subject: stringio.c: padding in ungetbyte * ext/stringio/stringio.c (strio_ungetbyte): pad with \000 when the current position is after the end. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53181 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/stringio/test_stringio.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'test/stringio') diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index af6efc684e..7b99d62f51 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -362,6 +362,11 @@ class TestStringIO < Test::Unit::TestCase 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) end def test_ungetc @@ -582,6 +587,31 @@ class TestStringIO < Test::Unit::TestCase assert_equal("b""\0""a", 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_frozen s = StringIO.new s.freeze -- cgit v1.2.3