gdbm 1.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +26 -8
- data/lib/gdbm.rb +9 -3
- metadata +24 -45
- data/test/test_gdbm-1.8.7.rb +0 -697
- data/test/test_gdbm-1.9.1.rb +0 -732
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d77e96cabac18ae26ee213c0cc34052096e1fd38
|
4
|
+
data.tar.gz: 790eb07656751d2b4ab5d3c785e3997d0296473c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 78271a2686aadce65e018a9d0656ed6bbc8de44d57ac3241c75257c2b88d9118557087186c5614b5f69cb4341af7bb9779365e1c10d3cd75fabf0e94414b8663
|
7
|
+
data.tar.gz: 1db72d78f9b8a2cca859dd0f98d2685b2718afb87c2112a9c2cbf80f7586234a86eea6fc6d4df0be3435df1fa64b2211f06aae1f654795c39a1c2c5f1cf4795e
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
## ffi-gdbm
|
2
2
|
|
3
|
-
An attempt to make gdbm available beyond the C Ruby implementation.
|
3
|
+
An attempt to make [gdbm](http://www.vivtek.com/gdbm/) available beyond the C Ruby implementation.
|
4
|
+
|
5
|
+
Faithfully mimics MRI's standard library and is compatible with gdbm files produced by that version.
|
4
6
|
|
5
7
|
## Installing
|
6
8
|
|
@@ -8,11 +10,15 @@ You can download and use `gdbm.rb` anyhow you would like.
|
|
8
10
|
|
9
11
|
You can also install it using Ruby Gems:
|
10
12
|
|
11
|
-
`gem install gdbm
|
13
|
+
`gem install gdbm`
|
12
14
|
|
13
15
|
or, if using JRuby:
|
14
16
|
|
15
|
-
`jgem install gdbm
|
17
|
+
`jgem install gdbm`
|
18
|
+
|
19
|
+
JRuby does not require further installation, but Rubinius will need the FFI gem:
|
20
|
+
|
21
|
+
`gem install ffi`
|
16
22
|
|
17
23
|
## Notes
|
18
24
|
|
@@ -22,11 +28,16 @@ or, if using JRuby:
|
|
22
28
|
|
23
29
|
## Status
|
24
30
|
|
25
|
-
|
31
|
+
Tests passing on 64 bit Linux with
|
32
|
+
|
33
|
+
* JRuby 1.7.17
|
34
|
+
* Rubinius 2.2.10 + FFI 1.9.6
|
26
35
|
|
27
|
-
|
36
|
+
### Older Tests
|
28
37
|
|
29
|
-
|
38
|
+
Passing all tests with JRuby 1.4, 1.5.3, 1.6 on 32-bit Linux.
|
39
|
+
|
40
|
+
Passing all tests with MRI Ruby 1.8.7, 1.9.1, 1.9.2 with Ruby-FFI 0.5.4, 0.6.3, 1.0.7 on 32-bit Linux.
|
30
41
|
|
31
42
|
Something weird happens with temp files (used in tests) with JRuby on Ubuntu. For some reason, it gets permission denied when trying to delete them. Any thoughts on that would be helpful.
|
32
43
|
|
@@ -36,6 +47,13 @@ Further testing on other systems is welcome!
|
|
36
47
|
|
37
48
|
Two sets of tests are included, copied straight from the MRI distribution. However, they do require the use of ObjectSpace, so this is how to run them with JRuby:
|
38
49
|
|
39
|
-
`jruby -X+O -r lib/gdbm test/test_gdbm-1.8.7.rb`
|
50
|
+
`jruby --1.8 -X+O -r lib/gdbm test/test_gdbm-1.8.7.rb`
|
51
|
+
|
52
|
+
`jruby -X+O -r lib/gdbm test/test_gdbm-1.9.1.rb`
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
Copyright (c), Justin Collins
|
40
57
|
|
41
|
-
|
58
|
+
This library is released under the same tri-license (GPL/LGPL/CPL) as JRuby.
|
59
|
+
Please see the COPYING file distributed with JRuby for details.
|
data/lib/gdbm.rb
CHANGED
@@ -149,11 +149,17 @@ module GDBM_FFI
|
|
149
149
|
|
150
150
|
#Iterates over each key in the _file_.
|
151
151
|
def self.each_key(file)
|
152
|
+
keys = []
|
152
153
|
current = self.gdbm_firstkey file
|
153
154
|
until current.value.nil?
|
154
|
-
|
155
|
+
if block_given?
|
156
|
+
yield current.value
|
157
|
+
else
|
158
|
+
keys << current.value
|
159
|
+
end
|
155
160
|
current = self.gdbm_nextkey file, current
|
156
161
|
end
|
162
|
+
block_given? ? nil : keys.each
|
157
163
|
end
|
158
164
|
|
159
165
|
#Iterates over each value in the _file_.
|
@@ -345,8 +351,8 @@ class GDBM
|
|
345
351
|
alias :reject! :delete_if
|
346
352
|
|
347
353
|
def each_key(&block)
|
348
|
-
GDBM_FFI.each_key
|
349
|
-
self
|
354
|
+
enumerator = GDBM_FFI.each_key(file, &block)
|
355
|
+
enumerator || self
|
350
356
|
end
|
351
357
|
|
352
358
|
def each_pair(&block)
|
metadata
CHANGED
@@ -1,69 +1,48 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: gdbm
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease: false
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 2
|
9
|
-
version: "1.2"
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.3.0
|
10
5
|
platform: ruby
|
11
|
-
authors:
|
6
|
+
authors:
|
12
7
|
- Justin Collins
|
13
8
|
autorequire:
|
14
9
|
bindir: bin
|
15
10
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2010-05-03 00:00:00 -07:00
|
18
|
-
default_executable:
|
11
|
+
date: 2014-12-14 00:00:00.000000000 Z
|
19
12
|
dependencies: []
|
20
|
-
|
21
|
-
|
13
|
+
description: This library provides a gdbm library compatible with the MRI standard
|
14
|
+
library, but using Ruby-FFI rather than a C extension. This allows gdbm to easily
|
15
|
+
be used from alternative Ruby implementations, such as JRuby. It can also be used
|
16
|
+
with MRI, if there is some kind of need for that.
|
22
17
|
email:
|
23
18
|
executables: []
|
24
|
-
|
25
19
|
extensions: []
|
26
|
-
|
27
20
|
extra_rdoc_files: []
|
28
|
-
|
29
|
-
files:
|
21
|
+
files:
|
30
22
|
- README.md
|
31
23
|
- lib/gdbm.rb
|
32
|
-
- test/test_gdbm-1.8.7.rb
|
33
|
-
- test/test_gdbm-1.9.1.rb
|
34
|
-
has_rdoc: true
|
35
24
|
homepage: http://github.com/presidentbeef/ffi-gdbm
|
36
25
|
licenses: []
|
37
|
-
|
26
|
+
metadata: {}
|
38
27
|
post_install_message:
|
39
28
|
rdoc_options: []
|
40
|
-
|
41
|
-
require_paths:
|
29
|
+
require_paths:
|
42
30
|
- lib
|
43
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
-
|
45
|
-
requirements:
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
46
33
|
- - ">="
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
version: "0"
|
52
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
-
none: false
|
54
|
-
requirements:
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
55
38
|
- - ">="
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
|
58
|
-
segments:
|
59
|
-
- 0
|
60
|
-
version: "0"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
61
41
|
requirements: []
|
62
|
-
|
63
42
|
rubyforge_project:
|
64
|
-
rubygems_version:
|
43
|
+
rubygems_version: 2.4.4
|
65
44
|
signing_key:
|
66
|
-
specification_version:
|
67
|
-
summary: Provides access to gdbm through Ruby-FFI, particularly for JRuby and other
|
45
|
+
specification_version: 4
|
46
|
+
summary: Provides access to gdbm through Ruby-FFI, particularly for JRuby and other
|
47
|
+
alternative Ruby implementations.
|
68
48
|
test_files: []
|
69
|
-
|
data/test/test_gdbm-1.8.7.rb
DELETED
@@ -1,697 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
|
3
|
-
begin
|
4
|
-
# require 'gdbm'
|
5
|
-
rescue LoadError
|
6
|
-
end
|
7
|
-
|
8
|
-
if defined? GDBM
|
9
|
-
|
10
|
-
$stderr.puts "WARNING: Loaded non-FFI gdbm library (probably from the standard lib)." unless defined? GDBM_FFI
|
11
|
-
|
12
|
-
require 'tmpdir'
|
13
|
-
require 'fileutils'
|
14
|
-
|
15
|
-
class TestGDBM < Test::Unit::TestCase
|
16
|
-
def TestGDBM.uname_s
|
17
|
-
require 'rbconfig'
|
18
|
-
case Config::CONFIG['target_os']
|
19
|
-
when 'cygwin'
|
20
|
-
require 'Win32API'
|
21
|
-
uname = Win32API.new('cygwin1', 'uname', 'P', 'I')
|
22
|
-
utsname = ' ' * 100
|
23
|
-
raise 'cannot get system name' if uname.call(utsname) == -1
|
24
|
-
|
25
|
-
utsname.unpack('A20' * 5)[0]
|
26
|
-
else
|
27
|
-
Config::CONFIG['target_os']
|
28
|
-
end
|
29
|
-
end
|
30
|
-
SYSTEM = uname_s
|
31
|
-
|
32
|
-
def setup
|
33
|
-
@path = "tmptest_gdbm_"
|
34
|
-
assert_instance_of(GDBM, @gdbm = GDBM.new(@path))
|
35
|
-
|
36
|
-
# prepare to make readonly GDBM file
|
37
|
-
GDBM.open("tmptest_gdbm_rdonly", 0400) {|gdbm|
|
38
|
-
gdbm['foo'] = 'FOO'
|
39
|
-
}
|
40
|
-
assert_instance_of(GDBM, @gdbm_rdonly = GDBM.new("tmptest_gdbm_rdonly", nil))
|
41
|
-
end
|
42
|
-
def teardown
|
43
|
-
assert_nil(@gdbm.close)
|
44
|
-
assert_nil(@gdbm_rdonly.close)
|
45
|
-
ObjectSpace.each_object(GDBM) do |obj|
|
46
|
-
obj.close unless obj.closed?
|
47
|
-
end
|
48
|
-
File.delete *Dir.glob("*tmptest_gdbm*").to_a
|
49
|
-
p Dir.glob("*tmptest_gdbm*") if $DEBUG
|
50
|
-
end
|
51
|
-
|
52
|
-
def check_size(expect, gdbm=@gdbm)
|
53
|
-
assert_equal(expect, gdbm.size)
|
54
|
-
n = 0
|
55
|
-
gdbm.each { n+=1 }
|
56
|
-
assert_equal(expect, n)
|
57
|
-
if expect == 0
|
58
|
-
assert_equal(true, gdbm.empty?)
|
59
|
-
else
|
60
|
-
assert_equal(false, gdbm.empty?)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def have_fork?
|
65
|
-
begin
|
66
|
-
fork{}
|
67
|
-
true
|
68
|
-
rescue NotImplementedError
|
69
|
-
false
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_s_new_has_no_block
|
74
|
-
# GDBM.new ignore the block
|
75
|
-
foo = true
|
76
|
-
assert_instance_of(GDBM, gdbm = GDBM.new("tmptest_gdbm") { foo = false })
|
77
|
-
assert_equal(foo, true)
|
78
|
-
assert_nil(gdbm.close)
|
79
|
-
end
|
80
|
-
def test_s_open_create_new
|
81
|
-
return if /^CYGWIN_9/ =~ SYSTEM
|
82
|
-
|
83
|
-
save_mask = File.umask(0)
|
84
|
-
begin
|
85
|
-
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm"))
|
86
|
-
gdbm.close
|
87
|
-
assert_equal(File.stat("tmptest_gdbm").mode & 0777, 0666)
|
88
|
-
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm2", 0644))
|
89
|
-
gdbm.close
|
90
|
-
assert_equal(File.stat("tmptest_gdbm2").mode & 0777, 0644)
|
91
|
-
ensure
|
92
|
-
File.umask save_mask
|
93
|
-
end
|
94
|
-
end
|
95
|
-
def test_s_open_no_create
|
96
|
-
assert_nil(gdbm = GDBM.open("tmptest_gdbm", nil),
|
97
|
-
"this test is failed on libgdbm 1.8.0")
|
98
|
-
ensure
|
99
|
-
gdbm.close if gdbm
|
100
|
-
end
|
101
|
-
def test_s_open_3rd_arg
|
102
|
-
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644,
|
103
|
-
GDBM::FAST))
|
104
|
-
gdbm.close
|
105
|
-
|
106
|
-
# gdbm 1.8.0 specific
|
107
|
-
if defined? GDBM::SYNC
|
108
|
-
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644,
|
109
|
-
GDBM::SYNC))
|
110
|
-
gdbm.close
|
111
|
-
end
|
112
|
-
# gdbm 1.8.0 specific
|
113
|
-
if defined? GDBM::NOLOCK
|
114
|
-
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644,
|
115
|
-
GDBM::NOLOCK))
|
116
|
-
gdbm.close
|
117
|
-
end
|
118
|
-
end
|
119
|
-
def test_s_open_with_block
|
120
|
-
assert_equal(GDBM.open("tmptest_gdbm") { :foo }, :foo)
|
121
|
-
end
|
122
|
-
def test_s_open_lock
|
123
|
-
return unless have_fork? # snip this test
|
124
|
-
pid = fork() {
|
125
|
-
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644))
|
126
|
-
sleep 2
|
127
|
-
}
|
128
|
-
begin
|
129
|
-
sleep 1
|
130
|
-
assert_raise(Errno::EWOULDBLOCK) {
|
131
|
-
begin
|
132
|
-
assert_instance_of(GDBM, gdbm2 = GDBM.open("tmptest_gdbm", 0644))
|
133
|
-
rescue Errno::EAGAIN, Errno::EACCES
|
134
|
-
raise Errno::EWOULDBLOCK
|
135
|
-
end
|
136
|
-
}
|
137
|
-
ensure
|
138
|
-
Process.wait pid
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
=begin
|
143
|
-
# Is it guaranteed on many OS?
|
144
|
-
def test_s_open_lock_one_process
|
145
|
-
# locking on one process
|
146
|
-
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644))
|
147
|
-
assert_raise(Errno::EWOULDBLOCK) {
|
148
|
-
begin
|
149
|
-
GDBM.open("tmptest_gdbm", 0644)
|
150
|
-
rescue Errno::EAGAIN
|
151
|
-
raise Errno::EWOULDBLOCK
|
152
|
-
end
|
153
|
-
}
|
154
|
-
end
|
155
|
-
=end
|
156
|
-
|
157
|
-
def test_s_open_nolock
|
158
|
-
# gdbm 1.8.0 specific
|
159
|
-
if not defined? GDBM::NOLOCK
|
160
|
-
return
|
161
|
-
end
|
162
|
-
return unless have_fork? # snip this test
|
163
|
-
|
164
|
-
pid = fork() {
|
165
|
-
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644,
|
166
|
-
GDBM::NOLOCK))
|
167
|
-
sleep 2
|
168
|
-
}
|
169
|
-
sleep 1
|
170
|
-
begin
|
171
|
-
gdbm2 = nil
|
172
|
-
assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
|
173
|
-
assert_instance_of(GDBM, gdbm2 = GDBM.open("tmptest_gdbm", 0644))
|
174
|
-
}
|
175
|
-
ensure
|
176
|
-
Process.wait pid
|
177
|
-
gdbm2.close if gdbm2
|
178
|
-
end
|
179
|
-
|
180
|
-
p Dir.glob("tmptest_gdbm*") if $DEBUG
|
181
|
-
|
182
|
-
pid = Process.fork() {
|
183
|
-
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644))
|
184
|
-
sleep 2
|
185
|
-
}
|
186
|
-
begin
|
187
|
-
sleep 1
|
188
|
-
gdbm2 = nil
|
189
|
-
assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
|
190
|
-
# this test is failed on Cygwin98 (???)
|
191
|
-
assert_instance_of(GDBM, gdbm2 = GDBM.open("tmptest_gdbm", 0644,
|
192
|
-
GDBM::NOLOCK))
|
193
|
-
}
|
194
|
-
ensure
|
195
|
-
Process.wait pid
|
196
|
-
gdbm2.close if gdbm2
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
def test_s_open_error
|
201
|
-
return if /(ms|bcc)win|mingw|djgpp/ =~ RUBY_PLATFORM
|
202
|
-
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0))
|
203
|
-
assert_raise(Errno::EACCES) {
|
204
|
-
GDBM.open("tmptest_gdbm", 0)
|
205
|
-
}
|
206
|
-
gdbm.close
|
207
|
-
end
|
208
|
-
|
209
|
-
def test_close
|
210
|
-
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm"))
|
211
|
-
assert_nil(gdbm.close)
|
212
|
-
|
213
|
-
# closed GDBM file
|
214
|
-
assert_raise(RuntimeError) { gdbm.close }
|
215
|
-
end
|
216
|
-
|
217
|
-
def test_aref
|
218
|
-
assert_equal('bar', @gdbm['foo'] = 'bar')
|
219
|
-
assert_equal('bar', @gdbm['foo'])
|
220
|
-
|
221
|
-
assert_nil(@gdbm['bar'])
|
222
|
-
end
|
223
|
-
|
224
|
-
def test_fetch
|
225
|
-
assert_equal('bar', @gdbm['foo']='bar')
|
226
|
-
assert_equal('bar', @gdbm.fetch('foo'))
|
227
|
-
|
228
|
-
# key not found
|
229
|
-
assert_raise(IndexError) {
|
230
|
-
@gdbm.fetch('bar')
|
231
|
-
}
|
232
|
-
|
233
|
-
# test for `ifnone' arg
|
234
|
-
assert_equal('baz', @gdbm.fetch('bar', 'baz'))
|
235
|
-
|
236
|
-
# test for `ifnone' block
|
237
|
-
assert_equal('foobar', @gdbm.fetch('bar') {|key| 'foo' + key })
|
238
|
-
end
|
239
|
-
|
240
|
-
def test_aset
|
241
|
-
num = 0
|
242
|
-
2.times {|i|
|
243
|
-
assert_equal('foo', @gdbm['foo'] = 'foo')
|
244
|
-
assert_equal('foo', @gdbm['foo'])
|
245
|
-
assert_equal('bar', @gdbm['foo'] = 'bar')
|
246
|
-
assert_equal('bar', @gdbm['foo'])
|
247
|
-
|
248
|
-
num += 1 if i == 0
|
249
|
-
assert_equal(num, @gdbm.size)
|
250
|
-
|
251
|
-
# assign nil
|
252
|
-
assert_equal('', @gdbm['bar'] = '')
|
253
|
-
assert_equal('', @gdbm['bar'])
|
254
|
-
|
255
|
-
num += 1 if i == 0
|
256
|
-
assert_equal(num, @gdbm.size)
|
257
|
-
|
258
|
-
# empty string
|
259
|
-
assert_equal('', @gdbm[''] = '')
|
260
|
-
assert_equal('', @gdbm[''])
|
261
|
-
|
262
|
-
num += 1 if i == 0
|
263
|
-
assert_equal(num, @gdbm.size)
|
264
|
-
|
265
|
-
# Fixnum
|
266
|
-
assert_equal('200', @gdbm['100'] = '200')
|
267
|
-
assert_equal('200', @gdbm['100'])
|
268
|
-
|
269
|
-
num += 1 if i == 0
|
270
|
-
assert_equal(num, @gdbm.size)
|
271
|
-
|
272
|
-
# Big key and value
|
273
|
-
assert_equal('y' * 100, @gdbm['x' * 100] = 'y' * 100)
|
274
|
-
assert_equal('y' * 100, @gdbm['x' * 100])
|
275
|
-
|
276
|
-
num += 1 if i == 0
|
277
|
-
assert_equal(num, @gdbm.size)
|
278
|
-
}
|
279
|
-
end
|
280
|
-
|
281
|
-
def test_index
|
282
|
-
assert_equal('bar', @gdbm['foo'] = 'bar')
|
283
|
-
assert_equal('foo', @gdbm.index('bar'))
|
284
|
-
assert_nil(@gdbm['bar'])
|
285
|
-
end
|
286
|
-
|
287
|
-
def test_values_at
|
288
|
-
keys = %w(foo bar baz)
|
289
|
-
values = %w(FOO BAR BAZ)
|
290
|
-
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
|
291
|
-
assert_equal(values.reverse, @gdbm.values_at(*keys.reverse))
|
292
|
-
end
|
293
|
-
|
294
|
-
def test_select_with_block
|
295
|
-
keys = %w(foo bar baz)
|
296
|
-
values = %w(FOO BAR BAZ)
|
297
|
-
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
|
298
|
-
ret = @gdbm.select {|k,v|
|
299
|
-
assert_equal(k.upcase, v)
|
300
|
-
k != "bar"
|
301
|
-
}
|
302
|
-
assert_equal([['baz', 'BAZ'], ['foo', 'FOO']],
|
303
|
-
ret.sort)
|
304
|
-
end
|
305
|
-
|
306
|
-
def test_length
|
307
|
-
num = 10
|
308
|
-
assert_equal(0, @gdbm.size)
|
309
|
-
num.times {|i|
|
310
|
-
i = i.to_s
|
311
|
-
@gdbm[i] = i
|
312
|
-
}
|
313
|
-
assert_equal(num, @gdbm.size)
|
314
|
-
|
315
|
-
@gdbm.shift
|
316
|
-
|
317
|
-
assert_equal(num - 1, @gdbm.size)
|
318
|
-
end
|
319
|
-
|
320
|
-
def test_empty?
|
321
|
-
assert_equal(true, @gdbm.empty?)
|
322
|
-
@gdbm['foo'] = 'FOO'
|
323
|
-
assert_equal(false, @gdbm.empty?)
|
324
|
-
end
|
325
|
-
|
326
|
-
def test_each_pair
|
327
|
-
n = 0
|
328
|
-
@gdbm.each_pair { n += 1 }
|
329
|
-
assert_equal(0, n)
|
330
|
-
|
331
|
-
keys = %w(foo bar baz)
|
332
|
-
values = %w(FOO BAR BAZ)
|
333
|
-
|
334
|
-
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
|
335
|
-
|
336
|
-
n = 0
|
337
|
-
ret = @gdbm.each_pair {|key, val|
|
338
|
-
assert_not_nil(i = keys.index(key))
|
339
|
-
assert_equal(val, values[i])
|
340
|
-
|
341
|
-
n += 1
|
342
|
-
}
|
343
|
-
assert_equal(keys.size, n)
|
344
|
-
assert_equal(@gdbm, ret)
|
345
|
-
end
|
346
|
-
|
347
|
-
def test_each_value
|
348
|
-
n = 0
|
349
|
-
@gdbm.each_value { n += 1 }
|
350
|
-
assert_equal(0, n)
|
351
|
-
|
352
|
-
keys = %w(foo bar baz)
|
353
|
-
values = %w(FOO BAR BAZ)
|
354
|
-
|
355
|
-
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
|
356
|
-
|
357
|
-
n = 0
|
358
|
-
ret = @gdbm.each_value {|val|
|
359
|
-
assert_not_nil(key = @gdbm.index(val))
|
360
|
-
assert_not_nil(i = keys.index(key))
|
361
|
-
assert_equal(val, values[i])
|
362
|
-
|
363
|
-
n += 1
|
364
|
-
}
|
365
|
-
assert_equal(keys.size, n)
|
366
|
-
assert_equal(@gdbm, ret)
|
367
|
-
end
|
368
|
-
|
369
|
-
def test_each_key
|
370
|
-
n = 0
|
371
|
-
@gdbm.each_key { n += 1 }
|
372
|
-
assert_equal(0, n)
|
373
|
-
|
374
|
-
keys = %w(foo bar baz)
|
375
|
-
values = %w(FOO BAR BAZ)
|
376
|
-
|
377
|
-
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
|
378
|
-
|
379
|
-
n = 0
|
380
|
-
ret = @gdbm.each_key {|key|
|
381
|
-
assert_not_nil(i = keys.index(key))
|
382
|
-
assert_equal(@gdbm[key], values[i])
|
383
|
-
|
384
|
-
n += 1
|
385
|
-
}
|
386
|
-
assert_equal(keys.size, n)
|
387
|
-
assert_equal(@gdbm, ret)
|
388
|
-
end
|
389
|
-
|
390
|
-
def test_keys
|
391
|
-
assert_equal([], @gdbm.keys)
|
392
|
-
|
393
|
-
keys = %w(foo bar baz)
|
394
|
-
values = %w(FOO BAR BAZ)
|
395
|
-
|
396
|
-
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
|
397
|
-
|
398
|
-
assert_equal(keys.sort, @gdbm.keys.sort)
|
399
|
-
assert_equal(values.sort, @gdbm.values.sort)
|
400
|
-
end
|
401
|
-
|
402
|
-
def test_values
|
403
|
-
test_keys
|
404
|
-
end
|
405
|
-
|
406
|
-
def test_shift
|
407
|
-
assert_nil(@gdbm.shift)
|
408
|
-
assert_equal(0, @gdbm.size)
|
409
|
-
|
410
|
-
keys = %w(foo bar baz)
|
411
|
-
values = %w(FOO BAR BAZ)
|
412
|
-
|
413
|
-
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
|
414
|
-
|
415
|
-
ret_keys = []
|
416
|
-
ret_values = []
|
417
|
-
while ret = @gdbm.shift
|
418
|
-
ret_keys.push ret[0]
|
419
|
-
ret_values.push ret[1]
|
420
|
-
|
421
|
-
assert_equal(keys.size - ret_keys.size, @gdbm.size)
|
422
|
-
end
|
423
|
-
|
424
|
-
assert_equal(keys.sort, ret_keys.sort)
|
425
|
-
assert_equal(values.sort, ret_values.sort)
|
426
|
-
end
|
427
|
-
|
428
|
-
def test_delete
|
429
|
-
keys = %w(foo bar baz)
|
430
|
-
values = %w(FOO BAR BAZ)
|
431
|
-
key = keys[1]
|
432
|
-
|
433
|
-
assert_nil(@gdbm.delete(key))
|
434
|
-
assert_equal(0, @gdbm.size)
|
435
|
-
|
436
|
-
@gdbm[keys[0]], @gdbm[keys[1]], @gdbm[keys[2]] = values
|
437
|
-
|
438
|
-
assert_equal('BAR', @gdbm.delete(key))
|
439
|
-
assert_nil(@gdbm[key])
|
440
|
-
assert_equal(2, @gdbm.size)
|
441
|
-
|
442
|
-
assert_nil(@gdbm.delete(key))
|
443
|
-
|
444
|
-
if /^CYGWIN_9/ !~ SYSTEM
|
445
|
-
assert_raise(GDBMError) {
|
446
|
-
@gdbm_rdonly.delete("foo")
|
447
|
-
}
|
448
|
-
|
449
|
-
assert_nil(@gdbm_rdonly.delete("bar"))
|
450
|
-
end
|
451
|
-
end
|
452
|
-
def test_delete_with_block
|
453
|
-
key = 'no called block'
|
454
|
-
@gdbm[key] = 'foo'
|
455
|
-
assert_equal('foo', @gdbm.delete(key) {|k| k.replace 'called block'})
|
456
|
-
assert_equal('no called block', key)
|
457
|
-
assert_equal(0, @gdbm.size)
|
458
|
-
|
459
|
-
key = 'no called block'
|
460
|
-
assert_equal(:blockval,
|
461
|
-
@gdbm.delete(key) {|k| k.replace 'called block'; :blockval})
|
462
|
-
assert_equal('called block', key)
|
463
|
-
assert_equal(0, @gdbm.size)
|
464
|
-
end
|
465
|
-
|
466
|
-
def test_delete_if
|
467
|
-
v = "0"
|
468
|
-
100.times {@gdbm[v] = v; v = v.next}
|
469
|
-
|
470
|
-
ret = @gdbm.delete_if {|key, val| key.to_i < 50}
|
471
|
-
assert_equal(@gdbm, ret)
|
472
|
-
check_size(50, @gdbm)
|
473
|
-
|
474
|
-
ret = @gdbm.delete_if {|key, val| key.to_i >= 50}
|
475
|
-
assert_equal(@gdbm, ret)
|
476
|
-
check_size(0, @gdbm)
|
477
|
-
|
478
|
-
# break
|
479
|
-
v = "0"
|
480
|
-
100.times {@gdbm[v] = v; v = v.next}
|
481
|
-
check_size(100, @gdbm)
|
482
|
-
n = 0;
|
483
|
-
@gdbm.delete_if {|key, val|
|
484
|
-
break if n > 50
|
485
|
-
n+=1
|
486
|
-
true
|
487
|
-
}
|
488
|
-
assert_equal(51, n)
|
489
|
-
check_size(49, @gdbm)
|
490
|
-
|
491
|
-
@gdbm.clear
|
492
|
-
|
493
|
-
# raise
|
494
|
-
v = "0"
|
495
|
-
100.times {@gdbm[v] = v; v = v.next}
|
496
|
-
check_size(100, @gdbm)
|
497
|
-
n = 0;
|
498
|
-
begin
|
499
|
-
@gdbm.delete_if {|key, val|
|
500
|
-
raise "runtime error" if n > 50
|
501
|
-
n+=1
|
502
|
-
true
|
503
|
-
}
|
504
|
-
rescue
|
505
|
-
end
|
506
|
-
assert_equal(51, n)
|
507
|
-
check_size(49, @gdbm)
|
508
|
-
end
|
509
|
-
|
510
|
-
def test_reject
|
511
|
-
v = "0"
|
512
|
-
100.times {@gdbm[v] = v; v = v.next}
|
513
|
-
|
514
|
-
hash = @gdbm.reject {|key, val| key.to_i < 50}
|
515
|
-
assert_instance_of(Hash, hash)
|
516
|
-
assert_equal(100, @gdbm.size)
|
517
|
-
|
518
|
-
assert_equal(50, hash.size)
|
519
|
-
hash.each_pair {|key,val|
|
520
|
-
assert_equal(false, key.to_i < 50)
|
521
|
-
assert_equal(key, val)
|
522
|
-
}
|
523
|
-
|
524
|
-
hash = @gdbm.reject {|key, val| key.to_i < 100}
|
525
|
-
assert_instance_of(Hash, hash)
|
526
|
-
assert_equal(true, hash.empty?)
|
527
|
-
end
|
528
|
-
|
529
|
-
def test_clear
|
530
|
-
v = "1"
|
531
|
-
100.times {v = v.next; @gdbm[v] = v}
|
532
|
-
|
533
|
-
assert_equal(@gdbm, @gdbm.clear)
|
534
|
-
|
535
|
-
# validate GDBM#size
|
536
|
-
i = 0
|
537
|
-
@gdbm.each { i += 1 }
|
538
|
-
assert_equal(@gdbm.size, i)
|
539
|
-
assert_equal(0, i)
|
540
|
-
end
|
541
|
-
|
542
|
-
def test_invert
|
543
|
-
v = "0"
|
544
|
-
100.times {@gdbm[v] = v; v = v.next}
|
545
|
-
|
546
|
-
hash = @gdbm.invert
|
547
|
-
assert_instance_of(Hash, hash)
|
548
|
-
assert_equal(100, hash.size)
|
549
|
-
hash.each_pair {|key, val|
|
550
|
-
assert_equal(key.to_i, val.to_i)
|
551
|
-
}
|
552
|
-
end
|
553
|
-
|
554
|
-
def test_update
|
555
|
-
hash = {}
|
556
|
-
v = "0"
|
557
|
-
100.times {v = v.next; hash[v] = v}
|
558
|
-
|
559
|
-
@gdbm["101"] = "101"
|
560
|
-
@gdbm.update hash
|
561
|
-
assert_equal(101, @gdbm.size)
|
562
|
-
@gdbm.each_pair {|key, val|
|
563
|
-
assert_equal(key.to_i, val.to_i)
|
564
|
-
}
|
565
|
-
end
|
566
|
-
|
567
|
-
def test_replace
|
568
|
-
hash = {}
|
569
|
-
v = "0"
|
570
|
-
100.times {v = v.next; hash[v] = v}
|
571
|
-
|
572
|
-
@gdbm["101"] = "101"
|
573
|
-
@gdbm.replace hash
|
574
|
-
assert_equal(100, @gdbm.size)
|
575
|
-
@gdbm.each_pair {|key, val|
|
576
|
-
assert_equal(key.to_i, val.to_i)
|
577
|
-
}
|
578
|
-
end
|
579
|
-
|
580
|
-
def test_reorganize
|
581
|
-
size1 = File.size(@path)
|
582
|
-
i = "1"
|
583
|
-
1000.times {i = i.next; @gdbm[i] = i}
|
584
|
-
@gdbm.clear
|
585
|
-
@gdbm.sync
|
586
|
-
|
587
|
-
size2 = File.size(@path)
|
588
|
-
@gdbm.reorganize
|
589
|
-
size3 = File.size(@path)
|
590
|
-
|
591
|
-
# p [size1, size2, size3]
|
592
|
-
assert_equal(true, size1 < size2)
|
593
|
-
# this test is failed on Cygwin98. `GDBM version 1.8.0, as of May 19, 1999'
|
594
|
-
assert_equal(true, size3 < size2)
|
595
|
-
assert_equal(size1, size3)
|
596
|
-
end
|
597
|
-
|
598
|
-
def test_sync
|
599
|
-
assert_instance_of(GDBM, gdbm = GDBM.open('tmptest_gdbm', 0666, GDBM::FAST))
|
600
|
-
assert_equal(gdbm.sync, gdbm)
|
601
|
-
gdbm.close
|
602
|
-
assert_instance_of(GDBM, gdbm = GDBM.open('tmptest_gdbm', 0666))
|
603
|
-
assert_equal(gdbm.sync, gdbm)
|
604
|
-
gdbm.close
|
605
|
-
end
|
606
|
-
|
607
|
-
def test_cachesize=
|
608
|
-
assert_equal(@gdbm.cachesize = 1024, 1024)
|
609
|
-
end
|
610
|
-
|
611
|
-
def test_fastmode=
|
612
|
-
assert_equal(@gdbm.fastmode = true, true)
|
613
|
-
end
|
614
|
-
|
615
|
-
def test_syncmode=
|
616
|
-
assert_equal(@gdbm.syncmode = true, true)
|
617
|
-
end
|
618
|
-
|
619
|
-
def test_haskey?
|
620
|
-
assert_equal('bar', @gdbm['foo']='bar')
|
621
|
-
assert_equal(true, @gdbm.has_key?('foo'))
|
622
|
-
assert_equal(false, @gdbm.has_key?('bar'))
|
623
|
-
end
|
624
|
-
|
625
|
-
def test_has_value?
|
626
|
-
assert_equal('bar', @gdbm['foo']='bar')
|
627
|
-
assert_equal(true, @gdbm.has_value?('bar'))
|
628
|
-
assert_equal(false, @gdbm.has_value?('foo'))
|
629
|
-
end
|
630
|
-
|
631
|
-
def test_to_a
|
632
|
-
v = "0"
|
633
|
-
100.times {v = v.next; @gdbm[v] = v}
|
634
|
-
|
635
|
-
ary = @gdbm.to_a
|
636
|
-
assert_instance_of(Array, ary)
|
637
|
-
assert_equal(100, ary.size)
|
638
|
-
ary.each {|key,val|
|
639
|
-
assert_equal(key.to_i, val.to_i)
|
640
|
-
}
|
641
|
-
end
|
642
|
-
|
643
|
-
def test_to_hash
|
644
|
-
v = "0"
|
645
|
-
100.times {v = v.next; @gdbm[v] = v}
|
646
|
-
|
647
|
-
hash = @gdbm.to_hash
|
648
|
-
assert_instance_of(Hash, hash)
|
649
|
-
assert_equal(100, hash.size)
|
650
|
-
hash.each {|key,val|
|
651
|
-
assert_equal(key.to_i, val.to_i)
|
652
|
-
}
|
653
|
-
end
|
654
|
-
end
|
655
|
-
|
656
|
-
class TestGDBM2 < Test::Unit::TestCase
|
657
|
-
TMPROOT = "#{Dir.tmpdir}/ruby-gdbm.#{$$}"
|
658
|
-
|
659
|
-
def setup
|
660
|
-
Dir.mkdir TMPROOT
|
661
|
-
end
|
662
|
-
|
663
|
-
def teardown
|
664
|
-
FileUtils.rm_rf TMPROOT if File.directory?(TMPROOT)
|
665
|
-
end
|
666
|
-
|
667
|
-
def test_reader_open
|
668
|
-
GDBM.open("#{TMPROOT}/a.dbm") {}
|
669
|
-
v = GDBM.open("#{TMPROOT}/a.dbm", nil, GDBM::READER) {|d|
|
670
|
-
assert_raises(GDBMError) { d["k"] = "v" }
|
671
|
-
true
|
672
|
-
}
|
673
|
-
assert(v)
|
674
|
-
end
|
675
|
-
|
676
|
-
def test_newdb_open
|
677
|
-
GDBM.open("#{TMPROOT}/a.dbm") {|dbm|
|
678
|
-
dbm["k"] = "v"
|
679
|
-
}
|
680
|
-
v = GDBM.open("#{TMPROOT}/a.dbm", nil, GDBM::NEWDB) {|d|
|
681
|
-
assert_equal(0, d.length)
|
682
|
-
assert_nil(d["k"])
|
683
|
-
true
|
684
|
-
}
|
685
|
-
assert(v)
|
686
|
-
end
|
687
|
-
|
688
|
-
def test_freeze
|
689
|
-
GDBM.open("#{TMPROOT}/a.dbm") {|d|
|
690
|
-
d.freeze
|
691
|
-
assert_raises(TypeError) { d["k"] = "v" }
|
692
|
-
}
|
693
|
-
end
|
694
|
-
end
|
695
|
-
else
|
696
|
-
$stderr.puts "WARNING: Did not load gdbm library."
|
697
|
-
end
|