Commit | Line | Data |
7c250e57 |
1 | #!./perl -w |
a0d0e21e |
2 | |
3 | BEGIN { |
610ab055 |
4 | @INC = '../lib' if -d '../lib' ; |
a0d0e21e |
5 | require Config; import Config; |
6 | if ($Config{'extensions'} !~ /\bDB_File\b/) { |
7 | print "1..0\n"; |
8 | exit 0; |
9 | } |
10 | } |
11 | |
12 | use DB_File; |
13 | use Fcntl; |
14 | |
610ab055 |
15 | print "1..51\n"; |
f6b705ef |
16 | |
17 | sub ok |
18 | { |
19 | my $no = shift ; |
20 | my $result = shift ; |
21 | |
22 | print "not " unless $result ; |
23 | print "ok $no\n" ; |
24 | } |
a0d0e21e |
25 | |
55d68b4a |
26 | $Dfile = "dbhash.tmp"; |
a0d0e21e |
27 | unlink $Dfile; |
28 | |
29 | umask(0); |
30 | |
31 | # Check the interface to HASHINFO |
32 | |
f6b705ef |
33 | my $dbh = new DB_File::HASHINFO ; |
34 | |
610ab055 |
35 | ok(1, $dbh->{bsize} == 0) ; |
36 | ok(2, $dbh->{ffactor} == 0) ; |
37 | ok(3, $dbh->{nelem} == 0) ; |
38 | ok(4, $dbh->{cachesize} == 0) ; |
f6b705ef |
39 | $^W = 0 ; |
f6b705ef |
40 | ok(5, $dbh->{hash} == undef) ; |
f6b705ef |
41 | $^W = 1 ; |
610ab055 |
42 | ok(6, $dbh->{lorder} == 0) ; |
a0d0e21e |
43 | |
44 | $dbh->{bsize} = 3000 ; |
f6b705ef |
45 | ok(7, $dbh->{bsize} == 3000 ); |
a0d0e21e |
46 | |
47 | $dbh->{ffactor} = 9000 ; |
f6b705ef |
48 | ok(8, $dbh->{ffactor} == 9000 ); |
49 | |
a0d0e21e |
50 | $dbh->{nelem} = 400 ; |
f6b705ef |
51 | ok(9, $dbh->{nelem} == 400 ); |
a0d0e21e |
52 | |
53 | $dbh->{cachesize} = 65 ; |
f6b705ef |
54 | ok(10, $dbh->{cachesize} == 65 ); |
a0d0e21e |
55 | |
56 | $dbh->{hash} = "abc" ; |
f6b705ef |
57 | ok(11, $dbh->{hash} eq "abc" ); |
a0d0e21e |
58 | |
59 | $dbh->{lorder} = 1234 ; |
f6b705ef |
60 | ok(12, $dbh->{lorder} == 1234 ); |
a0d0e21e |
61 | |
62 | # Check that an invalid entry is caught both for store & fetch |
63 | eval '$dbh->{fred} = 1234' ; |
f6b705ef |
64 | ok(13, $@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ ); |
610ab055 |
65 | eval 'my $q = $dbh->{fred}' ; |
f6b705ef |
66 | ok(14, $@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ ); |
a0d0e21e |
67 | |
610ab055 |
68 | |
a0d0e21e |
69 | # Now check the interface to HASH |
70 | |
f6b705ef |
71 | ok(15, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) ); |
a0d0e21e |
72 | |
73 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
74 | $blksize,$blocks) = stat($Dfile); |
b971f6e4 |
75 | ok(16, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) || $^O eq 'amigaos'); |
a0d0e21e |
76 | |
77 | while (($key,$value) = each(%h)) { |
78 | $i++; |
79 | } |
f6b705ef |
80 | ok(17, !$i ); |
a0d0e21e |
81 | |
82 | $h{'goner1'} = 'snork'; |
83 | |
84 | $h{'abc'} = 'ABC'; |
f6b705ef |
85 | ok(18, $h{'abc'} eq 'ABC' ); |
86 | ok(19, !defined $h{'jimmy'} ); |
87 | ok(20, !exists $h{'jimmy'} ); |
88 | ok(21, exists $h{'abc'} ); |
a0d0e21e |
89 | |
90 | $h{'def'} = 'DEF'; |
91 | $h{'jkl','mno'} = "JKL\034MNO"; |
92 | $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); |
93 | $h{'a'} = 'A'; |
94 | |
95 | #$h{'b'} = 'B'; |
96 | $X->STORE('b', 'B') ; |
97 | |
98 | $h{'c'} = 'C'; |
99 | |
100 | #$h{'d'} = 'D'; |
101 | $X->put('d', 'D') ; |
102 | |
103 | $h{'e'} = 'E'; |
104 | $h{'f'} = 'F'; |
105 | $h{'g'} = 'X'; |
106 | $h{'h'} = 'H'; |
107 | $h{'i'} = 'I'; |
108 | |
109 | $h{'goner2'} = 'snork'; |
110 | delete $h{'goner2'}; |
111 | |
112 | |
113 | # IMPORTANT - $X must be undefined before the untie otherwise the |
114 | # underlying DB close routine will not get called. |
115 | undef $X ; |
116 | untie(%h); |
117 | |
118 | |
119 | # tie to the same file again, do not supply a type - should default to HASH |
f6b705ef |
120 | ok(22, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640) ); |
a0d0e21e |
121 | |
122 | # Modify an entry from the previous tie |
123 | $h{'g'} = 'G'; |
124 | |
125 | $h{'j'} = 'J'; |
126 | $h{'k'} = 'K'; |
127 | $h{'l'} = 'L'; |
128 | $h{'m'} = 'M'; |
129 | $h{'n'} = 'N'; |
130 | $h{'o'} = 'O'; |
131 | $h{'p'} = 'P'; |
132 | $h{'q'} = 'Q'; |
133 | $h{'r'} = 'R'; |
134 | $h{'s'} = 'S'; |
135 | $h{'t'} = 'T'; |
136 | $h{'u'} = 'U'; |
137 | $h{'v'} = 'V'; |
138 | $h{'w'} = 'W'; |
139 | $h{'x'} = 'X'; |
140 | $h{'y'} = 'Y'; |
141 | $h{'z'} = 'Z'; |
142 | |
143 | $h{'goner3'} = 'snork'; |
144 | |
145 | delete $h{'goner1'}; |
146 | $X->DELETE('goner3'); |
147 | |
148 | @keys = keys(%h); |
149 | @values = values(%h); |
150 | |
f6b705ef |
151 | ok(23, $#keys == 29 && $#values == 29) ; |
a0d0e21e |
152 | |
f6b705ef |
153 | $i = 0 ; |
55d68b4a |
154 | while (($key,$value) = each(%h)) { |
2f52a358 |
155 | if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { |
a0d0e21e |
156 | $key =~ y/a-z/A-Z/; |
157 | $i++ if $key eq $value; |
158 | } |
159 | } |
160 | |
f6b705ef |
161 | ok(24, $i == 30) ; |
a0d0e21e |
162 | |
55d68b4a |
163 | @keys = ('blurfl', keys(%h), 'dyick'); |
f6b705ef |
164 | ok(25, $#keys == 31) ; |
a0d0e21e |
165 | |
166 | $h{'foo'} = ''; |
f6b705ef |
167 | ok(26, $h{'foo'} eq '' ); |
a0d0e21e |
168 | |
169 | $h{''} = 'bar'; |
f6b705ef |
170 | ok(27, $h{''} eq 'bar' ); |
a0d0e21e |
171 | |
172 | # check cache overflow and numeric keys and contents |
173 | $ok = 1; |
174 | for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } |
175 | for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } |
f6b705ef |
176 | ok(28, $ok ); |
a0d0e21e |
177 | |
178 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
179 | $blksize,$blocks) = stat($Dfile); |
f6b705ef |
180 | ok(29, $size > 0 ); |
a0d0e21e |
181 | |
182 | @h{0..200} = 200..400; |
183 | @foo = @h{0..200}; |
f6b705ef |
184 | ok(30, join(':',200..400) eq join(':',@foo) ); |
a0d0e21e |
185 | |
186 | |
187 | # Now check all the non-tie specific stuff |
188 | |
189 | # Check NOOVERWRITE will make put fail when attempting to overwrite |
190 | # an existing record. |
191 | |
192 | $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ; |
f6b705ef |
193 | ok(31, $status == 1 ); |
a0d0e21e |
194 | |
195 | # check that the value of the key 'x' has not been changed by the |
196 | # previous test |
f6b705ef |
197 | ok(32, $h{'x'} eq 'X' ); |
a0d0e21e |
198 | |
199 | # standard put |
200 | $status = $X->put('key', 'value') ; |
f6b705ef |
201 | ok(33, $status == 0 ); |
a0d0e21e |
202 | |
203 | #check that previous put can be retrieved |
f6b705ef |
204 | $value = 0 ; |
a0d0e21e |
205 | $status = $X->get('key', $value) ; |
f6b705ef |
206 | ok(34, $status == 0 ); |
207 | ok(35, $value eq 'value' ); |
a0d0e21e |
208 | |
209 | # Attempting to delete an existing key should work |
210 | |
211 | $status = $X->del('q') ; |
f6b705ef |
212 | ok(36, $status == 0 ); |
a0d0e21e |
213 | |
214 | # Make sure that the key deleted, cannot be retrieved |
f6b705ef |
215 | $^W = 0 ; |
216 | ok(37, $h{'q'} eq undef ); |
217 | $^W = 1 ; |
a0d0e21e |
218 | |
219 | # Attempting to delete a non-existant key should fail |
220 | |
221 | $status = $X->del('joe') ; |
f6b705ef |
222 | ok(38, $status == 1 ); |
a0d0e21e |
223 | |
224 | # Check the get interface |
225 | |
226 | # First a non-existing key |
227 | $status = $X->get('aaaa', $value) ; |
f6b705ef |
228 | ok(39, $status == 1 ); |
a0d0e21e |
229 | |
230 | # Next an existing key |
231 | $status = $X->get('a', $value) ; |
f6b705ef |
232 | ok(40, $status == 0 ); |
233 | ok(41, $value eq 'A' ); |
a0d0e21e |
234 | |
235 | # seq |
236 | # ### |
237 | |
238 | # ditto, but use put to replace the key/value pair. |
239 | |
240 | # use seq to walk backwards through a file - check that this reversed is |
241 | |
242 | # check seq FIRST/LAST |
243 | |
244 | # sync |
245 | # #### |
246 | |
247 | $status = $X->sync ; |
f6b705ef |
248 | ok(42, $status == 0 ); |
a0d0e21e |
249 | |
250 | |
251 | # fd |
252 | # ## |
253 | |
254 | $status = $X->fd ; |
f6b705ef |
255 | ok(43, $status != 0 ); |
a0d0e21e |
256 | |
257 | undef $X ; |
258 | untie %h ; |
259 | |
260 | unlink $Dfile; |
261 | |
f6b705ef |
262 | # clear |
263 | # ##### |
264 | |
265 | ok(44, tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) ); |
266 | foreach (1 .. 10) |
267 | { $h{$_} = $_ * 100 } |
268 | |
269 | # check that there are 10 elements in the hash |
270 | $i = 0 ; |
271 | while (($key,$value) = each(%h)) { |
272 | $i++; |
273 | } |
274 | ok(45, $i == 10); |
275 | |
276 | # now clear the hash |
277 | %h = () ; |
278 | |
279 | # check it is empty |
280 | $i = 0 ; |
281 | while (($key,$value) = each(%h)) { |
282 | $i++; |
283 | } |
284 | ok(46, $i == 0); |
285 | |
286 | untie %h ; |
287 | unlink $Dfile ; |
288 | |
289 | |
a0d0e21e |
290 | # Now try an in memory file |
f6b705ef |
291 | ok(47, $X = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_HASH ) ); |
a0d0e21e |
292 | |
293 | # fd with an in memory file should return fail |
294 | $status = $X->fd ; |
f6b705ef |
295 | ok(48, $status == -1 ); |
a0d0e21e |
296 | |
a0d0e21e |
297 | undef $X ; |
610ab055 |
298 | untie %h ; |
299 | |
300 | { |
301 | # check ability to override the default hashing |
302 | my %x ; |
303 | my $filename = "xyz" ; |
304 | my $hi = new DB_File::HASHINFO ; |
305 | $::count = 0 ; |
306 | $hi->{hash} = sub { ++$::count ; length $_[0] } ; |
307 | ok(49, tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $hi ) ; |
308 | $h{"abc"} = 123 ; |
309 | ok(50, $h{"abc"} == 123) ; |
310 | untie %x ; |
311 | unlink $filename ; |
312 | ok(51, $::count >0) ; |
313 | } |
a0d0e21e |
314 | |
315 | exit ; |