2005-06-03 12:43:39 -04:00
|
|
|
require 'test/unit'
|
2007-09-28 12:51:41 -04:00
|
|
|
require 'tmpdir'
|
2001-05-06 11:06:00 -04:00
|
|
|
|
2005-06-03 12:43:39 -04:00
|
|
|
begin
|
|
|
|
require 'sdbm'
|
|
|
|
rescue LoadError
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
2005-06-03 12:43:39 -04:00
|
|
|
class TestSDBM < Test::Unit::TestCase
|
2001-05-06 11:06:00 -04:00
|
|
|
def setup
|
2007-10-24 02:20:31 -04:00
|
|
|
@tmpdir = Dir.mktmpdir("tmptest_sdbm")
|
2007-09-28 12:51:41 -04:00
|
|
|
@prefix = "tmptest_sdbm_#{$$}"
|
|
|
|
@path = "#{@tmpdir}/#{@prefix}_"
|
2001-05-06 11:06:00 -04:00
|
|
|
assert_instance_of(SDBM, @sdbm = SDBM.new(@path))
|
|
|
|
end
|
|
|
|
def teardown
|
|
|
|
assert_nil(@sdbm.close)
|
2005-06-20 04:17:53 -04:00
|
|
|
ObjectSpace.each_object(SDBM) do |obj|
|
|
|
|
obj.close unless obj.closed?
|
|
|
|
end
|
2007-10-24 02:20:31 -04:00
|
|
|
FileUtils.remove_entry_secure @tmpdir
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_size(expect, sdbm=@sdbm)
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(expect, sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
n = 0
|
|
|
|
sdbm.each { n+=1 }
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(expect, n)
|
2001-05-06 11:06:00 -04:00
|
|
|
if expect == 0
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(true, sdbm.empty?)
|
2001-05-06 11:06:00 -04:00
|
|
|
else
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(false, sdbm.empty?)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-05 22:20:58 -04:00
|
|
|
def have_fork?
|
|
|
|
begin
|
|
|
|
fork{}
|
|
|
|
true
|
|
|
|
rescue NotImplementedError
|
|
|
|
false
|
2005-07-03 22:24:54 -04:00
|
|
|
end
|
2005-07-05 22:20:58 -04:00
|
|
|
end
|
2005-07-03 22:24:54 -04:00
|
|
|
|
2001-05-06 11:06:00 -04:00
|
|
|
def test_version
|
2003-05-06 02:51:31 -04:00
|
|
|
assert(! SDBM.const_defined?(:VERSION))
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_new_has_no_block
|
|
|
|
# SDBM.new ignore the block
|
|
|
|
foo = true
|
2007-09-28 12:51:41 -04:00
|
|
|
assert_instance_of(SDBM, sdbm = SDBM.new("#{@tmpdir}/#{@prefix}") { foo = false })
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(foo, true)
|
2001-05-06 11:06:00 -04:00
|
|
|
assert_nil(sdbm.close)
|
|
|
|
end
|
|
|
|
def test_s_open_no_create
|
2007-09-28 12:51:41 -04:00
|
|
|
assert_nil(sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", nil))
|
2001-05-06 11:06:00 -04:00
|
|
|
ensure
|
|
|
|
sdbm.close if sdbm
|
|
|
|
end
|
|
|
|
def test_s_open_with_block
|
2007-09-28 12:51:41 -04:00
|
|
|
assert_equal(SDBM.open("#{@tmpdir}/#{@prefix}") { :foo }, :foo)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
=begin
|
|
|
|
# Is it guaranteed on many OS?
|
|
|
|
def test_s_open_lock_one_process
|
|
|
|
# locking on one process
|
2007-09-28 12:51:41 -04:00
|
|
|
assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", 0644))
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_raise(Errno::EWOULDBLOCK) {
|
2001-05-06 11:06:00 -04:00
|
|
|
begin
|
2007-09-28 12:51:41 -04:00
|
|
|
SDBM.open("#{@tmpdir}/#{@prefix}", 0644)
|
2001-05-06 11:06:00 -04:00
|
|
|
rescue Errno::EAGAIN
|
|
|
|
raise Errno::EWOULDBLOCK
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
=end
|
|
|
|
|
|
|
|
def test_s_open_nolock
|
|
|
|
# sdbm 1.8.0 specific
|
|
|
|
if not defined? SDBM::NOLOCK
|
|
|
|
return
|
|
|
|
end
|
2005-07-03 22:24:54 -04:00
|
|
|
return unless have_fork? # snip this test
|
2001-05-06 11:06:00 -04:00
|
|
|
|
2005-08-12 11:53:06 -04:00
|
|
|
pid = fork() {
|
2007-09-28 12:51:41 -04:00
|
|
|
assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", 0644,
|
2001-05-06 11:06:00 -04:00
|
|
|
SDBM::NOLOCK))
|
|
|
|
sleep 2
|
|
|
|
}
|
|
|
|
sleep 1
|
|
|
|
begin
|
|
|
|
sdbm2 = nil
|
|
|
|
assert_no_exception(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
|
2007-09-28 12:51:41 -04:00
|
|
|
assert_instance_of(SDBM, sdbm2 = SDBM.open("#{@tmpdir}/#{@prefix}", 0644))
|
2001-05-06 11:06:00 -04:00
|
|
|
}
|
|
|
|
ensure
|
2005-08-12 11:53:06 -04:00
|
|
|
Process.wait pid
|
2001-05-06 11:06:00 -04:00
|
|
|
sdbm2.close if sdbm2
|
|
|
|
end
|
|
|
|
|
2007-09-28 12:51:41 -04:00
|
|
|
p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG
|
2001-05-06 11:06:00 -04:00
|
|
|
|
2005-08-12 11:53:06 -04:00
|
|
|
pid = fork() {
|
2007-09-28 12:51:41 -04:00
|
|
|
assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", 0644))
|
2001-05-06 11:06:00 -04:00
|
|
|
sleep 2
|
|
|
|
}
|
|
|
|
begin
|
|
|
|
sleep 1
|
|
|
|
sdbm2 = nil
|
|
|
|
assert_no_exception(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
|
|
|
|
# this test is failed on Cygwin98 (???)
|
2007-09-28 12:51:41 -04:00
|
|
|
assert_instance_of(SDBM, sdbm2 = SDBM.open("#{@tmpdir}/#{@prefix}", 0644,
|
2001-05-06 11:06:00 -04:00
|
|
|
SDBM::NOLOCK))
|
|
|
|
}
|
|
|
|
ensure
|
2005-08-12 11:53:06 -04:00
|
|
|
Process.wait pid
|
2001-05-06 11:06:00 -04:00
|
|
|
sdbm2.close if sdbm2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_s_open_error
|
2007-09-28 12:51:41 -04:00
|
|
|
assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", 0))
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_raise(Errno::EACCES) {
|
2007-09-28 12:51:41 -04:00
|
|
|
SDBM.open("#{@tmpdir}/#{@prefix}", 0)
|
2001-05-06 11:06:00 -04:00
|
|
|
}
|
|
|
|
sdbm.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_close
|
2007-09-28 12:51:41 -04:00
|
|
|
assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}"))
|
2001-05-06 11:06:00 -04:00
|
|
|
assert_nil(sdbm.close)
|
|
|
|
|
|
|
|
# closed SDBM file
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_raise(SDBMError) { sdbm.close }
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_aref
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('bar', @sdbm['foo'] = 'bar')
|
|
|
|
assert_equal('bar', @sdbm['foo'])
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
assert_nil(@sdbm['bar'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_fetch
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('bar', @sdbm['foo']='bar')
|
|
|
|
assert_equal('bar', @sdbm.fetch('foo'))
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
# key not found
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_raise(IndexError) {
|
2001-05-06 11:06:00 -04:00
|
|
|
@sdbm.fetch('bar')
|
|
|
|
}
|
|
|
|
|
|
|
|
# test for `ifnone' arg
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('baz', @sdbm.fetch('bar', 'baz'))
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
# test for `ifnone' block
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('foobar', @sdbm.fetch('bar') {|key| 'foo' + key })
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_aset
|
|
|
|
num = 0
|
|
|
|
2.times {|i|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('foo', @sdbm['foo'] = 'foo')
|
|
|
|
assert_equal('foo', @sdbm['foo'])
|
|
|
|
assert_equal('bar', @sdbm['foo'] = 'bar')
|
|
|
|
assert_equal('bar', @sdbm['foo'])
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
num += 1 if i == 0
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(num, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
# assign nil
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('', @sdbm['bar'] = '')
|
|
|
|
assert_equal('', @sdbm['bar'])
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
num += 1 if i == 0
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(num, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
# empty string
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('', @sdbm[''] = '')
|
|
|
|
assert_equal('', @sdbm[''])
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
num += 1 if i == 0
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(num, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
# Fixnum
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('200', @sdbm['100'] = '200')
|
|
|
|
assert_equal('200', @sdbm['100'])
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
num += 1 if i == 0
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(num, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
# Big key and value
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('y' * 100, @sdbm['x' * 100] = 'y' * 100)
|
|
|
|
assert_equal('y' * 100, @sdbm['x' * 100])
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
num += 1 if i == 0
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(num, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-11-10 03:38:20 -05:00
|
|
|
def test_key
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('bar', @sdbm['foo'] = 'bar')
|
2008-11-10 03:38:20 -05:00
|
|
|
assert_equal('foo', @sdbm.key('bar'))
|
2001-05-06 11:06:00 -04:00
|
|
|
assert_nil(@sdbm['bar'])
|
|
|
|
end
|
|
|
|
|
2003-05-06 02:51:31 -04:00
|
|
|
def test_values_at
|
2002-02-26 23:52:21 -05:00
|
|
|
keys = %w(foo bar baz)
|
|
|
|
values = %w(FOO BAR BAZ)
|
|
|
|
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(values.reverse, @sdbm.values_at(*keys.reverse))
|
2002-02-26 23:52:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_select_with_block
|
|
|
|
keys = %w(foo bar baz)
|
|
|
|
values = %w(FOO BAR BAZ)
|
|
|
|
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
|
|
|
|
ret = @sdbm.select {|k,v|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(k.upcase, v)
|
2002-02-26 23:52:21 -05:00
|
|
|
k != "bar"
|
|
|
|
}
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal([['baz', 'BAZ'], ['foo', 'FOO']],
|
2002-02-26 23:52:21 -05:00
|
|
|
ret.sort)
|
|
|
|
end
|
|
|
|
|
2001-05-06 11:06:00 -04:00
|
|
|
def test_length
|
|
|
|
num = 10
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(0, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
num.times {|i|
|
|
|
|
i = i.to_s
|
|
|
|
@sdbm[i] = i
|
|
|
|
}
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(num, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
@sdbm.shift
|
|
|
|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(num - 1, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_empty?
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(true, @sdbm.empty?)
|
2001-05-06 11:06:00 -04:00
|
|
|
@sdbm['foo'] = 'FOO'
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(false, @sdbm.empty?)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_pair
|
|
|
|
n = 0
|
|
|
|
@sdbm.each_pair { n += 1 }
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(0, n)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
keys = %w(foo bar baz)
|
|
|
|
values = %w(FOO BAR BAZ)
|
|
|
|
|
|
|
|
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
|
|
|
|
|
|
|
|
n = 0
|
|
|
|
ret = @sdbm.each_pair {|key, val|
|
|
|
|
assert_not_nil(i = keys.index(key))
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(val, values[i])
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
n += 1
|
|
|
|
}
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(keys.size, n)
|
|
|
|
assert_equal(@sdbm, ret)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_value
|
|
|
|
n = 0
|
|
|
|
@sdbm.each_value { n += 1 }
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(0, n)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
keys = %w(foo bar baz)
|
|
|
|
values = %w(FOO BAR BAZ)
|
|
|
|
|
|
|
|
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
|
|
|
|
|
|
|
|
n = 0
|
|
|
|
ret = @sdbm.each_value {|val|
|
2008-11-10 03:38:20 -05:00
|
|
|
assert_not_nil(key = @sdbm.key(val))
|
2001-05-06 11:06:00 -04:00
|
|
|
assert_not_nil(i = keys.index(key))
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(val, values[i])
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
n += 1
|
|
|
|
}
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(keys.size, n)
|
|
|
|
assert_equal(@sdbm, ret)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_key
|
|
|
|
n = 0
|
|
|
|
@sdbm.each_key { n += 1 }
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(0, n)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
keys = %w(foo bar baz)
|
|
|
|
values = %w(FOO BAR BAZ)
|
|
|
|
|
|
|
|
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
|
|
|
|
|
|
|
|
n = 0
|
|
|
|
ret = @sdbm.each_key {|key|
|
|
|
|
assert_not_nil(i = keys.index(key))
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(@sdbm[key], values[i])
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
n += 1
|
|
|
|
}
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(keys.size, n)
|
|
|
|
assert_equal(@sdbm, ret)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_keys
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal([], @sdbm.keys)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
keys = %w(foo bar baz)
|
|
|
|
values = %w(FOO BAR BAZ)
|
|
|
|
|
|
|
|
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
|
|
|
|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(keys.sort, @sdbm.keys.sort)
|
|
|
|
assert_equal(values.sort, @sdbm.values.sort)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_values
|
|
|
|
test_keys
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_shift
|
|
|
|
assert_nil(@sdbm.shift)
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(0, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
keys = %w(foo bar baz)
|
|
|
|
values = %w(FOO BAR BAZ)
|
|
|
|
|
|
|
|
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
|
|
|
|
|
|
|
|
ret_keys = []
|
|
|
|
ret_values = []
|
|
|
|
while ret = @sdbm.shift
|
|
|
|
ret_keys.push ret[0]
|
|
|
|
ret_values.push ret[1]
|
|
|
|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(keys.size - ret_keys.size, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(keys.sort, ret_keys.sort)
|
|
|
|
assert_equal(values.sort, ret_values.sort)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete
|
|
|
|
keys = %w(foo bar baz)
|
|
|
|
values = %w(FOO BAR BAZ)
|
|
|
|
key = keys[1]
|
|
|
|
|
|
|
|
assert_nil(@sdbm.delete(key))
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(0, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
@sdbm[keys[0]], @sdbm[keys[1]], @sdbm[keys[2]] = values
|
|
|
|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('BAR', @sdbm.delete(key))
|
2001-05-06 11:06:00 -04:00
|
|
|
assert_nil(@sdbm[key])
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(2, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
assert_nil(@sdbm.delete(key))
|
|
|
|
end
|
|
|
|
def test_delete_with_block
|
|
|
|
key = 'no called block'
|
|
|
|
@sdbm[key] = 'foo'
|
2008-10-22 01:55:22 -04:00
|
|
|
assert_equal('foo', @sdbm.delete(key) {|k| k.replace 'called block'; :blockval})
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(0, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
key = 'no called block'
|
2008-10-22 01:55:22 -04:00
|
|
|
assert_equal(:blockval, @sdbm.delete(key) {|k| k.replace 'called block'; :blockval})
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(0, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_if
|
|
|
|
v = "0"
|
|
|
|
100.times {@sdbm[v] = v; v = v.next}
|
|
|
|
|
|
|
|
ret = @sdbm.delete_if {|key, val| key.to_i < 50}
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(@sdbm, ret)
|
2001-05-06 11:06:00 -04:00
|
|
|
check_size(50, @sdbm)
|
|
|
|
|
|
|
|
ret = @sdbm.delete_if {|key, val| key.to_i >= 50}
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(@sdbm, ret)
|
2001-05-06 11:06:00 -04:00
|
|
|
check_size(0, @sdbm)
|
|
|
|
|
|
|
|
# break
|
|
|
|
v = "0"
|
|
|
|
100.times {@sdbm[v] = v; v = v.next}
|
|
|
|
check_size(100, @sdbm)
|
|
|
|
n = 0;
|
|
|
|
@sdbm.delete_if {|key, val|
|
|
|
|
break if n > 50
|
|
|
|
n+=1
|
|
|
|
true
|
|
|
|
}
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(51, n)
|
2001-05-06 11:06:00 -04:00
|
|
|
check_size(49, @sdbm)
|
|
|
|
|
|
|
|
@sdbm.clear
|
|
|
|
|
|
|
|
# raise
|
|
|
|
v = "0"
|
|
|
|
100.times {@sdbm[v] = v; v = v.next}
|
|
|
|
check_size(100, @sdbm)
|
|
|
|
n = 0;
|
|
|
|
begin
|
|
|
|
@sdbm.delete_if {|key, val|
|
|
|
|
raise "runtime error" if n > 50
|
|
|
|
n+=1
|
|
|
|
true
|
|
|
|
}
|
|
|
|
rescue
|
|
|
|
end
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(51, n)
|
2001-05-06 11:06:00 -04:00
|
|
|
check_size(49, @sdbm)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reject
|
|
|
|
v = "0"
|
|
|
|
100.times {@sdbm[v] = v; v = v.next}
|
|
|
|
|
|
|
|
hash = @sdbm.reject {|key, val| key.to_i < 50}
|
|
|
|
assert_instance_of(Hash, hash)
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(100, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(50, hash.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
hash.each_pair {|key,val|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(false, key.to_i < 50)
|
|
|
|
assert_equal(key, val)
|
2001-05-06 11:06:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hash = @sdbm.reject {|key, val| key.to_i < 100}
|
|
|
|
assert_instance_of(Hash, hash)
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(true, hash.empty?)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_clear
|
|
|
|
v = "1"
|
|
|
|
100.times {v = v.next; @sdbm[v] = v}
|
|
|
|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(@sdbm, @sdbm.clear)
|
2001-05-06 11:06:00 -04:00
|
|
|
|
|
|
|
# validate SDBM#size
|
|
|
|
i = 0
|
|
|
|
@sdbm.each { i += 1 }
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(@sdbm.size, i)
|
|
|
|
assert_equal(0, i)
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invert
|
|
|
|
v = "0"
|
|
|
|
100.times {@sdbm[v] = v; v = v.next}
|
|
|
|
|
|
|
|
hash = @sdbm.invert
|
|
|
|
assert_instance_of(Hash, hash)
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(100, hash.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
hash.each_pair {|key, val|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(key.to_i, val.to_i)
|
2001-05-06 11:06:00 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_update
|
|
|
|
hash = {}
|
|
|
|
v = "0"
|
|
|
|
100.times {v = v.next; hash[v] = v}
|
|
|
|
|
|
|
|
@sdbm["101"] = "101"
|
|
|
|
@sdbm.update hash
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(101, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
@sdbm.each_pair {|key, val|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(key.to_i, val.to_i)
|
2001-05-06 11:06:00 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_replace
|
|
|
|
hash = {}
|
|
|
|
v = "0"
|
|
|
|
100.times {v = v.next; hash[v] = v}
|
|
|
|
|
|
|
|
@sdbm["101"] = "101"
|
|
|
|
@sdbm.replace hash
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(100, @sdbm.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
@sdbm.each_pair {|key, val|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(key.to_i, val.to_i)
|
2001-05-06 11:06:00 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_haskey?
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('bar', @sdbm['foo']='bar')
|
|
|
|
assert_equal(true, @sdbm.has_key?('foo'))
|
|
|
|
assert_equal(false, @sdbm.has_key?('bar'))
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_value?
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal('bar', @sdbm['foo']='bar')
|
|
|
|
assert_equal(true, @sdbm.has_value?('bar'))
|
|
|
|
assert_equal(false, @sdbm.has_value?('foo'))
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_a
|
|
|
|
v = "0"
|
|
|
|
100.times {v = v.next; @sdbm[v] = v}
|
|
|
|
|
|
|
|
ary = @sdbm.to_a
|
|
|
|
assert_instance_of(Array, ary)
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(100, ary.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
ary.each {|key,val|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(key.to_i, val.to_i)
|
2001-05-06 11:06:00 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_to_hash
|
|
|
|
v = "0"
|
|
|
|
100.times {v = v.next; @sdbm[v] = v}
|
|
|
|
|
|
|
|
hash = @sdbm.to_hash
|
|
|
|
assert_instance_of(Hash, hash)
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(100, hash.size)
|
2001-05-06 11:06:00 -04:00
|
|
|
hash.each {|key,val|
|
2005-06-03 12:43:39 -04:00
|
|
|
assert_equal(key.to_i, val.to_i)
|
2001-05-06 11:06:00 -04:00
|
|
|
}
|
|
|
|
end
|
2008-08-01 08:50:17 -04:00
|
|
|
|
|
|
|
def test_closed
|
|
|
|
assert_equal(false, @sdbm.closed?)
|
|
|
|
@sdbm.close
|
|
|
|
assert_equal(true, @sdbm.closed?)
|
|
|
|
@sdbm = SDBM.new(@path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_readonly
|
|
|
|
@sdbm["bar"] = "baz"
|
|
|
|
@sdbm.close
|
|
|
|
File.chmod(0444, @path + ".dir")
|
|
|
|
File.chmod(0444, @path + ".pag")
|
|
|
|
@sdbm = SDBM.new(@path)
|
|
|
|
assert_raise(SDBMError) { @sdbm["bar"] = "foo" }
|
|
|
|
assert_raise(SDBMError) { @sdbm.delete("bar") }
|
|
|
|
assert_raise(SDBMError) { @sdbm.delete_if { true } }
|
|
|
|
assert_raise(SDBMError) { @sdbm.clear }
|
|
|
|
assert_nil(@sdbm.store("bar", nil))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_update2
|
|
|
|
obj = Object.new
|
|
|
|
def obj.each_pair
|
|
|
|
yield []
|
|
|
|
end
|
|
|
|
assert_raise(ArgumentError) { @sdbm.update(obj) }
|
|
|
|
end
|
2001-05-06 11:06:00 -04:00
|
|
|
end
|
|
|
|
|