|
| 1 | +require_relative '../../../spec_helper' |
| 2 | + |
| 3 | +describe "IO::Buffer.map" do |
| 4 | + after :each do |
| 5 | + @buffer&.free |
| 6 | + @buffer = nil |
| 7 | + @file&.close |
| 8 | + @file = nil |
| 9 | + end |
| 10 | + |
| 11 | + def open_fixture |
| 12 | + File.open("#{__dir__}/../fixtures/read_text.txt", "r+") |
| 13 | + end |
| 14 | + |
| 15 | + it "creates a new buffer mapped from a file" do |
| 16 | + @file = open_fixture |
| 17 | + @buffer = IO::Buffer.map(@file) |
| 18 | + |
| 19 | + @buffer.size.should == 9 |
| 20 | + @buffer.get_string.force_encoding(Encoding::UTF_8).should == "abcâdef\n" |
| 21 | + end |
| 22 | + |
| 23 | + it "allows to close the file after creating buffer, retaining mapping" do |
| 24 | + file = open_fixture |
| 25 | + @buffer = IO::Buffer.map(file) |
| 26 | + file.close |
| 27 | + |
| 28 | + @buffer.get_string.force_encoding(Encoding::UTF_8).should == "abcâdef\n" |
| 29 | + end |
| 30 | + |
| 31 | + ruby_version_is ""..."3.3" do |
| 32 | + it "creates a buffer with default state and expected flags" do |
| 33 | + @file = open_fixture |
| 34 | + @buffer = IO::Buffer.map(@file) |
| 35 | + |
| 36 | + @buffer.should_not.internal? |
| 37 | + @buffer.should.mapped? |
| 38 | + @buffer.should.external? |
| 39 | + |
| 40 | + @buffer.should_not.empty? |
| 41 | + @buffer.should_not.null? |
| 42 | + |
| 43 | + @buffer.should.shared? |
| 44 | + @buffer.should_not.readonly? |
| 45 | + |
| 46 | + @buffer.should_not.locked? |
| 47 | + @buffer.should.valid? |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + ruby_version_is "3.3" do |
| 52 | + it "creates a buffer with default state and expected flags" do |
| 53 | + @file = open_fixture |
| 54 | + @buffer = IO::Buffer.map(@file) |
| 55 | + |
| 56 | + @buffer.should_not.internal? |
| 57 | + @buffer.should.mapped? |
| 58 | + @buffer.should.external? |
| 59 | + |
| 60 | + @buffer.should_not.empty? |
| 61 | + @buffer.should_not.null? |
| 62 | + |
| 63 | + @buffer.should.shared? |
| 64 | + @buffer.should_not.private? |
| 65 | + @buffer.should_not.readonly? |
| 66 | + |
| 67 | + @buffer.should_not.locked? |
| 68 | + @buffer.should.valid? |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context "with an empty file" do |
| 73 | + ruby_version_is ""..."4.0" do |
| 74 | + platform_is_not :windows do |
| 75 | + it "raises Errno::EINVAL" do |
| 76 | + @file = File.open("#{__dir__}/../fixtures/empty.txt", "r+") |
| 77 | + -> { IO::Buffer.map(@file) }.should raise_error(Errno::EINVAL) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + platform_is :windows do |
| 82 | + it "raises Errno::ENOTTY" do |
| 83 | + @file = File.open("#{__dir__}/../fixtures/empty.txt", "r+") |
| 84 | + -> { IO::Buffer.map(@file) }.should raise_error(Errno::ENOTTY) |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + ruby_version_is "4.0" do |
| 90 | + it "raises ArgumentError" do |
| 91 | + @file = File.open("#{__dir__}/../fixtures/empty.txt", "r+") |
| 92 | + -> { IO::Buffer.map(@file) }.should raise_error(ArgumentError, "Invalid negative or zero file size!") |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + context "with size argument" do |
| 98 | + it "limits the buffer to the specified size in bytes, starting from the start of the file" do |
| 99 | + @file = open_fixture |
| 100 | + @buffer = IO::Buffer.map(@file, 4) |
| 101 | + |
| 102 | + @buffer.size.should == 4 |
| 103 | + @buffer.get_string.force_encoding(Encoding::UTF_8).should == "abc\xC3" |
| 104 | + end |
| 105 | + |
| 106 | + it "maps the whole file if size is nil" do |
| 107 | + @file = open_fixture |
| 108 | + @buffer = IO::Buffer.map(@file, nil) |
| 109 | + |
| 110 | + @buffer.size.should == 9 |
| 111 | + end |
| 112 | + |
| 113 | + ruby_version_is ""..."4.0" do |
| 114 | + platform_is_not :windows do |
| 115 | + it "raises Errno::EINVAL if size is 0" do |
| 116 | + @file = open_fixture |
| 117 | + -> { IO::Buffer.map(@file, 0) }.should raise_error(Errno::EINVAL) |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + ruby_version_is "4.0" do |
| 123 | + it "raises ArgumentError if size is 0" do |
| 124 | + @file = open_fixture |
| 125 | + -> { IO::Buffer.map(@file, 0) }.should raise_error(ArgumentError, "Size can't be zero!") |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + it "raises TypeError if size is not an Integer or nil" do |
| 130 | + @file = open_fixture |
| 131 | + -> { IO::Buffer.map(@file, "10") }.should raise_error(TypeError, "not an Integer") |
| 132 | + -> { IO::Buffer.map(@file, 10.0) }.should raise_error(TypeError, "not an Integer") |
| 133 | + end |
| 134 | + |
| 135 | + it "raises ArgumentError if size is negative" do |
| 136 | + @file = open_fixture |
| 137 | + -> { IO::Buffer.map(@file, -1) }.should raise_error(ArgumentError, "Size can't be negative!") |
| 138 | + end |
| 139 | + |
| 140 | + ruby_version_is ""..."4.0" do |
| 141 | + it "raises Errno::EINVAL if size is larger than file size" do |
| 142 | + @file = open_fixture |
| 143 | + -> { IO::Buffer.map(@file, 8192) }.should raise_error(Errno::EINVAL) |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + ruby_version_is "4.0" do |
| 148 | + it "raises ArgumentError if size is larger than file size" do |
| 149 | + @file = open_fixture |
| 150 | + -> { IO::Buffer.map(@file, 8192) }.should raise_error(ArgumentError, "Size can't be larger than file size!") |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + context "with size and offset arguments" do |
| 156 | + platform_is :linux do |
| 157 | + context "if offset is a multiple of page size" do |
| 158 | + it "maps the specified length starting from the offset" do |
| 159 | + @file = File.open("README.md", "r+") |
| 160 | + @buffer = IO::Buffer.map(@file, 14, 65536) |
| 161 | + |
| 162 | + @buffer.size.should == 14 |
| 163 | + # @buffer.get_string(0, 14).force_encoding(Encoding::UTF_8).should == "# Ruby Spec" |
| 164 | + end |
| 165 | + |
| 166 | + # A second mapping just doesn't work? |
| 167 | + it "maps the rest of the file if size is nil" do |
| 168 | + @file = File.open("#{__dir__}/fixtures/big_file.txt", "r+") |
| 169 | + @buffer = IO::Buffer.map(@file, nil, IO::Buffer::PAGE_SIZE) |
| 170 | + |
| 171 | + @buffer.size.should == 5895 # BUG: why is this the total size? |
| 172 | + @buffer.get_string(0, 1).force_encoding(Encoding::UTF_8).should == "-" |
| 173 | + end |
| 174 | + end |
| 175 | + |
| 176 | + it "raises Errno::EINVAL if offset is not a multiple of page size" do |
| 177 | + @file = open_fixture |
| 178 | + -> { IO::Buffer.map(@file, 4, IO::Buffer::PAGE_SIZE / 2) }.should raise_error(Errno::EINVAL) |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + it "maps the file from the start if offset is 0" do |
| 183 | + @file = open_fixture |
| 184 | + @buffer = IO::Buffer.map(@file, 4, 0) |
| 185 | + |
| 186 | + @buffer.size.should == 4 |
| 187 | + @buffer.get_string.force_encoding(Encoding::UTF_8).should == "abc\xC3" |
| 188 | + end |
| 189 | + |
| 190 | + it "raises TypeError if offset is not convertible to Integer" do |
| 191 | + @file = open_fixture |
| 192 | + -> { IO::Buffer.map(@file, 4, "4096") }.should raise_error(TypeError, "no implicit conversion of String into Integer") |
| 193 | + -> { IO::Buffer.map(@file, 4, nil) }.should raise_error(TypeError, "no implicit conversion from nil to integer") |
| 194 | + end |
| 195 | + |
| 196 | + it "raises Errno::EINVAL if offset is not an allowed value" do |
| 197 | + @file = open_fixture |
| 198 | + -> { IO::Buffer.map(@file, 4, 3) }.should raise_error(Errno::EINVAL) |
| 199 | + end |
| 200 | + |
| 201 | + it "raises Errno::EINVAL if offset is negative" do |
| 202 | + @file = open_fixture |
| 203 | + -> { IO::Buffer.map(@file, 4, -1) }.should raise_error(Errno::EINVAL) |
| 204 | + end |
| 205 | + end |
| 206 | +end |
0 commit comments