spec/io_spec.rb in bindata-0.9.3 vs spec/io_spec.rb in bindata-0.10.0
- old
+ new
@@ -37,10 +37,17 @@
io.seekbytes(2)
io.readbytes(4).should == "cdef"
end
+ it "should read all bytes" do
+ stream = StringIO.new("abcdefghij")
+ io = BinData::IO.new(stream)
+
+ io.read_all_bytes.should == "abcdefghij"
+ end
+
it "should raise error when reading at eof" do
stream = StringIO.new("abcdefghij")
io = BinData::IO.new(stream)
io.seekbytes(10)
lambda {
@@ -62,10 +69,21 @@
io.writebytes("abcd")
stream.rewind
stream.read.should == "abcd"
end
+
+ it "should flush" do
+ stream = StringIO.new
+ io = BinData::IO.new(stream)
+ io.writebytes("abcd")
+ io.flush
+
+ stream.rewind
+ stream.read.should == "abcd"
+ end
+
end
describe BinData::IO, "reading bits in big endian" do
before(:each) do
@b1 = 0b1111_1010
@@ -74,40 +92,40 @@
str = [@b1, @b2, @b3].pack("CCC")
@io = BinData::IO.new(str)
end
it "should read a bitfield less than 1 byte" do
- @io.readbits(3).should == 0b111
+ @io.readbits(3, :big).should == 0b111
end
it "should read a bitfield more than 1 byte" do
- @io.readbits(10).should == 0b1111_1010_11
+ @io.readbits(10, :big).should == 0b1111_1010_11
end
it "should read a bitfield more than 2 bytes" do
- @io.readbits(17).should == 0b1111_1010_1100_1110_0
+ @io.readbits(17, :big).should == 0b1111_1010_1100_1110_0
end
it "should read two bitfields totalling less than 1 byte" do
- @io.readbits(5).should == 0b1111_1
- @io.readbits(2).should == 0b01
+ @io.readbits(5, :big).should == 0b1111_1
+ @io.readbits(2, :big).should == 0b01
end
it "should read two bitfields totalling more than 1 byte" do
- @io.readbits(6).should == 0b1111_10
- @io.readbits(8).should == 0b10_1100_11
+ @io.readbits(6, :big).should == 0b1111_10
+ @io.readbits(8, :big).should == 0b10_1100_11
end
it "should read two bitfields totalling more than 2 bytes" do
- @io.readbits(7).should == 0b1111_101
- @io.readbits(12).should == 0b0_1100_1110_011
+ @io.readbits(7, :big).should == 0b1111_101
+ @io.readbits(12, :big).should == 0b0_1100_1110_011
end
it "should ignore unused bits when reading bytes" do
- @io.readbits(3).should == 0b111
+ @io.readbits(3, :big).should == 0b111
@io.readbytes(1).should == [@b2].pack("C")
- @io.readbits(2).should == 0b01
+ @io.readbits(2, :big).should == 0b01
end
end
describe BinData::IO, "reading bits in little endian" do
before(:each) do
@@ -156,11 +174,11 @@
def initialize
@stringio = StringIO.new
@io = BinData::IO.new(@stringio)
end
- def writebits(val, nbits, endian = :big)
+ def writebits(val, nbits, endian)
@io.writebits(val, nbits, endian)
end
def writebytes(val)
@io.writebytes(val)
@@ -177,46 +195,46 @@
before(:each) do
@io = BitWriterHelper.new
end
it "should write a bitfield less than 1 byte" do
- @io.writebits(0b010, 3)
+ @io.writebits(0b010, 3, :big)
@io.value.should == [0b0100_0000].pack("C")
end
it "should write a bitfield more than 1 byte" do
- @io.writebits(0b10_1001_1101, 10)
+ @io.writebits(0b10_1001_1101, 10, :big)
@io.value.should == [0b1010_0111, 0b0100_0000].pack("CC")
end
it "should write a bitfield more than 2 bytes" do
- @io.writebits(0b101_1000_0010_1001_1101, 19)
+ @io.writebits(0b101_1000_0010_1001_1101, 19, :big)
@io.value.should == [0b1011_0000, 0b0101_0011, 0b1010_0000].pack("CCC")
end
it "should write two bitfields totalling less than 1 byte" do
- @io.writebits(0b1_1001, 5)
- @io.writebits(0b00, 2)
+ @io.writebits(0b1_1001, 5, :big)
+ @io.writebits(0b00, 2, :big)
@io.value.should == [0b1100_1000].pack("C")
end
it "should write two bitfields totalling more than 1 byte" do
- @io.writebits(0b01_0101, 6)
- @io.writebits(0b001_1001, 7)
+ @io.writebits(0b01_0101, 6, :big)
+ @io.writebits(0b001_1001, 7, :big)
@io.value.should == [0b0101_0100, 0b1100_1000].pack("CC")
end
it "should write two bitfields totalling more than 2 bytes" do
- @io.writebits(0b01_0111, 6)
- @io.writebits(0b1_0010_1001_1001, 13)
+ @io.writebits(0b01_0111, 6, :big)
+ @io.writebits(0b1_0010_1001_1001, 13, :big)
@io.value.should == [0b0101_1110, 0b0101_0011, 0b0010_0000].pack("CCC")
end
it "should pad unused bits when writing bytes" do
- @io.writebits(0b101, 3)
+ @io.writebits(0b101, 3, :big)
@io.writebytes([0b1011_1111].pack("C"))
- @io.writebits(0b01, 2)
+ @io.writebits(0b01, 2, :big)
@io.value.should == [0b1010_0000, 0b1011_1111, 0b0100_0000].pack("CCC")
end
end
@@ -283,6 +301,5 @@
io.writebits(0b110, 3, :big)
io.writebits(0b010, 3, :little)
io.value.should == [0b1100_0000, 0b0000_0010].pack("CC")
end
end
-