|
| 1 | +require_relative '../../../spec_helper' |
| 2 | + |
| 3 | +describe :io_buffer_and, shared: true do |
| 4 | + it "applies the argument buffer as an AND bit mask across the whole buffer" do |
| 5 | + IO::Buffer.for(+"12345") do |buffer| |
| 6 | + IO::Buffer.for(+"\xF8\x8F") do |mask| |
| 7 | + result = buffer.send(@method, mask) |
| 8 | + result.get_string.should == "\x30\x02\x30\x04\x30".b |
| 9 | + result.free |
| 10 | + end |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + it "ignores extra parts of mask if it is longer than source buffer" do |
| 15 | + IO::Buffer.for(+"12345") do |buffer| |
| 16 | + IO::Buffer.for(+"\xF8\x8F\x00\x00\x00\xFF\xFF") do |mask| |
| 17 | + result = buffer.send(@method, mask) |
| 18 | + result.get_string.should == "\x30\x02\x00\x00\x00".b |
| 19 | + result.free |
| 20 | + end |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + it "raises TypeError if mask is not an IO::Buffer" do |
| 25 | + IO::Buffer.for(+"12345") do |buffer| |
| 26 | + -> { buffer.send(@method, "\xF8\x8F") }.should raise_error(TypeError, "wrong argument type String (expected IO::Buffer)") |
| 27 | + -> { buffer.send(@method, 0xF8) }.should raise_error(TypeError, "wrong argument type Integer (expected IO::Buffer)") |
| 28 | + -> { buffer.send(@method, nil) }.should raise_error(TypeError, "wrong argument type nil (expected IO::Buffer)") |
| 29 | + end |
| 30 | + end |
| 31 | +end |
| 32 | + |
| 33 | +describe "IO::Buffer#&" do |
| 34 | + it_behaves_like :io_buffer_and, :& |
| 35 | + |
| 36 | + it "creates a new internal buffer of the same size" do |
| 37 | + IO::Buffer.for(+"12345") do |buffer| |
| 38 | + IO::Buffer.for(+"\xF8\x8F") do |mask| |
| 39 | + result = buffer & mask |
| 40 | + result.should_not.equal? buffer |
| 41 | + result.should.internal? |
| 42 | + result.size.should == buffer.size |
| 43 | + result.free |
| 44 | + buffer.get_string.should == "12345".b |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +describe "IO::Buffer#and!" do |
| 51 | + it_behaves_like :io_buffer_and, :and! |
| 52 | + |
| 53 | + it "modifies the buffer in place" do |
| 54 | + IO::Buffer.for(+"12345") do |buffer| |
| 55 | + IO::Buffer.for(+"\xF8\x8F") do |mask| |
| 56 | + result = buffer.and!(mask) |
| 57 | + result.should.equal? buffer |
| 58 | + result.should.external? |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | +end |
0 commit comments