Skip to content

Commit d0c5c42

Browse files
trinistrandrykonchin
authored andcommitted
Add spec for IO::Buffer#initialize
1 parent 64a1a40 commit d0c5c42

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

core/io/buffer/initialize_spec.rb

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

0 commit comments

Comments
 (0)