bit_vector 0.9

Sign up to get free protection for your applications and to get access to all the features.
data/test/encoding.rb ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ # encoding: us-ascii
4
+ # encoding: iso-8859-1
5
+ #
6
+ # Test of encoding issues
7
+ # Shows how bit_vector is stored from left to right LSB to MSB so byte values are
8
+ #
9
+ require 'bit_vector'
10
+
11
+ a = BitVector.from_bin("0000000100000010000000110000010000000101000001100000011100001000")
12
+ # a is \x80\x40\xC0\x20\xA0\x60\xD0\x10
13
+ b = BitVector.from_bin("1000000001000000110000000010000010100000011000001110000000010000")
14
+ # b is \x01\x02\x03\x04\x05\x06\x07\x08
15
+ c = BitVector.new("µΩ")
16
+ # c is \xC1\xB5\xE2\x84\xA6
17
+
18
+ def dump name, bs
19
+ puts "#{name}: The first bit set is: #{bs.min}, the last is: #{bs.max}"
20
+ res = bs.to_bytes
21
+ puts ("I got size #{res.size} in encoding: #{res.encoding.name}: #{res.unpack('C*').join(',')}")
22
+ end
23
+
24
+ dump "A", a
25
+ dump "B", b
26
+ dump "C", c
data/test/spec.rb ADDED
@@ -0,0 +1,309 @@
1
+ # encoding: ASCII-8BIT
2
+ #
3
+ require 'minitest/autorun'
4
+ require 'bit_vector'
5
+
6
+ describe BitVector do
7
+
8
+ describe "new" do
9
+
10
+ describe "given a string" do
11
+
12
+ it "handles multiples of 8" do
13
+ a = BitVector.new("0000000000000000")
14
+ assert_equal(16, a.size)
15
+ assert_equal(2, a.bytesize)
16
+ end
17
+ it "handles arbitrary sizes" do
18
+ a = BitVector.new("000000000000000")
19
+ assert_equal(15, a.size)
20
+ end
21
+ it "encodes correctly" do
22
+ a = BitVector.new("010000100000001")
23
+ assert_equal(1, a.index(1,0))
24
+ assert_equal(6, a.index(1,2))
25
+ assert_equal(14, a.index(1,7))
26
+ end
27
+
28
+ end
29
+
30
+ describe "given a length" do
31
+
32
+ it "allocates correctly" do
33
+ { 1 => 1, 8 => 1, 9 => 2, 17 => 3, 31 => 4, 33 => 5, 63 => 8, 64 => 8, 65 => 9,
34
+ 255 => 32,256 => 32, 65535 => 8192, 65536 => 8192, 4294967295 => 536870912
35
+ }.each do |k,v|
36
+ a = BitVector.new(k)
37
+ assert_equal(v, a.bytesize)
38
+ end
39
+
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "set and get" do
45
+
46
+ it "use [] notation" do
47
+ a = BitVector.new("10101010111111100")
48
+ assert_equal(1,a[0])
49
+ assert_equal(0,a[1])
50
+ end
51
+
52
+ it "use []= notation" do
53
+ a = BitVector.new(13)
54
+ a[1] = 1
55
+ assert_equal("0100000000000",a.to_s)
56
+ end
57
+
58
+ it "don't check bounds" do
59
+ a = BitVector.new(13)
60
+ a[13] = 1
61
+ assert_equal(0, a[233])
62
+ end
63
+
64
+ it "manipulate correctly" do
65
+ [8,16,32,64,128,256,1111].each do |size|
66
+ a = BitVector.new(size)
67
+ [size/2,size-1, size].each do |pos|
68
+ a.set(pos,1)
69
+ assert_equal(1, a.get(pos))
70
+ assert_equal(0, a.get(pos+1))
71
+ end
72
+ end
73
+
74
+ end
75
+ end
76
+
77
+ describe "#from_bytes" do
78
+ it "creates from a string" do
79
+ a = BitVector.from_bytes("1")
80
+ assert_equal("10001100", a.to_s)
81
+ end
82
+ it "creates from a long string" do
83
+ a = BitVector.from_bytes("abcdefghij")
84
+ assert_equal('10000110'+'01000110'+'11000110'+'00100110'+'10100110'+'01100110'+'11100110'+'00010110'+'10010110'+'01010110', a.to_s)
85
+ end
86
+ end
87
+
88
+ describe "#bytesize" do
89
+ it "gets the number right" do
90
+ { 1 => 1, 8 => 1, 9 => 2, 17 => 3, 31 => 4, 33 => 5, 63 => 8, 64 => 8, 65 => 9,
91
+ 255 => 32,256 => 32, 65535 => 8192, 65536 => 8192
92
+ }.each do |k,v|
93
+ a = BitVector.new(k)
94
+ assert_equal(v, a.bytesize)
95
+ end
96
+ end
97
+ end
98
+
99
+ describe "#index" do
100
+ it "is consistent with #set" do
101
+ a = BitVector.new(33)
102
+ a.set(1,1)
103
+ a.set(6,1)
104
+ a.set(14,1)
105
+ assert_equal(1, a.index(1,0))
106
+ assert_equal(6, a.index(1,2))
107
+ assert_equal(14, a.index(1,7))
108
+ end
109
+ it "gets the positions right" do
110
+ a = BitVector.new("1000000001000000110000000010000010100000011000001110000000010000")
111
+ assert_equal(1, a[50])
112
+ assert_equal(0, a[51])
113
+ end
114
+ end
115
+
116
+ describe "to array" do
117
+ it "handles an empty bit_vector" do
118
+ a = BitVector.new("00000000000000000000000")
119
+ assert_equal [], a.to_ary
120
+ end
121
+ it "handles two single bits" do
122
+ a = BitVector.new("01010")
123
+ assert_equal([1,3], a.to_ary)
124
+ end
125
+ it "handles bit runs" do
126
+ a = BitVector.new("1100110101")
127
+ assert_equal([0..1, 4..5, 7, 9], a.to_ary)
128
+ end
129
+ end
130
+
131
+ describe "to_bytes" do
132
+ it "handles arbitrary bytes" do
133
+ a = BitVector.from_bytes("abcdefghijk")
134
+ assert_equal("abcdefghijk", a.to_bytes)
135
+ end
136
+ it "handles short binary" do
137
+ a = BitVector.new("0010000000000001")
138
+ assert_equal("\x04\x80", a.to_bytes)
139
+ end
140
+ it "handles odd bits at end" do
141
+ a = BitVector.new("00100000000000011")
142
+ assert_equal("\x04\x80\x01", a.to_bytes)
143
+ end
144
+ it "handles long binary" do
145
+ a = BitVector.new("1000000001000000110000000010000010100000011000001110000000010000")
146
+ assert_equal("\x01\x02\x03\x04\x05\x06\x07\x08", a.to_bytes)
147
+ end
148
+ end
149
+
150
+ describe "resize operator" do
151
+ it "trims it" do
152
+ a = BitVector.new("01010001010101010101000")
153
+ a.size = 7
154
+ assert_equal("0101000", a.to_s)
155
+ end
156
+ end
157
+
158
+ describe "normalize operator" do
159
+ it "trims" do
160
+ a = BitVector.new("01010001010101010101000")
161
+ assert_equal("01010001010101010101", a.normalize.to_s)
162
+ end
163
+ it "trims to single bit" do
164
+ a = BitVector.new("0000000000000000")
165
+ b = a.normalize
166
+ assert_equal(1, b.size)
167
+ assert_equal(0, b.get(0))
168
+ end
169
+ end
170
+
171
+ describe "min operator" do
172
+ it "finds the bottom" do
173
+ a = BitVector.new("01010001010101010101000")
174
+ assert_equal(1, a.min)
175
+ end
176
+ end
177
+
178
+ describe "max operator" do
179
+ it "finds the top" do
180
+ a = BitVector.new("01010001010101010101000")
181
+ assert_equal(19, a.max)
182
+ end
183
+ it "finds the top in a qw" do
184
+ a = BitVector.new("0000000100000010000000110000010000000101000001100000011100001000")
185
+ assert_equal(60, a.max)
186
+ end
187
+ it "finds the top in a +qw" do
188
+ a = BitVector.new("0000000100000010000000110000010000000101000001100000011100001000110100")
189
+ assert_equal(67, a.max)
190
+ end
191
+ end
192
+
193
+ describe "not operator" do
194
+ it "works" do
195
+ a = BitVector.new("01010")
196
+ assert_equal("10101", (~a).to_s)
197
+ end
198
+ it "doesn't flip the unused bits" do
199
+ a = BitVector.new("01010")
200
+ b = ~a
201
+ assert_equal(0, b.get(5))
202
+ end
203
+ it "preserves the length when result is empty" do
204
+ a = BitVector.new("1"*35)
205
+ b = ~a
206
+ assert_equal("0"*35, b.to_s)
207
+ end
208
+ it "flips a +qw" do
209
+ a = BitVector.new("0000000100000010000000110000010000000101000001100000011100001000110100")
210
+ b = "1111111011111101111111001111101111111010111110011111100011110111001011"
211
+ assert_equal(b, (~a).to_s)
212
+ end
213
+ it "flips all ones" do
214
+ a = BitVector.new("111111111")
215
+ assert_equal "000000000", (~a).to_s
216
+ end
217
+ end
218
+
219
+ describe "and" do
220
+ it "handles the simplest case" do
221
+ a = BitVector.new("1100110101")
222
+ b = BitVector.new("1001001100")
223
+ assert_equal "1000000100", (a & b).to_s
224
+ end
225
+ describe "when the two operands are different sizes" do
226
+ it "treats the missing hi bits as zero" do
227
+ a = BitVector.new("1001")
228
+ b = BitVector.new("1001001100")
229
+ assert_equal "1001", (a & b).to_s
230
+ end
231
+ it "returns a result the size of the shortest" do
232
+ a = BitVector.new("1")
233
+ b = BitVector.new("1001001100")
234
+ assert_equal "1", (a & b).to_s
235
+ end
236
+ it "masks correctly" do
237
+ a = BitVector.new("111111111111111111111111")
238
+ b = BitVector.new("000000000")
239
+ assert_equal "000000000000000000000000", (a & b).to_s
240
+ end
241
+ end
242
+ end
243
+
244
+ describe "or" do
245
+ it "handles the simplest case" do
246
+ a = BitVector.new("1100110101")
247
+ b = BitVector.new("1001001100")
248
+ assert_equal "1101111101", (a | b).to_s
249
+ end
250
+ describe "when the two operands are different sizes" do
251
+ it "treats the missing hi bits as zero" do
252
+ a = BitVector.new(1)
253
+ b = BitVector.new("1001001100")
254
+ assert_equal "1001001100", (a | b).to_s
255
+ end
256
+ it "returns a result the size of the longest" do
257
+ a = BitVector.new("1")
258
+ b = BitVector.new("1001001100")
259
+ assert_equal "1001001100", (a | b).to_s
260
+ end
261
+ end
262
+ end
263
+
264
+ describe "xor operator" do
265
+ it "ignores unused bits" do
266
+ a = BitVector.new("01010")
267
+ b = BitVector.new("01010")
268
+ c = b ^ a
269
+ assert_equal("00000", c.to_s)
270
+ end
271
+ end
272
+
273
+ describe "minus operator" do
274
+ it "subtracts simple" do
275
+ a = BitVector.new("01010")
276
+ b = BitVector.new("01110")
277
+ c = a - b
278
+ assert_equal("00000", c.to_s)
279
+ assert_equal((a & (~b)).to_s, c.to_s)
280
+ end
281
+ it "handles different lengths" do
282
+ a = BitVector.new("111111111111111111111111")
283
+ b = BitVector.new("111111111")
284
+ x = a - b
285
+ assert_equal "000000000111111111111111", x.to_s
286
+ end
287
+ end
288
+
289
+ describe "with a set over 2**32 bits" do
290
+ describe "index" do
291
+ it "finds a bit set below 2**32" do
292
+ size = 4_294_967_296
293
+ under32 = 4_004_967_298
294
+ a = BitVector.new(size)
295
+ a.set(under32,1)
296
+ assert_equal under32, a.index(1,0)
297
+ end
298
+ end
299
+ it "finds a bit set above 2**32 bits" do
300
+ size = 6_294_967_296
301
+ over32 = 4_294_967_298
302
+ a = BitVector.new(size)
303
+ a.set(over32,1)
304
+ assert_equal(over32, a.index(1,0))
305
+ assert_equal(over32, a.min)
306
+ assert_equal(over32, a.max)
307
+ end
308
+ end
309
+ end
data/test/test.rb ADDED
@@ -0,0 +1,585 @@
1
+ # coding: ascii-8bit
2
+ #
3
+ require 'test/unit'
4
+ require 'bit_vector'
5
+
6
+ class TC_BitVector < Test::Unit::TestCase
7
+
8
+ # BitVector.new
9
+ def test_s_new_nil
10
+ a = BitVector.new
11
+ assert_equal(1, a.size)
12
+ assert_equal("0", a.to_s)
13
+ end
14
+
15
+ def test_s_new_fixnum
16
+ assert_raises(ArgumentError) {
17
+ a = BitVector.new(0)
18
+ }
19
+
20
+ assert_raises(ArgumentError) {
21
+ a = BitVector.new(-1)
22
+ }
23
+
24
+ [1,7,8,15,16,255,256,65535,65536].each {|n|
25
+ a = BitVector.new(n)
26
+ assert_equal(n, a.size)
27
+ assert_equal("0" * n, a.to_s)
28
+ }
29
+
30
+ end
31
+
32
+ def test_s_new_string
33
+ a = BitVector.new("00000")
34
+ assert_equal(5, a.size)
35
+ assert_equal("00000", a.to_s)
36
+
37
+ a = BitVector.new("0000011111")
38
+ assert_equal(10, a.size)
39
+ assert_equal("0000011111", a.to_s)
40
+ end
41
+
42
+ def test_s_new_other
43
+ assert_raises(ArgumentError) {
44
+ a = BitVector.new({})
45
+ }
46
+
47
+ assert_raises(ArgumentError) {
48
+ a = BitVector.new(:symbol)
49
+ }
50
+
51
+ assert_raises(ArgumentError) {
52
+ a = BitVector.new(1.0)
53
+ }
54
+ end
55
+
56
+ def test_s_new_GC
57
+ ary = Array.new(10)
58
+ assert_nothing_raised {
59
+ 200.times do
60
+ idx = rand(10)
61
+ size = rand(1000000) + 1
62
+ bits = BitVector.new(size)
63
+ ary[idx] = bits
64
+ end
65
+ }
66
+ end
67
+
68
+ # BitVector.from_bytes
69
+ def test_s_from_bytes
70
+ a = BitVector.from_bytes("a")
71
+ assert_equal(8, a.size)
72
+ assert_equal("10000110", a.to_s)
73
+
74
+ a = BitVector.from_bytes("ab")
75
+ assert_equal(16, a.size)
76
+ assert_equal("1000011001000110", a.to_s)
77
+
78
+ # a\0b
79
+ a = BitVector.from_bytes("a\0b")
80
+ assert_equal(24, a.size)
81
+ assert_equal("100001100000000001000110", a.to_s)
82
+ end
83
+
84
+ # BitVector#to_bytes
85
+ def test_to_bytes_byte
86
+ a = BitVector.new("10000000")
87
+ assert_equal("\x01", a.to_bytes)
88
+ end
89
+
90
+ def test_to_bytes_qw
91
+ a = BitVector.new("1000000000000000000000000000000000000000000000000000000000000000")
92
+ assert_equal("\x01\x00\x00\x00\x00\x00\x00\x00", a.to_bytes)
93
+ end
94
+
95
+ def test_to_bytes_nul
96
+ a = BitVector.new(24)
97
+ assert_equal("\x00\x00\x00", a.to_bytes)
98
+ end
99
+
100
+ # BitVector#to_s
101
+ def test_to_s
102
+ (0..255).each {|i|
103
+ a = BitVector.from_bytes(i.chr)
104
+ bit = ("%08b" % i).reverse
105
+ assert_equal(bit, a.to_s)
106
+ }
107
+ end
108
+
109
+ # BitVector#size
110
+ def test_size_getter
111
+ [1,7,8,15,16,255,256,65535,65536,4294967295,4294967296,4294967297].each {|n|
112
+ a = BitVector.new(n)
113
+ assert_equal(a.size, n)
114
+ }
115
+ end
116
+
117
+ # BitVector#size=
118
+ def test_size_setter
119
+ a = BitVector.new()
120
+ [1,7,8,15,16,255,256,65535,65536,4294967295,4294967296,4294967297].each {|n|
121
+ a.size = n
122
+ assert_equal(n, a.size)
123
+ }
124
+
125
+ assert_raises(ArgumentError) {
126
+ a.size = 0
127
+ }
128
+
129
+ assert_raises(ArgumentError) {
130
+ a.size = -1
131
+ }
132
+
133
+ end
134
+
135
+ # BitVector#length
136
+ def test_length
137
+ [1,7,8,15,16,255,256,65535,65536,4294967295,4294967296,4294967297].each {|n|
138
+ a = BitVector.new(n)
139
+ assert_equal(a.length, n)
140
+ }
141
+ end
142
+
143
+ # BitVector#dup
144
+ def test_dup
145
+ a = BitVector.new("0")
146
+ b = a.dup
147
+ assert_equal("0", b.to_s)
148
+
149
+ a = BitVector.new("10101010101010101")
150
+ b = a.dup
151
+ assert_equal("10101010101010101", b.to_s)
152
+ end
153
+
154
+ # BitVector#index
155
+ def test_index
156
+ a = BitVector.new("00101010101010101")
157
+ assert_equal(2, a.index(1,0))
158
+ assert_equal(2, a.index(1,2))
159
+ assert_equal(4, a.index(1,3))
160
+ end
161
+
162
+ # BitVector#slice
163
+ def test_slice
164
+ a = BitVector.new("10101010101010101")
165
+ b = a.slice(2..7)
166
+ assert_equal("101010", b.to_s)
167
+ end
168
+
169
+ # BitVector#clone
170
+ def test_clone
171
+ a = BitVector.new("0")
172
+ b = a.clone
173
+ assert_equal("0", b.to_s)
174
+
175
+ a = BitVector.new("10101010101010101")
176
+ b = a.clone
177
+ assert_equal("10101010101010101", b.to_s)
178
+ end
179
+
180
+ # BitVector#get
181
+ def test_get
182
+ a = BitVector.new("1")
183
+ assert_equal(1, a.get(0))
184
+ assert_equal(0, a.get(1))
185
+ assert_equal(0, a.get(100))
186
+
187
+ a = BitVector.new("10"*50)
188
+ assert_equal(1, a.get(98))
189
+ assert_equal(0, a.get(99))
190
+ end
191
+
192
+ # BitVector#set
193
+ def test_set
194
+ a = BitVector.new
195
+ a.set 1,1
196
+ assert_equal("01", a.to_s)
197
+ a.set 5,1
198
+ assert_equal("010001", a.to_s)
199
+ a.set 6,1
200
+ assert_equal("0100011", a.to_s)
201
+ a.set 5,0
202
+ assert_equal("0100001", a.to_s)
203
+ end
204
+
205
+ # BitVector#on
206
+ def test_on
207
+ a = BitVector.new
208
+ a.on 3
209
+ assert_equal("0001", a.to_s)
210
+ a.on 9
211
+ assert_equal("0001000001", a.to_s)
212
+ end
213
+
214
+ # BitVector#on range
215
+ def test_on_range
216
+ a = BitVector.new
217
+ a.on 1..3
218
+ assert_equal("0111", a.to_s)
219
+ a.on 6..10
220
+ assert_equal("01110011111", a.to_s)
221
+ end
222
+
223
+ # BitVector#off
224
+ def test_off
225
+ a = BitVector.new("11111111111111111111")
226
+ a.off 5
227
+ assert_equal("11111011111111111111", a.to_s)
228
+ a.off 9
229
+ assert_equal("11111011101111111111", a.to_s)
230
+ end
231
+
232
+ # BitVector#off
233
+ def test_off_range
234
+ a = BitVector.new("11111111111111111111")
235
+ a.off 2..4
236
+ assert_equal("11000111111111111111", a.to_s)
237
+ a.off 6..10
238
+ assert_equal("11000100000111111111", a.to_s)
239
+ end
240
+
241
+ # BitVector#clear
242
+ def test_clear
243
+ a = BitVector.new("0")
244
+ a.clear
245
+ assert_equal(1, a.size)
246
+ assert_equal("0", a.to_s)
247
+
248
+ a = BitVector.new("1111")
249
+ a.clear
250
+ assert_equal(4, a.size)
251
+ assert_equal("0000", a.to_s)
252
+
253
+ a = BitVector.new("111100001111000011")
254
+ a.clear
255
+ assert_equal(18, a.size)
256
+ assert_equal("000000000000000000", a.to_s)
257
+ end
258
+
259
+ # BitVector#|
260
+ def test_or
261
+ a = BitVector.new("000000")
262
+ b = BitVector.new("111000")
263
+ x = a | b
264
+ assert_equal("000000", a.to_s)
265
+ assert_equal("111000", b.to_s)
266
+ assert_equal("111000", x.to_s)
267
+
268
+ a = BitVector.new("0101010101")
269
+ b = BitVector.new("1111100000")
270
+ x = a | b
271
+ assert_equal("0101010101", a.to_s)
272
+ assert_equal("1111100000", b.to_s)
273
+ assert_equal("1111110101", x.to_s)
274
+
275
+ a = BitVector.new("00000000000001")
276
+ b = BitVector.new("00000000000010")
277
+ x = a | b
278
+ assert_equal("00000000000011", x.to_s)
279
+
280
+ a = BitVector.new("1")
281
+ b = BitVector.new("000000001")
282
+ x = a | b
283
+ assert_equal("100000001", x.to_s)
284
+
285
+ a = BitVector.new("000000001")
286
+ b = BitVector.new("1")
287
+ x = a | b
288
+ assert_equal("100000001", x.to_s)
289
+ end
290
+
291
+ # BitVector#+
292
+ def test_plus
293
+ a = BitVector.new("000000")
294
+ b = BitVector.new("111000")
295
+ x = a + b
296
+ assert_equal("000000", a.to_s)
297
+ assert_equal("111000", b.to_s)
298
+ assert_equal("111000", x.to_s)
299
+
300
+ a = BitVector.new("0101010101")
301
+ b = BitVector.new("1111100000")
302
+ x = a + b
303
+ assert_equal("0101010101", a.to_s)
304
+ assert_equal("1111100000", b.to_s)
305
+ assert_equal("1111110101", x.to_s)
306
+
307
+ a = BitVector.new("00000000000001")
308
+ b = BitVector.new("00000000000010")
309
+ x = a + b
310
+ assert_equal("00000000000011", x.to_s)
311
+
312
+ a = BitVector.new("1")
313
+ b = BitVector.new("000000001")
314
+ x = a + b
315
+ assert_equal("100000001", x.to_s)
316
+
317
+ a = BitVector.new("000000001")
318
+ b = BitVector.new("1")
319
+ x = a + b
320
+ assert_equal("100000001", x.to_s)
321
+ end
322
+
323
+ # BitVector#-
324
+ def test_minus
325
+ a = BitVector.new("01010101")
326
+ b = BitVector.new("00001111")
327
+ x = a - b
328
+ assert_equal("01010101", a.to_s)
329
+ assert_equal("00001111", b.to_s)
330
+ assert_equal("01010000", x.to_s)
331
+
332
+ a = BitVector.new("111111111111111111111111")
333
+ assert_equal(24, a.size)
334
+ b = BitVector.new("111111111")
335
+ assert_equal(9, b.size)
336
+ x = b - a
337
+ assert_equal("000000000", x.to_s)
338
+ x = a - b
339
+ assert_equal("000000000111111111111111", x.to_s)
340
+ end
341
+
342
+ # BitVector#<=>
343
+ def test_cmp
344
+ a = BitVector.new("0")
345
+ b = BitVector.new("1")
346
+ assert_equal(-1, a <=> b)
347
+ assert_equal(0, a <=> a)
348
+ assert_equal(1, b <=> a)
349
+
350
+ a = BitVector.new("1000000000")
351
+ b = BitVector.new("1")
352
+ assert_equal(0, a <=> b)
353
+ end
354
+
355
+ # BitVector#&
356
+ def test_and
357
+ a = BitVector.new("010111")
358
+ b = BitVector.new("001110")
359
+ x = a & b
360
+ assert_equal("010111", a.to_s)
361
+ assert_equal("001110", b.to_s)
362
+ assert_equal("000110", x.to_s)
363
+
364
+ a = BitVector.new("01010101")
365
+ b = BitVector.new("00001111")
366
+ x = a & b
367
+ assert_equal("01010101", a.to_s)
368
+ assert_equal("00001111", b.to_s)
369
+ assert_equal("00000101", x.to_s)
370
+
371
+ a = BitVector.new("0")
372
+ b = BitVector.new("1111111111")
373
+ x = a & b
374
+ assert_equal("0", x.to_s)
375
+ x = b & a
376
+ assert_equal("0000000000", x.to_s)
377
+
378
+ a = BitVector.new("11111111")
379
+ b = BitVector.new("1111111111111111")
380
+ x = a & b
381
+ assert_equal("11111111", x.to_s)
382
+ x = b & a
383
+ assert_equal("1111111100000000", x.to_s)
384
+
385
+ a = BitVector.new("11001100")
386
+ b = BitVector.new("1110111011101110")
387
+ x = a & b
388
+ assert_equal("11001100", x.to_s)
389
+ x = b & a
390
+ assert_equal("1100110000000000", x.to_s)
391
+ end
392
+
393
+ # BitVector#^
394
+ def test_xor
395
+ a = BitVector.new("01010101")
396
+ b = BitVector.new("00110011")
397
+ x = a ^ b
398
+ assert_equal("01010101", a.to_s)
399
+ assert_equal("00110011", b.to_s)
400
+ assert_equal("01100110", x.to_s)
401
+
402
+ a = BitVector.new("0")
403
+ b = BitVector.new("10101010101010")
404
+ x = a ^ b
405
+ assert_equal("10101010101010", x.to_s)
406
+ x = b ^ a
407
+ assert_equal("10101010101010", x.to_s)
408
+ end
409
+
410
+ # BitVector#*
411
+ def test_multi
412
+ a = BitVector.new("010111")
413
+ b = BitVector.new("001110")
414
+ x = a * b
415
+ assert_equal("010111", a.to_s)
416
+ assert_equal("001110", b.to_s)
417
+ assert_equal("000110", x.to_s)
418
+
419
+ a = BitVector.new("01010101")
420
+ b = BitVector.new("00001111")
421
+ x = a * b
422
+ assert_equal("01010101", a.to_s)
423
+ assert_equal("00001111", b.to_s)
424
+ assert_equal("00000101", x.to_s)
425
+
426
+ a = BitVector.new("0")
427
+ b = BitVector.new("1111111111")
428
+ x = a * b
429
+ assert_equal("0", x.to_s)
430
+ x = b * a
431
+ assert_equal("0000000000", x.to_s)
432
+
433
+ a = BitVector.new("11111111")
434
+ b = BitVector.new("1111111111111111")
435
+ x = a * b
436
+ assert_equal("11111111", x.to_s)
437
+ x = b * a
438
+ assert_equal("1111111100000000", x.to_s)
439
+
440
+ a = BitVector.new("11001100")
441
+ b = BitVector.new("1110111011101110")
442
+ x = a * b
443
+ assert_equal("11001100", x.to_s)
444
+ x = b * a
445
+ assert_equal("1100110000000000", x.to_s)
446
+ end
447
+
448
+ # BitVector#~
449
+ def test_not
450
+ a = BitVector.new("0")
451
+ x = ~a
452
+ assert_equal("0", a.to_s)
453
+ assert_equal("1", x.to_s)
454
+
455
+ a = BitVector.new("0101")
456
+ x = ~a
457
+ assert_equal("1010", x.to_s)
458
+
459
+ a = BitVector.new("00000000001111111111")
460
+ x = ~a
461
+ assert_equal("11111111110000000000", x.to_s)
462
+ end
463
+
464
+ # BitVector#zero?
465
+ def test_zero?
466
+ a = BitVector.new("0")
467
+ assert_equal(true, a.zero?)
468
+ a = BitVector.new("0"*100)
469
+ assert_equal(true, a.zero?)
470
+
471
+ a = BitVector.new("1")
472
+ assert_equal(false, a.zero?)
473
+ a = BitVector.new("0"*100 + "1")
474
+ assert_equal(false, a.zero?)
475
+
476
+ end
477
+
478
+ # BitVector#nonzero?
479
+ def test_nonzero?
480
+ a = BitVector.new("0")
481
+ assert_equal(false, a.nonzero?)
482
+ a = BitVector.new("0"*100)
483
+ assert_equal(false, a.nonzero?)
484
+
485
+ a = BitVector.new("1")
486
+ assert_equal(true, a.nonzero?)
487
+ a = BitVector.new("0"*100 + "1")
488
+ assert_equal(true, a.nonzero?)
489
+ end
490
+
491
+ # BitVector#max
492
+ def test_max
493
+ a = BitVector.new("0")
494
+ assert_equal(nil, a.max)
495
+ a = BitVector.new("1")
496
+ assert_equal(0, a.max)
497
+ a = BitVector.new("11")
498
+ assert_equal(1, a.max)
499
+ a = BitVector.new("10000000001")
500
+ assert_equal(10, a.max)
501
+ end
502
+
503
+ # BitVector#min
504
+ def test_min
505
+ a = BitVector.new("0")
506
+ assert_equal(nil, a.min)
507
+ a = BitVector.new("1")
508
+ assert_equal(0, a.min)
509
+ a = BitVector.new("11")
510
+ assert_equal(0, a.min)
511
+ a = BitVector.new("0000100001")
512
+ assert_equal(4, a.min)
513
+ end
514
+
515
+ # BitVector#normalize
516
+ def test_normalize
517
+ a = BitVector.new("0")
518
+ x = a.normalize
519
+ assert_equal("0", x.to_s)
520
+ a = BitVector.new("00000")
521
+ x = a.normalize
522
+ assert_equal("0", x.to_s)
523
+ a = BitVector.new("0101010101010000000")
524
+ x = a.normalize
525
+ assert_equal("010101010101", x.to_s)
526
+ assert_equal("0101010101010000000", a.to_s)
527
+ end
528
+
529
+ # BitVector#normalize!
530
+ def test_normalize!
531
+ a = BitVector.new("0")
532
+ x = a.normalize!
533
+ assert_equal("0", a.to_s)
534
+ assert_equal("0", x.to_s)
535
+ a = BitVector.new("00000")
536
+ x = a.normalize!
537
+ assert_equal("0", a.to_s)
538
+ assert_equal("0", x.to_s)
539
+ a = BitVector.new("0101010101010000000")
540
+ x = a.normalize!
541
+ assert_equal("010101010101", x.to_s)
542
+ assert_equal("010101010101", a.to_s)
543
+ end
544
+
545
+ def test_index_ops
546
+ a = BitVector.new("0000100011")
547
+ assert_equal(4, a.index(1,0))
548
+ assert_equal(4, a.index(1,4))
549
+ assert_equal(8, a.index(1,5))
550
+ size = 42949
551
+ over = 40040
552
+ b = BitVector.new(size)
553
+ b.set(over,1)
554
+ b.set(over+2,1)
555
+ assert_equal(over, b.index(1,0))
556
+ assert_equal(over+2, b.index(1,over+1))
557
+ end
558
+
559
+ # BitVector#to_ary
560
+ def test_to_ary
561
+ a = BitVector.new("00000")
562
+ assert_equal([], a.to_ary)
563
+ a = BitVector.new("01010")
564
+ assert_equal([1,3], a.to_ary)
565
+ a = BitVector.new("1100110101")
566
+ assert_equal([0..1, 4..5, 7, 9], a.to_ary)
567
+ a = BitVector.new("00111111111111011111111111100000000001111111111")
568
+ assert_equal([2..13, 15..26, 37..46], a.to_ary)
569
+ end
570
+
571
+ # BitVector#each
572
+ def test_each
573
+ a = BitVector.new("010101011")
574
+ ary = []
575
+ a.each {|idx|
576
+ ary << idx
577
+ }
578
+ assert_equal([1, 3, 5, 7, 8], ary)
579
+ end
580
+
581
+ end
582
+
583
+ if $0==__FILE__
584
+ Test::Unit::UI::Console::TestRunner.run(TC_BitVector.suite)
585
+ end