Sha256: fea07aa5a409ecb04aa68db28cbe6d73ff6e2094c037a1cae72eb847fed8b048

Contents?: true

Size: 1.36 KB

Versions: 13

Compression:

Stored size: 1.36 KB

Contents

# -*- coding: utf-8 -*-


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

13 entries across 13 versions & 1 rubygems

Version Path
xot-0.1.34 test/test_bit_flag_accessor.rb
xot-0.1.33 test/test_bit_flag_accessor.rb
xot-0.1.32 test/test_bit_flag_accessor.rb
xot-0.1.31 test/test_bit_flag_accessor.rb
xot-0.1.30 test/test_bit_flag_accessor.rb
xot-0.1.29 test/test_bit_flag_accessor.rb
xot-0.1.28 test/test_bit_flag_accessor.rb
xot-0.1.27 test/test_bit_flag_accessor.rb
xot-0.1.26 test/test_bit_flag_accessor.rb
xot-0.1.25 test/test_bit_flag_accessor.rb
xot-0.1.24 test/test_bit_flag_accessor.rb
xot-0.1.23 test/test_bit_flag_accessor.rb
xot-0.1.22 test/test_bit_flag_accessor.rb