]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/test/maasha/test_bitarray.rb
changed layout of ruby source
[biopieces.git] / code_ruby / test / maasha / test_bitarray.rb
diff --git a/code_ruby/test/maasha/test_bitarray.rb b/code_ruby/test/maasha/test_bitarray.rb
new file mode 100755 (executable)
index 0000000..4745401
--- /dev/null
@@ -0,0 +1,141 @@
+#!/usr/bin/env ruby
+$:.unshift File.join(File.dirname(__FILE__),'..','lib')
+
+# Copyright (C) 2007-2010 Martin A. Hansen.
+
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# http://www.gnu.org/copyleft/gpl.html
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# This software is part of the Biopieces framework (www.biopieces.org).
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
+require 'maasha/bitarray'
+require 'test/unit'
+require 'pp'
+
+class TestBitArray < Test::Unit::TestCase 
+  def setup
+    @ba = BitArray.new(10)
+  end
+
+  def test_BitArray_initialize_raises_on_bad_sizes
+    [ -1, 0, 1.1, "a" ].each do |size|
+      assert_raise(BitArrayError) { BitArray.new(size) }
+    end
+  end
+
+  def test_BitArray_initialize_dont_raise_on_ok_sizes
+    [ 1, 10, 1000 ].each do |size|
+      assert_nothing_raised { BitArray.new(size) }
+    end
+  end
+
+  def test_BitArray_size_returns_correctly
+    assert_equal(10, @ba.size)
+  end
+
+  def test_BitArray_to_s_returns_correctly
+    assert_equal("0000000000", @ba.to_s)
+  end
+
+  def test_BitArray_bit_set_with_bad_pos_raises
+    [-1, 10, 1.1].each do |pos|
+      assert_raise(BitArrayError) { @ba.bit_set(pos) }
+    end
+  end
+
+  def test_BitArray_bit_set
+    str = "0000000000"
+
+    (0.upto 9).each do |pos|
+      @ba.bit_set(pos)
+      str[pos] = "1"
+      assert_equal(str, @ba.to_s)
+    end
+  end
+
+  def test_BitArray_bit_set_questionmark_with_bad_pos_raises
+    [-1, 10, 1.1].each do |pos|
+      assert_raise(BitArrayError) { @ba.bit_set?(pos) }
+    end
+  end
+
+  def test_BitArray_bit_set_questionmark
+    (0.upto 9).each do |pos|
+      @ba.bit_set(pos)
+      assert_equal(true, @ba.bit_set?(pos))
+    end
+  end
+
+  def test_BitArray_bits_on_returns_correctly
+    @ba.bit_set(4)
+    @ba.bit_set(0)
+    @ba.bit_set(1)
+    assert_equal(3, @ba.bits_on)
+  end
+
+  def test_BitArray_bits_off_returns_correctly
+    @ba.bit_set(4)
+    assert_equal(9, @ba.bits_off)
+  end
+
+  def test_BitArray_AND_with_uneven_sizes_raises
+    ba = BitArray.new(11)
+    assert_raise(BitArrayError) { @ba & ba }
+  end
+
+  def test_BitArray_AND_returns_correctly
+    ba = BitArray.new(10)
+    @ba.bit_set(4)
+    @ba.bit_set(5)
+    ba.bit_set(5)
+    ba.bit_set(6)
+    assert_equal( "0000010000", (@ba & ba).to_s)
+  end
+
+  def test_BitArray_OR_with_uneven_sizes_raises
+    ba = BitArray.new(11)
+    assert_raise(BitArrayError) { @ba | ba }
+  end
+
+  def test_BitArray_OR_returns_correctly
+    ba = BitArray.new(10)
+    @ba.bit_set(4)
+    @ba.bit_set(5)
+    ba.bit_set(5)
+    ba.bit_set(6)
+    assert_equal( "0000111000", (@ba | ba).to_s)
+  end
+
+  def test_BitArray_XOR_with_uneven_sizes_raises
+    ba = BitArray.new(11)
+    assert_raise(BitArrayError) { @ba ^ ba }
+  end
+
+  def test_BitArray_XOR_returns_correctly
+    ba = BitArray.new(10)
+    @ba.bit_set(4)
+    @ba.bit_set(5)
+    ba.bit_set(5)
+    ba.bit_set(6)
+    assert_equal( "0000101000", (@ba ^ ba).to_s)
+  end
+end
+