Sha256: 22ba6f77c11301f2c1c935a0625ffb3655b928ebff1aa39a7d3c119a60cc228c

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

#!/usr/bin/env ruby

require File.expand_path(File.join(File.dirname(__FILE__), "common"))

describe BinData::ResumeByteAlignment do
  class ResumeAlignmentRecord < BinData::Record
    bit4 :a
    resume_byte_alignment
    bit4 :b
  end

  let(:obj) { ResumeAlignmentRecord.new }

  it "resets read alignment" do
    obj.read "\x12\x34"

    obj.a.must_equal 1
    obj.b.must_equal 3
  end

  it "resets write alignment" do
    obj.assign(:a => 2, :b => 7)

    obj.to_binary_s.must_equal "\x20\x70"
  end
end

describe BinData::BitAligned do
  it "does not apply to BinData::Primitives" do
    lambda {
      class BitAlignedPrimitive < BinData::Primitive
        bit_aligned
      end
    }.must_raise RuntimeError
  end

  class BitString < BinData::String
    bit_aligned
  end

  class BitAlignedRecord < BinData::Record
    bit4 :preamble
    bit_string :str, :length => 2
    bit4 :afterward
  end

  let(:obj) { BitAlignedRecord.new }

  it "#num_bytes as expected" do
    obj.num_bytes.must_equal 3
  end

  it "reads as expected" do
    obj.read("\x56\x36\x42")
    obj.snapshot.must_equal({"preamble" => 5, "str" => "cd", "afterward" => 2})
  end

  it "writes as expected" do
    obj.assign(:preamble => 5, :str => "ab", :afterward => 1)
    obj.to_binary_s.must_equal "\x56\x16\x21"
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bindata-1.8.3 test/alignment_test.rb
bindata-1.8.2 test/alignment_test.rb
bindata-1.8.1 test/alignment_test.rb
bindata-1.8.0 test/alignment_test.rb
bindata-1.6.0 test/alignment_test.rb