summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/buffer/valid_spec.rb
blob: 680a35ae9ad716207b4da3c0ced0feb03158eaa9 (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
105
106
107
108
109
110
require_relative '../../../spec_helper'

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

  # Non-slices are always valid
  context "with a non-slice buffer" do
    it "is true for a regular buffer" do
      @buffer = IO::Buffer.new(4)
      @buffer.valid?.should be_true
    end

    it "is true for a 0-size buffer" do
      @buffer = IO::Buffer.new(0)
      @buffer.valid?.should be_true
    end

    it "is true for a freed buffer" do
      @buffer = IO::Buffer.new(4)
      @buffer.free
      @buffer.valid?.should be_true
    end

    it "is true for a freed file-backed buffer" do
      File.open(__FILE__, "r") do |file|
        @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY)
        @buffer.valid?.should be_true
        @buffer.free
        @buffer.valid?.should be_true
      end
    end

    it "is true for a freed string-backed buffer" do
      @buffer = IO::Buffer.for("hello")
      @buffer.valid?.should be_true
      @buffer.free
      @buffer.valid?.should be_true
    end
  end

  # "A buffer becomes invalid if it is a slice of another buffer (or string)
  # which has been freed or re-allocated at a different address."
  context "with a slice" do
    it "is true for a slice of a live buffer" do
      @buffer = IO::Buffer.new(4)
      slice = @buffer.slice(0, 2)
      slice.valid?.should be_true
    end

    context "when buffer is resized" do
      it "is false when slice becomes outside the buffer" do
        @buffer = IO::Buffer.new(4)
        slice = @buffer.slice(2, 2)
        @buffer.resize(3)
        slice.valid?.should be_false
      end

      platform_is_not :linux do
        # This test does not cause a copy-resize on Linux.
        # `#resize` MAY cause the buffer to move, but there is no guarantee.
        it "is false when buffer is copied on resize" do
          @buffer = IO::Buffer.new(4, IO::Buffer::MAPPED)
          slice = @buffer.slice(0, 2)
          @buffer.resize(8)
          slice.valid?.should be_false
        end
      end
    end

    it "is false for a slice of a transferred buffer" do
      buffer = IO::Buffer.new(4)
      slice = buffer.slice(0, 2)
      @buffer = buffer.transfer
      slice.valid?.should be_false
    end

    it "is false for a slice of a freed buffer" do
      @buffer = IO::Buffer.new(4)
      slice = @buffer.slice(0, 2)
      @buffer.free
      slice.valid?.should be_false
    end

    it "is false for a slice of a freed file-backed buffer" do
      File.open(__FILE__, "r") do |file|
        @buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::READONLY)
        slice = @buffer.slice(0, 2)
        slice.valid?.should be_true
        @buffer.free
        slice.valid?.should be_false
      end
    end

    it "is true for a slice of a freed string-backed buffer while string is alive" do
      @buffer = IO::Buffer.for("alive")
      slice = @buffer.slice(0, 2)
      slice.valid?.should be_true
      @buffer.free
      slice.valid?.should be_true
    end

    # There probably should be a test with a garbage-collected string,
    # but it's not clear how to force that.

    it "needs to be reviewed for spec completeness"
  end
end