* ext/stringio/stringio.c (strio_read): follow IO#read.

* test/ruby/ut_eof.rb, test/ruby/test_file.rb, test/ruby/test_pipe.rb,
  test/stringio/test_stringio.rb: add EOF test.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5115 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2003-12-05 02:54:48 +00:00
Родитель e3ed269193
Коммит 7fbf13f7f2
8 изменённых файлов: 93 добавлений и 14 удалений

Просмотреть файл

@ -1,3 +1,10 @@
Fri Dec 5 11:54:45 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/stringio/stringio.c (strio_read): follow IO#read.
* test/ruby/ut_eof.rb, test/ruby/test_file.rb, test/ruby/test_pipe.rb,
test/stringio/test_stringio.rb: add EOF test.
Fri Dec 5 02:49:35 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/test/unit/assertions.rb (Test::Unit::Assertions::assert_raises):
@ -15,7 +22,7 @@ Thu Dec 4 23:32:26 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
* io.c (read_all): do not depend on lseek position.
[ruby-dev:22026]
Thu Dec 4 22:37:26 2003 Nobuyoshi Nakada <nobu@ruby-lang.org>
* eval.c (rb_eval): preserve $! value when retry happens in the

Просмотреть файл

@ -866,10 +866,12 @@ strio_read(argc, argv, self)
rb_raise(rb_eArgError, "wrong number arguments (%d for 0)", argc);
}
str = rb_str_substr(ptr->string, ptr->pos, len);
if (len > 0 &&
(NIL_P(str) || (ptr->pos += RSTRING(str)->len) >= RSTRING(ptr->string)->len)) {
if (NIL_P(str)) {
ptr->flags |= STRIO_EOF;
}
else {
ptr->pos += RSTRING(str)->len;
}
return str;
}

Просмотреть файл

@ -1,6 +1,6 @@
require 'test/unit'
require 'tempfile'
$:.unshift(File.dirname(File.expand_path(__FILE__)))
$:.replace([File.dirname(File.expand_path(__FILE__))] | $:)
require 'envutil'
class TestBeginEndBlock < Test::Unit::TestCase

Просмотреть файл

@ -1,4 +1,7 @@
require 'test/unit'
require 'tempfile'
$:.replace([File.dirname(File.expand_path(__FILE__))] | $:)
require 'ut_eof'
$KCODE = 'none'
@ -29,4 +32,12 @@ class TestFile < Test::Unit::TestCase
File.unlink(filename) if File.exist?(filename)
end
end
include TestEOF
def open_file(content)
f = Tempfile.new("test-eof")
f << content
f.rewind
yield f
end
end

14
test/ruby/test_pipe.rb Normal file
Просмотреть файл

@ -0,0 +1,14 @@
require 'test/unit'
$:.replace([File.dirname(File.expand_path(__FILE__))] | $:)
require 'ut_eof'
require 'envutil'
$KCODE = 'none'
class TestPipe < Test::Unit::TestCase
include TestEOF
def open_file(content)
f = IO.popen("echo -n #{content}")
yield f
end
end

Просмотреть файл

@ -1,5 +1,5 @@
require 'test/unit'
$:.unshift(File.dirname(File.expand_path(__FILE__)))
$:.replace([File.dirname(File.expand_path(__FILE__))] | $:)
require 'envutil'
$KCODE = 'none'

46
test/ruby/ut_eof.rb Normal file
Просмотреть файл

@ -0,0 +1,46 @@
require 'test/unit'
module TestEOF
def test_eof_0
open_file("") {|f|
assert_equal("", f.read(0))
assert_equal("", f.read(0))
assert_equal("", f.read)
assert_equal(nil, f.read(0))
assert_equal(nil, f.read(0))
}
open_file("") {|f|
assert_equal(nil, f.read(1))
assert_equal(nil, f.read)
assert_equal(nil, f.read(1))
}
end
def test_eof_1
open_file("a") {|f|
assert_equal("", f.read(0))
assert_equal("a", f.read(1))
assert_equal("" , f.read(0))
assert_equal("" , f.read(0))
assert_equal("", f.read)
assert_equal(nil, f.read(0))
assert_equal(nil, f.read(0))
}
open_file("a") {|f|
assert_equal("a", f.read(1))
assert_equal(nil, f.read(1))
}
open_file("a") {|f|
assert_equal("a", f.read(2))
assert_equal(nil, f.read(1))
assert_equal(nil, f.read)
assert_equal(nil, f.read(1))
}
open_file("a") {|f|
assert_equal("a", f.read)
assert_equal(nil, f.read(1))
assert_equal(nil, f.read)
assert_equal(nil, f.read(1))
}
end
end

Просмотреть файл

@ -1,15 +1,14 @@
require 'test/unit'
require 'stringio'
dir = File.expand_path(__FILE__)
2.times {dir = File.dirname(dir)}
$:.replace([File.join(dir, "ruby")] | $:)
require 'ut_eof'
class TestStringIO < Test::Unit::TestCase
def test_empty_file
f = StringIO.new("")
assert_equal("", f.read(0))
assert_equal("", f.read)
assert_equal(nil, f.read(0))
f = StringIO.new("")
assert_equal(nil, f.read(1))
assert_equal(nil, f.read)
assert_equal(nil, f.read(1))
include TestEOF
def open_file(content)
f = StringIO.new(content)
yield f
end
end