summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/buffer/initialize_spec.rb
blob: c86d1e7f1d634aa5c7aacc929737355bc8ee6411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require_relative '../../../spec_helper'

describe "IO::Buffer#initialize" do
  after :each do
    @buffer&.free
    @buffer = nil
  end

  it "creates a new zero-filled buffer with default size" do
    @buffer = IO::Buffer.new
    @buffer.size.should == IO::Buffer::DEFAULT_SIZE
    @buffer.each(:U8).should.all? { |_offset, value| value.eql?(0) }
  end

  it "creates a buffer with default state" do
    @buffer = IO::Buffer.new
    @buffer.should_not.shared?
    @buffer.should_not.readonly?

    @buffer.should_not.empty?
    @buffer.should_not.null?

    # This is run-time state, set by #locked.
    @buffer.should_not.locked?
  end

  context "with size argument" do
    it "creates a new internal buffer if size is less than IO::Buffer::PAGE_SIZE" do
      size = IO::Buffer::PAGE_SIZE - 1
      @buffer = IO::Buffer.new(size)
      @buffer.size.should == size
      @buffer.should.internal?
      @buffer.should_not.mapped?
      @buffer.should_not.empty?
    end

    it "creates a new mapped buffer if size is greater than or equal to IO::Buffer::PAGE_SIZE" do
      size = IO::Buffer::PAGE_SIZE
      @buffer = IO::Buffer.new(size)
      @buffer.size.should == size
      @buffer.should_not.internal?
      @buffer.should.mapped?
      @buffer.should_not.empty?
    end

    it "creates a null buffer if size is 0" do
      @buffer = IO::Buffer.new(0)
      @buffer.size.should.zero?
      @buffer.should_not.internal?
      @buffer.should_not.mapped?
      @buffer.should.null?
      @buffer.should.empty?
    end

    it "raises TypeError if size is not an Integer" do
      -> { IO::Buffer.new(nil) }.should raise_error(TypeError, "not an Integer")
      -> { IO::Buffer.new(10.0) }.should raise_error(TypeError, "not an Integer")
    end

    it "raises ArgumentError if size is negative" do
      -> { IO::Buffer.new(-1) }.should raise_error(ArgumentError, "Size can't be negative!")
    end
  end

  context "with size and flags arguments" do
    it "forces mapped buffer with IO::Buffer::MAPPED flag" do
      @buffer = IO::Buffer.new(IO::Buffer::PAGE_SIZE - 1, IO::Buffer::MAPPED)
      @buffer.should.mapped?
      @buffer.should_not.internal?
      @buffer.should_not.empty?
    end

    it "forces internal buffer with IO::Buffer::INTERNAL flag" do
      @buffer = IO::Buffer.new(IO::Buffer::PAGE_SIZE, IO::Buffer::INTERNAL)
      @buffer.should.internal?
      @buffer.should_not.mapped?
      @buffer.should_not.empty?
    end

    it "raises IO::Buffer::AllocationError if neither IO::Buffer::MAPPED nor IO::Buffer::INTERNAL is given" do
      -> { IO::Buffer.new(10, IO::Buffer::READONLY) }.should raise_error(IO::Buffer::AllocationError, "Could not allocate buffer!")
      -> { IO::Buffer.new(10, 0) }.should raise_error(IO::Buffer::AllocationError, "Could not allocate buffer!")
    end

    ruby_version_is "3.3" do
      it "raises ArgumentError if flags is negative" do
        -> { IO::Buffer.new(10, -1) }.should raise_error(ArgumentError, "Flags can't be negative!")
      end
    end

    ruby_version_is ""..."3.3" do
      it "raises IO::Buffer::AllocationError with non-Integer flags" do
        -> { IO::Buffer.new(10, 0.0) }.should raise_error(IO::Buffer::AllocationError, "Could not allocate buffer!")
      end
    end

    ruby_version_is "3.3" do
      it "raises TypeError with non-Integer flags" do
        -> { IO::Buffer.new(10, 0.0) }.should raise_error(TypeError, "not an Integer")
      end
    end
  end
end