mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
More immutability and locking tests.
This commit is contained in:
parent
e73197dff5
commit
98b442e013
Notes:
git
2021-11-12 12:46:30 +09:00
2 changed files with 36 additions and 4 deletions
11
io_buffer.c
11
io_buffer.c
|
@ -417,7 +417,6 @@ rb_io_buffer_to_s(VALUE self)
|
||||||
}
|
}
|
||||||
|
|
||||||
return rb_str_cat2(result, ">");
|
return rb_str_cat2(result, ">");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
@ -701,6 +700,13 @@ size_t rb_io_buffer_copy(VALUE self, VALUE source, size_t offset)
|
||||||
const void *source_base = NULL;
|
const void *source_base = NULL;
|
||||||
size_t source_size = 0;
|
size_t source_size = 0;
|
||||||
|
|
||||||
|
struct rb_io_buffer *data = NULL;
|
||||||
|
TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, data);
|
||||||
|
|
||||||
|
if (data->flags & RB_IO_BUFFER_IMMUTABLE) {
|
||||||
|
rb_raise(rb_eRuntimeError, "Buffer is immutable!");
|
||||||
|
}
|
||||||
|
|
||||||
if (RB_TYPE_P(source, T_STRING)) {
|
if (RB_TYPE_P(source, T_STRING)) {
|
||||||
RSTRING_GETMEM(source, source_base, source_size);
|
RSTRING_GETMEM(source, source_base, source_size);
|
||||||
}
|
}
|
||||||
|
@ -708,9 +714,6 @@ size_t rb_io_buffer_copy(VALUE self, VALUE source, size_t offset)
|
||||||
rb_io_buffer_get_immutable(source, &source_base, &source_size);
|
rb_io_buffer_get_immutable(source, &source_base, &source_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct rb_io_buffer *data = NULL;
|
|
||||||
TypedData_Get_Struct(self, struct rb_io_buffer, &rb_io_buffer_type, data);
|
|
||||||
|
|
||||||
rb_io_buffer_validate(data, offset, source_size);
|
rb_io_buffer_validate(data, offset, source_size);
|
||||||
|
|
||||||
memcpy((char*)data->base + offset, source_base, source_size);
|
memcpy((char*)data->base + offset, source_base, source_size);
|
||||||
|
|
|
@ -44,6 +44,19 @@ class TestIOBuffer < Test::Unit::TestCase
|
||||||
assert buffer.mapped?
|
assert buffer.mapped?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_new_immutable
|
||||||
|
buffer = IO::Buffer.new(128, IO::Buffer::INTERNAL|IO::Buffer::IMMUTABLE)
|
||||||
|
assert buffer.immutable?
|
||||||
|
|
||||||
|
assert_raise RuntimeError do
|
||||||
|
buffer.copy("", 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_raise RuntimeError do
|
||||||
|
buffer.copy("!", 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_file_mapped
|
def test_file_mapped
|
||||||
buffer = File.open(__FILE__) {|file| IO::Buffer.map(file)}
|
buffer = File.open(__FILE__) {|file| IO::Buffer.map(file)}
|
||||||
assert_include buffer.to_str, "Hello World"
|
assert_include buffer.to_str, "Hello World"
|
||||||
|
@ -103,6 +116,22 @@ class TestIOBuffer < Test::Unit::TestCase
|
||||||
# end
|
# end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_locked
|
||||||
|
buffer = IO::Buffer.new(128, IO::Buffer::INTERNAL|IO::Buffer::LOCKED)
|
||||||
|
|
||||||
|
assert_raise RuntimeError do
|
||||||
|
buffer.resize(256, 0)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal 128, buffer.size
|
||||||
|
|
||||||
|
assert_raise RuntimeError do
|
||||||
|
buffer.free
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal 128, buffer.size
|
||||||
|
end
|
||||||
|
|
||||||
def test_invalidation
|
def test_invalidation
|
||||||
input, output = IO.pipe
|
input, output = IO.pipe
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue