Sha256: 3bd4fb079a9fed2bb273f6f16fdd7e412254c01586b462449db1571221ed0681

Contents?: true

Size: 1.33 KB

Versions: 15

Compression:

Stored size: 1.33 KB

Contents

require_relative 'helper'


class TestBitFlagAccessor < Test::Unit::TestCase

  include Xot::BitUtil

  class IntAccessor
    attr_accessor :bits
    alias set bits=
    alias get bits
    def initialize(&block)
      @bits = 0
      block.call self if block
    end
  end

  class SymbolAccessor < IntAccessor
    bit_flag_accessor :bits do
      flag :bit0, bit: 0
      flag :bit1, bit: 1
    end
  end

  def int(&block)
    IntAccessor.new(&block)
  end

  def symbol(&block)
    SymbolAccessor.new(&block)
  end

  def flag()
    SymbolAccessor.bits_flag
  end

  def test_int_accessor()
    assert_equal 0b0,  int.get
    assert_equal 0b1,  int{|o| o.bits = bit 0}.get
    assert_equal 0b10, int{|o| o.bits = bit 1}.get
  end

  def test_symbol_writer()
    assert_equal 0b0,  symbol.get
    assert_equal 0b1,  symbol{|o| o.bits = :bit0}.get
    assert_equal 0b10, symbol{|o| o.bits = :bit1}.get
  end

  def test_symbol_reader()
    assert_equal [],             symbol.bits
    assert_equal [:bit0],        symbol{|o| o.set bit 0}.bits
    assert_equal [:bit1],        symbol{|o| o.set bit 1}.bits
    assert_equal [:bit0, :bit1], symbol{|o| o.set bit 0, 1}.bits
  end

  def test_singleton_flag_reader()
    assert_equal 0b11,           flag.symbols2bits(:bit0, :bit1)
    assert_equal [:bit0, :bit1], flag.bits2symbols(0b11)
  end

end# TestBitFlagAccessor

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
xot-0.3.4 test/test_bit_flag_accessor.rb
xot-0.3.3 test/test_bit_flag_accessor.rb
xot-0.3.2 test/test_bit_flag_accessor.rb
xot-0.3.1 test/test_bit_flag_accessor.rb
xot-0.3 test/test_bit_flag_accessor.rb
xot-0.2.1 test/test_bit_flag_accessor.rb
xot-0.2 test/test_bit_flag_accessor.rb
xot-0.1.42 test/test_bit_flag_accessor.rb
xot-0.1.41 test/test_bit_flag_accessor.rb
xot-0.1.40 test/test_bit_flag_accessor.rb
xot-0.1.39 test/test_bit_flag_accessor.rb
xot-0.1.38 test/test_bit_flag_accessor.rb
xot-0.1.37 test/test_bit_flag_accessor.rb
xot-0.1.36 test/test_bit_flag_accessor.rb
xot-0.1.35 test/test_bit_flag_accessor.rb