summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authorSamuel Williams <samuel.williams@oriontransfer.co.nz>2024-10-04 00:11:36 +1300
committernagachika <nagachika@ruby-lang.org>2024-10-04 10:09:39 +0900
commit3d4d5c8d8cca0c290a234fb3c47c035822b18add (patch)
treefe4d37cd8b6d0a1fdaf6df17de9bbe96d1d568d1 /test/ruby
parentbc482e632c99f30db2e12aa51cc3b10b3b0f7886 (diff)
Add `IO::Buffer` tests for read and write with length & offset.
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/test_io_buffer.rb28
1 files changed, 26 insertions, 2 deletions
diff --git a/test/ruby/test_io_buffer.rb b/test/ruby/test_io_buffer.rb
index 0c62216642..ae3fba2e41 100644
--- a/test/ruby/test_io_buffer.rb
+++ b/test/ruby/test_io_buffer.rb
@@ -359,9 +359,11 @@ class TestIOBuffer < Test::Unit::TestCase
input.close
end
- def hello_world_tempfile
+ def hello_world_tempfile(repeats = 1)
io = Tempfile.new
- io.write("Hello World")
+ repeats.times do
+ io.write("Hello World")
+ end
io.seek(0)
yield io
@@ -393,6 +395,15 @@ class TestIOBuffer < Test::Unit::TestCase
end
end
+ def test_read_with_length_and_offset
+ hello_world_tempfile(100) do |io|
+ buffer = IO::Buffer.new(1024)
+ # Only read 24 bytes from the file, as we are starting at offset 1000 in the buffer.
+ assert_equal 24, buffer.read(io, 0, 1000)
+ assert_equal "Hello World", buffer.get_string(1000, 11)
+ end
+ end
+
def test_write
io = Tempfile.new
@@ -406,6 +417,19 @@ class TestIOBuffer < Test::Unit::TestCase
io.close!
end
+ def test_write_with_length_and_offset
+ io = Tempfile.new
+
+ buffer = IO::Buffer.new(5)
+ buffer.set_string("Hello")
+ buffer.write(io, 4, 1)
+
+ io.seek(0)
+ assert_equal "ello", io.read(4)
+ ensure
+ io.close!
+ end
+
def test_pread
io = Tempfile.new
io.write("Hello World")