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