Commit | Line | Data |
f6b705ef |
1 | #!./perl -w |
a0d0e21e |
2 | |
3 | BEGIN { |
55497cff |
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 | |
f6b705ef |
15 | print "1..91\n"; |
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 | |
55497cff |
26 | sub lexical |
27 | { |
28 | my(@a) = unpack ("C*", $a) ; |
29 | my(@b) = unpack ("C*", $b) ; |
30 | |
31 | my $len = (@a > @b ? @b : @a) ; |
32 | my $i = 0 ; |
33 | |
34 | foreach $i ( 0 .. $len -1) { |
35 | return $a[$i] - $b[$i] if $a[$i] != $b[$i] ; |
36 | } |
37 | |
38 | return @a - @b ; |
39 | } |
40 | |
55d68b4a |
41 | $Dfile = "dbbtree.tmp"; |
a0d0e21e |
42 | unlink $Dfile; |
43 | |
44 | umask(0); |
45 | |
46 | # Check the interface to BTREEINFO |
47 | |
f6b705ef |
48 | my $dbh = new DB_File::BTREEINFO ; |
55497cff |
49 | ok(1, $dbh->{flags} == 0) ; |
50 | ok(2, $dbh->{cachesize} == 0) ; |
51 | ok(3, $dbh->{psize} == 0) ; |
52 | ok(4, $dbh->{lorder} == 0) ; |
53 | ok(5, $dbh->{minkeypage} == 0) ; |
54 | ok(6, $dbh->{maxkeypage} == 0) ; |
f6b705ef |
55 | $^W = 0 ; |
f6b705ef |
56 | ok(7, $dbh->{compare} == undef) ; |
57 | ok(8, $dbh->{prefix} == undef) ; |
58 | $^W = 1 ; |
a0d0e21e |
59 | |
60 | $dbh->{flags} = 3000 ; |
f6b705ef |
61 | ok(9, $dbh->{flags} == 3000) ; |
a0d0e21e |
62 | |
63 | $dbh->{cachesize} = 9000 ; |
f6b705ef |
64 | ok(10, $dbh->{cachesize} == 9000); |
65 | |
a0d0e21e |
66 | $dbh->{psize} = 400 ; |
f6b705ef |
67 | ok(11, $dbh->{psize} == 400) ; |
a0d0e21e |
68 | |
69 | $dbh->{lorder} = 65 ; |
f6b705ef |
70 | ok(12, $dbh->{lorder} == 65) ; |
a0d0e21e |
71 | |
72 | $dbh->{minkeypage} = 123 ; |
f6b705ef |
73 | ok(13, $dbh->{minkeypage} == 123) ; |
a0d0e21e |
74 | |
75 | $dbh->{maxkeypage} = 1234 ; |
f6b705ef |
76 | ok(14, $dbh->{maxkeypage} == 1234 ); |
a0d0e21e |
77 | |
78 | $dbh->{compare} = 1234 ; |
f6b705ef |
79 | ok(15, $dbh->{compare} == 1234) ; |
a0d0e21e |
80 | |
81 | $dbh->{prefix} = 1234 ; |
f6b705ef |
82 | ok(16, $dbh->{prefix} == 1234 ); |
a0d0e21e |
83 | |
84 | # Check that an invalid entry is caught both for store & fetch |
85 | eval '$dbh->{fred} = 1234' ; |
f6b705ef |
86 | ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ; |
a0d0e21e |
87 | eval '$q = $dbh->{fred}' ; |
f6b705ef |
88 | ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ; |
a0d0e21e |
89 | |
90 | # Now check the interface to BTREE |
91 | |
f6b705ef |
92 | ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ; |
a0d0e21e |
93 | |
94 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
95 | $blksize,$blocks) = stat($Dfile); |
053b5721 |
96 | ok(20, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) ); |
a0d0e21e |
97 | |
98 | while (($key,$value) = each(%h)) { |
99 | $i++; |
100 | } |
f6b705ef |
101 | ok(21, !$i ) ; |
a0d0e21e |
102 | |
103 | $h{'goner1'} = 'snork'; |
104 | |
105 | $h{'abc'} = 'ABC'; |
f6b705ef |
106 | ok(22, $h{'abc'} eq 'ABC' ); |
107 | ok(23, ! defined $h{'jimmy'} ) ; |
108 | ok(24, ! exists $h{'jimmy'} ) ; |
109 | ok(25, defined $h{'abc'} ) ; |
a0d0e21e |
110 | |
111 | $h{'def'} = 'DEF'; |
112 | $h{'jkl','mno'} = "JKL\034MNO"; |
113 | $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); |
114 | $h{'a'} = 'A'; |
115 | |
116 | #$h{'b'} = 'B'; |
117 | $X->STORE('b', 'B') ; |
118 | |
119 | $h{'c'} = 'C'; |
120 | |
121 | #$h{'d'} = 'D'; |
122 | $X->put('d', 'D') ; |
123 | |
124 | $h{'e'} = 'E'; |
125 | $h{'f'} = 'F'; |
126 | $h{'g'} = 'X'; |
127 | $h{'h'} = 'H'; |
128 | $h{'i'} = 'I'; |
129 | |
130 | $h{'goner2'} = 'snork'; |
131 | delete $h{'goner2'}; |
132 | |
133 | |
134 | # IMPORTANT - $X must be undefined before the untie otherwise the |
135 | # underlying DB close routine will not get called. |
136 | undef $X ; |
137 | untie(%h); |
138 | |
139 | |
140 | # tie to the same file again |
f6b705ef |
141 | ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ; |
a0d0e21e |
142 | |
143 | # Modify an entry from the previous tie |
144 | $h{'g'} = 'G'; |
145 | |
146 | $h{'j'} = 'J'; |
147 | $h{'k'} = 'K'; |
148 | $h{'l'} = 'L'; |
149 | $h{'m'} = 'M'; |
150 | $h{'n'} = 'N'; |
151 | $h{'o'} = 'O'; |
152 | $h{'p'} = 'P'; |
153 | $h{'q'} = 'Q'; |
154 | $h{'r'} = 'R'; |
155 | $h{'s'} = 'S'; |
156 | $h{'t'} = 'T'; |
157 | $h{'u'} = 'U'; |
158 | $h{'v'} = 'V'; |
159 | $h{'w'} = 'W'; |
160 | $h{'x'} = 'X'; |
161 | $h{'y'} = 'Y'; |
162 | $h{'z'} = 'Z'; |
163 | |
164 | $h{'goner3'} = 'snork'; |
165 | |
166 | delete $h{'goner1'}; |
167 | $X->DELETE('goner3'); |
168 | |
169 | @keys = keys(%h); |
170 | @values = values(%h); |
171 | |
f6b705ef |
172 | ok(27, $#keys == 29 && $#values == 29) ; |
a0d0e21e |
173 | |
f6b705ef |
174 | $i = 0 ; |
a0d0e21e |
175 | while (($key,$value) = each(%h)) { |
2f52a358 |
176 | if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { |
a0d0e21e |
177 | $key =~ y/a-z/A-Z/; |
178 | $i++ if $key eq $value; |
179 | } |
180 | } |
181 | |
f6b705ef |
182 | ok(28, $i == 30) ; |
a0d0e21e |
183 | |
55d68b4a |
184 | @keys = ('blurfl', keys(%h), 'dyick'); |
f6b705ef |
185 | ok(29, $#keys == 31) ; |
a0d0e21e |
186 | |
187 | #Check that the keys can be retrieved in order |
55497cff |
188 | my @b = keys %h ; |
189 | my @c = sort lexical @b ; |
190 | ok(30, ArrayCompare(\@b, \@c)) ; |
a0d0e21e |
191 | |
192 | $h{'foo'} = ''; |
f6b705ef |
193 | ok(31, $h{'foo'} eq '' ) ; |
a0d0e21e |
194 | |
195 | $h{''} = 'bar'; |
f6b705ef |
196 | ok(32, $h{''} eq 'bar' ); |
a0d0e21e |
197 | |
198 | # check cache overflow and numeric keys and contents |
199 | $ok = 1; |
200 | for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } |
201 | for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } |
f6b705ef |
202 | ok(33, $ok); |
a0d0e21e |
203 | |
204 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
205 | $blksize,$blocks) = stat($Dfile); |
f6b705ef |
206 | ok(34, $size > 0 ); |
a0d0e21e |
207 | |
208 | @h{0..200} = 200..400; |
209 | @foo = @h{0..200}; |
f6b705ef |
210 | ok(35, join(':',200..400) eq join(':',@foo) ); |
a0d0e21e |
211 | |
212 | # Now check all the non-tie specific stuff |
213 | |
214 | |
215 | # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite |
216 | # an existing record. |
217 | |
218 | $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ; |
f6b705ef |
219 | ok(36, $status == 1 ); |
a0d0e21e |
220 | |
221 | # check that the value of the key 'x' has not been changed by the |
222 | # previous test |
f6b705ef |
223 | ok(37, $h{'x'} eq 'X' ); |
a0d0e21e |
224 | |
225 | # standard put |
226 | $status = $X->put('key', 'value') ; |
f6b705ef |
227 | ok(38, $status == 0 ); |
a0d0e21e |
228 | |
229 | #check that previous put can be retrieved |
f6b705ef |
230 | $value = 0 ; |
a0d0e21e |
231 | $status = $X->get('key', $value) ; |
f6b705ef |
232 | ok(39, $status == 0 ); |
233 | ok(40, $value eq 'value' ); |
a0d0e21e |
234 | |
235 | # Attempting to delete an existing key should work |
236 | |
237 | $status = $X->del('q') ; |
f6b705ef |
238 | ok(41, $status == 0 ); |
a0d0e21e |
239 | $status = $X->del('') ; |
f6b705ef |
240 | ok(42, $status == 0 ); |
a0d0e21e |
241 | |
242 | # Make sure that the key deleted, cannot be retrieved |
f6b705ef |
243 | $^W = 0 ; |
244 | ok(43, $h{'q'} eq undef) ; |
245 | ok(44, $h{''} eq undef) ; |
246 | $^W = 1 ; |
a0d0e21e |
247 | |
248 | undef $X ; |
249 | untie %h ; |
250 | |
f6b705ef |
251 | ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE )); |
a0d0e21e |
252 | |
253 | # Attempting to delete a non-existant key should fail |
254 | |
255 | $status = $X->del('joe') ; |
f6b705ef |
256 | ok(46, $status == 1 ); |
a0d0e21e |
257 | |
258 | # Check the get interface |
259 | |
260 | # First a non-existing key |
261 | $status = $X->get('aaaa', $value) ; |
f6b705ef |
262 | ok(47, $status == 1 ); |
a0d0e21e |
263 | |
264 | # Next an existing key |
265 | $status = $X->get('a', $value) ; |
f6b705ef |
266 | ok(48, $status == 0 ); |
267 | ok(49, $value eq 'A' ); |
a0d0e21e |
268 | |
269 | # seq |
270 | # ### |
271 | |
272 | # use seq to find an approximate match |
273 | $key = 'ke' ; |
274 | $value = '' ; |
275 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
276 | ok(50, $status == 0 ); |
277 | ok(51, $key eq 'key' ); |
278 | ok(52, $value eq 'value' ); |
a0d0e21e |
279 | |
280 | # seq when the key does not match |
281 | $key = 'zzz' ; |
282 | $value = '' ; |
283 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
284 | ok(53, $status == 1 ); |
a0d0e21e |
285 | |
286 | |
287 | # use seq to set the cursor, then delete the record @ the cursor. |
288 | |
289 | $key = 'x' ; |
290 | $value = '' ; |
291 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
292 | ok(54, $status == 0 ); |
293 | ok(55, $key eq 'x' ); |
294 | ok(56, $value eq 'X' ); |
a0d0e21e |
295 | $status = $X->del(0, R_CURSOR) ; |
f6b705ef |
296 | ok(57, $status == 0 ); |
a0d0e21e |
297 | $status = $X->get('x', $value) ; |
f6b705ef |
298 | ok(58, $status == 1 ); |
a0d0e21e |
299 | |
300 | # ditto, but use put to replace the key/value pair. |
301 | $key = 'y' ; |
302 | $value = '' ; |
303 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
304 | ok(59, $status == 0 ); |
305 | ok(60, $key eq 'y' ); |
306 | ok(61, $value eq 'Y' ); |
a0d0e21e |
307 | |
308 | $key = "replace key" ; |
309 | $value = "replace value" ; |
310 | $status = $X->put($key, $value, R_CURSOR) ; |
f6b705ef |
311 | ok(62, $status == 0 ); |
312 | ok(63, $key eq 'replace key' ); |
313 | ok(64, $value eq 'replace value' ); |
a0d0e21e |
314 | $status = $X->get('y', $value) ; |
f6b705ef |
315 | ok(65, $status == 1 ); |
a0d0e21e |
316 | |
317 | # use seq to walk forwards through a file |
318 | |
319 | $status = $X->seq($key, $value, R_FIRST) ; |
f6b705ef |
320 | ok(66, $status == 0 ); |
a0d0e21e |
321 | $previous = $key ; |
322 | |
323 | $ok = 1 ; |
324 | while (($status = $X->seq($key, $value, R_NEXT)) == 0) |
325 | { |
326 | ($ok = 0), last if ($previous cmp $key) == 1 ; |
327 | } |
328 | |
f6b705ef |
329 | ok(67, $status == 1 ); |
330 | ok(68, $ok == 1 ); |
a0d0e21e |
331 | |
332 | # use seq to walk backwards through a file |
333 | $status = $X->seq($key, $value, R_LAST) ; |
f6b705ef |
334 | ok(69, $status == 0 ); |
a0d0e21e |
335 | $previous = $key ; |
336 | |
337 | $ok = 1 ; |
338 | while (($status = $X->seq($key, $value, R_PREV)) == 0) |
339 | { |
340 | ($ok = 0), last if ($previous cmp $key) == -1 ; |
341 | #print "key = [$key] value = [$value]\n" ; |
342 | } |
343 | |
f6b705ef |
344 | ok(70, $status == 1 ); |
345 | ok(71, $ok == 1 ); |
a0d0e21e |
346 | |
347 | |
348 | # check seq FIRST/LAST |
349 | |
350 | # sync |
351 | # #### |
352 | |
353 | $status = $X->sync ; |
f6b705ef |
354 | ok(72, $status == 0 ); |
a0d0e21e |
355 | |
356 | |
357 | # fd |
358 | # ## |
359 | |
360 | $status = $X->fd ; |
f6b705ef |
361 | ok(73, $status != 0 ); |
a0d0e21e |
362 | |
363 | |
364 | undef $X ; |
365 | untie %h ; |
366 | |
367 | unlink $Dfile; |
368 | |
369 | # Now try an in memory file |
f6b705ef |
370 | ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE )); |
a0d0e21e |
371 | |
372 | # fd with an in memory file should return failure |
373 | $status = $Y->fd ; |
f6b705ef |
374 | ok(75, $status == -1 ); |
a0d0e21e |
375 | |
55d68b4a |
376 | |
a0d0e21e |
377 | undef $Y ; |
378 | untie %h ; |
379 | |
55d68b4a |
380 | # Duplicate keys |
381 | my $bt = new DB_File::BTREEINFO ; |
382 | $bt->{flags} = R_DUP ; |
f6b705ef |
383 | ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ; |
55d68b4a |
384 | |
385 | $hh{'Wall'} = 'Larry' ; |
386 | $hh{'Wall'} = 'Stone' ; # Note the duplicate key |
387 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key |
f6b705ef |
388 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value |
55d68b4a |
389 | $hh{'Smith'} = 'John' ; |
390 | $hh{'mouse'} = 'mickey' ; |
391 | |
392 | # first work in scalar context |
f6b705ef |
393 | ok(77, scalar $YY->get_dup('Unknown') == 0 ); |
394 | ok(78, scalar $YY->get_dup('Smith') == 1 ); |
395 | ok(79, scalar $YY->get_dup('Wall') == 4 ); |
55d68b4a |
396 | |
397 | # now in list context |
398 | my @unknown = $YY->get_dup('Unknown') ; |
f6b705ef |
399 | ok(80, "@unknown" eq "" ); |
55d68b4a |
400 | |
401 | my @smith = $YY->get_dup('Smith') ; |
f6b705ef |
402 | ok(81, "@smith" eq "John" ); |
55d68b4a |
403 | |
760ac839 |
404 | { |
f6b705ef |
405 | my @wall = $YY->get_dup('Wall') ; |
406 | my %wall ; |
407 | @wall{@wall} = @wall ; |
408 | ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) ); |
760ac839 |
409 | } |
55d68b4a |
410 | |
411 | # hash |
412 | my %unknown = $YY->get_dup('Unknown', 1) ; |
f6b705ef |
413 | ok(83, keys %unknown == 0 ); |
55d68b4a |
414 | |
415 | my %smith = $YY->get_dup('Smith', 1) ; |
f6b705ef |
416 | ok(84, keys %smith == 1 && $smith{'John'}) ; |
55d68b4a |
417 | |
f6b705ef |
418 | my %wall = $YY->get_dup('Wall', 1) ; |
419 | ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1 |
420 | && $wall{'Brick'} == 2); |
55d68b4a |
421 | |
422 | undef $YY ; |
423 | untie %hh ; |
424 | unlink $Dfile; |
425 | |
426 | |
8e07c86e |
427 | # test multiple callbacks |
428 | $Dfile1 = "btree1" ; |
429 | $Dfile2 = "btree2" ; |
430 | $Dfile3 = "btree3" ; |
431 | |
432 | $dbh1 = TIEHASH DB_File::BTREEINFO ; |
433 | $dbh1->{compare} = sub { $_[0] <=> $_[1] } ; |
434 | |
435 | $dbh2 = TIEHASH DB_File::BTREEINFO ; |
436 | $dbh2->{compare} = sub { $_[0] cmp $_[1] } ; |
437 | |
438 | $dbh3 = TIEHASH DB_File::BTREEINFO ; |
439 | $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ; |
440 | |
441 | |
f6b705ef |
442 | tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ; |
443 | tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ; |
444 | tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ; |
8e07c86e |
445 | |
446 | @Keys = qw( 0123 12 -1234 9 987654321 def ) ; |
f6b705ef |
447 | $^W = 0 ; |
8e07c86e |
448 | @srt_1 = sort { $a <=> $b } @Keys ; |
f6b705ef |
449 | $^W = 1 ; |
8e07c86e |
450 | @srt_2 = sort { $a cmp $b } @Keys ; |
451 | @srt_3 = sort { length $a <=> length $b } @Keys ; |
452 | |
453 | foreach (@Keys) { |
55497cff |
454 | $^W = 0 ; |
455 | $h{$_} = 1 ; |
456 | $^W = 1 ; |
8e07c86e |
457 | $g{$_} = 1 ; |
458 | $k{$_} = 1 ; |
459 | } |
460 | |
461 | sub ArrayCompare |
462 | { |
463 | my($a, $b) = @_ ; |
464 | |
465 | return 0 if @$a != @$b ; |
466 | |
467 | foreach (1 .. length @$a) |
468 | { |
469 | return 0 unless $$a[$_] eq $$b[$_] ; |
470 | } |
471 | |
472 | 1 ; |
473 | } |
474 | |
f6b705ef |
475 | ok(86, ArrayCompare (\@srt_1, [keys %h]) ); |
476 | ok(87, ArrayCompare (\@srt_2, [keys %g]) ); |
477 | ok(88, ArrayCompare (\@srt_3, [keys %k]) ); |
8e07c86e |
478 | |
479 | untie %h ; |
480 | untie %g ; |
481 | untie %k ; |
482 | unlink $Dfile1, $Dfile2, $Dfile3 ; |
483 | |
f6b705ef |
484 | # clear |
485 | # ##### |
486 | |
487 | ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
488 | foreach (1 .. 10) |
489 | { $h{$_} = $_ * 100 } |
490 | |
491 | # check that there are 10 elements in the hash |
492 | $i = 0 ; |
493 | while (($key,$value) = each(%h)) { |
494 | $i++; |
495 | } |
496 | ok(90, $i == 10); |
497 | |
498 | # now clear the hash |
499 | %h = () ; |
500 | |
501 | # check it is empty |
502 | $i = 0 ; |
503 | while (($key,$value) = each(%h)) { |
504 | $i++; |
505 | } |
506 | ok(91, $i == 0); |
507 | |
508 | untie %h ; |
509 | unlink $Dfile1 ; |
510 | |
a0d0e21e |
511 | exit ; |