summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/buffer/free_spec.rb
blob: f3a491897849aeabe2deff893837ce43de912871 (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
104
require_relative '../../../spec_helper'

describe "IO::Buffer#free" do
  context "with a buffer created with .new" do
    it "frees internal memory and nullifies the buffer" do
      buffer = IO::Buffer.new(4)
      buffer.free
      buffer.null?.should be_true
    end

    it "frees mapped memory and nullifies the buffer" do
      buffer = IO::Buffer.new(4, IO::Buffer::MAPPED)
      buffer.free
      buffer.null?.should be_true
    end
  end

  context "with a file-backed buffer created with .map" do
    it "frees mapped memory and nullifies the buffer" do
      File.open(__FILE__, "r") do |file|
        buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY)
        buffer.free
        buffer.null?.should be_true
      end
    end
  end

  context "with a String-backed buffer created with .for" do
    context "without a block" do
      it "disassociates the buffer from the string and nullifies the buffer" do
        string = +"test"
        buffer = IO::Buffer.for(string)
        # Read-only buffer, can't modify the string.
        buffer.free
        buffer.null?.should be_true
      end
    end

    context "with a block" do
      it "disassociates the buffer from the string and nullifies the buffer" do
        string = +"test"
        IO::Buffer.for(string) do |buffer|
          buffer.set_string("meat")
          buffer.free
          buffer.null?.should be_true
        end
        string.should == "meat"
      end
    end
  end

  ruby_version_is "3.3" do
    context "with a String-backed buffer created with .string" do
      it "disassociates the buffer from the string and nullifies the buffer" do
        string =
          IO::Buffer.string(4) do |buffer|
            buffer.set_string("meat")
            buffer.free
            buffer.null?.should be_true
          end
        string.should == "meat"
      end
    end
  end

  it "can be called repeatedly without an error" do
    buffer = IO::Buffer.new(4)
    buffer.free
    buffer.null?.should be_true
    buffer.free
    buffer.null?.should be_true
  end

  it "is disallowed while locked, raising IO::Buffer::LockedError" do
    buffer = IO::Buffer.new(4)
    buffer.locked do
      -> { buffer.free }.should raise_error(IO::Buffer::LockedError, "Buffer is locked!")
    end
    buffer.free
    buffer.null?.should be_true
  end

  context "with a slice of a buffer" do
    it "nullifies the slice, not touching the buffer" do
      buffer = IO::Buffer.new(4)
      slice = buffer.slice(0, 2)

      slice.free
      slice.null?.should be_true
      buffer.null?.should be_false

      buffer.free
    end

    it "nullifies buffer, invalidating the slice" do
      buffer = IO::Buffer.new(4)
      slice = buffer.slice(0, 2)

      buffer.free
      slice.null?.should be_false
      slice.valid?.should be_false
    end
  end
end