Skip to content

Commit 32f4a34

Browse files
trinistreregon
authored andcommitted
Add specs for IO::Buffer's bitwise operations
1 parent e6d8723 commit 32f4a34

4 files changed

Lines changed: 223 additions & 0 deletions

File tree

core/io/buffer/and_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

core/io/buffer/not_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require_relative '../../../spec_helper'
2+
3+
describe :io_buffer_not, shared: true do
4+
it "inverts every bit of the buffer" do
5+
IO::Buffer.for(+"12345") do |buffer|
6+
result = buffer.send(@method)
7+
result.get_string.should == "\xCE\xCD\xCC\xCB\xCA".b
8+
result.free
9+
end
10+
end
11+
end
12+
13+
describe "IO::Buffer#~" do
14+
it_behaves_like :io_buffer_not, :~
15+
16+
it "creates a new internal buffer of the same size" do
17+
IO::Buffer.for(+"12345") do |buffer|
18+
result = ~buffer
19+
result.should_not.equal? buffer
20+
result.should.internal?
21+
result.size.should == buffer.size
22+
result.free
23+
end
24+
end
25+
end
26+
27+
describe "IO::Buffer#not!" do
28+
it_behaves_like :io_buffer_not, :not!
29+
30+
it "modifies the buffer in place" do
31+
IO::Buffer.for(+"12345") do |buffer|
32+
result = buffer.not!
33+
result.should.equal? buffer
34+
result.should.external?
35+
end
36+
end
37+
end

core/io/buffer/or_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require_relative '../../../spec_helper'
2+
3+
describe :io_buffer_or, shared: true do
4+
it "applies the argument buffer as an OR 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 == "\xF9\xBF\xFB\xBF\xFD".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 == "\xF9\xBF345".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_or, :|
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#or!" do
51+
it_behaves_like :io_buffer_or, :or!
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.or!(mask)
57+
result.should.equal? buffer
58+
result.should.external?
59+
end
60+
end
61+
end
62+
end

core/io/buffer/xor_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require_relative '../../../spec_helper'
2+
3+
describe :io_buffer_xor, shared: true do
4+
it "applies the argument buffer as an XOR 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 == "\xC9\xBD\xCB\xBB\xCD".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 == "\xC9\xBD345".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_xor, :^
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#xor!" do
51+
it_behaves_like :io_buffer_xor, :xor!
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.xor!(mask)
57+
result.should.equal? buffer
58+
result.should.external?
59+
end
60+
end
61+
end
62+
end

0 commit comments

Comments
 (0)