Skip to content

Commit 4ce76f1

Browse files
committed
Add spec for IO::Buffer#initialize
1 parent 64a1a40 commit 4ce76f1

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

core/io/buffer/initialize_spec.rb

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
require_relative '../../../spec_helper'
2+
3+
describe "IO::Buffer#initialize" do
4+
after :each do
5+
@buffer&.free
6+
end
7+
8+
it "creates a new zero-filled buffer with default size" do
9+
@buffer = IO::Buffer.new
10+
@buffer.size.should == IO::Buffer::DEFAULT_SIZE
11+
@buffer.each(:U8).should.all? { |_offset, value| value.eql?(0) }
12+
end
13+
14+
it "creates a buffer with default state" do
15+
@buffer = IO::Buffer.new
16+
@buffer.should_not.shared?
17+
@buffer.should_not.readonly?
18+
19+
@buffer.should_not.empty?
20+
@buffer.should_not.null?
21+
22+
# This is run-time state, set by #locked.
23+
@buffer.should_not.locked?
24+
end
25+
26+
context "with size argument" do
27+
it "creates a new internal buffer if size is less than IO::Buffer::PAGE_SIZE" do
28+
size = IO::Buffer::PAGE_SIZE - 1
29+
@buffer = IO::Buffer.new(size)
30+
@buffer.size.should == size
31+
@buffer.should.internal?
32+
@buffer.should_not.empty?
33+
end
34+
35+
it "creates a new mapped buffer if size is greater than or equal to IO::Buffer::PAGE_SIZE" do
36+
size = IO::Buffer::PAGE_SIZE
37+
@buffer = IO::Buffer.new(size)
38+
@buffer.size.should == size
39+
@buffer.should.mapped?
40+
@buffer.should_not.empty?
41+
end
42+
43+
it "creates a null buffer if size is 0" do
44+
@buffer = IO::Buffer.new(0)
45+
@buffer.size.should.zero?
46+
@buffer.should.null?
47+
@buffer.should.empty?
48+
end
49+
50+
it "raises TypeError if size is not an Integer" do
51+
-> { IO::Buffer.new(nil) }.should raise_error(TypeError, "not an Integer")
52+
-> { IO::Buffer.new(10.0) }.should raise_error(TypeError, "not an Integer")
53+
end
54+
55+
it "raises ArgumentError if size is negative" do
56+
-> { IO::Buffer.new(-1) }.should raise_error(ArgumentError, "Size can't be negative!")
57+
end
58+
end
59+
60+
context "with size and flags arguments" do
61+
it "forces mapped buffer with IO::Buffer::MAPPED flag" do
62+
@buffer = IO::Buffer.new(IO::Buffer::PAGE_SIZE - 1, IO::Buffer::MAPPED)
63+
@buffer.should.mapped?
64+
@buffer.should_not.internal?
65+
@buffer.should_not.empty?
66+
end
67+
68+
it "forces internal buffer with IO::Buffer::INTERNAL flag" do
69+
@buffer = IO::Buffer.new(IO::Buffer::PAGE_SIZE, IO::Buffer::INTERNAL)
70+
@buffer.should.internal?
71+
@buffer.should_not.mapped?
72+
@buffer.should_not.empty?
73+
end
74+
75+
# This looks like a bug to me, these should be different memory models?
76+
# Definitely looks like a bug, created issue: https://bugs.ruby-lang.org/issues/21672
77+
it "creates internal-mapped buffer if INTERNAL and MAPPED are both specified" do
78+
@buffer = IO::Buffer.new(10, IO::Buffer::INTERNAL | IO::Buffer::MAPPED)
79+
@buffer.should.internal?
80+
@buffer.should.mapped?
81+
@buffer.should_not.empty?
82+
end
83+
84+
it "raises ArgumentError if flags is negative" do
85+
-> { IO::Buffer.new(10, -1) }.should raise_error(ArgumentError, "Flags can't be negative!")
86+
end
87+
88+
it "raises IO::Buffer::AllocationError with other values for flags" do
89+
-> { IO::Buffer.new(10, IO::Buffer::READONLY) }.should raise_error(IO::Buffer::AllocationError, "Could not allocate buffer!")
90+
-> { IO::Buffer.new(10, 0) }.should raise_error(IO::Buffer::AllocationError, "Could not allocate buffer!")
91+
end
92+
93+
ruby_version_is ""..."3.3" do
94+
it "raises IO::Buffer::AllocationError with non-Integer flags" do
95+
-> { IO::Buffer.new(10, 0.0) }.should raise_error(IO::Buffer::AllocationError, "Could not allocate buffer!")
96+
end
97+
end
98+
99+
ruby_version_is "3.3" do
100+
it "raises TypeError with non-Integer flags" do
101+
-> { IO::Buffer.new(10, 0.0) }.should raise_error(TypeError, "not an Integer")
102+
end
103+
end
104+
end
105+
end

0 commit comments

Comments
 (0)