#!/usr/bin/ruby $LOAD_PATH.unshift *%w{../src src tests} require 'bdb1' require 'runit_' module BDB1 class HashRuby < Hash def bdb1_h_hash a a.hash end end class AZ < Hash def bdb1_store_key(a) "xx_" + a end def bdb1_fetch_key(a) a.sub(/^xx_/, '') end def bdb1_store_value(a) "yy_" + a end def bdb1_fetch_value(a) a.sub(/^yy_/, '') end end end def clean Dir.foreach('tmp') do |x| if FileTest.file?("tmp/#{x}") File.unlink("tmp/#{x}") end end end $bdb, $env = nil, nil clean print "\nVERSION of BDB1 is #{BDB1::VERSION}\n" Inh = defined?(RUNIT) ? RUNIT : Test::Unit class TestHash < Inh::TestCase def test_00_error assert_raises(BDB1::Fatal, "invalid name") do BDB1::Hash.new(".", "a") end assert_raises(BDB1::Fatal, "invalid Env") do BDB1::Hash.open("tmp/aa", "env" => 1) end assert_raises(TypeError) { BDB1::Hash.new("tmp/aa", "set_h_ffactor" => "a") } assert_raises(BDB1::Fatal) { BDB1::Hash.new("tmp/aa", "set_h_nemem" => "a") } assert_raises(BDB1::Fatal) { BDB1::Hash.new("tmp/aa", "set_h_hash" => "a") } assert_raises(TypeError) { BDB1::Hash.new("tmp/aa", "set_cachesize" => "a") } assert_raises(BDB1::Fatal) { BDB1::Hash.new("tmp/aa", "set_fetch_key" => "a") } assert_raises(BDB1::Fatal) { BDB1::Hash.new("tmp/aa", "set_store_key" => "a") } assert_raises(BDB1::Fatal) { BDB1::Hash.new("tmp/aa", "set_fetch_value" => "a") } assert_raises(BDB1::Fatal) { BDB1::Hash.new("tmp/aa", "set_store_value" => "a") } assert_raises(TypeError) { BDB1::Hash.new("tmp/aa", "set_lorder" => "a") } end def test_01_init assert_kind_of(BDB1::Hash, $bdb = BDB1::Hash.new("tmp/aa", "a"), "") end def test_02_get_set assert_equal(true, $bdb.empty?, "") assert_equal("alpha", $bdb["alpha"] = "alpha", "") assert_equal(false, $bdb.empty?, "") assert_equal("alpha", $bdb["alpha"], "") assert_equal("alpha", $bdb.fetch("alpha"), "") assert_equal("beta", $bdb.fetch("xxx", "beta"), "") assert_equal("xxx", $bdb.fetch("xxx") {|x| x}, "") assert_raises(IndexError) { $bdb.fetch("xxx") } assert_raises(ArgumentError) { $bdb.fetch("xxx", "beta") {} } assert_equal(nil, $bdb["gamma"] = nil, "") assert_equal(nil, $bdb["gamma"], "") assert($bdb.key?("alpha") == "alpha", "") assert_equal(false, $bdb.key?("unknown"), "") assert($bdb.value?(nil), "") assert($bdb.value?("alpha"), "") assert_equal(false, $bdb.value?("unknown"), "") assert_equal(false, $bdb.put("alpha", "gamma", BDB1::NOOVERWRITE), "") assert_equal("alpha", $bdb["alpha"], "") assert($bdb.both?("alpha", "alpha"), "") assert(! $bdb.both?("alpha", "beta"), "") assert(! $bdb.both?("unknown", "alpha"), "") assert_equal([1, 2, 3], $bdb["array"] = [1, 2, 3], "") assert_equal([1, 2, 3].to_s, $bdb["array"], "") assert_equal({"a" => "b"}, $bdb["hash"] = {"a" => "b"}, "") assert_equal({"a" => "b"}.to_s, $bdb["hash"], "") assert($bdb.sync, "") end def test_03_delete size = $bdb.size i, arr = 0, [] $bdb.each do |key, value| arr << key i += 1 end arr.each do |key| assert_equal($bdb, $bdb.delete(key), "") end assert(size == i, "") assert_equal(0, $bdb.size, "") $hash = {} (33 .. 126).each do |i| key = i.to_s * 5 $bdb[key] = i.to_s * 7 $hash[key] = i.to_s * 7 assert_equal($bdb[key], $hash[key], "") end assert_equal($bdb.size, $hash.size, "") if $bdb.respond_to?(:select) assert_raises(ArgumentError) { $bdb.select("xxx") {}} assert_equal([], $bdb.select { false }, "") end arr0 = [] $bdb.each_key {|key| arr0 << key } assert_equal($hash.keys.sort, arr0.sort, "") arr0.each do |key| assert($hash.key?(key), "") assert_equal($bdb, $bdb.delete(key), "") end end def test_04_cursor array = ["a", "b", "c", "d", "e", "f", "g", "h", "i"] array.each do |x| assert_equal(x, $bdb[x] = x, "") end assert(array.size == $bdb.size, "") arr = [] $bdb.each_value do |x| arr << x end assert_equal(array, arr.sort, "") end def test_05_in_memory assert_equal(nil, $bdb.close, "") assert_kind_of(BDB1::Hash, $bdb = BDB1::Hash.open(nil), "") assert_equal(0, $bdb.size, "") end def test_06_in_memory_get_set (33 .. 126).each do |i| key = i.to_s * 5 val = i.to_s * 7 assert_equal(val, $bdb[key] = val, "") end assert_equal(94, $bdb.size, "") assert_equal(nil, $bdb.close, "") end def test_07_index assert_kind_of(BDB1::HashRuby, $bdb = BDB1::HashRuby.open("tmp/aa", "w", "set_pagesize" => 1024, "set_cachesize" => 32 * 1024), "") $hash = {} File.foreach("examples/wordtest") do |line| line.chomp! $hash[line] = line.reverse $bdb[line] = line.reverse end $bdb.each do |k, v| assert_equal($hash[k], v, "") end assert_equal($bdb.size, $hash.size, "") end def test_08_convert h = $bdb.to_hash h.each do |k, v| assert_equal(v, $hash[k], "") end $hash.each do |k, v| assert_equal(v, h[k], "") end h1 = $hash.to_a h2 = $bdb.to_a assert_equal(h1.size, h2.size, "") end def test_09_has assert_kind_of(BDB1::HashRuby, $bdb = BDB1::HashRuby.open("tmp/aa", "w", "set_pagesize" => 1024, "set_cachesize" => 32 * 1024), "") $hash = {} ('a' .. 'z').each do |k| assert_equal(($hash[k] = k * 4), ($bdb[k] = k * 4), "") end $hash.each_key do |k| assert($bdb.has_key?(k) == (k * 4), "") end $hash.each_value do |v| assert($bdb.has_value?(v), "") end $bdb.each do |k, v| assert($hash.has_key?(k), "") assert($hash.has_value?(v), "") end end def intern_sh val = 'a' .. 'zz' val.each do |l| assert_equal(l, $bdb[l] = l, "") end $bdb.each do |k, v| assert_equal(k, v, "") end assert_equal(nil, $bdb.close, "") assert_kind_of(BDB1::Hash, $bdb = BDB1::Hash.open("tmp/aa"), "") val.each do |l| assert_equal("yy_#{l}", $bdb["xx_#{l}"], "") end $bdb.each do |k, v| assert_equal("xx_", k[0, 3], "") assert_equal("yy_", v[0, 3], "") end assert_equal(nil, $bdb.close, "") clean end def test_10_sh assert_equal(nil, $bdb.close, "") assert_kind_of(BDB1::Hash, $bdb = BDB1::AZ.open("tmp/aa", "w"), "") intern_sh end def test_11_sh_call $bdb= BDB1::Hash.new("tmp/aa", "w", "set_store_key" => proc {|a| "xx_" + a }, "set_fetch_key" => proc {|a| a.sub(/^xx_/, '') }, "set_store_value" => proc {|a| "yy_" + a }, "set_fetch_value" => proc {|a| a.sub(/^yy_/, '') }) assert_kind_of(BDB1::Hash, $bdb) intern_sh end end if defined?(RUNIT) RUNIT::CUI::TestRunner.run(TestHash.suite) end