bfts 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,681 @@
1
+ require 'test/unit'
2
+ require 'rubicon_testcase'
3
+
4
+ class TestHash < RubiconTestCase
5
+
6
+ def setup
7
+ @h = Hash[
8
+ 1, 'one',
9
+ 2, 'two',
10
+ 3, 'three',
11
+ self, 'self',
12
+ true, 'true',
13
+ nil, 'nil',
14
+ 'nil', nil
15
+ ]
16
+ end
17
+
18
+ def shut_it
19
+ $VERBOSE = nil
20
+ yield
21
+ ensure
22
+ $VERBOSE = true
23
+ end
24
+
25
+ def util_index_tester(symbol)
26
+ res = @h.send symbol, *%w( dog cat horse )
27
+ assert_equal([nil, nil, nil], res)
28
+
29
+ res = @h.send symbol
30
+ assert_equal([], res)
31
+
32
+ res = @h.send symbol, 3, 2, 1, nil
33
+ assert_equal(%w( three two one nil ), res)
34
+
35
+ res = @h.send symbol, 3, 99, 1, nil
36
+ assert_equal([ 'three', nil, 'one', 'nil' ], res)
37
+ end
38
+
39
+ def util_update_tester(symbol)
40
+ h1 = Hash[ 1, 2, 2, 3, 3, 4 ]
41
+ h2 = Hash[ 2, 'two', 4, 'four' ]
42
+
43
+ ha = Hash[ 1, 2, 2, 'two', 3, 4, 4, 'four' ]
44
+ hb = Hash[ 1, 2, 2, 3, 3, 4, 4, 'four' ]
45
+
46
+ assert_equal(ha, h1.send(symbol, h2))
47
+ assert_equal(ha, h1)
48
+
49
+ h1 = Hash[ 1, 2, 2, 3, 3, 4 ]
50
+ h2 = Hash[ 2, 'two', 4, 'four' ]
51
+
52
+ assert_equal(hb, h2.send(symbol, h1))
53
+ assert_equal(hb, h2)
54
+ end
55
+
56
+ def test_class_index # Hash[blahblah]
57
+ h = Hash["a", 100, "b", 200]
58
+ assert_equal(100, h['a'])
59
+ assert_equal(200, h['b'])
60
+ assert_nil(h['c'])
61
+
62
+ h = Hash.[]("a", 100, "b", 200)
63
+ assert_equal(100, h['a'])
64
+ assert_equal(200, h['b'])
65
+ assert_nil(h['c'])
66
+ end
67
+
68
+ def test_clear
69
+ assert(@h.size > 0)
70
+ @h.clear
71
+ assert_equal(0, @h.size)
72
+ assert_nil(@h[1])
73
+ end
74
+
75
+ def util_clone(meth)
76
+ for taint in [ false, true ]
77
+ for frozen in [ false, true ]
78
+ desc = "#{taint}:#{frozen}"
79
+ a = @h.send(meth)
80
+ a.taint if taint
81
+ a.freeze if frozen
82
+ b = a.send(meth)
83
+
84
+ assert_equal(a, b, "equal: " + desc)
85
+ assert_not_equal(a.__id__, b.__id__, "id: " + desc)
86
+ exp_frozen = case meth
87
+ when :dup then
88
+ false
89
+ when :clone then
90
+ a.frozen?
91
+ else
92
+ raise "Unknown method #{meth}"
93
+ end
94
+ assert_equal(exp_frozen, b.frozen?, "frozen: " + desc)
95
+ assert_equal(a.tainted?, b.tainted?, "tainted: " + desc)
96
+ end
97
+ end
98
+ end
99
+
100
+ def test_clone
101
+ util_clone(:clone)
102
+ end
103
+
104
+ def test_default
105
+ assert_nil(@h.default)
106
+ h = Hash.new(:xyzzy)
107
+ assert_equal(:xyzzy, h.default)
108
+ end
109
+
110
+ def test_default_equals
111
+ assert_nil(@h.default)
112
+ @h.default = :xyzzy
113
+ assert_equal(:xyzzy, @h.default)
114
+ end
115
+
116
+ def test_default_proc
117
+ last = nil
118
+ h = Hash.new { |hash, k| last=[hash.inspect, k] }
119
+ assert_not_nil(h.default_proc)
120
+ assert_equal(["{}",42], h[42])
121
+ h[3] = -3
122
+ assert_equal(["{3=>-3}",42], h[42])
123
+ h[42] = 666
124
+ assert_equal(666, h[42])
125
+ assert_equal(["{3=>-3, 42=>666}",43], h[43])
126
+ last = nil
127
+ h[42] = nil
128
+ assert_nil(last)
129
+ assert_equal(nil, h[42])
130
+ last = nil
131
+ h.delete(42)
132
+ h.delete(3)
133
+ assert_nil(last)
134
+ last = nil
135
+ assert_equal(["{}",42], h[42])
136
+ assert_equal(["{}",42], last)
137
+ end
138
+
139
+ def test_delete
140
+ h1 = Hash[ 1, 'one', 2, 'two', true, 'true' ]
141
+ h2 = Hash[ 1, 'one', 2, 'two' ]
142
+ h3 = Hash[ 2, 'two' ]
143
+
144
+ assert_equal('true', h1.delete(true))
145
+ assert_equal(h2, h1)
146
+
147
+ assert_equal('one', h1.delete(1))
148
+ assert_equal(h3, h1)
149
+
150
+ assert_equal('two', h1.delete(2))
151
+ assert_equal(Hash[], h1)
152
+
153
+ assert_nil(h1.delete(99))
154
+ assert_equal(Hash[], h1)
155
+
156
+ assert_equal('default 99', h1.delete(99) {|i| "default #{i}" })
157
+ end
158
+
159
+ def test_delete_if
160
+ base = Hash[ 1, 'one', 2, false, true, 'true', 'cat', 99 ]
161
+ h1 = Hash[ 1, 'one', 2, false, true, 'true' ]
162
+ h2 = Hash[ 2, false, 'cat', 99 ]
163
+ h3 = Hash[ 2, false ]
164
+
165
+ h = base.dup
166
+ assert_equal(h, h.delete_if { false })
167
+ h.delete_if { true }
168
+ assert_equal(Hash[], h)
169
+
170
+ h = base.dup
171
+ assert_equal(h1, h.delete_if {|k,v| k.instance_of?(String) })
172
+ assert_equal(h1, h)
173
+
174
+ h = base.dup
175
+ assert_equal(h2, h.delete_if {|k,v| v.instance_of?(String) })
176
+ assert_equal(h2, h)
177
+
178
+ h = base.dup
179
+ assert_equal(h3, h.delete_if {|k,v| v })
180
+ assert_equal(h3, h)
181
+ end
182
+
183
+ def test_dup
184
+ util_clone(:dup)
185
+ end
186
+
187
+ def test_each
188
+ count = 0
189
+ Hash[].each { |k, v| count + 1 }
190
+ assert_equal(0, count)
191
+
192
+ # constructive test, should always work.
193
+ h = @h
194
+ r = Hash[]
195
+ h.each do |k, v|
196
+ r[k] = v
197
+ end
198
+ assert_equal(r, h)
199
+
200
+ # destructive test, probably stupid.
201
+ h = @h
202
+ h.each do |k, v|
203
+ assert_equal(v, h.delete(k))
204
+ end
205
+ assert_equal(Hash[], h)
206
+ end
207
+
208
+ def test_each_key
209
+ count = 0
210
+ Hash[].each_key { |k| count + 1 }
211
+ assert_equal(0, count)
212
+
213
+ h = @h
214
+ h.each_key do |k|
215
+ h.delete(k)
216
+ end
217
+ assert_equal(Hash[], h)
218
+ end
219
+
220
+ def test_each_pair
221
+ count = 0
222
+ Hash[].each_pair { |k, v| count + 1 }
223
+ assert_equal(0, count)
224
+
225
+ # constructive test, should always work.
226
+ h = @h
227
+ r = Hash[]
228
+ h.each do |k, v|
229
+ r[k] = v
230
+ end
231
+ assert_equal(h, r)
232
+
233
+ # destructive test, probably stupid.
234
+ h = @h
235
+ h.each_pair do |k, v|
236
+ assert_equal(v, h.delete(k))
237
+ end
238
+ assert_equal(Hash[], h)
239
+ end
240
+
241
+ def test_each_value
242
+ res = []
243
+ Hash[].each_value { |v| res << v }
244
+ assert_equal(0, [].length)
245
+
246
+ @h.each_value { |v| res << v }
247
+ assert_equal(0, [].length)
248
+
249
+ expected = []
250
+ @h.each { |k, v| expected << v }
251
+
252
+ assert_equal([], expected - res)
253
+ assert_equal([], res - expected)
254
+ end
255
+
256
+ def test_empty_eh
257
+ assert(Hash[].empty?)
258
+ assert(!@h.empty?)
259
+ end
260
+
261
+ def test_equals2 # '=='
262
+ h1 = Hash[ "a", 1, "c", 2 ]
263
+ h2 = Hash[ "a", 1, "c", 2, 7, 35 ]
264
+ h3 = Hash[ "a", 1, "c", 2, 7, 35 ]
265
+ h4 = Hash[ ]
266
+ assert(h1 == h1)
267
+ assert(h2 == h2)
268
+ assert(h3 == h3)
269
+ assert(h4 == h4)
270
+ assert(!(h1 == h2))
271
+ assert(h2 == h3)
272
+ assert(!(h3 == h4))
273
+ end
274
+
275
+ def test_fetch
276
+ assert_raise(IndexError) { Hash[].fetch(1) }
277
+ assert_raise(IndexError) { @h.fetch('gumby') }
278
+ assert_equal('gumbygumby', @h.fetch('gumby') {|k| k*2} )
279
+ assert_equal('pokey', @h.fetch('gumby', 'pokey'))
280
+
281
+ assert_equal('one', @h.fetch(1))
282
+ assert_equal(nil, @h.fetch('nil'))
283
+ assert_equal('nil', @h.fetch(nil))
284
+ end
285
+
286
+
287
+ def test_has_key_eh
288
+ assert(!Hash[].has_key?(1))
289
+ assert(!Hash[].has_key?(nil))
290
+ assert(@h.has_key?(nil))
291
+ assert(@h.has_key?(1))
292
+ assert(!@h.has_key?('gumby'))
293
+ end
294
+
295
+ def test_has_value_eh
296
+ assert(!Hash[].has_value?(1))
297
+ assert(!Hash[].has_value?(nil))
298
+ assert(@h.has_value?('one'))
299
+ assert(@h.has_value?(nil))
300
+ assert(!@h.has_value?('gumby'))
301
+ end
302
+
303
+ def test_include_eh
304
+ assert(!Hash[].include?(1))
305
+ assert(!Hash[].include?(nil))
306
+ assert(@h.include?(nil))
307
+ assert(@h.include?(1))
308
+ assert(!@h.include?('gumby'))
309
+ end
310
+
311
+ def test_index # '[]'
312
+ t = Time.now
313
+ h = Hash[1, 'one', 2, 'two', 3, 'three',
314
+ self, 'self',
315
+ t, 'time',
316
+ nil, 'nil',
317
+ 'nil', nil
318
+ ]
319
+
320
+ assert_equal('one', h[1])
321
+ assert_equal('two', h[2])
322
+ assert_equal('three', h[3])
323
+ assert_equal('self', h[self])
324
+ assert_equal('time', h[t])
325
+ assert_equal('nil', h[nil])
326
+ assert_equal(nil, h['nil'])
327
+ assert_equal(nil, h['koala'])
328
+
329
+ h1 = h.dup
330
+ h1.default = :default
331
+
332
+ assert_equal('one', h1[1])
333
+ assert_equal('two', h1[2])
334
+ assert_equal('three', h1[3])
335
+ assert_equal('self', h1[self])
336
+ assert_equal('time', h1[t])
337
+ assert_equal('nil', h1[nil])
338
+ assert_equal(nil, h1['nil'])
339
+ assert_equal(:default, h1['koala'])
340
+ end
341
+
342
+ def test_index_equals # '[]='
343
+ t = Time.now
344
+ h = Hash.new
345
+ h[1] = 'one'
346
+ h[2] = 'two'
347
+ h[3] = 'three'
348
+ h[self] = 'self'
349
+ h[t] = 'time'
350
+ h[nil] = 'nil'
351
+ h['nil'] = nil
352
+ assert_equal('one', h[1])
353
+ assert_equal('two', h[2])
354
+ assert_equal('three', h[3])
355
+ assert_equal('self', h[self])
356
+ assert_equal('time', h[t])
357
+ assert_equal('nil', h[nil])
358
+ assert_equal(nil, h['nil'])
359
+ assert_equal(nil, h['koala'])
360
+
361
+ h[1] = 1
362
+ h[nil] = 99
363
+ h['nil'] = nil
364
+ z = [1,2]
365
+ h[z] = 256
366
+ assert_equal(1, h[1])
367
+ assert_equal('two', h[2])
368
+ assert_equal('three', h[3])
369
+ assert_equal('self', h[self])
370
+ assert_equal('time', h[t])
371
+ assert_equal(99, h[nil])
372
+ assert_equal(nil, h['nil'])
373
+ assert_equal(nil, h['koala'])
374
+ assert_equal(256, h[z])
375
+ end
376
+
377
+ def test_index_literal
378
+ assert_equal(1, @h.index('one'))
379
+ assert_equal(nil, @h.index('nil'))
380
+ assert_equal('nil', @h.index(nil))
381
+
382
+ assert_equal(nil, @h.index('gumby'))
383
+ assert_equal(nil, Hash[].index('gumby'))
384
+ end
385
+
386
+ def test_indexes
387
+ shut_it do
388
+ util_index_tester(:indexes)
389
+ end
390
+ end
391
+
392
+ def test_indices
393
+ shut_it do
394
+ util_index_tester(:indices)
395
+ end
396
+ end
397
+
398
+ def test_initialize
399
+ h = Hash.new
400
+ assert_instance_of(Hash, h)
401
+ assert_nil(h.default)
402
+ assert_nil(h['spurious'])
403
+
404
+ h = Hash.new('default')
405
+ assert_instance_of(Hash, h)
406
+ assert_equal('default', h.default)
407
+ assert_equal('default', h['spurious'])
408
+
409
+ end
410
+
411
+ def test_inspect
412
+ assert_equal("{}", Hash[].inspect)
413
+ @h.delete(self)
414
+ s = @h.inspect
415
+ s = "{" + s[1..-2].split(/, /).sort.join(', ') + "}"
416
+ assert_equal("{\"nil\"=>nil, 1=>\"one\", 2=>\"two\", 3=>\"three\", nil=>\"nil\", true=>\"true\"}", s)
417
+ end
418
+
419
+ def test_invert
420
+ h = @h.invert
421
+ assert_equal(1, h['one'])
422
+ assert_equal(true, h['true'])
423
+ assert_equal(nil, h['nil'])
424
+
425
+ h.each do |k, v|
426
+ assert(@h.has_key?(v)) # not true in general, but works here
427
+ end
428
+
429
+ h = Hash[ 'a', 1, 'b', 2, 'c', 1].invert
430
+ assert_equal(2, h.length)
431
+ assert(h[1] == 'a' || h[1] == 'c')
432
+ assert_equal('b', h[2])
433
+ end
434
+
435
+ def test_key_eh
436
+ assert(!Hash[].key?(1))
437
+ assert(!Hash[].key?(nil))
438
+ assert(@h.key?(nil))
439
+ assert(@h.key?(1))
440
+ assert(!@h.key?('gumby'))
441
+ end
442
+
443
+ def test_keys
444
+ assert_equal([], Hash[].keys)
445
+
446
+ keys = @h.keys
447
+ expected = []
448
+ @h.each { |k, v| expected << k }
449
+ assert_equal([], keys - expected)
450
+ assert_equal([], expected - keys)
451
+ end
452
+
453
+ def test_length
454
+ assert_equal(0, Hash[].length)
455
+ assert_equal(7, @h.length)
456
+ end
457
+
458
+ def test_member_eh
459
+ assert(!Hash[].member?(1))
460
+ assert(!Hash[].member?(nil))
461
+ assert(@h.member?(nil))
462
+ assert(@h.member?(1))
463
+ assert(!@h.member?('gumby'))
464
+ end
465
+
466
+ def test_merge
467
+ h1 = Hash[ 1, 2, 2, 3, 3, 4 ]
468
+ h2 = Hash[ 2, 'two', 4, 'four' ]
469
+ hmerge = Hash[ 1, 2, 2, 'two', 3, 4, 4, 'four' ]
470
+ h1clone = h1.clone
471
+ assert_equal(hmerge, h1.merge(h2))
472
+ assert_equal(h1clone, h1)
473
+ assert_not_equal(hmerge, h1)
474
+
475
+ h1 = Hash[ 1, 2, 2, 3, 3, 4 ]
476
+ h2 = Hash[ 2, 'two', 4, 'four' ]
477
+ hmerge = Hash[ 1, 2, 2, 3, 3, 4, 4, 'four' ]
478
+ h2clone = h2.clone
479
+ assert_equal(hmerge, h2.merge(h1))
480
+ assert_equal(h2clone, h2)
481
+ assert_not_equal(hmerge, h2)
482
+ end
483
+
484
+ def test_merge_bang
485
+ h1 = Hash[ "a", 100, "b", 200 ]
486
+ h2 = Hash[ "b", 254, "c", 300 ]
487
+ expected = Hash["a", 100, "b", 254, "c", 300]
488
+ h1clone = h1.clone
489
+ assert_equal(expected, h1.merge!(h2))
490
+ assert_not_equal(h1clone, h1)
491
+ end
492
+
493
+ def test_rehash
494
+ a = [ "a", "b" ]
495
+ c = [ "c", "d" ]
496
+ h = Hash[ a, 100, c, 300 ]
497
+ assert_equal(100, h[a])
498
+ a[0] = "z"
499
+ # HACK assert_nil(h[a]) # this is current behavior, but not sure if spec
500
+ h.rehash
501
+ assert_equal(100, h[a])
502
+
503
+ # TODO: If +Hash#rehash+ is called while an iterator is traversing the hash, an +IndexError+ will be raised in the iterator.
504
+
505
+ end
506
+
507
+ def test_reject
508
+ base = Hash[ 1, 'one', 2, false, true, 'true', 'cat', 99 ]
509
+ h1 = Hash[ 1, 'one', 2, false, true, 'true' ]
510
+ h2 = Hash[ 2, false, 'cat', 99 ]
511
+ h3 = Hash[ 2, false ]
512
+
513
+ h = base.dup
514
+ assert_equal(h, h.reject { false })
515
+ assert_equal(Hash[], h.reject { true })
516
+
517
+ h = base.dup
518
+ assert_equal(h1, h.reject {|k,v| k.instance_of?(String) })
519
+
520
+ assert_equal(h2, h.reject {|k,v| v.instance_of?(String) })
521
+
522
+ assert_equal(h3, h.reject {|k,v| v })
523
+ assert_equal(base, h)
524
+ end
525
+
526
+ def test_reject_bang
527
+ base = Hash[ 1, 'one', 2, false, true, 'true', 'cat', 99 ]
528
+ h1 = Hash[ 1, 'one', 2, false, true, 'true' ]
529
+ h2 = Hash[ 2, false, 'cat', 99 ]
530
+ h3 = Hash[ 2, false ]
531
+
532
+ h = base.dup
533
+ assert_equal(nil, h.reject! { false })
534
+ assert_equal(Hash[], h.reject! { true })
535
+
536
+ h = base.dup
537
+ assert_equal(h1, h.reject! {|k,v| k.instance_of?(String) })
538
+ assert_equal(h1, h)
539
+
540
+ h = base.dup
541
+ assert_equal(h2, h.reject! {|k,v| v.instance_of?(String) })
542
+ assert_equal(h2, h)
543
+
544
+ h = base.dup
545
+ assert_equal(h3, h.reject! {|k,v| v })
546
+ assert_equal(h3, h)
547
+ end
548
+
549
+ def test_replace
550
+ h = Hash[ 1, 2, 3, 4 ]
551
+ h1 = h.replace(Hash[ 9, 8, 7, 6 ])
552
+ assert_equal(h, h1)
553
+ assert_equal(8, h[9])
554
+ assert_equal(6, h[7])
555
+ assert_nil(h[1])
556
+ assert_nil(h[2])
557
+ end
558
+
559
+ def test_select
560
+ h = Hash["a", 100, "b", 200, "c", 300]
561
+ r = h.select {|k,v| k > "a"}
562
+ assert_equal([["b", 200], ["c", 300]], r.sort_by {|o| o.first })
563
+ r = h.select {|k,v| v < 200}
564
+ assert_equal([["a", 100]], r)
565
+ end
566
+
567
+ def test_shift
568
+ h = @h.dup
569
+
570
+ @h.length.times {
571
+ k, v = h.shift
572
+ assert(@h.has_key?(k))
573
+ assert_equal(@h[k], v)
574
+ }
575
+
576
+ assert_equal(0, h.length)
577
+ end
578
+
579
+ def test_size
580
+ assert_equal(0, Hash[].length)
581
+ assert_equal(7, @h.length)
582
+ end
583
+
584
+ def test_sort
585
+ h = Hash.new
586
+ h = h.sort
587
+ assert_equal([], h)
588
+
589
+ h = Hash[ 1, 1, 2, 1 ].sort
590
+ assert_equal([[1,1], [2,1]], h)
591
+
592
+ h = Hash[ 'cat', 'feline', 'ass', 'asinine', 'bee', 'beeline' ]
593
+ h1 = h.sort
594
+ assert_equal([ %w(ass asinine), %w(bee beeline), %w(cat feline)], h1)
595
+ end
596
+
597
+ def test_store
598
+ t = Time.now
599
+ h = Hash.new
600
+ h.store(1, 'one')
601
+ h.store(2, 'two')
602
+ h.store(3, 'three')
603
+ h.store(self, 'self')
604
+ h.store(t, 'time')
605
+ h.store(nil, 'nil')
606
+ h.store('nil', nil)
607
+ assert_equal('one', h[1])
608
+ assert_equal('two', h[2])
609
+ assert_equal('three', h[3])
610
+ assert_equal('self', h[self])
611
+ assert_equal('time', h[t])
612
+ assert_equal('nil', h[nil])
613
+ assert_equal(nil, h['nil'])
614
+ assert_equal(nil, h['koala'])
615
+
616
+ h.store(1, 1)
617
+ h.store(nil, 99)
618
+ h.store('nil', nil)
619
+ assert_equal(1, h[1])
620
+ assert_equal('two', h[2])
621
+ assert_equal('three', h[3])
622
+ assert_equal('self', h[self])
623
+ assert_equal('time', h[t])
624
+ assert_equal(99, h[nil])
625
+ assert_equal(nil, h['nil'])
626
+ assert_equal(nil, h['koala'])
627
+ end
628
+
629
+ def test_to_a
630
+ assert_equal([], Hash[].to_a)
631
+ assert_equal([[1,2]], Hash[ 1, 2 ].to_a)
632
+ a = Hash[ 1, 2, 3, 4, 5, 6 ].to_a
633
+ assert_equal([1,2], a.delete([1,2]))
634
+ assert_equal([3,4], a.delete([3,4]))
635
+ assert_equal([5,6], a.delete([5,6]))
636
+ assert_equal(0, a.length)
637
+ end
638
+
639
+ def test_to_hash
640
+ h = @h.to_hash
641
+ assert_equal(@h, h)
642
+ end
643
+
644
+ def test_to_s
645
+ h = Hash[ 1, 2, "cat", "dog", 1.5, :fred ]
646
+ assert_equal(h.to_a.join, h.to_s)
647
+ $, = ":"
648
+ assert_equal(h.to_a.join, h.to_s)
649
+ h = Hash[]
650
+ assert_equal(h.to_a.join, h.to_s)
651
+ $, = nil
652
+ end
653
+
654
+ def test_update
655
+ util_update_tester(:update)
656
+ util_update_tester(:merge!)
657
+ end
658
+
659
+ def test_value_eh
660
+ assert(!Hash[].value?(1))
661
+ assert(!Hash[].value?(nil))
662
+ assert(@h.value?(nil))
663
+ assert(@h.value?('one'))
664
+ assert(!@h.value?('gumby'))
665
+ end
666
+
667
+ def test_values
668
+ assert_equal([], Hash[].values)
669
+
670
+ vals = @h.values
671
+ expected = []
672
+ @h.each { |k, v| expected << v }
673
+ assert_equal([], vals - expected)
674
+ assert_equal([], expected - vals)
675
+ end
676
+
677
+ def test_values_at
678
+ util_index_tester(:values_at)
679
+ end
680
+
681
+ end