bdb1 0.2.4

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.
data/test/helper.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'fileutils'
12
+
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
15
+ require 'bdb1'
16
+
17
+ STDERR.puts "VERSION of BDB1 is #{BDB1::VERSION}"
18
+
19
+ class Test::Unit::TestCase
20
+ def setup
21
+ clean_tmpdir
22
+ end
23
+
24
+ def teardown
25
+ clean_tmpdir
26
+ end
27
+
28
+ def tmpdir(file)
29
+ File.join(File.dirname(__FILE__), 'tmp', file)
30
+ end
31
+
32
+ def clean_tmpdir
33
+ File.unlink(*Dir[tmpdir('*')])
34
+ end
35
+
36
+ def examplesdir(file)
37
+ File.join(File.dirname(__FILE__), '..', 'examples', file)
38
+ end
39
+ end
@@ -0,0 +1,360 @@
1
+ require 'helper'
2
+
3
+ class TestBtree < Test::Unit::TestCase
4
+ class BTCompare < BDB1::Btree
5
+ def bdb1_bt_compare(a, b)
6
+ a <=> b
7
+ end
8
+ end
9
+
10
+ class AZ < BDB1::Btree
11
+ def bdb1_store_key(a)
12
+ "xx_" + a
13
+ end
14
+ def bdb1_fetch_key(a)
15
+ a.sub(/^xx_/, '')
16
+ end
17
+ def bdb1_store_value(a)
18
+ "yy_" + a
19
+ end
20
+ def bdb1_fetch_value(a)
21
+ a.sub(/^yy_/, '')
22
+ end
23
+ end
24
+
25
+ def test_00_error
26
+ assert_raises(BDB1::Fatal, "invalid name") do
27
+ BDB1::Btree.new(tmpdir("."), "a")
28
+ end
29
+ assert_raises(BDB1::Fatal, "invalid Env") do
30
+ BDB1::Btree.open(tmpdir("aa"), "env" => 1)
31
+ end
32
+ assert_raises(TypeError) { BDB1::Btree.new(tmpdir("aa"), "set_cachesize" => "a") }
33
+ assert_raises(TypeError) { BDB1::Btree.new(tmpdir("aa"), "set_pagesize" => "a") }
34
+ assert_raises(TypeError) { BDB1::Btree.new(tmpdir("aa"), "set_bt_minkey" => "a") }
35
+ assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_bt_compare" => "a") }
36
+ assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_bt_prefix" => "a") }
37
+ assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_fetch_key" => "a") }
38
+ assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_store_key" => "a") }
39
+ assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_fetch_value" => "a") }
40
+ assert_raises(BDB1::Fatal) { BDB1::Btree.new(tmpdir("aa"), "set_store_value" => "a") }
41
+ assert_raises(TypeError) { BDB1::Btree.new(tmpdir("aa"), "set_lorder" => "a") }
42
+ end
43
+
44
+ def test_01_file
45
+ sub_file_init
46
+ sub_file_get_set
47
+ sub_file_delete
48
+ sub_file_cursor
49
+ sub_file_reopen
50
+ sub_file_dup
51
+ sub_file_close
52
+ end
53
+
54
+ def sub_file_init
55
+ assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree.new(tmpdir("aa"), "a"), "<open>")
56
+ end
57
+
58
+ def sub_file_get_set
59
+ assert_equal(true, @bdb.empty?, "<empty>")
60
+ assert_equal("alpha", @bdb["alpha"] = "alpha", "<set value>")
61
+ assert_equal(false, @bdb.empty?, "<empty>")
62
+ assert_equal("alpha", @bdb["alpha"], "<retrieve value>")
63
+ assert_equal("alpha", @bdb.fetch("alpha"), "<fetch value>")
64
+ assert_equal("beta", @bdb.fetch("xxx", "beta"), "<fetch nil>")
65
+ assert_equal("xxx", @bdb.fetch("xxx") {|x| x}, "<fetch nil>")
66
+ assert_raises(IndexError) { @bdb.fetch("xxx") }
67
+ assert_raises(ArgumentError) { @bdb.fetch("xxx", "beta") {} }
68
+ assert_equal(nil, @bdb["gamma"] = nil, "<set nil>")
69
+ assert_equal(nil, @bdb["gamma"], "<retrieve nil>")
70
+ assert(@bdb.key?("alpha") == "alpha", "<has key>")
71
+ assert_equal(false, @bdb.key?("unknown"), "<has unknown key>")
72
+ assert(@bdb.value?(nil), "<has nil>")
73
+ assert(@bdb.value?("alpha"), "<has value>")
74
+ assert_equal(false, @bdb.value?("unknown"), "<has unknown value>")
75
+ assert_equal(false, @bdb.put("alpha", "gamma", BDB1::NOOVERWRITE), "<nooverwrite>")
76
+ assert_equal("alpha", @bdb["alpha"], "<must not be changed>")
77
+ assert(@bdb.both?("alpha", "alpha"), "<has both>")
78
+ assert(! @bdb.both?("alpha", "beta"), "<don't has both>")
79
+ assert(! @bdb.both?("unknown", "alpha"), "<don't has both>")
80
+ assert_equal([1, 2, 3], @bdb["array"] = [1, 2, 3], "<array>")
81
+ assert_equal([1, 2, 3].to_s, @bdb["array"], "<retrieve array>")
82
+ assert_equal({"a" => "b"}, @bdb["hash"] = {"a" => "b"}, "<hash>")
83
+ assert_equal({"a" => "b"}.to_s, @bdb["hash"], "<retrieve hash>")
84
+ assert(@bdb.sync, "<sync>")
85
+ end
86
+
87
+ def sub_file_delete
88
+ size = @bdb.size
89
+ i = 0
90
+ @bdb.each do |key, value|
91
+ assert_equal(@bdb, @bdb.delete(key), "<delete value>")
92
+ i += 1
93
+ end
94
+ assert(size == i, "<delete count>")
95
+ assert_equal(0, @bdb.size, "<empty>")
96
+ $hash = {}
97
+ (33 .. 126).each do |i|
98
+ key = i.to_s * 5
99
+ @bdb[key] = i.to_s * 7
100
+ $hash[key] = i.to_s * 7
101
+ assert_equal(@bdb[key], $hash[key], "<set #{key}>")
102
+ end
103
+ assert_equal(@bdb.size, $hash.size, "<size after load>")
104
+ if @bdb.respond_to?(:select)
105
+ assert_raises(ArgumentError) { @bdb.select("xxx") {}}
106
+ assert_equal([], @bdb.select { false }, "<select none>")
107
+ assert_equal($hash.values.sort, @bdb.select { true }.sort, "<select all>")
108
+ end
109
+ arr0 = []
110
+ @bdb.each_key {|key| arr0 << key }
111
+ assert_equal($hash.keys.sort, arr0.sort, "<each key>")
112
+ arr1 = []
113
+ @bdb.reverse_each_key {|key| arr1 << key }
114
+ assert_equal($hash.keys.sort, arr1.sort, "<reverse each key>")
115
+ assert_equal(arr0, arr1.reverse, "<reverse>")
116
+ assert_equal($hash.invert, @bdb.invert, "<invert>")
117
+ @bdb.each do |key, value|
118
+ if rand < 0.5
119
+ assert_equal(@bdb, @bdb.delete(key), "<delete value>")
120
+ $hash.delete(key)
121
+ end
122
+ end
123
+ assert_equal(@bdb.size, $hash.size, "<size after load>")
124
+ @bdb.each do |key, value|
125
+ assert_equal($hash[key], value, "<after delete>")
126
+ end
127
+ @bdb.each do |key, value|
128
+ assert($hash.key?(key), "<key after delete>")
129
+ assert_equal(@bdb, @bdb.delete(key), "<delete value>")
130
+ end
131
+ end
132
+
133
+ def sub_file_cursor
134
+ array = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
135
+ array.each do |x|
136
+ assert_equal(x, @bdb[x] = x, "<set value>")
137
+ end
138
+ assert(array.size == @bdb.size, "<set count>")
139
+ arr = []
140
+ @bdb.each_value do |x|
141
+ arr << x
142
+ end
143
+ assert_equal(array, arr.sort, "<order>")
144
+ arr = []
145
+ @bdb.reverse_each_value do |x|
146
+ arr << x
147
+ end
148
+ assert_equal(array, arr.sort, "<reverse order>")
149
+ arr = @bdb.reject {|k, v| k == "e" || v == "i" }
150
+ has = array.reject {|k, v| k == "e" || k == "i" }
151
+ assert_equal(has, arr.keys.sort, "<reject>")
152
+ @bdb.reject! {|k, v| k == "e" || v == "i" }
153
+ array.reject! {|k, v| k == "e" || k == "i" }
154
+ assert_equal(array, @bdb.keys.sort, "<keys after reject>")
155
+ assert_equal(array, @bdb.values.sort, "<values after reject>")
156
+ end
157
+
158
+ def sub_file_reopen
159
+ assert_equal(nil, @bdb.close, "<close>")
160
+ assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree.open(tmpdir("aa"), "w",
161
+ "set_flags" => BDB1::DUP),
162
+ "<reopen with DB_DUP>")
163
+ assert_equal(0, @bdb.size, "<must be 0 after reopen>")
164
+ end
165
+
166
+ def sub_file_dup
167
+ assert_equal("a", @bdb["0"] = "a", "<set dup>")
168
+ assert_equal("b", @bdb["0"] = "b", "<set dup>")
169
+ assert_equal("c", @bdb["0"] = "c", "<set dup>")
170
+ assert_equal("d", @bdb["0"] = "d", "<set dup>")
171
+ assert_equal("aa", @bdb["1"] = "aa", "<set dup>")
172
+ assert_equal("bb", @bdb["1"] = "bb", "<set dup>")
173
+ assert_equal("cc", @bdb["1"] = "cc", "<set dup>")
174
+ assert_equal("aaa", @bdb["2"] = "aaa", "<set dup>")
175
+ assert_equal("bbb", @bdb["2"] = "bbb", "<set dup>")
176
+ assert_equal("aaaa", @bdb["3"] = "aaaa", "<set dup>")
177
+ rep = [["a", "b", "c", "d"], ['aa', 'bb', 'cc'], ['aaa', 'bbb'], ['aaaa']]
178
+ for i in [0, 1, 2, 3]
179
+ k0, v0 = [], []
180
+ @bdb.duplicates(i.to_s).each {|k, v| k0 << k; v0 << v}
181
+ assert_equal(k0, [i.to_s] * (4 - i), "<dup key #{i}>")
182
+ assert_equal(v0.sort, rep[i], "<dup val #{i}>")
183
+ k0, v0 = [], []
184
+ @bdb.each_dup(i.to_s) {|k, v| k0 << k; v0 << v}
185
+ assert_equal(k0, [i.to_s] * (4 - i), "<dup key #{i}>")
186
+ assert_equal(v0.sort, rep[i], "<dup val #{i}>")
187
+ v0 = []
188
+ @bdb.each_dup_value(i.to_s) {|v| v0 << v}
189
+ assert_equal(v0.sort, rep[i], "<dup val #{i}>")
190
+ end
191
+ end
192
+
193
+ def sub_file_close
194
+ assert_equal(nil, @bdb.close, "<close>")
195
+ end
196
+
197
+ def test_02_memory
198
+ sub_memory_open
199
+ sub_memory_get_set
200
+ sub_memory_close
201
+ end
202
+
203
+ def sub_memory_open
204
+ assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree.open, "<open in memory>")
205
+ assert_equal(0, @bdb.size, "<must be 0 after open>")
206
+ end
207
+
208
+ def sub_memory_get_set
209
+ (33 .. 126).each do |i|
210
+ key = i.to_s * 5
211
+ val = i.to_s * 7
212
+ assert_equal(val, @bdb[key] = val, "<set in memory>")
213
+ end
214
+ assert_equal(94, @bdb.size, "<length in memory>")
215
+ end
216
+
217
+ def sub_memory_close
218
+ assert_equal(nil, @bdb.close, "<close>")
219
+ end
220
+
221
+ def test_03_btree
222
+ sub_btree_delete_1
223
+ end
224
+
225
+ def intern_btree_delete
226
+ @hash = {}
227
+ File.foreach(examplesdir("wordtest")) do |line|
228
+ line.chomp!
229
+ @hash[line] = line.reverse
230
+ @bdb[line] = line.reverse
231
+ end
232
+ @bdb.each do |k, v|
233
+ assert_equal(@hash[k], v, "<value>")
234
+ end
235
+ @bdb.delete_if {|k, v| k[0] == ?a}
236
+ @hash.delete_if {|k, v| k[0] == ?a}
237
+ assert_equal(@bdb.size, @hash.size, "<size after delete_if>")
238
+ @bdb.each do |k, v|
239
+ assert_equal(@hash[k], v, "<value>")
240
+ end
241
+ end
242
+
243
+ def sub_btree_delete_1
244
+ assert_kind_of(BTCompare,
245
+ @bdb = BTCompare.open(tmpdir("aa"), "w",
246
+ "set_pagesize" => 1024,
247
+ "set_cachesize" => 32 * 1024),
248
+ "<open>")
249
+ intern_btree_delete
250
+ end
251
+
252
+ def test_04_btree
253
+ sub_btree_delete_2
254
+ sub_index
255
+ sub_convert
256
+ sub_sh
257
+ sub_sh_call
258
+ sub_create
259
+ end
260
+
261
+ def sub_btree_delete_2
262
+ assert_kind_of(BDB1::Btree,
263
+ @bdb = BDB1::Btree.open(tmpdir("aa"), "w",
264
+ "set_bt_compare" => proc {|a, b| a <=> b},
265
+ "set_pagesize" => 1024,
266
+ "set_cachesize" => 32 * 1024),
267
+ "<open>")
268
+ intern_btree_delete
269
+ end
270
+
271
+ def sub_index
272
+ lines = @hash.keys
273
+ array = []
274
+ 10.times do
275
+ h = lines[rand(lines.size - 1)]
276
+ array.push h
277
+ assert_equal(@hash.index(h.reverse), @bdb.index(h.reverse), "<index>")
278
+ end
279
+ assert_equal(@hash.values_at(array), @bdb.values_at(array), "<values_at>")
280
+ end
281
+
282
+ def sub_convert
283
+ h = @bdb.to_hash
284
+ h.each do |k, v|
285
+ assert_equal(v, @hash[k], "<to_hash>")
286
+ end
287
+ @hash.each do |k, v|
288
+ assert_equal(v, h[k], "<to_hash>")
289
+ end
290
+ h1 = @hash.to_a
291
+ h2 = @bdb.to_a
292
+ assert_equal(h1.size, h2.size, "<equal>")
293
+ end
294
+
295
+ def intern_sh
296
+ val = 'a' .. 'zz'
297
+ val.each do |l|
298
+ assert_equal(l, @bdb[l] = l, "<store>")
299
+ end
300
+ @bdb.each do |k, v|
301
+ assert_equal(k, v, "<fetch>")
302
+ end
303
+ assert_equal(nil, @bdb.close, "<close>")
304
+ assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree.open(tmpdir("aa")), "<sh>")
305
+ val.each do |l|
306
+ assert_equal("yy_#{l}", @bdb["xx_#{l}"], "<fetch value>")
307
+ end
308
+ @bdb.each do |k, v|
309
+ assert_equal("xx_", k[0, 3], "<fetch key>")
310
+ assert_equal("yy_", v[0, 3], "<fetch key>")
311
+ end
312
+ assert_equal(nil, @bdb.close, "<close>")
313
+ clean_tmpdir
314
+ end
315
+
316
+ def sub_sh
317
+ assert_equal(nil, @bdb.close, "<close>")
318
+ assert_kind_of(BDB1::Btree, @bdb = AZ.open(tmpdir("aa"), "w"), "<sh>")
319
+ intern_sh
320
+ end
321
+
322
+ def sub_sh_call
323
+ @bdb= BDB1::Btree.new(tmpdir("aa"), "w",
324
+ "set_store_key" => proc {|a| "xx_" + a },
325
+ "set_fetch_key" => proc {|a| a.sub(/^xx_/, '') },
326
+ "set_store_value" => proc {|a| "yy_" + a },
327
+ "set_fetch_value" => proc {|a| a.sub(/^yy_/, '') })
328
+ assert_kind_of(BDB1::Btree, @bdb)
329
+ intern_sh
330
+ end
331
+
332
+
333
+ def intern_create(opt = true)
334
+ if opt
335
+ assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree[@hash], "<create>")
336
+ else
337
+ aa = @hash.to_a.flatten
338
+ assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree[*aa], "<create>")
339
+ end
340
+ assert_equal(@bdb.size, @hash.size, "<size after load>")
341
+ if @bdb.respond_to?(:select)
342
+ assert_raises(ArgumentError) { @bdb.select("xxx") {}}
343
+ assert_equal([], @bdb.select { false }, "<select none>")
344
+ assert_equal(@hash.values.sort, @bdb.select { true }.sort, "<select all>")
345
+ end
346
+ arr0 = []
347
+ @bdb.each_key {|key| arr0 << key }
348
+ assert_equal(@hash.keys.sort, arr0.sort, "<each key>")
349
+ arr0 = []
350
+ @bdb.each_value {|value| arr0 << value }
351
+ assert_equal(@hash.values.sort, arr0.sort, "<each value>")
352
+ @bdb.close
353
+ clean_tmpdir
354
+ end
355
+
356
+ def sub_create
357
+ intern_create
358
+ #intern_create(false) # Change 1.9
359
+ end
360
+ end
data/test/test_hash.rb ADDED
@@ -0,0 +1,293 @@
1
+ require 'helper'
2
+
3
+ class TestHash < Test::Unit::TestCase
4
+ class HashRuby < BDB1::Hash
5
+ def bdb1_h_hash a
6
+ a.hash
7
+ end
8
+ end
9
+
10
+ class AZ < BDB1::Hash
11
+ def bdb1_store_key(a)
12
+ "xx_" + a
13
+ end
14
+ def bdb1_fetch_key(a)
15
+ a.sub(/^xx_/, '')
16
+ end
17
+ def bdb1_store_value(a)
18
+ "yy_" + a
19
+ end
20
+ def bdb1_fetch_value(a)
21
+ a.sub(/^yy_/, '')
22
+ end
23
+ end
24
+
25
+ def test_00_error
26
+ assert_raises(BDB1::Fatal, "invalid name") do
27
+ BDB1::Hash.new(tmpdir("."), "a")
28
+ end
29
+ assert_raises(BDB1::Fatal, "invalid Env") do
30
+ BDB1::Hash.open(tmpdir("aa"), "env" => 1)
31
+ end
32
+ assert_raises(TypeError) { BDB1::Hash.new(tmpdir("aa"), "set_h_ffactor" => "a") }
33
+ assert_raises(BDB1::Fatal) { BDB1::Hash.new(tmpdir("aa"), "set_h_nemem" => "a") }
34
+ assert_raises(BDB1::Fatal) { BDB1::Hash.new(tmpdir("aa"), "set_h_hash" => "a") }
35
+ assert_raises(TypeError) { BDB1::Hash.new(tmpdir("aa"), "set_cachesize" => "a") }
36
+ assert_raises(BDB1::Fatal) { BDB1::Hash.new(tmpdir("aa"), "set_fetch_key" => "a") }
37
+ assert_raises(BDB1::Fatal) { BDB1::Hash.new(tmpdir("aa"), "set_store_key" => "a") }
38
+ assert_raises(BDB1::Fatal) { BDB1::Hash.new(tmpdir("aa"), "set_fetch_value" => "a") }
39
+ assert_raises(BDB1::Fatal) { BDB1::Hash.new(tmpdir("aa"), "set_store_value" => "a") }
40
+ assert_raises(TypeError) { BDB1::Hash.new(tmpdir("aa"), "set_lorder" => "a") }
41
+ end
42
+
43
+ def test_01_file
44
+ sub_file_init
45
+ sub_file_get_set
46
+ sub_file_delete
47
+ sub_file_cursor
48
+ sub_file_reopen
49
+ sub_file_dup
50
+ sub_file_close
51
+ end
52
+
53
+ def sub_file_init
54
+ assert_kind_of(BDB1::Hash, @bdb = BDB1::Hash.new(tmpdir("aa"), "a"), "<open>")
55
+ end
56
+
57
+ def sub_file_get_set
58
+ assert_equal(true, @bdb.empty?, "<empty>")
59
+ assert_equal("alpha", @bdb["alpha"] = "alpha", "<set value>")
60
+ assert_equal(false, @bdb.empty?, "<empty>")
61
+ assert_equal("alpha", @bdb["alpha"], "<retrieve value>")
62
+ assert_equal("alpha", @bdb.fetch("alpha"), "<fetch value>")
63
+ assert_equal("beta", @bdb.fetch("xxx", "beta"), "<fetch nil>")
64
+ assert_equal("xxx", @bdb.fetch("xxx") {|x| x}, "<fetch nil>")
65
+ assert_raises(IndexError) { @bdb.fetch("xxx") }
66
+ assert_raises(ArgumentError) { @bdb.fetch("xxx", "beta") {} }
67
+ assert_equal(nil, @bdb["gamma"] = nil, "<set nil>")
68
+ assert_equal(nil, @bdb["gamma"], "<retrieve nil>")
69
+ assert(@bdb.key?("alpha") == "alpha", "<has key>")
70
+ assert_equal(false, @bdb.key?("unknown"), "<has unknown key>")
71
+ assert(@bdb.value?(nil), "<has nil>")
72
+ assert(@bdb.value?("alpha"), "<has value>")
73
+ assert_equal(false, @bdb.value?("unknown"), "<has unknown value>")
74
+ assert_equal(false, @bdb.put("alpha", "gamma", BDB1::NOOVERWRITE), "<nooverwrite>")
75
+ assert_equal("alpha", @bdb["alpha"], "<must not be changed>")
76
+ assert(@bdb.both?("alpha", "alpha"), "<has both>")
77
+ assert(! @bdb.both?("alpha", "beta"), "<don't has both>")
78
+ assert(! @bdb.both?("unknown", "alpha"), "<don't has both>")
79
+ assert_equal([1, 2, 3], @bdb["array"] = [1, 2, 3], "<array>")
80
+ assert_equal([1, 2, 3].to_s, @bdb["array"], "<retrieve array>")
81
+ assert_equal({"a" => "b"}, @bdb["hash"] = {"a" => "b"}, "<hash>")
82
+ assert_equal({"a" => "b"}.to_s, @bdb["hash"], "<retrieve hash>")
83
+ assert(@bdb.sync, "<sync>")
84
+ end
85
+
86
+ def sub_file_delete
87
+ size = @bdb.size
88
+ i, arr = 0, []
89
+ @bdb.each do |key, value|
90
+ arr << key
91
+ i += 1
92
+ end
93
+ arr.each do |key|
94
+ assert_equal(@bdb, @bdb.delete(key), "<delete value>")
95
+ end
96
+ assert(size == i, "<delete count>")
97
+ assert_equal(0, @bdb.size, "<empty>")
98
+ $hash = {}
99
+ (33 .. 126).each do |i|
100
+ key = i.to_s * 5
101
+ @bdb[key] = i.to_s * 7
102
+ $hash[key] = i.to_s * 7
103
+ assert_equal(@bdb[key], $hash[key], "<set #{key}>")
104
+ end
105
+ assert_equal(@bdb.size, $hash.size, "<size after load>")
106
+ if @bdb.respond_to?(:select)
107
+ assert_raises(ArgumentError) { @bdb.select("xxx") {}}
108
+ assert_equal([], @bdb.select { false }, "<select none>")
109
+ assert_equal($hash.values.sort, @bdb.select { true }.sort, "<select all>")
110
+ end
111
+ arr0 = []
112
+ @bdb.each_key {|key| arr0 << key }
113
+ assert_equal($hash.keys.sort, arr0.sort, "<each key>")
114
+ arr0.each do |key|
115
+ assert($hash.key?(key), "<key after delete>")
116
+ assert_equal(@bdb, @bdb.delete(key), "<delete value>")
117
+ end
118
+ end
119
+
120
+ def sub_file_cursor
121
+ array = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
122
+ array.each do |x|
123
+ assert_equal(x, @bdb[x] = x, "<set value>")
124
+ end
125
+ assert(array.size == @bdb.size, "<set count>")
126
+ arr = []
127
+ @bdb.each_value do |x|
128
+ arr << x
129
+ end
130
+ assert_equal(array, arr.sort, "<order>")
131
+ end
132
+
133
+ def sub_file_reopen
134
+ assert_equal(nil, @bdb.close, "<close>")
135
+ assert_kind_of(BDB1::Btree, @bdb = BDB1::Btree.open(tmpdir("aa"), "w",
136
+ "set_flags" => BDB1::DUP),
137
+ "<reopen with DB_DUP>")
138
+ assert_equal(0, @bdb.size, "<must be 0 after reopen>")
139
+ end
140
+
141
+ def sub_file_dup
142
+ assert_equal("a", @bdb["0"] = "a", "<set dup>")
143
+ assert_equal("b", @bdb["0"] = "b", "<set dup>")
144
+ assert_equal("c", @bdb["0"] = "c", "<set dup>")
145
+ assert_equal("d", @bdb["0"] = "d", "<set dup>")
146
+ assert_equal("aa", @bdb["1"] = "aa", "<set dup>")
147
+ assert_equal("bb", @bdb["1"] = "bb", "<set dup>")
148
+ assert_equal("cc", @bdb["1"] = "cc", "<set dup>")
149
+ assert_equal("aaa", @bdb["2"] = "aaa", "<set dup>")
150
+ assert_equal("bbb", @bdb["2"] = "bbb", "<set dup>")
151
+ assert_equal("aaaa", @bdb["3"] = "aaaa", "<set dup>")
152
+ rep = [["a", "b", "c", "d"], ['aa', 'bb', 'cc'], ['aaa', 'bbb'], ['aaaa']]
153
+ for i in [0, 1, 2, 3]
154
+ k0, v0 = [], []
155
+ @bdb.duplicates(i.to_s).each {|k, v| k0 << k; v0 << v}
156
+ assert_equal(k0, [i.to_s] * (4 - i), "<dup key #{i}>")
157
+ assert_equal(v0.sort, rep[i], "<dup val #{i}>")
158
+ k0, v0 = [], []
159
+ @bdb.each_dup(i.to_s) {|k, v| k0 << k; v0 << v}
160
+ assert_equal(k0, [i.to_s] * (4 - i), "<dup key #{i}>")
161
+ assert_equal(v0.sort, rep[i], "<dup val #{i}>")
162
+ v0 = []
163
+ @bdb.each_dup_value(i.to_s) {|v| v0 << v}
164
+ assert_equal(v0.sort, rep[i], "<dup val #{i}>")
165
+ end
166
+ end
167
+
168
+ def sub_file_close
169
+ assert_equal(nil, @bdb.close, "<close>")
170
+ end
171
+
172
+ def test_02_memory
173
+ sub_memory_open
174
+ sub_memory_get_set
175
+ sub_memory_close
176
+ end
177
+
178
+ def sub_memory_open
179
+ assert_kind_of(BDB1::Hash, @bdb = BDB1::Hash.open(nil), "<open in memory>")
180
+ assert_equal(0, @bdb.size, "<must be 0 after open>")
181
+ end
182
+
183
+ def sub_memory_get_set
184
+ (33 .. 126).each do |i|
185
+ key = i.to_s * 5
186
+ val = i.to_s * 7
187
+ assert_equal(val, @bdb[key] = val, "<set in memory>")
188
+ end
189
+ assert_equal(94, @bdb.size, "<length in memory>")
190
+ end
191
+
192
+ def sub_memory_close
193
+ assert_equal(nil, @bdb.close, "<close>")
194
+ end
195
+
196
+ def test_04_btree
197
+ sub_index
198
+ sub_convert
199
+ sub_has
200
+ sub_sh
201
+ sub_sh_call
202
+ end
203
+
204
+ def sub_index
205
+ assert_kind_of(HashRuby,
206
+ @bdb = HashRuby.open(tmpdir("aa"), "w",
207
+ "set_pagesize" => 1024,
208
+ "set_cachesize" => 32 * 1024),
209
+ "<open>")
210
+ @hash = {}
211
+ File.foreach(examplesdir("wordtest")) do |line|
212
+ line.chomp!
213
+ @hash[line] = line.reverse
214
+ @bdb[line] = line.reverse
215
+ end
216
+ @bdb.each do |k, v|
217
+ assert_equal(@hash[k], v, "<value>")
218
+ end
219
+ assert_equal(@bdb.size, @hash.size, "<size after delete_if>")
220
+ end
221
+
222
+ def sub_convert
223
+ h = @bdb.to_hash
224
+ h.each do |k, v|
225
+ assert_equal(v, @hash[k], "<to_hash>")
226
+ end
227
+ @hash.each do |k, v|
228
+ assert_equal(v, h[k], "<to_hash>")
229
+ end
230
+ h1 = @hash.to_a
231
+ h2 = @bdb.to_a
232
+ assert_equal(h1.size, h2.size, "<equal>")
233
+ end
234
+
235
+ def sub_has
236
+ assert_kind_of(HashRuby,
237
+ @bdb = HashRuby.open(tmpdir("aa"), "w",
238
+ "set_pagesize" => 1024,
239
+ "set_cachesize" => 32 * 1024),
240
+ "<open>")
241
+ @hash = {}
242
+ ('a' .. 'z').each do |k|
243
+ assert_equal((@hash[k] = k * 4), (@bdb[k] = k * 4), "<set>")
244
+ end
245
+ @hash.each_key do |k|
246
+ assert(@bdb.has_key?(k) == (k * 4), "<has key>")
247
+ end
248
+ @hash.each_value do |v|
249
+ assert(@bdb.has_value?(v), "<has key>")
250
+ end
251
+ @bdb.each do |k, v|
252
+ assert(@hash.has_key?(k), "<has key>")
253
+ assert(@hash.has_value?(v), "<has key>")
254
+ end
255
+ end
256
+
257
+ def intern_sh
258
+ val = 'a' .. 'zz'
259
+ val.each do |l|
260
+ assert_equal(l, @bdb[l] = l, "<store>")
261
+ end
262
+ @bdb.each do |k, v|
263
+ assert_equal(k, v, "<fetch>")
264
+ end
265
+ assert_equal(nil, @bdb.close, "<close>")
266
+ assert_kind_of(BDB1::Hash, @bdb = BDB1::Hash.open(tmpdir("aa")), "<sh>")
267
+ val.each do |l|
268
+ assert_equal("yy_#{l}", @bdb["xx_#{l}"], "<fetch value>")
269
+ end
270
+ @bdb.each do |k, v|
271
+ assert_equal("xx_", k[0, 3], "<fetch key>")
272
+ assert_equal("yy_", v[0, 3], "<fetch key>")
273
+ end
274
+ assert_equal(nil, @bdb.close, "<close>")
275
+ clean_tmpdir
276
+ end
277
+
278
+ def sub_sh
279
+ assert_equal(nil, @bdb.close, "<close>")
280
+ assert_kind_of(BDB1::Hash, @bdb = AZ.open(tmpdir("aa"), "w"), "<sh>")
281
+ intern_sh
282
+ end
283
+
284
+ def sub_sh_call
285
+ @bdb = BDB1::Hash.new(tmpdir("aa"), "w",
286
+ "set_store_key" => proc {|a| "xx_" + a },
287
+ "set_fetch_key" => proc {|a| a.sub(/^xx_/, '') },
288
+ "set_store_value" => proc {|a| "yy_" + a },
289
+ "set_fetch_value" => proc {|a| a.sub(/^yy_/, '') })
290
+ assert_kind_of(BDB1::Hash, @bdb)
291
+ intern_sh
292
+ end
293
+ end