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