Skip to content

Commit c927c07

Browse files
committed
Fix initialize with custom field
1 parent eac22b2 commit c927c07

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/bitarray.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class BitArray
77

88
def initialize(size, field = nil)
99
@size = size
10-
@field = "\0" * (size / 8 + 1)
10+
@field = field || "\0" * (size / 8 + 1)
1111
end
1212

1313
# Set a bit (1/0)

test/test_bitarray.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,26 @@ def test_size
4848

4949
def test_to_s
5050
ba = BitArray.new(35)
51-
[1, 5, 6, 7, 10, 16, 33].each{|i|ba[i] = 1}
51+
[1, 5, 6, 7, 10, 16, 33].each { |i| ba[i] = 1}
5252
assert_equal "01000111001000001000000000000000010", ba.to_s
5353
end
5454

5555
def test_field
5656
ba = BitArray.new(35)
57-
[1, 5, 6, 7, 10, 16, 33].each{|i|ba[i] = 1}
57+
[1, 5, 6, 7, 10, 16, 33].each { |i| ba[i] = 1}
5858
assert_equal "0100011100100000100000000000000001000000", ba.field.unpack('B*')[0]
5959
end
6060

61+
def test_initialize_with_field
62+
ba = BitArray.new(15, ["0100011100100001"].pack('B*'))
63+
64+
assert_equal [1, 5, 6, 7, 10, 15], 0.upto(15).select { |i| ba[i] == 1 }
65+
66+
ba[2] = 1
67+
ba[12] = 1
68+
assert_equal [1, 2, 5, 6, 7, 10, 12, 15], 0.upto(15).select { |i| ba[i] == 1 }
69+
end
70+
6171
def test_total_set
6272
ba = BitArray.new(10)
6373
ba[1] = 1

0 commit comments

Comments
 (0)