8 if(-d "lib" && -f "TEST") {
9 if ($Config{'extensions'} !~ /\bDB_File\b/ ) {
10 print "1..0 # Skip: DB_File was not built\n";
19 && (split(/\./, $Config{osvers}))[0] < 7 # Mac OS X 10.3 == Darwin 7
20 && $Config{db_version_major} == 1
21 && $Config{db_version_minor} == 0
22 && $Config{db_version_patch} == 0) {
25 # This test is known to crash in Mac OS X versions 10.2 (or earlier)
26 # because of the buggy Berkeley DB version included with the OS.
44 print "not " unless $result ;
50 my(@a) = unpack ("C*", $a) ;
51 my(@b) = unpack ("C*", $b) ;
53 my $len = (@a > @b ? @b : @a) ;
56 foreach $i ( 0 .. $len -1) {
57 return $a[$i] - $b[$i] if $a[$i] != $b[$i] ;
70 my $filename = shift ;
72 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
73 my $real_stdout = select($fh) ;
74 return bless [$fh, $real_stdout ] ;
89 open(CAT,$file) || die "Cannot open $file: $!";
92 $result = normalise($result) ;
99 my $result = docat($file);
115 my $hashref = shift ;
117 local $SIG{__WARN__} = sub {-- $no_inner } ;
124 my $db185mode = ($DB_File::db_version == 1 && ! $DB_File::db_185_compat) ;
125 my $null_keys_allowed = ($DB_File::db_ver < 2.004010
126 || $DB_File::db_ver >= 3.1 );
128 my $Dfile = "dbbtree.tmp";
133 # Check the interface to BTREEINFO
135 my $dbh = new DB_File::BTREEINFO ;
136 ok(1, ! defined $dbh->{flags}) ;
137 ok(2, ! defined $dbh->{cachesize}) ;
138 ok(3, ! defined $dbh->{psize}) ;
139 ok(4, ! defined $dbh->{lorder}) ;
140 ok(5, ! defined $dbh->{minkeypage}) ;
141 ok(6, ! defined $dbh->{maxkeypage}) ;
142 ok(7, ! defined $dbh->{compare}) ;
143 ok(8, ! defined $dbh->{prefix}) ;
145 $dbh->{flags} = 3000 ;
146 ok(9, $dbh->{flags} == 3000) ;
148 $dbh->{cachesize} = 9000 ;
149 ok(10, $dbh->{cachesize} == 9000);
151 $dbh->{psize} = 400 ;
152 ok(11, $dbh->{psize} == 400) ;
154 $dbh->{lorder} = 65 ;
155 ok(12, $dbh->{lorder} == 65) ;
157 $dbh->{minkeypage} = 123 ;
158 ok(13, $dbh->{minkeypage} == 123) ;
160 $dbh->{maxkeypage} = 1234 ;
161 ok(14, $dbh->{maxkeypage} == 1234 );
163 # Check that an invalid entry is caught both for store & fetch
164 eval '$dbh->{fred} = 1234' ;
165 ok(15, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
166 eval 'my $q = $dbh->{fred}' ;
167 ok(16, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
169 # Now check the interface to BTREE
172 ok(17, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
173 die "Could not tie: $!" unless $X;
175 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
176 $blksize,$blocks) = stat($Dfile);
178 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
180 ok(18, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
183 my ($key, $value, $i);
184 while (($key,$value) = each(%h)) {
189 $h{'goner1'} = 'snork';
192 ok(20, $h{'abc'} eq 'ABC' );
193 ok(21, ! defined $h{'jimmy'} ) ;
194 ok(22, ! exists $h{'jimmy'} ) ;
195 ok(23, defined $h{'abc'} ) ;
198 $h{'jkl','mno'} = "JKL\034MNO";
199 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
203 $X->STORE('b', 'B') ;
216 $h{'goner2'} = 'snork';
220 # IMPORTANT - $X must be undefined before the untie otherwise the
221 # underlying DB close routine will not get called.
225 # tie to the same file again
226 ok(24, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
228 # Modify an entry from the previous tie
249 $h{'goner3'} = 'snork';
252 $X->DELETE('goner3');
255 my @values = values(%h);
257 ok(25, $#keys == 29 && $#values == 29) ;
260 while (($key,$value) = each(%h)) {
261 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
263 $i++ if $key eq $value;
269 @keys = ('blurfl', keys(%h), 'dyick');
270 ok(27, $#keys == 31) ;
272 #Check that the keys can be retrieved in order
274 my @c = sort lexical @b ;
275 ok(28, ArrayCompare(\@b, \@c)) ;
278 ok(29, $h{'foo'} eq '' ) ;
280 # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
281 # This feature was reenabled in version 3.1 of Berkeley DB.
283 if ($null_keys_allowed) {
285 $result = ( $h{''} eq 'bar' );
291 # check cache overflow and numeric keys and contents
293 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
294 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
297 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
298 $blksize,$blocks) = stat($Dfile);
301 @h{0..200} = 200..400;
302 my @foo = @h{0..200};
303 ok(33, join(':',200..400) eq join(':',@foo) );
305 # Now check all the non-tie specific stuff
308 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
309 # an existing record.
311 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
312 ok(34, $status == 1 );
314 # check that the value of the key 'x' has not been changed by the
316 ok(35, $h{'x'} eq 'X' );
319 $status = $X->put('key', 'value') ;
320 ok(36, $status == 0 );
322 #check that previous put can be retrieved
324 $status = $X->get('key', $value) ;
325 ok(37, $status == 0 );
326 ok(38, $value eq 'value' );
328 # Attempting to delete an existing key should work
330 $status = $X->del('q') ;
331 ok(39, $status == 0 );
332 if ($null_keys_allowed) {
333 $status = $X->del('') ;
337 ok(40, $status == 0 );
339 # Make sure that the key deleted, cannot be retrieved
340 ok(41, ! defined $h{'q'}) ;
341 ok(42, ! defined $h{''}) ;
346 ok(43, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
348 # Attempting to delete a non-existent key should fail
350 $status = $X->del('joe') ;
351 ok(44, $status == 1 );
353 # Check the get interface
355 # First a non-existing key
356 $status = $X->get('aaaa', $value) ;
357 ok(45, $status == 1 );
359 # Next an existing key
360 $status = $X->get('a', $value) ;
361 ok(46, $status == 0 );
362 ok(47, $value eq 'A' );
367 # use seq to find an approximate match
370 $status = $X->seq($key, $value, R_CURSOR) ;
371 ok(48, $status == 0 );
372 ok(49, $key eq 'key' );
373 ok(50, $value eq 'value' );
375 # seq when the key does not match
378 $status = $X->seq($key, $value, R_CURSOR) ;
379 ok(51, $status == 1 );
382 # use seq to set the cursor, then delete the record @ the cursor.
386 $status = $X->seq($key, $value, R_CURSOR) ;
387 ok(52, $status == 0 );
388 ok(53, $key eq 'x' );
389 ok(54, $value eq 'X' );
390 $status = $X->del(0, R_CURSOR) ;
391 ok(55, $status == 0 );
392 $status = $X->get('x', $value) ;
393 ok(56, $status == 1 );
395 # ditto, but use put to replace the key/value pair.
398 $status = $X->seq($key, $value, R_CURSOR) ;
399 ok(57, $status == 0 );
400 ok(58, $key eq 'y' );
401 ok(59, $value eq 'Y' );
403 $key = "replace key" ;
404 $value = "replace value" ;
405 $status = $X->put($key, $value, R_CURSOR) ;
406 ok(60, $status == 0 );
407 ok(61, $key eq 'replace key' );
408 ok(62, $value eq 'replace value' );
409 $status = $X->get('y', $value) ;
410 ok(63, 1) ; # hard-wire to always pass. the previous test ($status == 1)
411 # only worked because of a bug in 1.85/6
413 # use seq to walk forwards through a file
415 $status = $X->seq($key, $value, R_FIRST) ;
416 ok(64, $status == 0 );
417 my $previous = $key ;
420 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
422 ($ok = 0), last if ($previous cmp $key) == 1 ;
425 ok(65, $status == 1 );
428 # use seq to walk backwards through a file
429 $status = $X->seq($key, $value, R_LAST) ;
430 ok(67, $status == 0 );
434 while (($status = $X->seq($key, $value, R_PREV)) == 0)
436 ($ok = 0), last if ($previous cmp $key) == -1 ;
437 #print "key = [$key] value = [$value]\n" ;
440 ok(68, $status == 1 );
444 # check seq FIRST/LAST
450 ok(70, $status == 0 );
458 #ok(71, $status != 0 );
466 # Now try an in memory file
468 ok(72, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
470 # fd with an in memory file should return failure
472 ok(73, $status == -1 );
479 my $bt = new DB_File::BTREEINFO ;
480 $bt->{flags} = R_DUP ;
482 ok(74, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
484 $hh{'Wall'} = 'Larry' ;
485 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
486 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
487 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
488 $hh{'Smith'} = 'John' ;
489 $hh{'mouse'} = 'mickey' ;
491 # first work in scalar context
492 ok(75, scalar $YY->get_dup('Unknown') == 0 );
493 ok(76, scalar $YY->get_dup('Smith') == 1 );
494 ok(77, scalar $YY->get_dup('Wall') == 4 );
496 # now in list context
497 my @unknown = $YY->get_dup('Unknown') ;
498 ok(78, "@unknown" eq "" );
500 my @smith = $YY->get_dup('Smith') ;
501 ok(79, "@smith" eq "John" );
504 my @wall = $YY->get_dup('Wall') ;
506 @wall{@wall} = @wall ;
507 ok(80, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
511 my %unknown = $YY->get_dup('Unknown', 1) ;
512 ok(81, keys %unknown == 0 );
514 my %smith = $YY->get_dup('Smith', 1) ;
515 ok(82, keys %smith == 1 && $smith{'John'}) ;
517 my %wall = $YY->get_dup('Wall', 1) ;
518 ok(83, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
519 && $wall{'Brick'} == 2);
526 # test multiple callbacks
527 my $Dfile1 = "btree1" ;
528 my $Dfile2 = "btree2" ;
529 my $Dfile3 = "btree3" ;
531 my $dbh1 = new DB_File::BTREEINFO ;
532 $dbh1->{compare} = sub {
533 no warnings 'numeric' ;
536 my $dbh2 = new DB_File::BTREEINFO ;
537 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
539 my $dbh3 = new DB_File::BTREEINFO ;
540 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
544 tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) or die $!;
545 tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) or die $!;
546 tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) or die $!;
548 my @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
549 my (@srt_1, @srt_2, @srt_3);
551 no warnings 'numeric' ;
552 @srt_1 = sort { $a <=> $b } @Keys ;
554 @srt_2 = sort { $a cmp $b } @Keys ;
555 @srt_3 = sort { length $a <=> length $b } @Keys ;
567 return 0 if @$a != @$b ;
569 foreach (1 .. length @$a)
571 return 0 unless $$a[$_] eq $$b[$_] ;
577 ok(84, ArrayCompare (\@srt_1, [keys %h]) );
578 ok(85, ArrayCompare (\@srt_2, [keys %g]) );
579 ok(86, ArrayCompare (\@srt_3, [keys %k]) );
584 unlink $Dfile1, $Dfile2, $Dfile3 ;
589 ok(87, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
591 { $h{$_} = $_ * 100 }
593 # check that there are 10 elements in the hash
595 while (($key,$value) = each(%h)) {
605 while (($key,$value) = each(%h)) {
614 # check that attempting to tie an array to a DB_BTREE will fail
616 my $filename = "xyz" ;
618 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ;
619 ok(90, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ;
631 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
643 @EXPORT = @DB_File::EXPORT ;
649 $self->SUPER::STORE($key, $value * 2) ;
655 $self->SUPER::FETCH($key) - 1 ;
662 $self->SUPER::put($key, $value * 3) ;
667 $self->SUPER::get($_[0], $_[1]) ;
675 my $value = $self->FETCH($key) ;
676 return "[[$value]]" ;
684 BEGIN { push @INC, '.'; }
686 main::ok(91, $@ eq "") ;
690 $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE );
693 main::ok(92, $@ eq "") ;
695 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
696 main::ok(93, $@ eq "") ;
697 main::ok(94, $ret == 5) ;
700 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
701 main::ok(95, $@ eq "") ;
702 main::ok(96, $ret == 10) ;
704 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
705 main::ok(97, $@ eq "" ) ;
706 main::ok(98, $ret == 1) ;
708 $ret = eval '$X->A_new_method("joe") ' ;
709 main::ok(99, $@ eq "") ;
710 main::ok(100, $ret eq "[[11]]") ;
714 unlink "SubDB.pm", "dbbtree.tmp" ;
723 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
728 my($fk, $sk, $fv, $sv) = @_ ;
730 $fetch_key eq $fk && $store_key eq $sk &&
731 $fetch_value eq $fv && $store_value eq $sv &&
735 ok(101, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
737 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
738 $db->filter_store_key (sub { $store_key = $_ }) ;
739 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
740 $db->filter_store_value (sub { $store_value = $_ }) ;
746 ok(102, checkOutput( "", "fred", "", "joe")) ;
748 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
749 ok(103, $h{"fred"} eq "joe");
751 ok(104, checkOutput( "", "fred", "joe", "")) ;
753 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
754 ok(105, $db->FIRSTKEY() eq "fred") ;
756 ok(106, checkOutput( "fred", "", "", "")) ;
758 # replace the filters, but remember the previous set
759 my ($old_fk) = $db->filter_fetch_key
760 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
761 my ($old_sk) = $db->filter_store_key
762 (sub { $_ = lc $_ ; $store_key = $_ }) ;
763 my ($old_fv) = $db->filter_fetch_value
764 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
765 my ($old_sv) = $db->filter_store_value
766 (sub { s/o/x/g; $store_value = $_ }) ;
768 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
771 ok(107, checkOutput( "", "fred", "", "Jxe")) ;
773 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
774 ok(108, $h{"Fred"} eq "[Jxe]");
776 ok(109, checkOutput( "", "fred", "[Jxe]", "")) ;
778 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
779 ok(110, $db->FIRSTKEY() eq "FRED") ;
781 ok(111, checkOutput( "FRED", "", "", "")) ;
783 # put the original filters back
784 $db->filter_fetch_key ($old_fk);
785 $db->filter_store_key ($old_sk);
786 $db->filter_fetch_value ($old_fv);
787 $db->filter_store_value ($old_sv);
789 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
791 ok(112, checkOutput( "", "fred", "", "joe")) ;
793 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
794 ok(113, $h{"fred"} eq "joe");
795 ok(114, checkOutput( "", "fred", "joe", "")) ;
797 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
798 ok(115, $db->FIRSTKEY() eq "fred") ;
799 ok(116, checkOutput( "fred", "", "", "")) ;
802 $db->filter_fetch_key (undef);
803 $db->filter_store_key (undef);
804 $db->filter_fetch_value (undef);
805 $db->filter_store_value (undef);
807 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
809 ok(117, checkOutput( "", "", "", "")) ;
811 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
812 ok(118, $h{"fred"} eq "joe");
813 ok(119, checkOutput( "", "", "", "")) ;
815 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
816 ok(120, $db->FIRSTKEY() eq "fred") ;
817 ok(121, checkOutput( "", "", "", "")) ;
825 # DBM Filter with a closure
832 ok(122, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
842 return sub { ++$count ;
844 $result{$name} = "$name - $count: [@kept]" ;
848 $db->filter_store_key(Closure("store key")) ;
849 $db->filter_store_value(Closure("store value")) ;
850 $db->filter_fetch_key(Closure("fetch key")) ;
851 $db->filter_fetch_value(Closure("fetch value")) ;
856 ok(123, $result{"store key"} eq "store key - 1: [fred]");
857 ok(124, $result{"store value"} eq "store value - 1: [joe]");
858 ok(125, ! defined $result{"fetch key"} );
859 ok(126, ! defined $result{"fetch value"} );
860 ok(127, $_ eq "original") ;
862 ok(128, $db->FIRSTKEY() eq "fred") ;
863 ok(129, $result{"store key"} eq "store key - 1: [fred]");
864 ok(130, $result{"store value"} eq "store value - 1: [joe]");
865 ok(131, $result{"fetch key"} eq "fetch key - 1: [fred]");
866 ok(132, ! defined $result{"fetch value"} );
867 ok(133, $_ eq "original") ;
870 ok(134, $result{"store key"} eq "store key - 2: [fred jim]");
871 ok(135, $result{"store value"} eq "store value - 2: [joe john]");
872 ok(136, $result{"fetch key"} eq "fetch key - 1: [fred]");
873 ok(137, ! defined $result{"fetch value"} );
874 ok(138, $_ eq "original") ;
876 ok(139, $h{"fred"} eq "joe");
877 ok(140, $result{"store key"} eq "store key - 3: [fred jim fred]");
878 ok(141, $result{"store value"} eq "store value - 2: [joe john]");
879 ok(142, $result{"fetch key"} eq "fetch key - 1: [fred]");
880 ok(143, $result{"fetch value"} eq "fetch value - 1: [joe]");
881 ok(144, $_ eq "original") ;
889 # DBM Filter recursion detection
895 ok(145, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
897 $db->filter_store_key (sub { $_ = $h{$_} }) ;
899 eval '$h{1} = 1234' ;
900 ok(146, $@ =~ /^recursion detected in filter_store_key at/ );
909 # Examples from the POD
914 my $redirect = new Redirect $file ;
919 use warnings FATAL => qw(all) ;
927 my ($key1, $key2) = @_ ;
928 "\L$key1" cmp "\L$key2" ;
931 # specify the Perl sub that will do the comparison
932 $DB_BTREE->{'compare'} = \&Compare ;
935 tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
936 or die "Cannot open file 'tree': $!\n" ;
938 # Add a key/value pair to the file
939 $h{'Wall'} = 'Larry' ;
940 $h{'Smith'} = 'John' ;
941 $h{'mouse'} = 'mickey' ;
942 $h{'duck'} = 'donald' ;
947 # Cycle through the keys printing them in order.
948 # Note it is not necessary to sort the keys as
949 # the btree will have kept them in order automatically.
958 delete $DB_BTREE->{'compare'} ;
960 ok(147, docat_del($file) eq <<'EOM') ;
967 my $redirect = new Redirect $file ;
972 use warnings FATAL => qw(all) ;
981 # Enable duplicate records
982 $DB_BTREE->{'flags'} = R_DUP ;
984 tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
985 or die "Cannot open $filename: $!\n";
987 # Add some key/value pairs to the file
988 $h{'Wall'} = 'Larry' ;
989 $h{'Wall'} = 'Brick' ; # Note the duplicate key
990 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
991 $h{'Smith'} = 'John' ;
992 $h{'mouse'} = 'mickey' ;
994 # iterate through the associative array
995 # and print each key/value pair.
997 { print "$_ -> $h{$_}\n" }
1004 ok(148, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ;
1019 my $redirect = new Redirect $file ;
1024 use warnings FATAL => qw(all) ;
1028 my ($filename, $x, %h, $status, $key, $value);
1030 $filename = "tree" ;
1033 # Enable duplicate records
1034 $DB_BTREE->{'flags'} = R_DUP ;
1036 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1037 or die "Cannot open $filename: $!\n";
1039 # Add some key/value pairs to the file
1040 $h{'Wall'} = 'Larry' ;
1041 $h{'Wall'} = 'Brick' ; # Note the duplicate key
1042 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
1043 $h{'Smith'} = 'John' ;
1044 $h{'mouse'} = 'mickey' ;
1046 # iterate through the btree using seq
1047 # and print each key/value pair.
1049 for ($status = $x->seq($key, $value, R_FIRST) ;
1051 $status = $x->seq($key, $value, R_NEXT) )
1052 { print "$key -> $value\n" }
1059 ok(149, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ;
1075 my $redirect = new Redirect $file ;
1080 use warnings FATAL => qw(all) ;
1084 my ($filename, $x, %h);
1086 $filename = "tree" ;
1088 # Enable duplicate records
1089 $DB_BTREE->{'flags'} = R_DUP ;
1091 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1092 or die "Cannot open $filename: $!\n";
1094 my $cnt = $x->get_dup("Wall") ;
1095 print "Wall occurred $cnt times\n" ;
1097 my %hash = $x->get_dup("Wall", 1) ;
1098 print "Larry is there\n" if $hash{'Larry'} ;
1099 print "There are $hash{'Brick'} Brick Walls\n" ;
1101 my @list = sort $x->get_dup("Wall") ;
1102 print "Wall => [@list]\n" ;
1104 @list = $x->get_dup("Smith") ;
1105 print "Smith => [@list]\n" ;
1107 @list = $x->get_dup("Dog") ;
1108 print "Dog => [@list]\n" ;
1114 ok(150, docat_del($file) eq <<'EOM') ;
1115 Wall occurred 3 times
1117 There are 2 Brick Walls
1118 Wall => [Brick Brick Larry]
1124 my $redirect = new Redirect $file ;
1129 use warnings FATAL => qw(all) ;
1133 my ($filename, $x, %h, $found);
1135 $filename = "tree" ;
1137 # Enable duplicate records
1138 $DB_BTREE->{'flags'} = R_DUP ;
1140 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1141 or die "Cannot open $filename: $!\n";
1143 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1144 print "Larry Wall is $found there\n" ;
1146 $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
1147 print "Harry Wall is $found there\n" ;
1153 ok(151, docat_del($file) eq <<'EOM') ;
1155 Harry Wall is not there
1159 my $redirect = new Redirect $file ;
1164 use warnings FATAL => qw(all) ;
1168 my ($filename, $x, %h, $found);
1170 $filename = "tree" ;
1172 # Enable duplicate records
1173 $DB_BTREE->{'flags'} = R_DUP ;
1175 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1176 or die "Cannot open $filename: $!\n";
1178 $x->del_dup("Wall", "Larry") ;
1180 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1181 print "Larry Wall is $found there\n" ;
1189 ok(152, docat_del($file) eq <<'EOM') ;
1190 Larry Wall is not there
1194 my $redirect = new Redirect $file ;
1199 use warnings FATAL => qw(all) ;
1204 my ($filename, $x, %h, $st, $key, $value);
1210 my $orig_key = $key ;
1211 $x->seq($key, $value, R_CURSOR) ;
1212 print "$orig_key\t-> $key\t-> $value\n" ;
1215 $filename = "tree" ;
1218 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1219 or die "Cannot open $filename: $!\n";
1221 # Add some key/value pairs to the file
1222 $h{'mouse'} = 'mickey' ;
1223 $h{'Wall'} = 'Larry' ;
1224 $h{'Walls'} = 'Brick' ;
1225 $h{'Smith'} = 'John' ;
1229 print "IN ORDER\n" ;
1230 for ($st = $x->seq($key, $value, R_FIRST) ;
1232 $st = $x->seq($key, $value, R_NEXT) )
1234 { print "$key -> $value\n" }
1236 print "\nPARTIAL MATCH\n" ;
1249 ok(153, docat_del($file) eq <<'EOM') ;
1259 a -> mouse -> mickey
1270 # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1273 # my $k = "newest" ;
1275 # my $status = $db->put($k, $v, R_SETCURSOR) ;
1276 # print "status = [$status]\n" ;
1277 # ok(157, $status == 0) ;
1278 # $status = $db->del($k, R_CURSOR) ;
1279 # print "status = [$status]\n" ;
1280 # ok(158, $status == 0) ;
1282 # ok(159, $db->get($k, $v, R_CURSOR)) ;
1284 # ok(160, keys %h == 1) ;
1292 # Bug ID 20001013.009
1294 # test that $hash{KEY} = undef doesn't produce the warning
1295 # Use of uninitialized value in null operation
1303 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1305 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1306 or die "Can't open file: $!\n" ;
1314 # test that %hash = () doesn't produce the warning
1315 # Argument "" isn't numeric in entersub
1323 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1325 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1326 or die "Can't open file: $!\n" ;
1334 # When iterating over a tied hash using "each", the key passed to FETCH
1335 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
1336 # key in FETCH via a filter_fetch_key method we need to check that the
1337 # modified key doesn't get passed to NEXTKEY.
1338 # Also Test "keys" & "values" while we are at it.
1348 ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1349 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
1350 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
1352 $h{'Alpha_ABC'} = 2 ;
1353 $h{'Alpha_DEF'} = 5 ;
1355 ok(157, $h{'Alpha_ABC'} == 2);
1356 ok(158, $h{'Alpha_DEF'} == 5);
1358 my ($k, $v) = ("","");
1359 while (($k, $v) = each %h) {}
1360 ok(159, $bad_key == 0);
1363 foreach $k (keys %h) {}
1364 ok(160, $bad_key == 0);
1367 foreach $v (values %h) {}
1368 ok(161, $bad_key == 0);
1376 # now an error to pass 'compare' a non-code reference
1377 my $dbh = new DB_File::BTREEINFO ;
1379 eval { $dbh->{compare} = 2 };
1380 ok(162, $@ =~ /^Key 'compare' not associated with a code reference at/);
1382 eval { $dbh->{prefix} = 2 };
1383 ok(163, $@ =~ /^Key 'prefix' not associated with a code reference at/);
1389 # # recursion detection in btree
1392 # my $dbh = new DB_File::BTREEINFO ;
1393 # $dbh->{compare} = sub { $hash{3} = 4 ; length $_[0] } ;
1397 # ok(164, tie(%hash, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh ) );
1399 # eval { $hash{1} = 2;
1403 # ok(165, $@ =~ /^DB_File btree_compare: recursion detected/);
1414 # Check that two callbacks don't interact
1419 unlink $Dfile, $Dfile2;
1420 my $dbh1 = new DB_File::BTREEINFO ;
1421 $dbh1->{compare} = sub { ++ $h1_count ; $_[0] cmp $_[1] } ;
1423 my $dbh2 = new DB_File::BTREEINFO ;
1424 $dbh2->{compare} = sub { ;++ $h2_count ; $_[0] cmp $_[1] } ;
1429 ok(166, tie(%hash1, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh1 ) );
1430 ok(167, tie(%hash2, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) );
1440 ok(168, $h1_count > 0);
1441 ok(169, $h1_count == $h2_count);
1443 ok(170, safeUntie \%hash1);
1444 ok(171, safeUntie \%hash2);
1445 unlink $Dfile, $Dfile2;
1449 # Check that DBM Filter can cope with read-only $_
1456 ok(172, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1458 $db->filter_fetch_key (sub { }) ;
1459 $db->filter_store_key (sub { }) ;
1460 $db->filter_fetch_value (sub { }) ;
1461 $db->filter_store_value (sub { }) ;
1465 $h{"fred"} = "joe" ;
1466 ok(173, $h{"fred"} eq "joe");
1468 eval { my @r= grep { $h{$_} } (1, 2, 3) };
1472 # delete the filters
1473 $db->filter_fetch_key (undef);
1474 $db->filter_store_key (undef);
1475 $db->filter_fetch_value (undef);
1476 $db->filter_store_value (undef);
1478 $h{"fred"} = "joe" ;
1480 ok(175, $h{"fred"} eq "joe");
1482 ok(176, $db->FIRSTKEY() eq "fred") ;
1484 eval { my @r= grep { $h{$_} } (1, 2, 3) };
1493 # Check low-level API works with filter
1498 my $Dfile = "xxy.db";
1501 ok(178, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1504 $db->filter_fetch_key (sub { $_ = unpack("i", $_) } );
1505 $db->filter_store_key (sub { $_ = pack("i", $_) } );
1506 $db->filter_fetch_value (sub { $_ = unpack("i", $_) } );
1507 $db->filter_store_value (sub { $_ = pack("i", $_) } );
1514 $db->put($key, $value) ;
1516 ok 180, $value == 34 ;
1517 ok 181, $_ eq 'fred';
1518 #print "k [$key][$value]\n" ;
1521 $db->get($key, $val) ;
1523 ok 183, $val == 34 ;
1524 ok 184, $_ eq 'fred';
1530 ok 186, $value == 454 ;
1531 ok 187, $_ eq 'fred';
1541 # Regression Test for bug 30237
1542 # Check that substr can be used in the key to db_put
1543 # and that db_put does not trigger the warning
1545 # Use of uninitialized value in subroutine entry
1551 my $Dfile = "xxy.db";
1554 ok(188, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
1557 local $SIG{__WARN__} = sub {$warned = $_[0]} ;
1559 # db-put with substr of key
1561 for my $ix ( 10 .. 12 )
1563 my $key = $ix . "data" ;
1564 my $value = "value$ix" ;
1565 $remember{$key} = $value ;
1566 $db->put(substr($key,0), $value) ;
1569 ok 189, $warned eq ''
1570 or print "# Caught warning [$warned]\n" ;
1572 # db-put with substr of value
1574 for my $ix ( 20 .. 22 )
1576 my $key = $ix . "data" ;
1577 my $value = "value$ix" ;
1578 $remember{$key} = $value ;
1579 $db->put($key, substr($value,0)) ;
1582 ok 190, $warned eq ''
1583 or print "# Caught warning [$warned]\n" ;
1585 # via the tied hash is not a problem, but check anyway
1588 for my $ix ( 30 .. 32 )
1590 my $key = $ix . "data" ;
1591 my $value = "value$ix" ;
1592 $remember{$key} = $value ;
1593 $h{substr($key,0)} = $value ;
1596 ok 191, $warned eq ''
1597 or print "# Caught warning [$warned]\n" ;
1599 # via the tied hash is not a problem, but check anyway
1602 for my $ix ( 40 .. 42 )
1604 my $key = $ix . "data" ;
1605 my $value = "value$ix" ;
1606 $remember{$key} = $value ;
1607 $h{$key} = substr($value,0) ;
1610 ok 192, $warned eq ''
1611 or print "# Caught warning [$warned]\n" ;
1615 for ($status = $db->seq($key, $value, R_FIRST ) ;
1617 $status = $db->seq($key, $value, R_NEXT ) ) {
1619 #print "# key [$key] value [$value]\n" ;
1620 if (defined $remember{$key} && defined $value &&
1621 $remember{$key} eq $value) {
1622 delete $remember{$key} ;
1625 $bad{$key} = $value ;
1629 ok 193, keys %bad == 0 ;
1630 ok 194, keys %remember == 0 ;
1632 print "# missing -- $key $value\n" while ($key, $value) = each %remember;
1633 print "# bad -- $key $value\n" while ($key, $value) = each %bad;
1635 # Make sure this fix does not break code to handle an undef key
1636 # Berkeley DB undef key is bron between versions 2.3.16 and
1639 $db->put(undef, $value) ;
1640 ok 195, $warned eq ''
1641 or print "# Caught warning [$warned]\n" ;
1644 my $no_NULL = ($DB_File::db_ver >= 2.003016 && $DB_File::db_ver < 3.001) ;
1645 print "# db_ver $DB_File::db_ver\n";
1647 $db->get(undef, $value) ;
1648 ok 196, $no_NULL || $value eq 'fred' or print "# got [$value]\n" ;
1649 ok 197, $warned eq ''
1650 or print "# Caught warning [$warned]\n" ;