From c833ece5f78b8c2e43263e08ccbd3ce1628bf610 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Fri, 22 Oct 2021 15:05:00 +1300 Subject: Rework implementation of `IO::Buffer.for(string)` to use string locking. --- test/ruby/test_io_buffer.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'test/ruby/test_io_buffer.rb') diff --git a/test/ruby/test_io_buffer.rb b/test/ruby/test_io_buffer.rb index 1bd839e163..12937729f4 100644 --- a/test/ruby/test_io_buffer.rb +++ b/test/ruby/test_io_buffer.rb @@ -62,6 +62,25 @@ class TestIOBuffer < Test::Unit::TestCase assert_include buffer.to_str, "Hello World" end + def test_string_mapped + string = "Hello World" + buffer = IO::Buffer.for(string) + + # Cannot modify string as it's locked by the buffer: + assert_raise RuntimeError do + string[0] = "h" + end + + buffer.set(:U8, 0, "h".ord) + + # Buffer releases it's ownership of the string: + buffer.free + + assert_equal "hello World", string + string[0] = "H" + assert_equal "Hello World", string + end + def test_resize buffer = IO::Buffer.new(1024, IO::Buffer::MAPPED) buffer.resize(2048, 0) -- cgit v1.2.3