require 'test/unit' require 'bit_struct/struct_base' include BitStruct class TestTextStruct < Test::Unit::TestCase class AlignedText < StructBase unsigned :before, 8 text :aligned, 32 unsigned :after, 8 end def test_aligned_text data = [ 0x12, ?t, ?s, ?e, ?t, 0x34 ].pack( 'C*' ) struct = AlignedText.new data assert_equal 0x34, struct.before assert_equal 'test', struct.aligned assert_equal 0x12, struct.after struct.before = ?A struct.aligned = 'goal' struct.after = ?B assert_equal ?A, struct.before assert_equal 'goal', struct.aligned assert_equal ?B, struct.after assert_equal 'BlaogA', struct.data end class UnalignedText < StructBase unsigned :before, 4 text :unaligned, 24 unsigned :after, 4 end def test_unaligned_text a_hi, a_lo = get_hi_lo ?a b_hi, b_lo = get_hi_lo ?b c_hi, c_lo = get_hi_lo ?c data = [ 12 << 4 | c_hi, c_lo << 4 | b_hi, b_lo << 4 | a_hi, a_lo << 4 | 3 ].pack( 'C*' ) struct = UnalignedText.new data assert_equal 3, struct.before assert_equal 'abc', struct.unaligned assert_equal 12, struct.after end def get_hi_lo( char ) return char >> 4, char & 0x0F end end