bit_vector 0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gemtest +0 -0
- data/README.md +52 -0
- data/Rakefile +55 -0
- data/ext/bit_vector/Makefile +238 -0
- data/ext/bit_vector/bit_vector.bundle +0 -0
- data/ext/bit_vector/bit_vector.c +1019 -0
- data/ext/bit_vector/bit_vector.o +0 -0
- data/ext/bit_vector/extconf.rb +8 -0
- data/lib/bit_vector/version.rb +3 -0
- data/test/benchmark.rb +496 -0
- data/test/encoding.rb +26 -0
- data/test/spec.rb +309 -0
- data/test/test.rb +585 -0
- metadata +78 -0
Binary file
|
data/test/benchmark.rb
ADDED
@@ -0,0 +1,496 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
require 'benchmark'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'optparse'
|
6
|
+
require 'ostruct'
|
7
|
+
require 'bit_vector'
|
8
|
+
require 'bit_vector/version'
|
9
|
+
|
10
|
+
opts = OpenStruct.new( n: 1000000, z: '100k' )
|
11
|
+
|
12
|
+
OptionParser.new do |o|
|
13
|
+
o.banner = "Benchmark Bitset"
|
14
|
+
|
15
|
+
o.on( '-?', '--help', 'Display this screen' ) do
|
16
|
+
puts o
|
17
|
+
exit
|
18
|
+
end
|
19
|
+
o.on('-n', '--number N', Integer, "The number of iterations (default: #{opts.n})") do |n|
|
20
|
+
opts.n = n
|
21
|
+
end
|
22
|
+
o.on('-z', '--size N', String, "The size of each IO (default: #{opts.z})") do |z|
|
23
|
+
opts.z = z
|
24
|
+
end
|
25
|
+
o.parse!
|
26
|
+
if /^(?<size>\d+)(?<mult>[kKmM]?)$/ =~ opts.z
|
27
|
+
opts.size = size.to_i
|
28
|
+
case mult
|
29
|
+
when 'k' then opts.size *= 1000
|
30
|
+
when 'K' then opts.size *= 1024
|
31
|
+
when 'm' then opts.size *= 1000000
|
32
|
+
when 'M' then opts.size *= 1054876
|
33
|
+
end
|
34
|
+
else
|
35
|
+
puts "Invalid size specification!"
|
36
|
+
end
|
37
|
+
|
38
|
+
if ARGV.size > 0
|
39
|
+
puts "You don't need arguments!"
|
40
|
+
puts o.help
|
41
|
+
exit 2
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
bsa = BitVector.new(opts.size)
|
46
|
+
|
47
|
+
puts "Commencing benchmark tests for Bitset #{BitVector::VERSION} \n"
|
48
|
+
puts "(Testing #{opts.n} iterations with #{opts.size} bits)\n\n"
|
49
|
+
|
50
|
+
Benchmark.bm(16) do |x|
|
51
|
+
x.report("Random set") do
|
52
|
+
opts.n.times do
|
53
|
+
bsa.set Random.rand(opts.size), 1
|
54
|
+
end
|
55
|
+
end
|
56
|
+
x.report("Random get") do
|
57
|
+
opts.n.times do
|
58
|
+
bsa.get Random.rand(opts.size)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
x.report("Random index") do
|
62
|
+
opts.n.times do
|
63
|
+
off = Random.rand(opts.size)
|
64
|
+
bsa.set off, 1
|
65
|
+
found = bsa.index 1, 0
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
__END__
|
71
|
+
|
72
|
+
# BitVector#index
|
73
|
+
def test_index
|
74
|
+
a = BitVector.from_bin("00101010101010101")
|
75
|
+
assert_equal(2, a.index(1,0))
|
76
|
+
assert_equal(2, a.index(1,2))
|
77
|
+
assert_equal(4, a.index(1,3))
|
78
|
+
end
|
79
|
+
|
80
|
+
# BitVector#slice
|
81
|
+
def test_slice
|
82
|
+
a = BitVector.from_bin("10101010101010101")
|
83
|
+
b = a.slice(2..7)
|
84
|
+
assert_equal("101010", b.to_s)
|
85
|
+
end
|
86
|
+
|
87
|
+
# BitVector#clone
|
88
|
+
def test_clone
|
89
|
+
a = BitVector.from_bin("0")
|
90
|
+
b = a.clone
|
91
|
+
assert_equal("0", b.to_s)
|
92
|
+
|
93
|
+
a = BitVector.from_bin("10101010101010101")
|
94
|
+
b = a.clone
|
95
|
+
assert_equal("10101010101010101", b.to_s)
|
96
|
+
end
|
97
|
+
|
98
|
+
# BitVector#get
|
99
|
+
def test_get
|
100
|
+
a = BitVector.from_bin("1")
|
101
|
+
assert_equal(1, a.get(0))
|
102
|
+
assert_equal(0, a.get(1))
|
103
|
+
assert_equal(0, a.get(100))
|
104
|
+
|
105
|
+
a = BitVector.from_bin("10"*50)
|
106
|
+
assert_equal(1, a.get(98))
|
107
|
+
assert_equal(0, a.get(99))
|
108
|
+
end
|
109
|
+
|
110
|
+
# BitVector#set
|
111
|
+
def test_set
|
112
|
+
a = BitVector.new
|
113
|
+
a.set 1,1
|
114
|
+
assert_equal("01", a.to_s)
|
115
|
+
a.set 5,1
|
116
|
+
assert_equal("010001", a.to_s)
|
117
|
+
a.set 6,1
|
118
|
+
assert_equal("0100011", a.to_s)
|
119
|
+
a.set 5,0
|
120
|
+
assert_equal("0100001", a.to_s)
|
121
|
+
end
|
122
|
+
|
123
|
+
# BitVector#on
|
124
|
+
def test_on
|
125
|
+
a = BitVector.new
|
126
|
+
a.on 3
|
127
|
+
assert_equal("0001", a.to_s)
|
128
|
+
a.on 9
|
129
|
+
assert_equal("0001000001", a.to_s)
|
130
|
+
end
|
131
|
+
|
132
|
+
# BitVector#on range
|
133
|
+
def test_on_range
|
134
|
+
a = BitVector.new
|
135
|
+
a.on 1..3
|
136
|
+
assert_equal("0111", a.to_s)
|
137
|
+
a.on 6..10
|
138
|
+
assert_equal("01110011111", a.to_s)
|
139
|
+
end
|
140
|
+
|
141
|
+
# BitVector#off
|
142
|
+
def test_off
|
143
|
+
a = BitVector.from_bin("11111111111111111111")
|
144
|
+
a.off 5
|
145
|
+
assert_equal("11111011111111111111", a.to_s)
|
146
|
+
a.off 9
|
147
|
+
assert_equal("11111011101111111111", a.to_s)
|
148
|
+
end
|
149
|
+
|
150
|
+
# BitVector#off
|
151
|
+
def test_off_range
|
152
|
+
a = BitVector.from_bin("11111111111111111111")
|
153
|
+
a.off 2..4
|
154
|
+
assert_equal("11000111111111111111", a.to_s)
|
155
|
+
a.off 6..10
|
156
|
+
assert_equal("11000100000111111111", a.to_s)
|
157
|
+
end
|
158
|
+
|
159
|
+
# BitVector#clear
|
160
|
+
def test_clear
|
161
|
+
a = BitVector.from_bin("0")
|
162
|
+
a.clear
|
163
|
+
assert_equal(1, a.size)
|
164
|
+
assert_equal("0", a.to_s)
|
165
|
+
|
166
|
+
a = BitVector.from_bin("1111")
|
167
|
+
a.clear
|
168
|
+
assert_equal(4, a.size)
|
169
|
+
assert_equal("0000", a.to_s)
|
170
|
+
|
171
|
+
a = BitVector.from_bin("111100001111000011")
|
172
|
+
a.clear
|
173
|
+
assert_equal(18, a.size)
|
174
|
+
assert_equal("000000000000000000", a.to_s)
|
175
|
+
end
|
176
|
+
|
177
|
+
# BitVector#|
|
178
|
+
def test_or
|
179
|
+
a = BitVector.from_bin("000000")
|
180
|
+
b = BitVector.from_bin("111000")
|
181
|
+
x = a | b
|
182
|
+
assert_equal("000000", a.to_s)
|
183
|
+
assert_equal("111000", b.to_s)
|
184
|
+
assert_equal("111000", x.to_s)
|
185
|
+
|
186
|
+
a = BitVector.from_bin("0101010101")
|
187
|
+
b = BitVector.from_bin("1111100000")
|
188
|
+
x = a | b
|
189
|
+
assert_equal("0101010101", a.to_s)
|
190
|
+
assert_equal("1111100000", b.to_s)
|
191
|
+
assert_equal("1111110101", x.to_s)
|
192
|
+
|
193
|
+
a = BitVector.from_bin("00000000000001")
|
194
|
+
b = BitVector.from_bin("00000000000010")
|
195
|
+
x = a | b
|
196
|
+
assert_equal("00000000000011", x.to_s)
|
197
|
+
|
198
|
+
a = BitVector.from_bin("1")
|
199
|
+
b = BitVector.from_bin("000000001")
|
200
|
+
x = a | b
|
201
|
+
assert_equal("100000001", x.to_s)
|
202
|
+
|
203
|
+
a = BitVector.from_bin("000000001")
|
204
|
+
b = BitVector.from_bin("1")
|
205
|
+
x = a | b
|
206
|
+
assert_equal("100000001", x.to_s)
|
207
|
+
end
|
208
|
+
|
209
|
+
# BitVector#+
|
210
|
+
def test_plus
|
211
|
+
a = BitVector.from_bin("000000")
|
212
|
+
b = BitVector.from_bin("111000")
|
213
|
+
x = a + b
|
214
|
+
assert_equal("000000", a.to_s)
|
215
|
+
assert_equal("111000", b.to_s)
|
216
|
+
assert_equal("111000", x.to_s)
|
217
|
+
|
218
|
+
a = BitVector.from_bin("0101010101")
|
219
|
+
b = BitVector.from_bin("1111100000")
|
220
|
+
x = a + b
|
221
|
+
assert_equal("0101010101", a.to_s)
|
222
|
+
assert_equal("1111100000", b.to_s)
|
223
|
+
assert_equal("1111110101", x.to_s)
|
224
|
+
|
225
|
+
a = BitVector.from_bin("00000000000001")
|
226
|
+
b = BitVector.from_bin("00000000000010")
|
227
|
+
x = a + b
|
228
|
+
assert_equal("00000000000011", x.to_s)
|
229
|
+
|
230
|
+
a = BitVector.from_bin("1")
|
231
|
+
b = BitVector.from_bin("000000001")
|
232
|
+
x = a + b
|
233
|
+
assert_equal("100000001", x.to_s)
|
234
|
+
|
235
|
+
a = BitVector.from_bin("000000001")
|
236
|
+
b = BitVector.from_bin("1")
|
237
|
+
x = a + b
|
238
|
+
assert_equal("100000001", x.to_s)
|
239
|
+
end
|
240
|
+
|
241
|
+
# BitVector#-
|
242
|
+
def test_minus
|
243
|
+
a = BitVector.from_bin("01010101")
|
244
|
+
b = BitVector.from_bin("00001111")
|
245
|
+
x = a - b
|
246
|
+
assert_equal("01010101", a.to_s)
|
247
|
+
assert_equal("00001111", b.to_s)
|
248
|
+
assert_equal("01010000", x.to_s)
|
249
|
+
|
250
|
+
a = BitVector.from_bin("111111111111111111111111")
|
251
|
+
b = BitVector.from_bin("111111111")
|
252
|
+
x = a - b
|
253
|
+
assert_equal("000000000111111111111111", x.to_s)
|
254
|
+
x = b - a
|
255
|
+
assert_equal("000000000", x.to_s)
|
256
|
+
end
|
257
|
+
|
258
|
+
# BitVector#<=>
|
259
|
+
def test_cmp
|
260
|
+
a = BitVector.from_bin("0")
|
261
|
+
b = BitVector.from_bin("1")
|
262
|
+
assert_equal(-1, a <=> b)
|
263
|
+
assert_equal(0, a <=> a)
|
264
|
+
assert_equal(1, b <=> a)
|
265
|
+
|
266
|
+
a = BitVector.from_bin("1000000000")
|
267
|
+
b = BitVector.from_bin("1")
|
268
|
+
assert_equal(0, a <=> b)
|
269
|
+
end
|
270
|
+
|
271
|
+
# BitVector#&
|
272
|
+
def test_and
|
273
|
+
a = BitVector.from_bin("010111")
|
274
|
+
b = BitVector.from_bin("001110")
|
275
|
+
x = a & b
|
276
|
+
assert_equal("010111", a.to_s)
|
277
|
+
assert_equal("001110", b.to_s)
|
278
|
+
assert_equal("000110", x.to_s)
|
279
|
+
|
280
|
+
a = BitVector.from_bin("01010101")
|
281
|
+
b = BitVector.from_bin("00001111")
|
282
|
+
x = a & b
|
283
|
+
assert_equal("01010101", a.to_s)
|
284
|
+
assert_equal("00001111", b.to_s)
|
285
|
+
assert_equal("00000101", x.to_s)
|
286
|
+
|
287
|
+
a = BitVector.from_bin("0")
|
288
|
+
b = BitVector.from_bin("1111111111")
|
289
|
+
x = a & b
|
290
|
+
assert_equal("0", x.to_s)
|
291
|
+
x = b & a
|
292
|
+
assert_equal("0000000000", x.to_s)
|
293
|
+
|
294
|
+
a = BitVector.from_bin("11111111")
|
295
|
+
b = BitVector.from_bin("1111111111111111")
|
296
|
+
x = a & b
|
297
|
+
assert_equal("11111111", x.to_s)
|
298
|
+
x = b & a
|
299
|
+
assert_equal("1111111100000000", x.to_s)
|
300
|
+
|
301
|
+
a = BitVector.from_bin("11001100")
|
302
|
+
b = BitVector.from_bin("1110111011101110")
|
303
|
+
x = a & b
|
304
|
+
assert_equal("11001100", x.to_s)
|
305
|
+
x = b & a
|
306
|
+
assert_equal("1100110000000000", x.to_s)
|
307
|
+
end
|
308
|
+
|
309
|
+
# BitVector#^
|
310
|
+
def test_xor
|
311
|
+
a = BitVector.from_bin("01010101")
|
312
|
+
b = BitVector.from_bin("00110011")
|
313
|
+
x = a ^ b
|
314
|
+
assert_equal("01010101", a.to_s)
|
315
|
+
assert_equal("00110011", b.to_s)
|
316
|
+
assert_equal("01100110", x.to_s)
|
317
|
+
|
318
|
+
a = BitVector.from_bin("0")
|
319
|
+
b = BitVector.from_bin("10101010101010")
|
320
|
+
x = a ^ b
|
321
|
+
assert_equal("10101010101010", x.to_s)
|
322
|
+
x = b ^ a
|
323
|
+
assert_equal("10101010101010", x.to_s)
|
324
|
+
end
|
325
|
+
|
326
|
+
# BitVector#*
|
327
|
+
def test_multi
|
328
|
+
a = BitVector.from_bin("010111")
|
329
|
+
b = BitVector.from_bin("001110")
|
330
|
+
x = a * b
|
331
|
+
assert_equal("010111", a.to_s)
|
332
|
+
assert_equal("001110", b.to_s)
|
333
|
+
assert_equal("000110", x.to_s)
|
334
|
+
|
335
|
+
a = BitVector.from_bin("01010101")
|
336
|
+
b = BitVector.from_bin("00001111")
|
337
|
+
x = a * b
|
338
|
+
assert_equal("01010101", a.to_s)
|
339
|
+
assert_equal("00001111", b.to_s)
|
340
|
+
assert_equal("00000101", x.to_s)
|
341
|
+
|
342
|
+
a = BitVector.from_bin("0")
|
343
|
+
b = BitVector.from_bin("1111111111")
|
344
|
+
x = a * b
|
345
|
+
assert_equal("0", x.to_s)
|
346
|
+
x = b * a
|
347
|
+
assert_equal("0000000000", x.to_s)
|
348
|
+
|
349
|
+
a = BitVector.from_bin("11111111")
|
350
|
+
b = BitVector.from_bin("1111111111111111")
|
351
|
+
x = a * b
|
352
|
+
assert_equal("11111111", x.to_s)
|
353
|
+
x = b * a
|
354
|
+
assert_equal("1111111100000000", x.to_s)
|
355
|
+
|
356
|
+
a = BitVector.from_bin("11001100")
|
357
|
+
b = BitVector.from_bin("1110111011101110")
|
358
|
+
x = a * b
|
359
|
+
assert_equal("11001100", x.to_s)
|
360
|
+
x = b * a
|
361
|
+
assert_equal("1100110000000000", x.to_s)
|
362
|
+
end
|
363
|
+
|
364
|
+
# BitVector#~
|
365
|
+
def test_not
|
366
|
+
a = BitVector.from_bin("0")
|
367
|
+
x = ~a
|
368
|
+
assert_equal("0", a.to_s)
|
369
|
+
assert_equal("1", x.to_s)
|
370
|
+
|
371
|
+
a = BitVector.from_bin("0101")
|
372
|
+
x = ~a
|
373
|
+
assert_equal("1010", x.to_s)
|
374
|
+
|
375
|
+
a = BitVector.from_bin("00000000001111111111")
|
376
|
+
x = ~a
|
377
|
+
assert_equal("11111111110000000000", x.to_s)
|
378
|
+
end
|
379
|
+
|
380
|
+
# BitVector#zero?
|
381
|
+
def test_zero?
|
382
|
+
a = BitVector.from_bin("0")
|
383
|
+
assert_equal(true, a.zero?)
|
384
|
+
a = BitVector.from_bin("0"*100)
|
385
|
+
assert_equal(true, a.zero?)
|
386
|
+
|
387
|
+
a = BitVector.from_bin("1")
|
388
|
+
assert_equal(false, a.zero?)
|
389
|
+
a = BitVector.from_bin("0"*100 + "1")
|
390
|
+
assert_equal(false, a.zero?)
|
391
|
+
|
392
|
+
end
|
393
|
+
|
394
|
+
# BitVector#nonzero?
|
395
|
+
def test_nonzero?
|
396
|
+
a = BitVector.from_bin("0")
|
397
|
+
assert_equal(false, a.nonzero?)
|
398
|
+
a = BitVector.from_bin("0"*100)
|
399
|
+
assert_equal(false, a.nonzero?)
|
400
|
+
|
401
|
+
a = BitVector.from_bin("1")
|
402
|
+
assert_equal(true, a.nonzero?)
|
403
|
+
a = BitVector.from_bin("0"*100 + "1")
|
404
|
+
assert_equal(true, a.nonzero?)
|
405
|
+
end
|
406
|
+
|
407
|
+
# BitVector#max
|
408
|
+
def test_max
|
409
|
+
a = BitVector.from_bin("0")
|
410
|
+
assert_equal(nil, a.max)
|
411
|
+
a = BitVector.from_bin("1")
|
412
|
+
assert_equal(0, a.max)
|
413
|
+
a = BitVector.from_bin("11")
|
414
|
+
assert_equal(1, a.max)
|
415
|
+
a = BitVector.from_bin("10000000001")
|
416
|
+
assert_equal(10, a.max)
|
417
|
+
end
|
418
|
+
|
419
|
+
# BitVector#min
|
420
|
+
def test_min
|
421
|
+
a = BitVector.from_bin("0")
|
422
|
+
assert_equal(nil, a.min)
|
423
|
+
a = BitVector.from_bin("1")
|
424
|
+
assert_equal(0, a.min)
|
425
|
+
a = BitVector.from_bin("11")
|
426
|
+
assert_equal(0, a.min)
|
427
|
+
a = BitVector.from_bin("0000100001")
|
428
|
+
assert_equal(4, a.min)
|
429
|
+
end
|
430
|
+
|
431
|
+
# BitVector#normalize
|
432
|
+
def test_normalize
|
433
|
+
a = BitVector.from_bin("0")
|
434
|
+
x = a.normalize
|
435
|
+
assert_equal("0", x.to_s)
|
436
|
+
a = BitVector.from_bin("00000")
|
437
|
+
x = a.normalize
|
438
|
+
assert_equal("0", x.to_s)
|
439
|
+
a = BitVector.from_bin("0101010101010000000")
|
440
|
+
x = a.normalize
|
441
|
+
assert_equal("010101010101", x.to_s)
|
442
|
+
assert_equal("0101010101010000000", a.to_s)
|
443
|
+
end
|
444
|
+
|
445
|
+
# BitVector#normalize!
|
446
|
+
def test_normalize!
|
447
|
+
a = BitVector.from_bin("0")
|
448
|
+
x = a.normalize!
|
449
|
+
assert_equal("0", a.to_s)
|
450
|
+
assert_equal("0", x.to_s)
|
451
|
+
a = BitVector.from_bin("00000")
|
452
|
+
x = a.normalize!
|
453
|
+
assert_equal("0", a.to_s)
|
454
|
+
assert_equal("0", x.to_s)
|
455
|
+
a = BitVector.from_bin("0101010101010000000")
|
456
|
+
x = a.normalize!
|
457
|
+
assert_equal("010101010101", x.to_s)
|
458
|
+
assert_equal("010101010101", a.to_s)
|
459
|
+
end
|
460
|
+
|
461
|
+
# BitVector#to_ary
|
462
|
+
def test_to_ary
|
463
|
+
a = BitVector.from_bin("00000")
|
464
|
+
assert_equal([], a.to_ary)
|
465
|
+
a = BitVector.from_bin("01010")
|
466
|
+
assert_equal([1,3], a.to_ary)
|
467
|
+
a = BitVector.from_bin("1100110101")
|
468
|
+
assert_equal([0..1, 4..5, 7, 9], a.to_ary)
|
469
|
+
a = BitVector.from_bin("00111111111111011111111111100000000001111111111")
|
470
|
+
assert_equal([2..13, 15..26, 37..46], a.to_ary)
|
471
|
+
end
|
472
|
+
|
473
|
+
# BitVector#each
|
474
|
+
def test_each
|
475
|
+
a = BitVector.from_bin("010101011")
|
476
|
+
ary = []
|
477
|
+
a.each {|idx|
|
478
|
+
ary << idx
|
479
|
+
}
|
480
|
+
assert_equal([1, 3, 5, 7, 8], ary)
|
481
|
+
end
|
482
|
+
|
483
|
+
# BitVector#to_bytes
|
484
|
+
def test_to_bytes
|
485
|
+
a = BitVector.new("abc")
|
486
|
+
assert_equal("abc", a.to_bytes)
|
487
|
+
a = BitVector.from_bin("00000000000000000")
|
488
|
+
assert_equal("\x00\x00\x00", a.to_bytes)
|
489
|
+
end
|
490
|
+
|
491
|
+
end
|
492
|
+
|
493
|
+
|
494
|
+
if $0==__FILE__
|
495
|
+
Test::Unit::UI::Console::TestRunner.run(TC_BitVector.suite)
|
496
|
+
end
|