diff options
Diffstat (limited to 'spec/ruby/core/io/buffer/and_spec.rb')
| -rw-r--r-- | spec/ruby/core/io/buffer/and_spec.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/spec/ruby/core/io/buffer/and_spec.rb b/spec/ruby/core/io/buffer/and_spec.rb new file mode 100644 index 0000000000..3cea163b0e --- /dev/null +++ b/spec/ruby/core/io/buffer/and_spec.rb @@ -0,0 +1,62 @@ +require_relative '../../../spec_helper' + +describe :io_buffer_and, shared: true do + it "applies the argument buffer as an AND bit mask across the whole buffer" do + IO::Buffer.for(+"12345") do |buffer| + IO::Buffer.for(+"\xF8\x8F") do |mask| + result = buffer.send(@method, mask) + result.get_string.should == "\x30\x02\x30\x04\x30".b + result.free + end + end + end + + it "ignores extra parts of mask if it is longer than source buffer" do + IO::Buffer.for(+"12345") do |buffer| + IO::Buffer.for(+"\xF8\x8F\x00\x00\x00\xFF\xFF") do |mask| + result = buffer.send(@method, mask) + result.get_string.should == "\x30\x02\x00\x00\x00".b + result.free + end + end + end + + it "raises TypeError if mask is not an IO::Buffer" do + IO::Buffer.for(+"12345") do |buffer| + -> { buffer.send(@method, "\xF8\x8F") }.should.raise(TypeError, "wrong argument type String (expected IO::Buffer)") + -> { buffer.send(@method, 0xF8) }.should.raise(TypeError, "wrong argument type Integer (expected IO::Buffer)") + -> { buffer.send(@method, nil) }.should.raise(TypeError, "wrong argument type nil (expected IO::Buffer)") + end + end +end + +describe "IO::Buffer#&" do + it_behaves_like :io_buffer_and, :& + + it "creates a new internal buffer of the same size" do + IO::Buffer.for(+"12345") do |buffer| + IO::Buffer.for(+"\xF8\x8F") do |mask| + result = buffer & mask + result.should_not.equal? buffer + result.should.internal? + result.size.should == buffer.size + result.free + buffer.get_string.should == "12345".b + end + end + end +end + +describe "IO::Buffer#and!" do + it_behaves_like :io_buffer_and, :and! + + it "modifies the buffer in place" do + IO::Buffer.for(+"12345") do |buffer| + IO::Buffer.for(+"\xF8\x8F") do |mask| + result = buffer.and!(mask) + result.should.equal? buffer + result.should.external? + end + end + end +end |
