require 'test/unit' require 'bit_struct/struct_base' include BitStruct class Flags < StructBase unsigned :direction, 4 unsigned :multiplier, 2 unsigned :offset, 2 end class Entry < StructBase unsigned :offset, 4 nested :flags, Flags unsigned :address, 24 unsigned :cache_id, 16 end class TestBitStruct < Test::Unit::TestCase def test_flags_class fields = Flags.fields assert_equal 3, fields.size assert_equal :direction, fields[ 0 ].name assert_equal 4, fields[ 0 ].length assert_equal :multiplier, fields[ 1 ].name assert_equal 2, fields[ 1 ].length assert_equal :offset, fields[ 2 ].name assert_equal 2, fields[ 2 ].length end def test_attributes_class fields = Entry.fields assert_equal 4, fields.size assert_equal :offset, fields[ 0 ].name assert_equal 4, fields[ 0 ].length assert_equal :flags, fields[ 1 ].name assert_equal 8, fields[ 1 ].length assert_equal :address, fields[ 2 ].name assert_equal 24, fields[ 2 ].length assert_equal :cache_id, fields[ 3 ].name assert_equal 16, fields[ 3 ].length end def test_flags_read flags = Flags.new [ 0xFF ].pack( 'C*' ) assert_equal 0x0F, flags.direction assert_equal 0x03, flags.multiplier assert_equal 0x03, flags.offset flags = Flags.new [ 0x0F ].pack( 'C*' ) assert_equal 0x0F, flags.direction assert_equal 0x00, flags.multiplier assert_equal 0x00, flags.offset flags = Flags.new [ 0x3F ].pack( 'C*' ) assert_equal 0x0F, flags.direction assert_equal 0x03, flags.multiplier assert_equal 0x00, flags.offset flags = Flags.new [ 0xCF ].pack( 'C*' ) assert_equal 0x0F, flags.direction assert_equal 0x00, flags.multiplier assert_equal 0x03, flags.offset end def test_flags_write data = [ 0xFF ].pack( 'C*' ) flags = Flags.new data assert_equal '0xff', flags.data.hexify assert_equal 0x0F, flags.direction assert_equal 0x03, flags.multiplier assert_equal 0x03, flags.offset flags.multiplier = 0 assert_equal '0xcf', flags.data.hexify assert_equal 0x0F, flags.direction assert_equal 0x00, flags.multiplier assert_equal 0x03, flags.offset end def test_entry_read data = [ 0xAA, 0xFF, 0x01, 0x23, 0x45, 0x67, 0x89 ].pack( 'C*' ) entry = Entry.new data assert_equal 9, entry.offset assert_equal '0x12 0x34 0x56', entry.address.bytes.hexify assert_equal '0xaf 0xf0', entry.cache_id.bytes.hexify flags = entry.flags assert_equal 8, flags.direction assert_equal 3, flags.multiplier assert_equal 1, flags.offset end def test_attributes_write data = [ 0xAA, 0xFF, 0x01, 0x23, 0x45, 0x67, 0x89 ].pack( 'C*' ) entry = Entry.new data entry.offset = 15 assert_equal '0x0f', entry.offset.bytes.hexify assert_equal '0xaa 0xff 0x01 0x23 0x45 0x67 0x8f', entry.data.hexify entry.address = 0x44332211 assert_equal '0x33 0x22 0x11', entry.address.bytes.hexify assert_equal '0xaa 0xff 0x03 0x32 0x21 0x17 0x8f', entry.data.hexify entry.cache_id = 0xAABBCCDD assert_equal '0xcc 0xdd', entry.cache_id.bytes.hexify assert_equal '0xac 0xcd 0xd3 0x32 0x21 0x17 0x8f', entry.data.hexify flags = entry.flags assert_equal 8, flags.direction assert_equal 3, flags.multiplier assert_equal 1, flags.offset end end