4 unless(grep /blib/, @INC) {
6 @INC = '../lib' if -d '../lib';
15 if(-d "lib" && -f "TEST") {
16 if ($Config{'extensions'} !~ /\bDB_File\b/ ) {
17 print "1..0 # Skip: DB_File was not built\n";
26 && (split(/\./, $Config{osvers}))[0] < 7 # Mac OS X 10.3 == Darwin 7
27 && $Config{db_version_major} == 1
28 && $Config{db_version_minor} == 0
29 && $Config{db_version_patch} == 0) {
32 # This test is known to crash in Mac OS X versions 10.2 (or earlier)
33 # because of the buggy Berkeley DB version included with the OS.
51 print "not " unless $result ;
57 my(@a) = unpack ("C*", $a) ;
58 my(@b) = unpack ("C*", $b) ;
60 my $len = (@a > @b ? @b : @a) ;
63 foreach $i ( 0 .. $len -1) {
64 return $a[$i] - $b[$i] if $a[$i] != $b[$i] ;
77 my $filename = shift ;
79 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
80 my $real_stdout = select($fh) ;
81 return bless [$fh, $real_stdout ] ;
96 open(CAT,$file) || die "Cannot open $file: $!";
99 $result = normalise($result) ;
106 my $result = docat($file);
122 my $hashref = shift ;
124 local $SIG{__WARN__} = sub {-- $no_inner } ;
131 my $db185mode = ($DB_File::db_version == 1 && ! $DB_File::db_185_compat) ;
132 my $null_keys_allowed = ($DB_File::db_ver < 2.004010
133 || $DB_File::db_ver >= 3.1 );
135 my $Dfile = "dbbtree.tmp";
140 # Check the interface to BTREEINFO
142 my $dbh = new DB_File::BTREEINFO ;
143 ok(1, ! defined $dbh->{flags}) ;
144 ok(2, ! defined $dbh->{cachesize}) ;
145 ok(3, ! defined $dbh->{psize}) ;
146 ok(4, ! defined $dbh->{lorder}) ;
147 ok(5, ! defined $dbh->{minkeypage}) ;
148 ok(6, ! defined $dbh->{maxkeypage}) ;
149 ok(7, ! defined $dbh->{compare}) ;
150 ok(8, ! defined $dbh->{prefix}) ;
152 $dbh->{flags} = 3000 ;
153 ok(9, $dbh->{flags} == 3000) ;
155 $dbh->{cachesize} = 9000 ;
156 ok(10, $dbh->{cachesize} == 9000);
158 $dbh->{psize} = 400 ;
159 ok(11, $dbh->{psize} == 400) ;
161 $dbh->{lorder} = 65 ;
162 ok(12, $dbh->{lorder} == 65) ;
164 $dbh->{minkeypage} = 123 ;
165 ok(13, $dbh->{minkeypage} == 123) ;
167 $dbh->{maxkeypage} = 1234 ;
168 ok(14, $dbh->{maxkeypage} == 1234 );
170 # Check that an invalid entry is caught both for store & fetch
171 eval '$dbh->{fred} = 1234' ;
172 ok(15, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
173 eval 'my $q = $dbh->{fred}' ;
174 ok(16, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
176 # Now check the interface to BTREE
179 ok(17, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
180 die "Could not tie: $!" unless $X;
182 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
183 $blksize,$blocks) = stat($Dfile);
185 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
187 ok(18, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
190 my ($key, $value, $i);
191 while (($key,$value) = each(%h)) {
196 $h{'goner1'} = 'snork';
199 ok(20, $h{'abc'} eq 'ABC' );
200 ok(21, ! defined $h{'jimmy'} ) ;
201 ok(22, ! exists $h{'jimmy'} ) ;
202 ok(23, defined $h{'abc'} ) ;
205 $h{'jkl','mno'} = "JKL\034MNO";
206 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
210 $X->STORE('b', 'B') ;
223 $h{'goner2'} = 'snork';
227 # IMPORTANT - $X must be undefined before the untie otherwise the
228 # underlying DB close routine will not get called.
232 # tie to the same file again
233 ok(24, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
235 # Modify an entry from the previous tie
256 $h{'goner3'} = 'snork';
259 $X->DELETE('goner3');
262 my @values = values(%h);
264 ok(25, $#keys == 29 && $#values == 29) ;
267 while (($key,$value) = each(%h)) {
268 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
270 $i++ if $key eq $value;
276 @keys = ('blurfl', keys(%h), 'dyick');
277 ok(27, $#keys == 31) ;
279 #Check that the keys can be retrieved in order
281 my @c = sort lexical @b ;
282 ok(28, ArrayCompare(\@b, \@c)) ;
285 ok(29, $h{'foo'} eq '' ) ;
287 # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
288 # This feature was reenabled in version 3.1 of Berkeley DB.
290 if ($null_keys_allowed) {
292 $result = ( $h{''} eq 'bar' );
298 # check cache overflow and numeric keys and contents
300 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
301 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
304 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
305 $blksize,$blocks) = stat($Dfile);
308 @h{0..200} = 200..400;
309 my @foo = @h{0..200};
310 ok(33, join(':',200..400) eq join(':',@foo) );
312 # Now check all the non-tie specific stuff
315 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
316 # an existing record.
318 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
319 ok(34, $status == 1 );
321 # check that the value of the key 'x' has not been changed by the
323 ok(35, $h{'x'} eq 'X' );
326 $status = $X->put('key', 'value') ;
327 ok(36, $status == 0 );
329 #check that previous put can be retrieved
331 $status = $X->get('key', $value) ;
332 ok(37, $status == 0 );
333 ok(38, $value eq 'value' );
335 # Attempting to delete an existing key should work
337 $status = $X->del('q') ;
338 ok(39, $status == 0 );
339 if ($null_keys_allowed) {
340 $status = $X->del('') ;
344 ok(40, $status == 0 );
346 # Make sure that the key deleted, cannot be retrieved
347 ok(41, ! defined $h{'q'}) ;
348 ok(42, ! defined $h{''}) ;
353 ok(43, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
355 # Attempting to delete a non-existant key should fail
357 $status = $X->del('joe') ;
358 ok(44, $status == 1 );
360 # Check the get interface
362 # First a non-existing key
363 $status = $X->get('aaaa', $value) ;
364 ok(45, $status == 1 );
366 # Next an existing key
367 $status = $X->get('a', $value) ;
368 ok(46, $status == 0 );
369 ok(47, $value eq 'A' );
374 # use seq to find an approximate match
377 $status = $X->seq($key, $value, R_CURSOR) ;
378 ok(48, $status == 0 );
379 ok(49, $key eq 'key' );
380 ok(50, $value eq 'value' );
382 # seq when the key does not match
385 $status = $X->seq($key, $value, R_CURSOR) ;
386 ok(51, $status == 1 );
389 # use seq to set the cursor, then delete the record @ the cursor.
393 $status = $X->seq($key, $value, R_CURSOR) ;
394 ok(52, $status == 0 );
395 ok(53, $key eq 'x' );
396 ok(54, $value eq 'X' );
397 $status = $X->del(0, R_CURSOR) ;
398 ok(55, $status == 0 );
399 $status = $X->get('x', $value) ;
400 ok(56, $status == 1 );
402 # ditto, but use put to replace the key/value pair.
405 $status = $X->seq($key, $value, R_CURSOR) ;
406 ok(57, $status == 0 );
407 ok(58, $key eq 'y' );
408 ok(59, $value eq 'Y' );
410 $key = "replace key" ;
411 $value = "replace value" ;
412 $status = $X->put($key, $value, R_CURSOR) ;
413 ok(60, $status == 0 );
414 ok(61, $key eq 'replace key' );
415 ok(62, $value eq 'replace value' );
416 $status = $X->get('y', $value) ;
417 ok(63, 1) ; # hard-wire to always pass. the previous test ($status == 1)
418 # only worked because of a bug in 1.85/6
420 # use seq to walk forwards through a file
422 $status = $X->seq($key, $value, R_FIRST) ;
423 ok(64, $status == 0 );
424 my $previous = $key ;
427 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
429 ($ok = 0), last if ($previous cmp $key) == 1 ;
432 ok(65, $status == 1 );
435 # use seq to walk backwards through a file
436 $status = $X->seq($key, $value, R_LAST) ;
437 ok(67, $status == 0 );
441 while (($status = $X->seq($key, $value, R_PREV)) == 0)
443 ($ok = 0), last if ($previous cmp $key) == -1 ;
444 #print "key = [$key] value = [$value]\n" ;
447 ok(68, $status == 1 );
451 # check seq FIRST/LAST
457 ok(70, $status == 0 );
465 #ok(71, $status != 0 );
473 # Now try an in memory file
475 ok(72, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
477 # fd with an in memory file should return failure
479 ok(73, $status == -1 );
486 my $bt = new DB_File::BTREEINFO ;
487 $bt->{flags} = R_DUP ;
489 ok(74, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
491 $hh{'Wall'} = 'Larry' ;
492 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
493 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
494 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
495 $hh{'Smith'} = 'John' ;
496 $hh{'mouse'} = 'mickey' ;
498 # first work in scalar context
499 ok(75, scalar $YY->get_dup('Unknown') == 0 );
500 ok(76, scalar $YY->get_dup('Smith') == 1 );
501 ok(77, scalar $YY->get_dup('Wall') == 4 );
503 # now in list context
504 my @unknown = $YY->get_dup('Unknown') ;
505 ok(78, "@unknown" eq "" );
507 my @smith = $YY->get_dup('Smith') ;
508 ok(79, "@smith" eq "John" );
511 my @wall = $YY->get_dup('Wall') ;
513 @wall{@wall} = @wall ;
514 ok(80, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
518 my %unknown = $YY->get_dup('Unknown', 1) ;
519 ok(81, keys %unknown == 0 );
521 my %smith = $YY->get_dup('Smith', 1) ;
522 ok(82, keys %smith == 1 && $smith{'John'}) ;
524 my %wall = $YY->get_dup('Wall', 1) ;
525 ok(83, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
526 && $wall{'Brick'} == 2);
533 # test multiple callbacks
534 my $Dfile1 = "btree1" ;
535 my $Dfile2 = "btree2" ;
536 my $Dfile3 = "btree3" ;
538 my $dbh1 = new DB_File::BTREEINFO ;
539 $dbh1->{compare} = sub {
540 no warnings 'numeric' ;
543 my $dbh2 = new DB_File::BTREEINFO ;
544 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
546 my $dbh3 = new DB_File::BTREEINFO ;
547 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
551 tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) or die $!;
552 tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) or die $!;
553 tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) or die $!;
555 my @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
556 my (@srt_1, @srt_2, @srt_3);
558 no warnings 'numeric' ;
559 @srt_1 = sort { $a <=> $b } @Keys ;
561 @srt_2 = sort { $a cmp $b } @Keys ;
562 @srt_3 = sort { length $a <=> length $b } @Keys ;
574 return 0 if @$a != @$b ;
576 foreach (1 .. length @$a)
578 return 0 unless $$a[$_] eq $$b[$_] ;
584 ok(84, ArrayCompare (\@srt_1, [keys %h]) );
585 ok(85, ArrayCompare (\@srt_2, [keys %g]) );
586 ok(86, ArrayCompare (\@srt_3, [keys %k]) );
591 unlink $Dfile1, $Dfile2, $Dfile3 ;
596 ok(87, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
598 { $h{$_} = $_ * 100 }
600 # check that there are 10 elements in the hash
602 while (($key,$value) = each(%h)) {
612 while (($key,$value) = each(%h)) {
621 # check that attempting to tie an array to a DB_BTREE will fail
623 my $filename = "xyz" ;
625 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ;
626 ok(90, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ;
638 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
650 @EXPORT = @DB_File::EXPORT ;
656 $self->SUPER::STORE($key, $value * 2) ;
662 $self->SUPER::FETCH($key) - 1 ;
669 $self->SUPER::put($key, $value * 3) ;
674 $self->SUPER::get($_[0], $_[1]) ;
682 my $value = $self->FETCH($key) ;
683 return "[[$value]]" ;
691 BEGIN { push @INC, '.'; }
693 main::ok(91, $@ eq "") ;
697 $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE );
700 main::ok(92, $@ eq "") ;
702 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
703 main::ok(93, $@ eq "") ;
704 main::ok(94, $ret == 5) ;
707 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
708 main::ok(95, $@ eq "") ;
709 main::ok(96, $ret == 10) ;
711 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
712 main::ok(97, $@ eq "" ) ;
713 main::ok(98, $ret == 1) ;
715 $ret = eval '$X->A_new_method("joe") ' ;
716 main::ok(99, $@ eq "") ;
717 main::ok(100, $ret eq "[[11]]") ;
721 unlink "SubDB.pm", "dbbtree.tmp" ;
730 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
735 my($fk, $sk, $fv, $sv) = @_ ;
737 $fetch_key eq $fk && $store_key eq $sk &&
738 $fetch_value eq $fv && $store_value eq $sv &&
742 ok(101, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
744 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
745 $db->filter_store_key (sub { $store_key = $_ }) ;
746 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
747 $db->filter_store_value (sub { $store_value = $_ }) ;
753 ok(102, checkOutput( "", "fred", "", "joe")) ;
755 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
756 ok(103, $h{"fred"} eq "joe");
758 ok(104, checkOutput( "", "fred", "joe", "")) ;
760 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
761 ok(105, $db->FIRSTKEY() eq "fred") ;
763 ok(106, checkOutput( "fred", "", "", "")) ;
765 # replace the filters, but remember the previous set
766 my ($old_fk) = $db->filter_fetch_key
767 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
768 my ($old_sk) = $db->filter_store_key
769 (sub { $_ = lc $_ ; $store_key = $_ }) ;
770 my ($old_fv) = $db->filter_fetch_value
771 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
772 my ($old_sv) = $db->filter_store_value
773 (sub { s/o/x/g; $store_value = $_ }) ;
775 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
778 ok(107, checkOutput( "", "fred", "", "Jxe")) ;
780 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
781 ok(108, $h{"Fred"} eq "[Jxe]");
783 ok(109, checkOutput( "", "fred", "[Jxe]", "")) ;
785 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
786 ok(110, $db->FIRSTKEY() eq "FRED") ;
788 ok(111, checkOutput( "FRED", "", "", "")) ;
790 # put the original filters back
791 $db->filter_fetch_key ($old_fk);
792 $db->filter_store_key ($old_sk);
793 $db->filter_fetch_value ($old_fv);
794 $db->filter_store_value ($old_sv);
796 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
798 ok(112, checkOutput( "", "fred", "", "joe")) ;
800 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
801 ok(113, $h{"fred"} eq "joe");
802 ok(114, checkOutput( "", "fred", "joe", "")) ;
804 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
805 ok(115, $db->FIRSTKEY() eq "fred") ;
806 ok(116, checkOutput( "fred", "", "", "")) ;
809 $db->filter_fetch_key (undef);
810 $db->filter_store_key (undef);
811 $db->filter_fetch_value (undef);
812 $db->filter_store_value (undef);
814 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
816 ok(117, checkOutput( "", "", "", "")) ;
818 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
819 ok(118, $h{"fred"} eq "joe");
820 ok(119, checkOutput( "", "", "", "")) ;
822 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
823 ok(120, $db->FIRSTKEY() eq "fred") ;
824 ok(121, checkOutput( "", "", "", "")) ;
832 # DBM Filter with a closure
839 ok(122, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
849 return sub { ++$count ;
851 $result{$name} = "$name - $count: [@kept]" ;
855 $db->filter_store_key(Closure("store key")) ;
856 $db->filter_store_value(Closure("store value")) ;
857 $db->filter_fetch_key(Closure("fetch key")) ;
858 $db->filter_fetch_value(Closure("fetch value")) ;
863 ok(123, $result{"store key"} eq "store key - 1: [fred]");
864 ok(124, $result{"store value"} eq "store value - 1: [joe]");
865 ok(125, ! defined $result{"fetch key"} );
866 ok(126, ! defined $result{"fetch value"} );
867 ok(127, $_ eq "original") ;
869 ok(128, $db->FIRSTKEY() eq "fred") ;
870 ok(129, $result{"store key"} eq "store key - 1: [fred]");
871 ok(130, $result{"store value"} eq "store value - 1: [joe]");
872 ok(131, $result{"fetch key"} eq "fetch key - 1: [fred]");
873 ok(132, ! defined $result{"fetch value"} );
874 ok(133, $_ eq "original") ;
877 ok(134, $result{"store key"} eq "store key - 2: [fred jim]");
878 ok(135, $result{"store value"} eq "store value - 2: [joe john]");
879 ok(136, $result{"fetch key"} eq "fetch key - 1: [fred]");
880 ok(137, ! defined $result{"fetch value"} );
881 ok(138, $_ eq "original") ;
883 ok(139, $h{"fred"} eq "joe");
884 ok(140, $result{"store key"} eq "store key - 3: [fred jim fred]");
885 ok(141, $result{"store value"} eq "store value - 2: [joe john]");
886 ok(142, $result{"fetch key"} eq "fetch key - 1: [fred]");
887 ok(143, $result{"fetch value"} eq "fetch value - 1: [joe]");
888 ok(144, $_ eq "original") ;
896 # DBM Filter recursion detection
902 ok(145, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
904 $db->filter_store_key (sub { $_ = $h{$_} }) ;
906 eval '$h{1} = 1234' ;
907 ok(146, $@ =~ /^recursion detected in filter_store_key at/ );
916 # Examples from the POD
921 my $redirect = new Redirect $file ;
926 use warnings FATAL => qw(all) ;
934 my ($key1, $key2) = @_ ;
935 "\L$key1" cmp "\L$key2" ;
938 # specify the Perl sub that will do the comparison
939 $DB_BTREE->{'compare'} = \&Compare ;
942 tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
943 or die "Cannot open file 'tree': $!\n" ;
945 # Add a key/value pair to the file
946 $h{'Wall'} = 'Larry' ;
947 $h{'Smith'} = 'John' ;
948 $h{'mouse'} = 'mickey' ;
949 $h{'duck'} = 'donald' ;
954 # Cycle through the keys printing them in order.
955 # Note it is not necessary to sort the keys as
956 # the btree will have kept them in order automatically.
965 delete $DB_BTREE->{'compare'} ;
967 ok(147, docat_del($file) eq <<'EOM') ;
974 my $redirect = new Redirect $file ;
979 use warnings FATAL => qw(all) ;
988 # Enable duplicate records
989 $DB_BTREE->{'flags'} = R_DUP ;
991 tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
992 or die "Cannot open $filename: $!\n";
994 # Add some key/value pairs to the file
995 $h{'Wall'} = 'Larry' ;
996 $h{'Wall'} = 'Brick' ; # Note the duplicate key
997 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
998 $h{'Smith'} = 'John' ;
999 $h{'mouse'} = 'mickey' ;
1001 # iterate through the associative array
1002 # and print each key/value pair.
1004 { print "$_ -> $h{$_}\n" }
1011 ok(148, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ;
1026 my $redirect = new Redirect $file ;
1031 use warnings FATAL => qw(all) ;
1035 my ($filename, $x, %h, $status, $key, $value);
1037 $filename = "tree" ;
1040 # Enable duplicate records
1041 $DB_BTREE->{'flags'} = R_DUP ;
1043 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1044 or die "Cannot open $filename: $!\n";
1046 # Add some key/value pairs to the file
1047 $h{'Wall'} = 'Larry' ;
1048 $h{'Wall'} = 'Brick' ; # Note the duplicate key
1049 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
1050 $h{'Smith'} = 'John' ;
1051 $h{'mouse'} = 'mickey' ;
1053 # iterate through the btree using seq
1054 # and print each key/value pair.
1056 for ($status = $x->seq($key, $value, R_FIRST) ;
1058 $status = $x->seq($key, $value, R_NEXT) )
1059 { print "$key -> $value\n" }
1066 ok(149, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ;
1082 my $redirect = new Redirect $file ;
1087 use warnings FATAL => qw(all) ;
1091 my ($filename, $x, %h);
1093 $filename = "tree" ;
1095 # Enable duplicate records
1096 $DB_BTREE->{'flags'} = R_DUP ;
1098 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1099 or die "Cannot open $filename: $!\n";
1101 my $cnt = $x->get_dup("Wall") ;
1102 print "Wall occurred $cnt times\n" ;
1104 my %hash = $x->get_dup("Wall", 1) ;
1105 print "Larry is there\n" if $hash{'Larry'} ;
1106 print "There are $hash{'Brick'} Brick Walls\n" ;
1108 my @list = sort $x->get_dup("Wall") ;
1109 print "Wall => [@list]\n" ;
1111 @list = $x->get_dup("Smith") ;
1112 print "Smith => [@list]\n" ;
1114 @list = $x->get_dup("Dog") ;
1115 print "Dog => [@list]\n" ;
1121 ok(150, docat_del($file) eq <<'EOM') ;
1122 Wall occurred 3 times
1124 There are 2 Brick Walls
1125 Wall => [Brick Brick Larry]
1131 my $redirect = new Redirect $file ;
1136 use warnings FATAL => qw(all) ;
1140 my ($filename, $x, %h, $found);
1142 $filename = "tree" ;
1144 # Enable duplicate records
1145 $DB_BTREE->{'flags'} = R_DUP ;
1147 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1148 or die "Cannot open $filename: $!\n";
1150 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1151 print "Larry Wall is $found there\n" ;
1153 $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
1154 print "Harry Wall is $found there\n" ;
1160 ok(151, docat_del($file) eq <<'EOM') ;
1162 Harry Wall is not there
1166 my $redirect = new Redirect $file ;
1171 use warnings FATAL => qw(all) ;
1175 my ($filename, $x, %h, $found);
1177 $filename = "tree" ;
1179 # Enable duplicate records
1180 $DB_BTREE->{'flags'} = R_DUP ;
1182 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1183 or die "Cannot open $filename: $!\n";
1185 $x->del_dup("Wall", "Larry") ;
1187 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1188 print "Larry Wall is $found there\n" ;
1196 ok(152, docat_del($file) eq <<'EOM') ;
1197 Larry Wall is not there
1201 my $redirect = new Redirect $file ;
1206 use warnings FATAL => qw(all) ;
1211 my ($filename, $x, %h, $st, $key, $value);
1217 my $orig_key = $key ;
1218 $x->seq($key, $value, R_CURSOR) ;
1219 print "$orig_key\t-> $key\t-> $value\n" ;
1222 $filename = "tree" ;
1225 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1226 or die "Cannot open $filename: $!\n";
1228 # Add some key/value pairs to the file
1229 $h{'mouse'} = 'mickey' ;
1230 $h{'Wall'} = 'Larry' ;
1231 $h{'Walls'} = 'Brick' ;
1232 $h{'Smith'} = 'John' ;
1236 print "IN ORDER\n" ;
1237 for ($st = $x->seq($key, $value, R_FIRST) ;
1239 $st = $x->seq($key, $value, R_NEXT) )
1241 { print "$key -> $value\n" }
1243 print "\nPARTIAL MATCH\n" ;
1256 ok(153, docat_del($file) eq <<'EOM') ;
1266 a -> mouse -> mickey
1277 # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1280 # my $k = "newest" ;
1282 # my $status = $db->put($k, $v, R_SETCURSOR) ;
1283 # print "status = [$status]\n" ;
1284 # ok(157, $status == 0) ;
1285 # $status = $db->del($k, R_CURSOR) ;
1286 # print "status = [$status]\n" ;
1287 # ok(158, $status == 0) ;
1289 # ok(159, $db->get($k, $v, R_CURSOR)) ;
1291 # ok(160, keys %h == 1) ;
1299 # Bug ID 20001013.009
1301 # test that $hash{KEY} = undef doesn't produce the warning
1302 # Use of uninitialized value in null operation
1310 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1312 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1313 or die "Can't open file: $!\n" ;
1321 # test that %hash = () doesn't produce the warning
1322 # Argument "" isn't numeric in entersub
1330 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1332 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1333 or die "Can't open file: $!\n" ;
1341 # When iterating over a tied hash using "each", the key passed to FETCH
1342 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
1343 # key in FETCH via a filter_fetch_key method we need to check that the
1344 # modified key doesn't get passed to NEXTKEY.
1345 # Also Test "keys" & "values" while we are at it.
1355 ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1356 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
1357 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
1359 $h{'Alpha_ABC'} = 2 ;
1360 $h{'Alpha_DEF'} = 5 ;
1362 ok(157, $h{'Alpha_ABC'} == 2);
1363 ok(158, $h{'Alpha_DEF'} == 5);
1365 my ($k, $v) = ("","");
1366 while (($k, $v) = each %h) {}
1367 ok(159, $bad_key == 0);
1370 foreach $k (keys %h) {}
1371 ok(160, $bad_key == 0);
1374 foreach $v (values %h) {}
1375 ok(161, $bad_key == 0);
1383 # now an error to pass 'compare' a non-code reference
1384 my $dbh = new DB_File::BTREEINFO ;
1386 eval { $dbh->{compare} = 2 };
1387 ok(162, $@ =~ /^Key 'compare' not associated with a code reference at/);
1389 eval { $dbh->{prefix} = 2 };
1390 ok(163, $@ =~ /^Key 'prefix' not associated with a code reference at/);
1396 # # recursion detection in btree
1399 # my $dbh = new DB_File::BTREEINFO ;
1400 # $dbh->{compare} = sub { $hash{3} = 4 ; length $_[0] } ;
1404 # ok(164, tie(%hash, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh ) );
1406 # eval { $hash{1} = 2;
1410 # ok(165, $@ =~ /^DB_File btree_compare: recursion detected/);
1421 # Check that two callbacks don't interact
1426 unlink $Dfile, $Dfile2;
1427 my $dbh1 = new DB_File::BTREEINFO ;
1428 $dbh1->{compare} = sub { ++ $h1_count ; $_[0] cmp $_[1] } ;
1430 my $dbh2 = new DB_File::BTREEINFO ;
1431 $dbh2->{compare} = sub { ;++ $h2_count ; $_[0] cmp $_[1] } ;
1436 ok(166, tie(%hash1, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh1 ) );
1437 ok(167, tie(%hash2, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) );
1447 ok(168, $h1_count > 0);
1448 ok(169, $h1_count == $h2_count);
1450 ok(170, safeUntie \%hash1);
1451 ok(171, safeUntie \%hash2);
1452 unlink $Dfile, $Dfile2;
1456 # Check that DBM Filter can cope with read-only $_
1463 ok(172, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1465 $db->filter_fetch_key (sub { }) ;
1466 $db->filter_store_key (sub { }) ;
1467 $db->filter_fetch_value (sub { }) ;
1468 $db->filter_store_value (sub { }) ;
1472 $h{"fred"} = "joe" ;
1473 ok(173, $h{"fred"} eq "joe");
1475 eval { my @r= grep { $h{$_} } (1, 2, 3) };
1479 # delete the filters
1480 $db->filter_fetch_key (undef);
1481 $db->filter_store_key (undef);
1482 $db->filter_fetch_value (undef);
1483 $db->filter_store_value (undef);
1485 $h{"fred"} = "joe" ;
1487 ok(175, $h{"fred"} eq "joe");
1489 ok(176, $db->FIRSTKEY() eq "fred") ;
1491 eval { my @r= grep { $h{$_} } (1, 2, 3) };
1500 # Check low-level API works with filter
1505 my $Dfile = "xxy.db";
1508 ok(178, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1511 $db->filter_fetch_key (sub { $_ = unpack("i", $_) } );
1512 $db->filter_store_key (sub { $_ = pack("i", $_) } );
1513 $db->filter_fetch_value (sub { $_ = unpack("i", $_) } );
1514 $db->filter_store_value (sub { $_ = pack("i", $_) } );
1521 $db->put($key, $value) ;
1523 ok 180, $value == 34 ;
1524 ok 181, $_ eq 'fred';
1525 #print "k [$key][$value]\n" ;
1528 $db->get($key, $val) ;
1530 ok 183, $val == 34 ;
1531 ok 184, $_ eq 'fred';
1537 ok 186, $value == 454 ;
1538 ok 187, $_ eq 'fred';
1548 # Regression Test for bug 30237
1549 # Check that substr can be used in the key to db_put
1550 # and that db_put does not trigger the warning
1552 # Use of uninitialized value in subroutine entry
1558 my $Dfile = "xxy.db";
1561 ok(188, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
1564 local $SIG{__WARN__} = sub {$warned = $_[0]} ;
1566 # db-put with substr of key
1568 for my $ix ( 10 .. 12 )
1570 my $key = $ix . "data" ;
1571 my $value = "value$ix" ;
1572 $remember{$key} = $value ;
1573 $db->put(substr($key,0), $value) ;
1576 ok 189, $warned eq ''
1577 or print "# Caught warning [$warned]\n" ;
1579 # db-put with substr of value
1581 for my $ix ( 20 .. 22 )
1583 my $key = $ix . "data" ;
1584 my $value = "value$ix" ;
1585 $remember{$key} = $value ;
1586 $db->put($key, substr($value,0)) ;
1589 ok 190, $warned eq ''
1590 or print "# Caught warning [$warned]\n" ;
1592 # via the tied hash is not a problem, but check anyway
1595 for my $ix ( 30 .. 32 )
1597 my $key = $ix . "data" ;
1598 my $value = "value$ix" ;
1599 $remember{$key} = $value ;
1600 $h{substr($key,0)} = $value ;
1603 ok 191, $warned eq ''
1604 or print "# Caught warning [$warned]\n" ;
1606 # via the tied hash is not a problem, but check anyway
1609 for my $ix ( 40 .. 42 )
1611 my $key = $ix . "data" ;
1612 my $value = "value$ix" ;
1613 $remember{$key} = $value ;
1614 $h{$key} = substr($value,0) ;
1617 ok 192, $warned eq ''
1618 or print "# Caught warning [$warned]\n" ;
1622 for ($status = $db->seq($key, $value, R_FIRST ) ;
1624 $status = $db->seq($key, $value, R_NEXT ) ) {
1626 #print "# key [$key] value [$value]\n" ;
1627 if (defined $remember{$key} && defined $value &&
1628 $remember{$key} eq $value) {
1629 delete $remember{$key} ;
1632 $bad{$key} = $value ;
1636 ok 193, keys %bad == 0 ;
1637 ok 194, keys %remember == 0 ;
1639 print "# missing -- $key $value\n" while ($key, $value) = each %remember;
1640 print "# bad -- $key $value\n" while ($key, $value) = each %bad;
1642 # Make sure this fix does not break code to handle an undef key
1643 # Berkeley DB undef key is bron between versions 2.3.16 and
1646 $db->put(undef, $value) ;
1647 ok 195, $warned eq ''
1648 or print "# Caught warning [$warned]\n" ;
1651 my $no_NULL = ($DB_File::db_ver >= 2.003016 && $DB_File::db_ver < 3.001) ;
1652 print "# db_ver $DB_File::db_ver\n";
1654 $db->get(undef, $value) ;
1655 ok 196, $no_NULL || $value eq 'fred' or print "# got [$value]\n" ;
1656 ok 197, $warned eq ''
1657 or print "# Caught warning [$warned]\n" ;