]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/Maasha/test/test_bitarray.rb
add dist option to find_adaptor
[biopieces.git] / code_ruby / Maasha / test / test_bitarray.rb
1 #!/usr/bin/env ruby
2 $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
4 # Copyright (C) 2007-2010 Martin A. Hansen.
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 # http://www.gnu.org/copyleft/gpl.html
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # This software is part of the Biopieces framework (www.biopieces.org).
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28
29 require 'bitarray'
30 require 'test/unit'
31 require 'pp'
32
33 class TestBitArray < Test::Unit::TestCase 
34   def setup
35     @ba = BitArray.new(10)
36   end
37
38   def test_BitArray_initialize_raises_on_bad_sizes
39     [ -1, 0, 1.1, "a" ].each do |size|
40       assert_raise(BitArrayError) { BitArray.new(size) }
41     end
42   end
43
44   def test_BitArray_initialize_dont_raise_on_ok_sizes
45     [ 1, 10, 1000 ].each do |size|
46       assert_nothing_raised { BitArray.new(size) }
47     end
48   end
49
50   def test_BitArray_size_returns_correctly
51     assert_equal(10, @ba.size)
52   end
53
54   def test_BitArray_to_s_returns_correctly
55     assert_equal("0000000000", @ba.to_s)
56   end
57
58   def test_BitArray_bit_set_with_bad_pos_raises
59     [-1, 10, 1.1].each do |pos|
60       assert_raise(BitArrayError) { @ba.bit_set(pos) }
61     end
62   end
63
64   def test_BitArray_bit_set
65     str = "0000000000"
66
67     (0.upto 9).each do |pos|
68       @ba.bit_set(pos)
69       str[pos] = "1"
70       assert_equal(str, @ba.to_s)
71     end
72   end
73
74   def test_BitArray_bit_set_questionmark_with_bad_pos_raises
75     [-1, 10, 1.1].each do |pos|
76       assert_raise(BitArrayError) { @ba.bit_set?(pos) }
77     end
78   end
79
80   def test_BitArray_bit_set_questionmark
81     (0.upto 9).each do |pos|
82       @ba.bit_set(pos)
83       assert_equal(true, @ba.bit_set?(pos))
84     end
85   end
86
87   def test_BitArray_bits_on_returns_correctly
88     @ba.bit_set(4)
89     @ba.bit_set(0)
90     @ba.bit_set(1)
91     assert_equal(3, @ba.bits_on)
92   end
93
94   def test_BitArray_bits_off_returns_correctly
95     @ba.bit_set(4)
96     assert_equal(9, @ba.bits_off)
97   end
98
99   def test_BitArray_AND_with_uneven_sizes_raises
100     ba = BitArray.new(11)
101     assert_raise(BitArrayError) { @ba & ba }
102   end
103
104   def test_BitArray_AND_returns_correctly
105     ba = BitArray.new(10)
106     @ba.bit_set(4)
107     @ba.bit_set(5)
108     ba.bit_set(5)
109     ba.bit_set(6)
110     assert_equal( "0000010000", (@ba & ba).to_s)
111   end
112
113   def test_BitArray_OR_with_uneven_sizes_raises
114     ba = BitArray.new(11)
115     assert_raise(BitArrayError) { @ba | ba }
116   end
117
118   def test_BitArray_OR_returns_correctly
119     ba = BitArray.new(10)
120     @ba.bit_set(4)
121     @ba.bit_set(5)
122     ba.bit_set(5)
123     ba.bit_set(6)
124     assert_equal( "0000111000", (@ba | ba).to_s)
125   end
126
127   def test_BitArray_XOR_with_uneven_sizes_raises
128     ba = BitArray.new(11)
129     assert_raise(BitArrayError) { @ba ^ ba }
130   end
131
132   def test_BitArray_XOR_returns_correctly
133     ba = BitArray.new(10)
134     @ba.bit_set(4)
135     @ba.bit_set(5)
136     ba.bit_set(5)
137     ba.bit_set(6)
138     assert_equal( "0000101000", (@ba ^ ba).to_s)
139   end
140 end
141