summaryrefslogtreecommitdiff
path: root/test/gdbm/test_gdbm.rb
blob: 6033a7640be5a89322a45fc50a5aefbdefa4cec5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
require 'test/unit'
require 'tmpdir'

begin
  require 'gdbm'
rescue LoadError
end

if defined? GDBM
  require 'tmpdir'
  require 'fileutils'

  class TestGDBM < Test::Unit::TestCase
    def TestGDBM.uname_s
      require 'rbconfig'
      case RbConfig::CONFIG['target_os']
      when 'cygwin'
        require 'Win32API'
        uname = Win32API.new('cygwin1', 'uname', 'P', 'I')
        utsname = ' ' * 100
        raise 'cannot get system name' if uname.call(utsname) == -1

        utsname.unpack('A20' * 5)[0]
      else
        RbConfig::CONFIG['target_os']
      end
    end
    SYSTEM = uname_s

    def setup
      @tmpdir = Dir.mktmpdir("tmptest_gdbm")
      @prefix = "tmptest_gdbm_#{$$}"
      @path = "#{@tmpdir}/#{@prefix}_"
      assert_instance_of(GDBM, @gdbm = GDBM.new(@path))

      # prepare to make readonly GDBM file
      GDBM.open("#{@tmpdir}/#{@prefix}_rdonly", 0400) {|gdbm|
        gdbm['foo'] = 'FOO'
      }
      assert_instance_of(GDBM, @gdbm_rdonly = GDBM.new("#{@tmpdir}/#{@prefix}_rdonly", nil))
    end
    def teardown
      assert_nil(@gdbm.close)
      assert_nil(@gdbm_rdonly.close)
      ObjectSpace.each_object(GDBM) do |obj|
        obj.close unless obj.closed?
      end
      FileUtils.remove_entry_secure @tmpdir
    end

    def check_size(expect, gdbm=@gdbm)
      assert_equal(expect, gdbm.size)
      n = 0
      gdbm.each { n+=1 }
      assert_equal(expect, n)
      if expect == 0
        assert_equal(true, gdbm.empty?)
      else
        assert_equal(false, gdbm.empty?)
      end
    end

    def have_fork?
      begin
        fork{}
        true
      rescue NotImplementedError
        false
      end
    end

    def test_s_new_has_no_block
      # GDBM.new ignore the block
      foo = true
      assert_instance_of(GDBM, gdbm = GDBM.new("#{@tmpdir}/#{@prefix}") { foo = false })
      assert_equal(foo, true)
      assert_nil(gdbm.close)
    end
    def test_s_open_create_new
      return if /^CYGWIN_9/ =~ SYSTEM

      save_mask = File.umask(0)
      begin
        assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}"))
        gdbm.close
        assert_equal(File.stat("#{@tmpdir}/#{@prefix}").mode & 0777, 0666)
        assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}2", 0644))
        gdbm.close
        assert_equal(File.stat("#{@tmpdir}/#{@prefix}2").mode & 0777, 0644)
      ensure
        File.umask save_mask
      end
    end
    def test_s_open_no_create
      assert_nil(gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", nil),
                 "this test is failed on libgdbm 1.8.0")
    ensure
      gdbm.close if gdbm
    end
    def test_s_open_3rd_arg
      assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
                                                GDBM::FAST))
      gdbm.close

      # gdbm 1.8.0 specific
      if defined? GDBM::SYNC
        assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
                                                  GDBM::SYNC))
        gdbm.close
      end
      # gdbm 1.8.0 specific
      if defined? GDBM::NOLOCK
        assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
                                                  GDBM::NOLOCK))
        gdbm.close
      end
    end
    def test_s_open_with_block
      assert_equal(GDBM.open("#{@tmpdir}/#{@prefix}") { :foo }, :foo)
    end
    def test_s_open_lock
      return unless have_fork?	# snip this test
      pid = fork() {
        assert_instance_of(GDBM, gdbm  = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
        sleep 2
      }
      begin
        sleep 1
        assert_raise(Errno::EWOULDBLOCK) {
          begin
            assert_instance_of(GDBM, gdbm2 = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
          rescue Errno::EAGAIN, Errno::EACCES
            raise Errno::EWOULDBLOCK
          end
        }
      ensure
        Process.wait pid
      end
    end

=begin
    # Is it guaranteed on many OS?
    def test_s_open_lock_one_process
      # locking on one process
      assert_instance_of(GDBM, gdbm  = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
      assert_raise(Errno::EWOULDBLOCK) {
        begin
          GDBM.open("#{@tmpdir}/#{@prefix}", 0644)
        rescue Errno::EAGAIN
          raise Errno::EWOULDBLOCK
        end
      }
    end
=end

    def test_s_open_nolock
      # gdbm 1.8.0 specific
      if not defined? GDBM::NOLOCK
        return
      end
      return unless have_fork?	# snip this test

      pid = fork() {
        assert_instance_of(GDBM, gdbm  = GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
                                                  GDBM::NOLOCK))
        sleep 2
      }
      sleep 1
      begin
        gdbm2 = nil
        assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
          assert_instance_of(GDBM, gdbm2 = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
        }
      ensure
        Process.wait pid
        gdbm2.close if gdbm2
      end

      p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG

      pid = fork() {
        assert_instance_of(GDBM, gdbm  = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
        sleep 2
      }
      begin
        sleep 1
        gdbm2 = nil
        assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
          # this test is failed on Cygwin98 (???)
          assert_instance_of(GDBM, gdbm2 = GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
                                                     GDBM::NOLOCK))
        }
      ensure
        Process.wait pid
        gdbm2.close if gdbm2
      end
    end

    def test_s_open_error
      assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0))
      assert_raise(Errno::EACCES) {
        GDBM.open("#{@tmpdir}/#{@prefix}", 0)
      }
      gdbm.close
    end

    def test_close
      assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}"))
      assert_nil(gdbm.close)

      # closed GDBM file
      assert_raise(RuntimeError) { gdbm.close }
    end

    def test_aref
      assert_equal('bar', @gdbm['foo'] = 'bar')
      assert_equal('bar', @gdbm['foo'])

      assert_nil(@gdbm['bar'])
    end

    def test_fetch
      assert_equal('bar', @gdbm['foo']='bar')
      assert_equal('bar', @gdbm.fetch('foo'))

      # key not found
      assert_raise(IndexError) {
        @gdbm.fetch('bar')
      }

      # test for `ifnone' arg
      assert_equal('baz', @gdbm.fetch('bar', 'baz'))

      # test for `ifnone' block
      assert_equal('foobar', @gdbm.fetch('bar') {|key| 'foo' + key })
    end

    def test_aset
      num = 0
      2.times {|i|
        assert_equal('foo', @gdbm['foo'] = 'foo')
        assert_equal('foo', @gdbm['foo'])
        assert_equal('bar', @gdbm['foo'] = 'bar')
        assert_equal('bar', @gdbm['foo'])

        num += 1 if i == 0
        assert_equal(num, @gdbm.size)

        # assign nil
        assert_equal('', @gdbm['bar'] = '')
        assert_equal('', @gdbm['bar'])

        num += 1 if i == 0
        assert_equal(num, @gdbm.size)

        # empty string
        assert_equal('', @gdbm[''] = '')
        assert_equal('', @gdbm[''])

        num += 1 if i == 0
        assert_equal(num, @gdbm.size)

        # Fixnum
        assert_equal('200', @gdbm['100'] = '200')
        assert_equal('200', @gdbm['100'])

        num += 1 if i == 0
        assert_equal(num, @gdbm.size)

        # Big key and value
        assert_equal('y' * 100, @gdbm['x' * 100] = 'y' * 100)
        assert_equal('y' * 100, @gdbm['x' * 100])

        num += 1 if i == 0
        assert_equal(num, @gdbm.size)
      }
    end

    def test_index
      assert_equal('bar', @gdbm['foo'] = 'bar')
      assert_equal('foo', @gdbm.index('bar'))
      assert_nil(@gdbm['bar'])
    end

    def test_values_at
      keys = %w(foo bar baz)
      values = %w(FOO BAR BAZ)
      @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
      assert_equal(values.reverse, @gdbm.values_at(*keys.reverse))
    end

    def test_select_with_block
      keys = %w(foo bar baz)
      values = %w(FOO BAR BAZ)
      @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
      ret = @gdbm.select {|k,v|
        assert_equal(k.upcase, v)
        k != "bar"
      }
      assert_equal([['baz', 'BAZ'], ['foo', 'FOO']],
                    ret.sort)
    end

    def test_length
      num = 10
      assert_equal(0, @gdbm.size)
      num.times {|i|
        i = i.to_s
        @gdbm[i] = i
      }
      assert_equal(num, @gdbm.size)

      @gdbm.shift

      assert_equal(num - 1, @gdbm.size)
    end

    def test_empty?
      assert_equal(true, @gdbm.empty?)
      @gdbm['foo'] = 'FOO'
      assert_equal(false, @gdbm.empty?)
    end

    def test_each_pair
      n = 0
      @gdbm.each_pair { n += 1 }
      assert_equal(0, n)

      keys = %w(foo bar baz)
      values = %w(FOO BAR BAZ)

      @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values

      n = 0
      ret = @gdbm.each_pair {|key, val|
        assert_not_nil(i = keys.index(key))
        assert_equal(val, values[i])

        n += 1
      }
      assert_equal(keys.size, n)
      assert_equal(@gdbm, ret)
    end

    def test_each_value
      n = 0
      @gdbm.each_value { n += 1 }
      assert_equal(0, n)

      keys = %w(foo bar baz)
      values = %w(FOO BAR BAZ)

      @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values

      n = 0
      ret = @gdbm.each_value {|val|
        assert_not_nil(key = @gdbm.index(val))
        assert_not_nil(i = keys.index(key))
        assert_equal(val, values[i])

        n += 1
      }
      assert_equal(keys.size, n)
      assert_equal(@gdbm, ret)
    end

    def test_each_key
      n = 0
      @gdbm.each_key { n += 1 }
      assert_equal(0, n)

      keys = %w(foo bar baz)
      values = %w(FOO BAR BAZ)

      @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values

      n = 0
      ret = @gdbm.each_key {|key|
        assert_not_nil(i = keys.index(key))
        assert_equal(@gdbm[key], values[i])

        n += 1
      }
      assert_equal(keys.size, n)
      assert_equal(@gdbm, ret)
    end

    def test_keys
      assert_equal([], @gdbm.keys)

      keys = %w(foo bar baz)
      values = %w(FOO BAR BAZ)

      @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values

      assert_equal(keys.sort, @gdbm.keys.sort)
      assert_equal(values.sort, @gdbm.values.sort)
    end

    def test_values
      test_keys
    end

    def test_shift
      assert_nil(@gdbm.shift)
      assert_equal(0, @gdbm.size)

      keys = %w(foo bar baz)
      values = %w(FOO BAR BAZ)

      @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values

      ret_keys = []
      ret_values = []
      while ret = @gdbm.shift
        ret_keys.push ret[0]
        ret_values.push ret[1]

        assert_equal(keys.size - ret_keys.size, @gdbm.size)
      end

      assert_equal(keys.sort, ret_keys.sort)
      assert_equal(values.sort, ret_values.sort)
    end

    def test_delete
      keys = %w(foo bar baz)
      values = %w(FOO BAR BAZ)
      key = keys[1]

      assert_nil(@gdbm.delete(key))
      assert_equal(0, @gdbm.size)

      @gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values

      assert_equal('BAR', @gdbm.delete(key))
      assert_nil(@gdbm[key])
      assert_equal(2, @gdbm.size)

      assert_nil(@gdbm.delete(key))

      if /^CYGWIN_9/ !~ SYSTEM
        assert_raise(GDBMError) {
          @gdbm_rdonly.delete("foo")
        }

        assert_nil(@gdbm_rdonly.delete("bar"))
      end
    end
    def test_delete_with_block
      key = 'no called block'
      @gdbm[key] = 'foo'
      assert_equal('foo', @gdbm.delete(key) {|k| k.replace 'called block'})
      assert_equal('no called block', key)
      assert_equal(0, @gdbm.size)

      key = 'no called block'
      assert_equal(:blockval,
                    @gdbm.delete(key) {|k| k.replace 'called block'; :blockval})
      assert_equal('called block', key)
      assert_equal(0, @gdbm.size)
    end

    def test_delete_if
      v = "0"
      100.times {@gdbm[v] = v; v = v.next}

      ret = @gdbm.delete_if {|key, val| key.to_i < 50}
      assert_equal(@gdbm, ret)
      check_size(50, @gdbm)

      ret = @gdbm.delete_if {|key, val| key.to_i >= 50}
      assert_equal(@gdbm, ret)
      check_size(0, @gdbm)

      # break
      v = "0"
      100.times {@gdbm[v] = v; v = v.next}
      check_size(100, @gdbm)
      n = 0;
      @gdbm.delete_if {|key, val|
        break if n > 50
        n+=1
        true
      }
      assert_equal(51, n)
      check_size(49, @gdbm)

      @gdbm.clear

      # raise
      v = "0"
      100.times {@gdbm[v] = v; v = v.next}
      check_size(100, @gdbm)
      n = 0;
      begin
        @gdbm.delete_if {|key, val|
          raise "runtime error" if n > 50
          n+=1
          true
        }
      rescue
      end
      assert_equal(51, n)
      check_size(49, @gdbm)
    end

    def test_reject
      v = "0"
      100.times {@gdbm[v] = v; v = v.next}

      hash = @gdbm.reject {|key, val| key.to_i < 50}
      assert_instance_of(Hash, hash)
      assert_equal(100, @gdbm.size)

      assert_equal(50, hash.size)
      hash.each_pair {|key,val|
        assert_equal(false, key.to_i < 50)
        assert_equal(key, val)
      }

      hash = @gdbm.reject {|key, val| key.to_i < 100}
      assert_instance_of(Hash, hash)
      assert_equal(true, hash.empty?)
    end

    def test_clear
      v = "1"
      100.times {v = v.next; @gdbm[v] = v}

      assert_equal(@gdbm, @gdbm.clear)

      # validate GDBM#size
      i = 0
      @gdbm.each { i += 1 }
      assert_equal(@gdbm.size, i)
      assert_equal(0, i)
    end

    def test_invert
      v = "0"
      100.times {@gdbm[v] = v; v = v.next}

      hash = @gdbm.invert
      assert_instance_of(Hash, hash)
      assert_equal(100, hash.size)
      hash.each_pair {|key, val|
        assert_equal(key.to_i, val.to_i)
      }
    end

    def test_update
      hash = {}
      v = "0"
      100.times {v = v.next; hash[v] = v}

      @gdbm["101"] = "101"
      @gdbm.update hash
      assert_equal(101, @gdbm.size)
      @gdbm.each_pair {|key, val|
        assert_equal(key.to_i, val.to_i)
      }
    end

    def test_replace
      hash = {}
      v = "0"
      100.times {v = v.next; hash[v] = v}

      @gdbm["101"] = "101"
      @gdbm.replace hash
      assert_equal(100, @gdbm.size)
      @gdbm.each_pair {|key, val|
        assert_equal(key.to_i, val.to_i)
      }
    end

    def test_reorganize
      size1 = File.size(@path)
      i = "1"
      1000.times {i = i.next; @gdbm[i] = i}
      @gdbm.clear
      @gdbm.sync

      size2 = File.size(@path)
      @gdbm.reorganize
      size3 = File.size(@path)

      # p [size1, size2, size3]
      assert_equal(true, size1 < size2)
      # this test is failed on Cygwin98. `GDBM version 1.8.0, as of May 19, 1999'
      assert_equal(true, size3 < size2)
      assert_equal(size1, size3)
    end

    def test_sync
      assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0666, GDBM::FAST))
      assert_equal(gdbm.sync, gdbm)
      gdbm.close
      assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0666))
      assert_equal(gdbm.sync, gdbm)
      gdbm.close
    end

    def test_cachesize=
        assert_equal(@gdbm.cachesize = 1024, 1024)
    end

    def test_fastmode=
        assert_equal(@gdbm.fastmode = true, true)
    end

    def test_syncmode=
        assert_equal(@gdbm.syncmode = true, true)
    end

    def test_haskey?
      assert_equal('bar', @gdbm['foo']='bar')
      assert_equal(true,  @gdbm.has_key?('foo'))
      assert_equal(false, @gdbm.has_key?('bar'))
    end

    def test_has_value?
      assert_equal('bar', @gdbm['foo']='bar')
      assert_equal(true,  @gdbm.has_value?('bar'))
      assert_equal(false, @gdbm.has_value?('foo'))
    end

    def test_to_a
      v = "0"
      100.times {v = v.next; @gdbm[v] = v}

      ary = @gdbm.to_a
      assert_instance_of(Array, ary)
      assert_equal(100, ary.size)
      ary.each {|key,val|
        assert_equal(key.to_i, val.to_i)
      }
    end

    def test_to_hash
      v = "0"
      100.times {v = v.next; @gdbm[v] = v}

      hash = @gdbm.to_hash
      assert_instance_of(Hash, hash)
      assert_equal(100, hash.size)
      hash.each {|key,val|
        assert_equal(key.to_i, val.to_i)
      }
    end
  end

  class TestGDBM2 < Test::Unit::TestCase
    def setup
      @tmproot = Dir.mktmpdir('ruby-gdbm')
    end

    def teardown
      FileUtils.remove_entry_secure @tmproot if File.directory?(@tmproot)
    end

    def test_reader_open_notexist
      assert_raise(Errno::ENOENT) {
        GDBM.open("#{@tmproot}/a", 0666, GDBM::READER)
      }
    end

    def test_writer_open_notexist
      assert_raise(Errno::ENOENT) {
        GDBM.open("#{@tmproot}/a", 0666, GDBM::WRITER)
      }
    end

    def test_wrcreat_open_notexist
      v = GDBM.open("#{@tmproot}/a", 0666, GDBM::WRCREAT)
      assert_instance_of(GDBM, v)
      v.close
    end

    def test_newdb_open_notexist
      v = GDBM.open("#{@tmproot}/a", 0666, GDBM::NEWDB)
      assert_instance_of(GDBM, v)
      v.close
    end

    def test_reader_open
      GDBM.open("#{@tmproot}/a.dbm") {} # create a db.
      v = GDBM.open("#{@tmproot}/a.dbm", nil, GDBM::READER) {|d|
        assert_raises(GDBMError) { d["k"] = "v" }
        true
      }
      assert(v)
    end

    def test_newdb_open
      GDBM.open("#{@tmproot}/a.dbm") {|dbm|
        dbm["k"] = "v"
      } 
      v = GDBM.open("#{@tmproot}/a.dbm", nil, GDBM::NEWDB) {|d|
        assert_equal(0, d.length)
        assert_nil(d["k"])
        true
      }
      assert(v)
    end

    def test_freeze
      GDBM.open("#{@tmproot}/a.dbm") {|d|
        d.freeze
        assert_raises(RuntimeError) { d["k"] = "v" }
      }
    end
  end
end