require 'helper' class TestBtree < Test::Unit::TestCase class BTCompare < BDB1::Btree def bdb1_bt_compare(a, b) a <=> b end end class AZ < BDB1::Btree 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 def test_00_error assert_raises(BDB1::Fatal, "invalid name") do BDB1::Btree.new(tmpdir("."), "a") end assert_raises(BDB1::Fatal, "invalid Env") do BDB1::Btree.open(tmpdir("aa"), "env" => 1) end assert_raises(TypeError) { BDB1::Btree.new(tmpdir("aa"), "set_cachesize" => "a") } assert_raises(TypeError) { BDB1::Btree.new(tmpdir("aa"), "set_pagesize" => "a") } assert_raises(TypeError) { BDB1::Btree.new(tmpdir("aa"), "set_bt_minkey" => "a") } assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_bt_compare" => "a") } assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_bt_prefix" => "a") } assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_fetch_key" => "a") } assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_store_key" => "a") } assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_fetch_value" => "a") } assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_store_value" => "a") } assert_raises(TypeError) { BDB1::Btree.new(tmpdir("aa"), "set_lorder" => "a") } end def test_01_file sub_file_init sub_file_get_set sub_file_delete sub_file_cursor sub_file_reopen sub_file_dup sub_file_close end def sub_file_init assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree.new(tmpdir("aa"), "a"), "") end def sub_file_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 sub_file_delete size = @bdb.size i = 0 @bdb.each do |key, value| assert_equal(@bdb, @bdb.delete(key), "") i += 1 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, "") arr1 = [] @bdb.reverse_each_key {|key| arr1 << key } assert_equal($hash.keys.sort, arr1.sort, "") assert_equal(arr0, arr1.reverse, "") assert_equal($hash.invert, @bdb.invert, "") @bdb.each do |key, value| if rand < 0.5 assert_equal(@bdb, @bdb.delete(key), "") $hash.delete(key) end end assert_equal(@bdb.size, $hash.size, "") @bdb.each do |key, value| assert_equal($hash[key], value, "") end @bdb.each do |key, value| assert($hash.key?(key), "") assert_equal(@bdb, @bdb.delete(key), "") end end def sub_file_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, "") arr = [] @bdb.reverse_each_value do |x| arr << x end assert_equal(array, arr.sort, "") arr = @bdb.reject {|k, v| k == "e" || v == "i" } has = array.reject {|k, v| k == "e" || k == "i" } assert_equal(has, arr.keys.sort, "") @bdb.reject! {|k, v| k == "e" || v == "i" } array.reject! {|k, v| k == "e" || k == "i" } assert_equal(array, @bdb.keys.sort, "") assert_equal(array, @bdb.values.sort, "") end def sub_file_reopen assert_equal(nil, @bdb.close, "") assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree.open(tmpdir("aa"), "w", "set_flags" => BDB1::DUP), "") assert_equal(0, @bdb.size, "") end def sub_file_dup assert_equal("a", @bdb["0"] = "a", "") assert_equal("b", @bdb["0"] = "b", "") assert_equal("c", @bdb["0"] = "c", "") assert_equal("d", @bdb["0"] = "d", "") assert_equal("aa", @bdb["1"] = "aa", "") assert_equal("bb", @bdb["1"] = "bb", "") assert_equal("cc", @bdb["1"] = "cc", "") assert_equal("aaa", @bdb["2"] = "aaa", "") assert_equal("bbb", @bdb["2"] = "bbb", "") assert_equal("aaaa", @bdb["3"] = "aaaa", "") rep = [["a", "b", "c", "d"], ['aa', 'bb', 'cc'], ['aaa', 'bbb'], ['aaaa']] for i in [0, 1, 2, 3] k0, v0 = [], [] @bdb.duplicates(i.to_s).each {|k, v| k0 << k; v0 << v} assert_equal(k0, [i.to_s] * (4 - i), "") assert_equal(v0.sort, rep[i], "") k0, v0 = [], [] @bdb.each_dup(i.to_s) {|k, v| k0 << k; v0 << v} assert_equal(k0, [i.to_s] * (4 - i), "") assert_equal(v0.sort, rep[i], "") v0 = [] @bdb.each_dup_value(i.to_s) {|v| v0 << v} assert_equal(v0.sort, rep[i], "") end end def sub_file_close assert_equal(nil, @bdb.close, "") end def test_02_memory sub_memory_open sub_memory_get_set sub_memory_close end def sub_memory_open assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree.open, "") assert_equal(0, @bdb.size, "") end def sub_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, "") end def sub_memory_close assert_equal(nil, @bdb.close, "") end def test_03_btree sub_btree_delete_1 end def intern_btree_delete @hash = {} File.foreach(examplesdir("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 @bdb.delete_if {|k, v| k[0] == ?a} @hash.delete_if {|k, v| k[0] == ?a} assert_equal(@bdb.size, @hash.size, "") @bdb.each do |k, v| assert_equal(@hash[k], v, "") end end def sub_btree_delete_1 assert_kind_of(BTCompare, @bdb = BTCompare.open(tmpdir("aa"), "w", "set_pagesize" => 1024, "set_cachesize" => 32 * 1024), "") intern_btree_delete end def test_04_btree sub_btree_delete_2 sub_index sub_convert sub_sh sub_sh_call sub_create end def sub_btree_delete_2 assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree.open(tmpdir("aa"), "w", "set_bt_compare" => proc {|a, b| a <=> b}, "set_pagesize" => 1024, "set_cachesize" => 32 * 1024), "") intern_btree_delete end def sub_index lines = @hash.keys array = [] 10.times do h = lines[rand(lines.size - 1)] array.push h assert_equal(@hash.index(h.reverse), @bdb.index(h.reverse), "") end assert_equal(@hash.values_at(array), @bdb.values_at(array), "") end def sub_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 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::Btree, @bdb = BDB1::Btree.open(tmpdir("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_tmpdir end def sub_sh assert_equal(nil, @bdb.close, "") assert_kind_of(BDB1::Btree, @bdb = AZ.open(tmpdir("aa"), "w"), "") intern_sh end def sub_sh_call @bdb= BDB1::Btree.new(tmpdir("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::Btree, @bdb) intern_sh end def intern_create(opt = true) if opt assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree[@hash], "") else aa = @hash.to_a.flatten assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree[*aa], "") 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 = [] @bdb.each_value {|value| arr0 << value } assert_equal(@hash.values.sort, arr0.sort, "") @bdb.close clean_tmpdir end def sub_create intern_create #intern_create(false) # Change 1.9 end end