Commit | Line | Data |
f6b705ef |
1 | #!./perl -w |
a0d0e21e |
2 | |
3 | BEGIN { |
20822f61 |
4 | @INC = '../lib'; |
a0d0e21e |
5 | require Config; import Config; |
6 | if ($Config{'extensions'} !~ /\bDB_File\b/) { |
45c0de28 |
7 | print "1..0 # Skip: DB_File was not built\n"; |
a0d0e21e |
8 | exit 0; |
9 | } |
10 | } |
11 | |
3245f058 |
12 | use warnings; |
13 | use strict; |
a0d0e21e |
14 | use DB_File; |
15 | use Fcntl; |
16 | |
3245f058 |
17 | print "1..157\n"; |
f6b705ef |
18 | |
19 | sub ok |
20 | { |
21 | my $no = shift ; |
22 | my $result = shift ; |
23 | |
24 | print "not " unless $result ; |
25 | print "ok $no\n" ; |
26 | } |
a0d0e21e |
27 | |
55497cff |
28 | sub lexical |
29 | { |
30 | my(@a) = unpack ("C*", $a) ; |
31 | my(@b) = unpack ("C*", $b) ; |
32 | |
33 | my $len = (@a > @b ? @b : @a) ; |
34 | my $i = 0 ; |
35 | |
36 | foreach $i ( 0 .. $len -1) { |
37 | return $a[$i] - $b[$i] if $a[$i] != $b[$i] ; |
38 | } |
39 | |
40 | return @a - @b ; |
41 | } |
42 | |
2c2d71f5 |
43 | { |
44 | package Redirect ; |
45 | use Symbol ; |
46 | |
47 | sub new |
48 | { |
49 | my $class = shift ; |
50 | my $filename = shift ; |
51 | my $fh = gensym ; |
52 | open ($fh, ">$filename") || die "Cannot open $filename: $!" ; |
53 | my $real_stdout = select($fh) ; |
54 | return bless [$fh, $real_stdout ] ; |
55 | |
56 | } |
57 | sub DESTROY |
58 | { |
59 | my $self = shift ; |
60 | close $self->[0] ; |
61 | select($self->[1]) ; |
62 | } |
63 | } |
64 | |
65 | sub docat |
66 | { |
67 | my $file = shift; |
68 | #local $/ = undef unless wantarray ; |
69 | open(CAT,$file) || die "Cannot open $file: $!"; |
70 | my @result = <CAT>; |
71 | close(CAT); |
72 | wantarray ? @result : join("", @result) ; |
73 | } |
74 | |
75 | sub docat_del |
76 | { |
77 | my $file = shift; |
78 | #local $/ = undef unless wantarray ; |
79 | open(CAT,$file) || die "Cannot open $file: $!"; |
80 | my @result = <CAT>; |
81 | close(CAT); |
82 | unlink $file ; |
83 | wantarray ? @result : join("", @result) ; |
84 | } |
85 | |
86 | |
3245f058 |
87 | my $db185mode = ($DB_File::db_version == 1 && ! $DB_File::db_185_compat) ; |
88 | my $null_keys_allowed = ($DB_File::db_ver < 2.004010 |
89 | || $DB_File::db_ver >= 3.1 ); |
039d031f |
90 | |
9fe6733a |
91 | my $Dfile = "dbbtree.tmp"; |
a0d0e21e |
92 | unlink $Dfile; |
93 | |
94 | umask(0); |
95 | |
96 | # Check the interface to BTREEINFO |
97 | |
f6b705ef |
98 | my $dbh = new DB_File::BTREEINFO ; |
3fe9a6f1 |
99 | ok(1, ! defined $dbh->{flags}) ; |
100 | ok(2, ! defined $dbh->{cachesize}) ; |
101 | ok(3, ! defined $dbh->{psize}) ; |
102 | ok(4, ! defined $dbh->{lorder}) ; |
103 | ok(5, ! defined $dbh->{minkeypage}) ; |
104 | ok(6, ! defined $dbh->{maxkeypage}) ; |
105 | ok(7, ! defined $dbh->{compare}) ; |
106 | ok(8, ! defined $dbh->{prefix}) ; |
a0d0e21e |
107 | |
108 | $dbh->{flags} = 3000 ; |
f6b705ef |
109 | ok(9, $dbh->{flags} == 3000) ; |
a0d0e21e |
110 | |
111 | $dbh->{cachesize} = 9000 ; |
f6b705ef |
112 | ok(10, $dbh->{cachesize} == 9000); |
113 | |
a0d0e21e |
114 | $dbh->{psize} = 400 ; |
f6b705ef |
115 | ok(11, $dbh->{psize} == 400) ; |
a0d0e21e |
116 | |
117 | $dbh->{lorder} = 65 ; |
f6b705ef |
118 | ok(12, $dbh->{lorder} == 65) ; |
a0d0e21e |
119 | |
120 | $dbh->{minkeypage} = 123 ; |
f6b705ef |
121 | ok(13, $dbh->{minkeypage} == 123) ; |
a0d0e21e |
122 | |
123 | $dbh->{maxkeypage} = 1234 ; |
f6b705ef |
124 | ok(14, $dbh->{maxkeypage} == 1234 ); |
a0d0e21e |
125 | |
126 | $dbh->{compare} = 1234 ; |
f6b705ef |
127 | ok(15, $dbh->{compare} == 1234) ; |
a0d0e21e |
128 | |
129 | $dbh->{prefix} = 1234 ; |
f6b705ef |
130 | ok(16, $dbh->{prefix} == 1234 ); |
a0d0e21e |
131 | |
132 | # Check that an invalid entry is caught both for store & fetch |
133 | eval '$dbh->{fred} = 1234' ; |
f6b705ef |
134 | ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ; |
3245f058 |
135 | eval 'my $q = $dbh->{fred}' ; |
f6b705ef |
136 | ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ; |
a0d0e21e |
137 | |
138 | # Now check the interface to BTREE |
139 | |
3245f058 |
140 | my ($X, %h) ; |
f6b705ef |
141 | ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ; |
a0d0e21e |
142 | |
3245f058 |
143 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
a0d0e21e |
144 | $blksize,$blocks) = stat($Dfile); |
d536870a |
145 | ok(20, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640) |
146 | || $^O eq 'amigaos' || $^O eq 'MSWin32' || $^O eq 'NetWare'); |
a0d0e21e |
147 | |
3245f058 |
148 | my ($key, $value, $i); |
a0d0e21e |
149 | while (($key,$value) = each(%h)) { |
150 | $i++; |
151 | } |
f6b705ef |
152 | ok(21, !$i ) ; |
a0d0e21e |
153 | |
154 | $h{'goner1'} = 'snork'; |
155 | |
156 | $h{'abc'} = 'ABC'; |
f6b705ef |
157 | ok(22, $h{'abc'} eq 'ABC' ); |
158 | ok(23, ! defined $h{'jimmy'} ) ; |
159 | ok(24, ! exists $h{'jimmy'} ) ; |
160 | ok(25, defined $h{'abc'} ) ; |
a0d0e21e |
161 | |
162 | $h{'def'} = 'DEF'; |
163 | $h{'jkl','mno'} = "JKL\034MNO"; |
164 | $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); |
165 | $h{'a'} = 'A'; |
166 | |
167 | #$h{'b'} = 'B'; |
168 | $X->STORE('b', 'B') ; |
169 | |
170 | $h{'c'} = 'C'; |
171 | |
172 | #$h{'d'} = 'D'; |
173 | $X->put('d', 'D') ; |
174 | |
175 | $h{'e'} = 'E'; |
176 | $h{'f'} = 'F'; |
177 | $h{'g'} = 'X'; |
178 | $h{'h'} = 'H'; |
179 | $h{'i'} = 'I'; |
180 | |
181 | $h{'goner2'} = 'snork'; |
182 | delete $h{'goner2'}; |
183 | |
184 | |
185 | # IMPORTANT - $X must be undefined before the untie otherwise the |
186 | # underlying DB close routine will not get called. |
187 | undef $X ; |
188 | untie(%h); |
189 | |
a0d0e21e |
190 | # tie to the same file again |
f6b705ef |
191 | ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ; |
a0d0e21e |
192 | |
193 | # Modify an entry from the previous tie |
194 | $h{'g'} = 'G'; |
195 | |
196 | $h{'j'} = 'J'; |
197 | $h{'k'} = 'K'; |
198 | $h{'l'} = 'L'; |
199 | $h{'m'} = 'M'; |
200 | $h{'n'} = 'N'; |
201 | $h{'o'} = 'O'; |
202 | $h{'p'} = 'P'; |
203 | $h{'q'} = 'Q'; |
204 | $h{'r'} = 'R'; |
205 | $h{'s'} = 'S'; |
206 | $h{'t'} = 'T'; |
207 | $h{'u'} = 'U'; |
208 | $h{'v'} = 'V'; |
209 | $h{'w'} = 'W'; |
210 | $h{'x'} = 'X'; |
211 | $h{'y'} = 'Y'; |
212 | $h{'z'} = 'Z'; |
213 | |
214 | $h{'goner3'} = 'snork'; |
215 | |
216 | delete $h{'goner1'}; |
217 | $X->DELETE('goner3'); |
218 | |
3245f058 |
219 | my @keys = keys(%h); |
220 | my @values = values(%h); |
a0d0e21e |
221 | |
f6b705ef |
222 | ok(27, $#keys == 29 && $#values == 29) ; |
a0d0e21e |
223 | |
f6b705ef |
224 | $i = 0 ; |
a0d0e21e |
225 | while (($key,$value) = each(%h)) { |
2f52a358 |
226 | if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { |
a0d0e21e |
227 | $key =~ y/a-z/A-Z/; |
228 | $i++ if $key eq $value; |
229 | } |
230 | } |
231 | |
f6b705ef |
232 | ok(28, $i == 30) ; |
a0d0e21e |
233 | |
55d68b4a |
234 | @keys = ('blurfl', keys(%h), 'dyick'); |
f6b705ef |
235 | ok(29, $#keys == 31) ; |
a0d0e21e |
236 | |
237 | #Check that the keys can be retrieved in order |
55497cff |
238 | my @b = keys %h ; |
239 | my @c = sort lexical @b ; |
240 | ok(30, ArrayCompare(\@b, \@c)) ; |
a0d0e21e |
241 | |
242 | $h{'foo'} = ''; |
f6b705ef |
243 | ok(31, $h{'foo'} eq '' ) ; |
a0d0e21e |
244 | |
3245f058 |
245 | # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys. |
246 | # This feature was reenabled in version 3.1 of Berkeley DB. |
247 | my $result = 0 ; |
248 | if ($null_keys_allowed) { |
249 | $h{''} = 'bar'; |
250 | $result = ( $h{''} eq 'bar' ); |
251 | } |
252 | else |
253 | { $result = 1 } |
254 | ok(32, $result) ; |
a0d0e21e |
255 | |
256 | # check cache overflow and numeric keys and contents |
3245f058 |
257 | my $ok = 1; |
a0d0e21e |
258 | for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } |
259 | for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } |
f6b705ef |
260 | ok(33, $ok); |
a0d0e21e |
261 | |
262 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, |
263 | $blksize,$blocks) = stat($Dfile); |
f6b705ef |
264 | ok(34, $size > 0 ); |
a0d0e21e |
265 | |
266 | @h{0..200} = 200..400; |
3245f058 |
267 | my @foo = @h{0..200}; |
f6b705ef |
268 | ok(35, join(':',200..400) eq join(':',@foo) ); |
a0d0e21e |
269 | |
270 | # Now check all the non-tie specific stuff |
271 | |
272 | |
273 | # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite |
274 | # an existing record. |
275 | |
3245f058 |
276 | my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ; |
f6b705ef |
277 | ok(36, $status == 1 ); |
a0d0e21e |
278 | |
279 | # check that the value of the key 'x' has not been changed by the |
280 | # previous test |
f6b705ef |
281 | ok(37, $h{'x'} eq 'X' ); |
a0d0e21e |
282 | |
283 | # standard put |
284 | $status = $X->put('key', 'value') ; |
f6b705ef |
285 | ok(38, $status == 0 ); |
a0d0e21e |
286 | |
287 | #check that previous put can be retrieved |
f6b705ef |
288 | $value = 0 ; |
a0d0e21e |
289 | $status = $X->get('key', $value) ; |
f6b705ef |
290 | ok(39, $status == 0 ); |
291 | ok(40, $value eq 'value' ); |
a0d0e21e |
292 | |
293 | # Attempting to delete an existing key should work |
294 | |
295 | $status = $X->del('q') ; |
f6b705ef |
296 | ok(41, $status == 0 ); |
3245f058 |
297 | if ($null_keys_allowed) { |
298 | $status = $X->del('') ; |
299 | } else { |
300 | $status = 0 ; |
301 | } |
302 | ok(42, $status == 0 ); |
a0d0e21e |
303 | |
304 | # Make sure that the key deleted, cannot be retrieved |
ac1ad7f0 |
305 | ok(43, ! defined $h{'q'}) ; |
306 | ok(44, ! defined $h{''}) ; |
a0d0e21e |
307 | |
308 | undef $X ; |
309 | untie %h ; |
310 | |
f6b705ef |
311 | ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE )); |
a0d0e21e |
312 | |
313 | # Attempting to delete a non-existant key should fail |
314 | |
315 | $status = $X->del('joe') ; |
f6b705ef |
316 | ok(46, $status == 1 ); |
a0d0e21e |
317 | |
318 | # Check the get interface |
319 | |
320 | # First a non-existing key |
321 | $status = $X->get('aaaa', $value) ; |
f6b705ef |
322 | ok(47, $status == 1 ); |
a0d0e21e |
323 | |
324 | # Next an existing key |
325 | $status = $X->get('a', $value) ; |
f6b705ef |
326 | ok(48, $status == 0 ); |
327 | ok(49, $value eq 'A' ); |
a0d0e21e |
328 | |
329 | # seq |
330 | # ### |
331 | |
332 | # use seq to find an approximate match |
333 | $key = 'ke' ; |
334 | $value = '' ; |
335 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
336 | ok(50, $status == 0 ); |
337 | ok(51, $key eq 'key' ); |
338 | ok(52, $value eq 'value' ); |
a0d0e21e |
339 | |
340 | # seq when the key does not match |
341 | $key = 'zzz' ; |
342 | $value = '' ; |
343 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
344 | ok(53, $status == 1 ); |
a0d0e21e |
345 | |
346 | |
347 | # use seq to set the cursor, then delete the record @ the cursor. |
348 | |
349 | $key = 'x' ; |
350 | $value = '' ; |
351 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
352 | ok(54, $status == 0 ); |
353 | ok(55, $key eq 'x' ); |
354 | ok(56, $value eq 'X' ); |
a0d0e21e |
355 | $status = $X->del(0, R_CURSOR) ; |
f6b705ef |
356 | ok(57, $status == 0 ); |
a0d0e21e |
357 | $status = $X->get('x', $value) ; |
f6b705ef |
358 | ok(58, $status == 1 ); |
a0d0e21e |
359 | |
360 | # ditto, but use put to replace the key/value pair. |
361 | $key = 'y' ; |
362 | $value = '' ; |
363 | $status = $X->seq($key, $value, R_CURSOR) ; |
f6b705ef |
364 | ok(59, $status == 0 ); |
365 | ok(60, $key eq 'y' ); |
366 | ok(61, $value eq 'Y' ); |
a0d0e21e |
367 | |
368 | $key = "replace key" ; |
369 | $value = "replace value" ; |
370 | $status = $X->put($key, $value, R_CURSOR) ; |
f6b705ef |
371 | ok(62, $status == 0 ); |
372 | ok(63, $key eq 'replace key' ); |
373 | ok(64, $value eq 'replace value' ); |
a0d0e21e |
374 | $status = $X->get('y', $value) ; |
1f70e1ea |
375 | ok(65, 1) ; # hard-wire to always pass. the previous test ($status == 1) |
a9fd575d |
376 | # only worked because of a bug in 1.85/6 |
a0d0e21e |
377 | |
378 | # use seq to walk forwards through a file |
379 | |
380 | $status = $X->seq($key, $value, R_FIRST) ; |
f6b705ef |
381 | ok(66, $status == 0 ); |
3245f058 |
382 | my $previous = $key ; |
a0d0e21e |
383 | |
384 | $ok = 1 ; |
385 | while (($status = $X->seq($key, $value, R_NEXT)) == 0) |
386 | { |
387 | ($ok = 0), last if ($previous cmp $key) == 1 ; |
388 | } |
389 | |
f6b705ef |
390 | ok(67, $status == 1 ); |
391 | ok(68, $ok == 1 ); |
a0d0e21e |
392 | |
393 | # use seq to walk backwards through a file |
394 | $status = $X->seq($key, $value, R_LAST) ; |
f6b705ef |
395 | ok(69, $status == 0 ); |
a0d0e21e |
396 | $previous = $key ; |
397 | |
398 | $ok = 1 ; |
399 | while (($status = $X->seq($key, $value, R_PREV)) == 0) |
400 | { |
401 | ($ok = 0), last if ($previous cmp $key) == -1 ; |
402 | #print "key = [$key] value = [$value]\n" ; |
403 | } |
404 | |
f6b705ef |
405 | ok(70, $status == 1 ); |
406 | ok(71, $ok == 1 ); |
a0d0e21e |
407 | |
408 | |
409 | # check seq FIRST/LAST |
410 | |
411 | # sync |
412 | # #### |
413 | |
414 | $status = $X->sync ; |
f6b705ef |
415 | ok(72, $status == 0 ); |
a0d0e21e |
416 | |
417 | |
418 | # fd |
419 | # ## |
420 | |
421 | $status = $X->fd ; |
f6b705ef |
422 | ok(73, $status != 0 ); |
a0d0e21e |
423 | |
424 | |
425 | undef $X ; |
426 | untie %h ; |
427 | |
428 | unlink $Dfile; |
429 | |
430 | # Now try an in memory file |
3245f058 |
431 | my $Y; |
f6b705ef |
432 | ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE )); |
a0d0e21e |
433 | |
434 | # fd with an in memory file should return failure |
435 | $status = $Y->fd ; |
f6b705ef |
436 | ok(75, $status == -1 ); |
a0d0e21e |
437 | |
55d68b4a |
438 | |
a0d0e21e |
439 | undef $Y ; |
440 | untie %h ; |
441 | |
55d68b4a |
442 | # Duplicate keys |
443 | my $bt = new DB_File::BTREEINFO ; |
444 | $bt->{flags} = R_DUP ; |
3245f058 |
445 | my ($YY, %hh); |
f6b705ef |
446 | ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ; |
55d68b4a |
447 | |
448 | $hh{'Wall'} = 'Larry' ; |
449 | $hh{'Wall'} = 'Stone' ; # Note the duplicate key |
450 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key |
f6b705ef |
451 | $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value |
55d68b4a |
452 | $hh{'Smith'} = 'John' ; |
453 | $hh{'mouse'} = 'mickey' ; |
454 | |
455 | # first work in scalar context |
f6b705ef |
456 | ok(77, scalar $YY->get_dup('Unknown') == 0 ); |
457 | ok(78, scalar $YY->get_dup('Smith') == 1 ); |
458 | ok(79, scalar $YY->get_dup('Wall') == 4 ); |
55d68b4a |
459 | |
460 | # now in list context |
461 | my @unknown = $YY->get_dup('Unknown') ; |
f6b705ef |
462 | ok(80, "@unknown" eq "" ); |
55d68b4a |
463 | |
464 | my @smith = $YY->get_dup('Smith') ; |
f6b705ef |
465 | ok(81, "@smith" eq "John" ); |
55d68b4a |
466 | |
760ac839 |
467 | { |
f6b705ef |
468 | my @wall = $YY->get_dup('Wall') ; |
469 | my %wall ; |
470 | @wall{@wall} = @wall ; |
471 | ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) ); |
760ac839 |
472 | } |
55d68b4a |
473 | |
474 | # hash |
475 | my %unknown = $YY->get_dup('Unknown', 1) ; |
f6b705ef |
476 | ok(83, keys %unknown == 0 ); |
55d68b4a |
477 | |
478 | my %smith = $YY->get_dup('Smith', 1) ; |
f6b705ef |
479 | ok(84, keys %smith == 1 && $smith{'John'}) ; |
55d68b4a |
480 | |
f6b705ef |
481 | my %wall = $YY->get_dup('Wall', 1) ; |
482 | ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1 |
483 | && $wall{'Brick'} == 2); |
55d68b4a |
484 | |
485 | undef $YY ; |
486 | untie %hh ; |
487 | unlink $Dfile; |
488 | |
489 | |
8e07c86e |
490 | # test multiple callbacks |
3245f058 |
491 | my $Dfile1 = "btree1" ; |
492 | my $Dfile2 = "btree2" ; |
493 | my $Dfile3 = "btree3" ; |
8e07c86e |
494 | |
3245f058 |
495 | my $dbh1 = new DB_File::BTREEINFO ; |
496 | $dbh1->{compare} = sub { |
497 | no warnings 'numeric' ; |
498 | $_[0] <=> $_[1] } ; |
8e07c86e |
499 | |
3245f058 |
500 | my $dbh2 = new DB_File::BTREEINFO ; |
8e07c86e |
501 | $dbh2->{compare} = sub { $_[0] cmp $_[1] } ; |
502 | |
3245f058 |
503 | my $dbh3 = new DB_File::BTREEINFO ; |
8e07c86e |
504 | $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ; |
505 | |
506 | |
3245f058 |
507 | my (%g, %k); |
508 | tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ; |
f6b705ef |
509 | tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ; |
510 | tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ; |
8e07c86e |
511 | |
3245f058 |
512 | my @Keys = qw( 0123 12 -1234 9 987654321 def ) ; |
513 | my (@srt_1, @srt_2, @srt_3); |
514 | { |
515 | no warnings 'numeric' ; |
516 | @srt_1 = sort { $a <=> $b } @Keys ; |
517 | } |
8e07c86e |
518 | @srt_2 = sort { $a cmp $b } @Keys ; |
519 | @srt_3 = sort { length $a <=> length $b } @Keys ; |
520 | |
521 | foreach (@Keys) { |
3245f058 |
522 | $h{$_} = 1 ; |
8e07c86e |
523 | $g{$_} = 1 ; |
524 | $k{$_} = 1 ; |
525 | } |
526 | |
527 | sub ArrayCompare |
528 | { |
529 | my($a, $b) = @_ ; |
530 | |
531 | return 0 if @$a != @$b ; |
532 | |
533 | foreach (1 .. length @$a) |
534 | { |
535 | return 0 unless $$a[$_] eq $$b[$_] ; |
536 | } |
537 | |
538 | 1 ; |
539 | } |
540 | |
f6b705ef |
541 | ok(86, ArrayCompare (\@srt_1, [keys %h]) ); |
542 | ok(87, ArrayCompare (\@srt_2, [keys %g]) ); |
543 | ok(88, ArrayCompare (\@srt_3, [keys %k]) ); |
8e07c86e |
544 | |
545 | untie %h ; |
546 | untie %g ; |
547 | untie %k ; |
548 | unlink $Dfile1, $Dfile2, $Dfile3 ; |
549 | |
f6b705ef |
550 | # clear |
551 | # ##### |
552 | |
553 | ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
554 | foreach (1 .. 10) |
555 | { $h{$_} = $_ * 100 } |
556 | |
557 | # check that there are 10 elements in the hash |
558 | $i = 0 ; |
559 | while (($key,$value) = each(%h)) { |
560 | $i++; |
561 | } |
562 | ok(90, $i == 10); |
563 | |
564 | # now clear the hash |
565 | %h = () ; |
566 | |
567 | # check it is empty |
568 | $i = 0 ; |
569 | while (($key,$value) = each(%h)) { |
570 | $i++; |
571 | } |
572 | ok(91, $i == 0); |
573 | |
574 | untie %h ; |
575 | unlink $Dfile1 ; |
576 | |
05475680 |
577 | { |
578 | # check that attempting to tie an array to a DB_BTREE will fail |
579 | |
580 | my $filename = "xyz" ; |
581 | my @x ; |
582 | eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ; |
583 | ok(92, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ; |
584 | unlink $filename ; |
585 | } |
586 | |
a6ed719b |
587 | { |
588 | # sub-class test |
589 | |
590 | package Another ; |
591 | |
3245f058 |
592 | use warnings ; |
a6ed719b |
593 | use strict ; |
594 | |
595 | open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ; |
596 | print FILE <<'EOM' ; |
597 | |
598 | package SubDB ; |
599 | |
3245f058 |
600 | use warnings ; |
a6ed719b |
601 | use strict ; |
602 | use vars qw( @ISA @EXPORT) ; |
603 | |
604 | require Exporter ; |
605 | use DB_File; |
606 | @ISA=qw(DB_File); |
607 | @EXPORT = @DB_File::EXPORT ; |
608 | |
609 | sub STORE { |
610 | my $self = shift ; |
611 | my $key = shift ; |
612 | my $value = shift ; |
613 | $self->SUPER::STORE($key, $value * 2) ; |
614 | } |
615 | |
616 | sub FETCH { |
617 | my $self = shift ; |
618 | my $key = shift ; |
619 | $self->SUPER::FETCH($key) - 1 ; |
620 | } |
621 | |
622 | sub put { |
623 | my $self = shift ; |
624 | my $key = shift ; |
625 | my $value = shift ; |
626 | $self->SUPER::put($key, $value * 3) ; |
627 | } |
628 | |
629 | sub get { |
630 | my $self = shift ; |
631 | $self->SUPER::get($_[0], $_[1]) ; |
632 | $_[1] -= 2 ; |
633 | } |
634 | |
635 | sub A_new_method |
636 | { |
637 | my $self = shift ; |
638 | my $key = shift ; |
639 | my $value = $self->FETCH($key) ; |
640 | return "[[$value]]" ; |
641 | } |
642 | |
643 | 1 ; |
644 | EOM |
645 | |
646 | close FILE ; |
647 | |
a9fd575d |
648 | BEGIN { push @INC, '.'; } |
a6ed719b |
649 | eval 'use SubDB ; '; |
650 | main::ok(93, $@ eq "") ; |
651 | my %h ; |
652 | my $X ; |
653 | eval ' |
654 | $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE ); |
655 | ' ; |
656 | |
657 | main::ok(94, $@ eq "") ; |
658 | |
659 | my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ; |
660 | main::ok(95, $@ eq "") ; |
661 | main::ok(96, $ret == 5) ; |
662 | |
663 | my $value = 0; |
664 | $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ; |
665 | main::ok(97, $@ eq "") ; |
666 | main::ok(98, $ret == 10) ; |
667 | |
668 | $ret = eval ' R_NEXT eq main::R_NEXT ' ; |
669 | main::ok(99, $@ eq "" ) ; |
670 | main::ok(100, $ret == 1) ; |
671 | |
672 | $ret = eval '$X->A_new_method("joe") ' ; |
673 | main::ok(101, $@ eq "") ; |
674 | main::ok(102, $ret eq "[[11]]") ; |
675 | |
fac76ed7 |
676 | undef $X; |
677 | untie(%h); |
a6ed719b |
678 | unlink "SubDB.pm", "dbbtree.tmp" ; |
679 | |
680 | } |
681 | |
9fe6733a |
682 | { |
683 | # DBM Filter tests |
3245f058 |
684 | use warnings ; |
9fe6733a |
685 | use strict ; |
686 | my (%h, $db) ; |
687 | my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
688 | unlink $Dfile; |
689 | |
690 | sub checkOutput |
691 | { |
692 | my($fk, $sk, $fv, $sv) = @_ ; |
693 | return |
694 | $fetch_key eq $fk && $store_key eq $sk && |
695 | $fetch_value eq $fv && $store_value eq $sv && |
696 | $_ eq 'original' ; |
697 | } |
698 | |
699 | ok(103, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
700 | |
701 | $db->filter_fetch_key (sub { $fetch_key = $_ }) ; |
702 | $db->filter_store_key (sub { $store_key = $_ }) ; |
703 | $db->filter_fetch_value (sub { $fetch_value = $_}) ; |
704 | $db->filter_store_value (sub { $store_value = $_ }) ; |
705 | |
706 | $_ = "original" ; |
707 | |
708 | $h{"fred"} = "joe" ; |
709 | # fk sk fv sv |
710 | ok(104, checkOutput( "", "fred", "", "joe")) ; |
711 | |
712 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
713 | ok(105, $h{"fred"} eq "joe"); |
714 | # fk sk fv sv |
715 | ok(106, checkOutput( "", "fred", "joe", "")) ; |
716 | |
717 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
718 | ok(107, $db->FIRSTKEY() eq "fred") ; |
719 | # fk sk fv sv |
720 | ok(108, checkOutput( "fred", "", "", "")) ; |
721 | |
722 | # replace the filters, but remember the previous set |
723 | my ($old_fk) = $db->filter_fetch_key |
724 | (sub { $_ = uc $_ ; $fetch_key = $_ }) ; |
725 | my ($old_sk) = $db->filter_store_key |
726 | (sub { $_ = lc $_ ; $store_key = $_ }) ; |
727 | my ($old_fv) = $db->filter_fetch_value |
728 | (sub { $_ = "[$_]"; $fetch_value = $_ }) ; |
729 | my ($old_sv) = $db->filter_store_value |
730 | (sub { s/o/x/g; $store_value = $_ }) ; |
731 | |
732 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
733 | $h{"Fred"} = "Joe" ; |
734 | # fk sk fv sv |
735 | ok(109, checkOutput( "", "fred", "", "Jxe")) ; |
736 | |
737 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
738 | ok(110, $h{"Fred"} eq "[Jxe]"); |
739 | # fk sk fv sv |
740 | ok(111, checkOutput( "", "fred", "[Jxe]", "")) ; |
741 | |
742 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
743 | ok(112, $db->FIRSTKEY() eq "FRED") ; |
744 | # fk sk fv sv |
745 | ok(113, checkOutput( "FRED", "", "", "")) ; |
746 | |
747 | # put the original filters back |
748 | $db->filter_fetch_key ($old_fk); |
749 | $db->filter_store_key ($old_sk); |
750 | $db->filter_fetch_value ($old_fv); |
751 | $db->filter_store_value ($old_sv); |
752 | |
753 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
754 | $h{"fred"} = "joe" ; |
755 | ok(114, checkOutput( "", "fred", "", "joe")) ; |
756 | |
757 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
758 | ok(115, $h{"fred"} eq "joe"); |
759 | ok(116, checkOutput( "", "fred", "joe", "")) ; |
760 | |
761 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
762 | ok(117, $db->FIRSTKEY() eq "fred") ; |
763 | ok(118, checkOutput( "fred", "", "", "")) ; |
764 | |
765 | # delete the filters |
766 | $db->filter_fetch_key (undef); |
767 | $db->filter_store_key (undef); |
768 | $db->filter_fetch_value (undef); |
769 | $db->filter_store_value (undef); |
770 | |
771 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
772 | $h{"fred"} = "joe" ; |
773 | ok(119, checkOutput( "", "", "", "")) ; |
774 | |
775 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
776 | ok(120, $h{"fred"} eq "joe"); |
777 | ok(121, checkOutput( "", "", "", "")) ; |
778 | |
779 | ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; |
780 | ok(122, $db->FIRSTKEY() eq "fred") ; |
781 | ok(123, checkOutput( "", "", "", "")) ; |
782 | |
783 | undef $db ; |
784 | untie %h; |
785 | unlink $Dfile; |
786 | } |
787 | |
788 | { |
789 | # DBM Filter with a closure |
790 | |
3245f058 |
791 | use warnings ; |
9fe6733a |
792 | use strict ; |
793 | my (%h, $db) ; |
794 | |
795 | unlink $Dfile; |
796 | ok(124, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
797 | |
798 | my %result = () ; |
799 | |
800 | sub Closure |
801 | { |
802 | my ($name) = @_ ; |
803 | my $count = 0 ; |
804 | my @kept = () ; |
805 | |
806 | return sub { ++$count ; |
807 | push @kept, $_ ; |
808 | $result{$name} = "$name - $count: [@kept]" ; |
809 | } |
810 | } |
811 | |
812 | $db->filter_store_key(Closure("store key")) ; |
813 | $db->filter_store_value(Closure("store value")) ; |
814 | $db->filter_fetch_key(Closure("fetch key")) ; |
815 | $db->filter_fetch_value(Closure("fetch value")) ; |
816 | |
817 | $_ = "original" ; |
818 | |
819 | $h{"fred"} = "joe" ; |
820 | ok(125, $result{"store key"} eq "store key - 1: [fred]"); |
821 | ok(126, $result{"store value"} eq "store value - 1: [joe]"); |
822 | ok(127, ! defined $result{"fetch key"} ); |
823 | ok(128, ! defined $result{"fetch value"} ); |
824 | ok(129, $_ eq "original") ; |
825 | |
826 | ok(130, $db->FIRSTKEY() eq "fred") ; |
827 | ok(131, $result{"store key"} eq "store key - 1: [fred]"); |
828 | ok(132, $result{"store value"} eq "store value - 1: [joe]"); |
829 | ok(133, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
830 | ok(134, ! defined $result{"fetch value"} ); |
831 | ok(135, $_ eq "original") ; |
832 | |
833 | $h{"jim"} = "john" ; |
834 | ok(136, $result{"store key"} eq "store key - 2: [fred jim]"); |
835 | ok(137, $result{"store value"} eq "store value - 2: [joe john]"); |
836 | ok(138, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
837 | ok(139, ! defined $result{"fetch value"} ); |
838 | ok(140, $_ eq "original") ; |
839 | |
840 | ok(141, $h{"fred"} eq "joe"); |
841 | ok(142, $result{"store key"} eq "store key - 3: [fred jim fred]"); |
842 | ok(143, $result{"store value"} eq "store value - 2: [joe john]"); |
843 | ok(144, $result{"fetch key"} eq "fetch key - 1: [fred]"); |
844 | ok(145, $result{"fetch value"} eq "fetch value - 1: [joe]"); |
845 | ok(146, $_ eq "original") ; |
846 | |
847 | undef $db ; |
848 | untie %h; |
849 | unlink $Dfile; |
850 | } |
851 | |
852 | { |
853 | # DBM Filter recursion detection |
3245f058 |
854 | use warnings ; |
9fe6733a |
855 | use strict ; |
856 | my (%h, $db) ; |
857 | unlink $Dfile; |
858 | |
859 | ok(147, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
860 | |
861 | $db->filter_store_key (sub { $_ = $h{$_} }) ; |
862 | |
863 | eval '$h{1} = 1234' ; |
864 | ok(148, $@ =~ /^recursion detected in filter_store_key at/ ); |
865 | |
866 | undef $db ; |
867 | untie %h; |
868 | unlink $Dfile; |
869 | } |
870 | |
871 | |
2c2d71f5 |
872 | { |
873 | # Examples from the POD |
874 | |
875 | |
876 | my $file = "xyzt" ; |
877 | { |
878 | my $redirect = new Redirect $file ; |
879 | |
880 | # BTREE example 1 |
881 | ### |
882 | |
3245f058 |
883 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
884 | use strict ; |
885 | use DB_File ; |
886 | |
887 | my %h ; |
888 | |
889 | sub Compare |
890 | { |
891 | my ($key1, $key2) = @_ ; |
892 | "\L$key1" cmp "\L$key2" ; |
893 | } |
894 | |
895 | # specify the Perl sub that will do the comparison |
896 | $DB_BTREE->{'compare'} = \&Compare ; |
897 | |
898 | unlink "tree" ; |
899 | tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE |
900 | or die "Cannot open file 'tree': $!\n" ; |
901 | |
902 | # Add a key/value pair to the file |
903 | $h{'Wall'} = 'Larry' ; |
904 | $h{'Smith'} = 'John' ; |
905 | $h{'mouse'} = 'mickey' ; |
906 | $h{'duck'} = 'donald' ; |
907 | |
908 | # Delete |
909 | delete $h{"duck"} ; |
910 | |
911 | # Cycle through the keys printing them in order. |
912 | # Note it is not necessary to sort the keys as |
913 | # the btree will have kept them in order automatically. |
914 | foreach (keys %h) |
915 | { print "$_\n" } |
916 | |
917 | untie %h ; |
918 | |
919 | unlink "tree" ; |
920 | } |
921 | |
922 | delete $DB_BTREE->{'compare'} ; |
923 | |
924 | ok(149, docat_del($file) eq <<'EOM') ; |
925 | mouse |
926 | Smith |
927 | Wall |
928 | EOM |
929 | |
930 | { |
931 | my $redirect = new Redirect $file ; |
932 | |
933 | # BTREE example 2 |
934 | ### |
935 | |
3245f058 |
936 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
937 | use strict ; |
938 | use DB_File ; |
939 | |
940 | use vars qw($filename %h ) ; |
941 | |
942 | $filename = "tree" ; |
943 | unlink $filename ; |
944 | |
945 | # Enable duplicate records |
946 | $DB_BTREE->{'flags'} = R_DUP ; |
947 | |
948 | tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
949 | or die "Cannot open $filename: $!\n"; |
950 | |
951 | # Add some key/value pairs to the file |
952 | $h{'Wall'} = 'Larry' ; |
953 | $h{'Wall'} = 'Brick' ; # Note the duplicate key |
954 | $h{'Wall'} = 'Brick' ; # Note the duplicate key and value |
955 | $h{'Smith'} = 'John' ; |
956 | $h{'mouse'} = 'mickey' ; |
957 | |
958 | # iterate through the associative array |
959 | # and print each key/value pair. |
960 | foreach (keys %h) |
961 | { print "$_ -> $h{$_}\n" } |
962 | |
963 | untie %h ; |
964 | |
965 | unlink $filename ; |
966 | } |
967 | |
039d031f |
968 | ok(150, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ; |
2c2d71f5 |
969 | Smith -> John |
970 | Wall -> Brick |
971 | Wall -> Brick |
972 | Wall -> Brick |
973 | mouse -> mickey |
974 | EOM |
975 | Smith -> John |
976 | Wall -> Larry |
977 | Wall -> Larry |
978 | Wall -> Larry |
979 | mouse -> mickey |
980 | EOM |
981 | |
982 | { |
983 | my $redirect = new Redirect $file ; |
984 | |
985 | # BTREE example 3 |
986 | ### |
987 | |
3245f058 |
988 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
989 | use strict ; |
990 | use DB_File ; |
991 | |
992 | use vars qw($filename $x %h $status $key $value) ; |
993 | |
994 | $filename = "tree" ; |
995 | unlink $filename ; |
996 | |
997 | # Enable duplicate records |
998 | $DB_BTREE->{'flags'} = R_DUP ; |
999 | |
1000 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1001 | or die "Cannot open $filename: $!\n"; |
1002 | |
1003 | # Add some key/value pairs to the file |
1004 | $h{'Wall'} = 'Larry' ; |
1005 | $h{'Wall'} = 'Brick' ; # Note the duplicate key |
1006 | $h{'Wall'} = 'Brick' ; # Note the duplicate key and value |
1007 | $h{'Smith'} = 'John' ; |
1008 | $h{'mouse'} = 'mickey' ; |
1009 | |
1010 | # iterate through the btree using seq |
1011 | # and print each key/value pair. |
1012 | $key = $value = 0 ; |
1013 | for ($status = $x->seq($key, $value, R_FIRST) ; |
1014 | $status == 0 ; |
1015 | $status = $x->seq($key, $value, R_NEXT) ) |
1016 | { print "$key -> $value\n" } |
1017 | |
1018 | |
1019 | undef $x ; |
1020 | untie %h ; |
1021 | } |
1022 | |
039d031f |
1023 | ok(151, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ; |
2c2d71f5 |
1024 | Smith -> John |
1025 | Wall -> Brick |
1026 | Wall -> Brick |
1027 | Wall -> Larry |
1028 | mouse -> mickey |
1029 | EOM |
1030 | Smith -> John |
1031 | Wall -> Larry |
1032 | Wall -> Brick |
1033 | Wall -> Brick |
1034 | mouse -> mickey |
1035 | EOM |
1036 | |
1037 | |
1038 | { |
1039 | my $redirect = new Redirect $file ; |
1040 | |
1041 | # BTREE example 4 |
1042 | ### |
1043 | |
3245f058 |
1044 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1045 | use strict ; |
1046 | use DB_File ; |
1047 | |
1048 | use vars qw($filename $x %h ) ; |
1049 | |
1050 | $filename = "tree" ; |
1051 | |
1052 | # Enable duplicate records |
1053 | $DB_BTREE->{'flags'} = R_DUP ; |
1054 | |
1055 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1056 | or die "Cannot open $filename: $!\n"; |
1057 | |
1058 | my $cnt = $x->get_dup("Wall") ; |
1059 | print "Wall occurred $cnt times\n" ; |
1060 | |
1061 | my %hash = $x->get_dup("Wall", 1) ; |
1062 | print "Larry is there\n" if $hash{'Larry'} ; |
1063 | print "There are $hash{'Brick'} Brick Walls\n" ; |
1064 | |
1065 | my @list = sort $x->get_dup("Wall") ; |
1066 | print "Wall => [@list]\n" ; |
1067 | |
1068 | @list = $x->get_dup("Smith") ; |
1069 | print "Smith => [@list]\n" ; |
1070 | |
1071 | @list = $x->get_dup("Dog") ; |
1072 | print "Dog => [@list]\n" ; |
1073 | |
1074 | undef $x ; |
1075 | untie %h ; |
1076 | } |
1077 | |
1078 | ok(152, docat_del($file) eq <<'EOM') ; |
1079 | Wall occurred 3 times |
1080 | Larry is there |
1081 | There are 2 Brick Walls |
1082 | Wall => [Brick Brick Larry] |
1083 | Smith => [John] |
1084 | Dog => [] |
1085 | EOM |
1086 | |
1087 | { |
1088 | my $redirect = new Redirect $file ; |
1089 | |
1090 | # BTREE example 5 |
1091 | ### |
1092 | |
3245f058 |
1093 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1094 | use strict ; |
1095 | use DB_File ; |
1096 | |
1097 | use vars qw($filename $x %h $found) ; |
1098 | |
1099 | my $filename = "tree" ; |
1100 | |
1101 | # Enable duplicate records |
1102 | $DB_BTREE->{'flags'} = R_DUP ; |
1103 | |
1104 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1105 | or die "Cannot open $filename: $!\n"; |
1106 | |
1107 | $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ; |
1108 | print "Larry Wall is $found there\n" ; |
1109 | |
1110 | $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ; |
1111 | print "Harry Wall is $found there\n" ; |
1112 | |
1113 | undef $x ; |
1114 | untie %h ; |
1115 | } |
1116 | |
1117 | ok(153, docat_del($file) eq <<'EOM') ; |
1118 | Larry Wall is there |
1119 | Harry Wall is not there |
1120 | EOM |
1121 | |
1122 | { |
1123 | my $redirect = new Redirect $file ; |
1124 | |
1125 | # BTREE example 6 |
1126 | ### |
1127 | |
3245f058 |
1128 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1129 | use strict ; |
1130 | use DB_File ; |
1131 | |
1132 | use vars qw($filename $x %h $found) ; |
1133 | |
1134 | my $filename = "tree" ; |
1135 | |
1136 | # Enable duplicate records |
1137 | $DB_BTREE->{'flags'} = R_DUP ; |
1138 | |
1139 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1140 | or die "Cannot open $filename: $!\n"; |
1141 | |
1142 | $x->del_dup("Wall", "Larry") ; |
1143 | |
1144 | $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ; |
1145 | print "Larry Wall is $found there\n" ; |
1146 | |
1147 | undef $x ; |
1148 | untie %h ; |
1149 | |
1150 | unlink $filename ; |
1151 | } |
1152 | |
1153 | ok(154, docat_del($file) eq <<'EOM') ; |
1154 | Larry Wall is not there |
1155 | EOM |
1156 | |
1157 | { |
1158 | my $redirect = new Redirect $file ; |
1159 | |
1160 | # BTREE example 7 |
1161 | ### |
1162 | |
3245f058 |
1163 | use warnings FATAL => qw(all) ; |
2c2d71f5 |
1164 | use strict ; |
1165 | use DB_File ; |
1166 | use Fcntl ; |
1167 | |
1168 | use vars qw($filename $x %h $st $key $value) ; |
1169 | |
1170 | sub match |
1171 | { |
1172 | my $key = shift ; |
1173 | my $value = 0; |
1174 | my $orig_key = $key ; |
1175 | $x->seq($key, $value, R_CURSOR) ; |
1176 | print "$orig_key\t-> $key\t-> $value\n" ; |
1177 | } |
1178 | |
1179 | $filename = "tree" ; |
1180 | unlink $filename ; |
1181 | |
1182 | $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE |
1183 | or die "Cannot open $filename: $!\n"; |
1184 | |
1185 | # Add some key/value pairs to the file |
1186 | $h{'mouse'} = 'mickey' ; |
1187 | $h{'Wall'} = 'Larry' ; |
1188 | $h{'Walls'} = 'Brick' ; |
1189 | $h{'Smith'} = 'John' ; |
1190 | |
1191 | |
1192 | $key = $value = 0 ; |
1193 | print "IN ORDER\n" ; |
1194 | for ($st = $x->seq($key, $value, R_FIRST) ; |
1195 | $st == 0 ; |
1196 | $st = $x->seq($key, $value, R_NEXT) ) |
1197 | |
1198 | { print "$key -> $value\n" } |
1199 | |
1200 | print "\nPARTIAL MATCH\n" ; |
1201 | |
1202 | match "Wa" ; |
1203 | match "A" ; |
1204 | match "a" ; |
1205 | |
1206 | undef $x ; |
1207 | untie %h ; |
1208 | |
1209 | unlink $filename ; |
1210 | |
1211 | } |
1212 | |
1213 | ok(155, docat_del($file) eq <<'EOM') ; |
1214 | IN ORDER |
1215 | Smith -> John |
1216 | Wall -> Larry |
1217 | Walls -> Brick |
1218 | mouse -> mickey |
1219 | |
1220 | PARTIAL MATCH |
1221 | Wa -> Wall -> Larry |
1222 | A -> Smith -> John |
1223 | a -> mouse -> mickey |
1224 | EOM |
1225 | |
1226 | } |
1227 | |
a62982a8 |
1228 | #{ |
1229 | # # R_SETCURSOR |
1230 | # use strict ; |
1231 | # my (%h, $db) ; |
1232 | # unlink $Dfile; |
1233 | # |
1234 | # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) ); |
1235 | # |
1236 | # $h{abc} = 33 ; |
1237 | # my $k = "newest" ; |
1238 | # my $v = 44 ; |
1239 | # my $status = $db->put($k, $v, R_SETCURSOR) ; |
1240 | # print "status = [$status]\n" ; |
1241 | # ok(157, $status == 0) ; |
1242 | # $status = $db->del($k, R_CURSOR) ; |
1243 | # print "status = [$status]\n" ; |
1244 | # ok(158, $status == 0) ; |
1245 | # $k = "newest" ; |
1246 | # ok(159, $db->get($k, $v, R_CURSOR)) ; |
1247 | # |
1248 | # ok(160, keys %h == 1) ; |
1249 | # |
1250 | # undef $db ; |
1251 | # untie %h; |
1252 | # unlink $Dfile; |
1253 | #} |
1254 | |
cbc5248d |
1255 | { |
1256 | # Bug ID 20001013.009 |
1257 | # |
1258 | # test that $hash{KEY} = undef doesn't produce the warning |
1259 | # Use of uninitialized value in null operation |
1260 | use warnings ; |
1261 | use strict ; |
1262 | use DB_File ; |
1263 | |
1264 | unlink $Dfile; |
1265 | my %h ; |
1266 | my $a = ""; |
1267 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
1268 | |
3245f058 |
1269 | tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE |
cbc5248d |
1270 | or die "Can't open file: $!\n" ; |
1271 | $h{ABC} = undef; |
1272 | ok(156, $a eq "") ; |
1273 | untie %h ; |
1274 | unlink $Dfile; |
1275 | } |
1276 | |
3245f058 |
1277 | { |
1278 | # test that %hash = () doesn't produce the warning |
1279 | # Argument "" isn't numeric in entersub |
1280 | use warnings ; |
1281 | use strict ; |
1282 | use DB_File ; |
1283 | |
1284 | unlink $Dfile; |
1285 | my %h ; |
1286 | my $a = ""; |
1287 | local $SIG{__WARN__} = sub {$a = $_[0]} ; |
1288 | |
1289 | tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE |
1290 | or die "Can't open file: $!\n" ; |
1291 | %h = (); ; |
1292 | ok(157, $a eq "") ; |
1293 | untie %h ; |
1294 | unlink $Dfile; |
1295 | } |
1296 | |
a0d0e21e |
1297 | exit ; |