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 && $Config{db_version_major} == 1
27 && $Config{db_version_minor} == 0
28 && $Config{db_version_patch} == 0) {
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.
50 print "not " unless $result ;
56 my(@a) = unpack ("C*", $a) ;
57 my(@b) = unpack ("C*", $b) ;
59 my $len = (@a > @b ? @b : @a) ;
62 foreach $i ( 0 .. $len -1) {
63 return $a[$i] - $b[$i] if $a[$i] != $b[$i] ;
76 my $filename = shift ;
78 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
79 my $real_stdout = select($fh) ;
80 return bless [$fh, $real_stdout ] ;
95 open(CAT,$file) || die "Cannot open $file: $!";
98 $result = normalise($result) ;
105 my $result = docat($file);
121 my $hashref = shift ;
123 local $SIG{__WARN__} = sub {-- $no_inner } ;
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 );
134 my $Dfile = "dbbtree.tmp";
139 # Check the interface to BTREEINFO
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}) ;
151 $dbh->{flags} = 3000 ;
152 ok(9, $dbh->{flags} == 3000) ;
154 $dbh->{cachesize} = 9000 ;
155 ok(10, $dbh->{cachesize} == 9000);
157 $dbh->{psize} = 400 ;
158 ok(11, $dbh->{psize} == 400) ;
160 $dbh->{lorder} = 65 ;
161 ok(12, $dbh->{lorder} == 65) ;
163 $dbh->{minkeypage} = 123 ;
164 ok(13, $dbh->{minkeypage} == 123) ;
166 $dbh->{maxkeypage} = 1234 ;
167 ok(14, $dbh->{maxkeypage} == 1234 );
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/ ) ;
175 # Now check the interface to BTREE
178 ok(17, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
179 die "Could not tie: $!" unless $X;
181 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
182 $blksize,$blocks) = stat($Dfile);
184 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
186 ok(18, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
189 my ($key, $value, $i);
190 while (($key,$value) = each(%h)) {
195 $h{'goner1'} = 'snork';
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'} ) ;
204 $h{'jkl','mno'} = "JKL\034MNO";
205 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
209 $X->STORE('b', 'B') ;
222 $h{'goner2'} = 'snork';
226 # IMPORTANT - $X must be undefined before the untie otherwise the
227 # underlying DB close routine will not get called.
231 # tie to the same file again
232 ok(24, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
234 # Modify an entry from the previous tie
255 $h{'goner3'} = 'snork';
258 $X->DELETE('goner3');
261 my @values = values(%h);
263 ok(25, $#keys == 29 && $#values == 29) ;
266 while (($key,$value) = each(%h)) {
267 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
269 $i++ if $key eq $value;
275 @keys = ('blurfl', keys(%h), 'dyick');
276 ok(27, $#keys == 31) ;
278 #Check that the keys can be retrieved in order
280 my @c = sort lexical @b ;
281 ok(28, ArrayCompare(\@b, \@c)) ;
284 ok(29, $h{'foo'} eq '' ) ;
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.
289 if ($null_keys_allowed) {
291 $result = ( $h{''} eq 'bar' );
297 # check cache overflow and numeric keys and contents
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; }
303 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
304 $blksize,$blocks) = stat($Dfile);
307 @h{0..200} = 200..400;
308 my @foo = @h{0..200};
309 ok(33, join(':',200..400) eq join(':',@foo) );
311 # Now check all the non-tie specific stuff
314 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
315 # an existing record.
317 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
318 ok(34, $status == 1 );
320 # check that the value of the key 'x' has not been changed by the
322 ok(35, $h{'x'} eq 'X' );
325 $status = $X->put('key', 'value') ;
326 ok(36, $status == 0 );
328 #check that previous put can be retrieved
330 $status = $X->get('key', $value) ;
331 ok(37, $status == 0 );
332 ok(38, $value eq 'value' );
334 # Attempting to delete an existing key should work
336 $status = $X->del('q') ;
337 ok(39, $status == 0 );
338 if ($null_keys_allowed) {
339 $status = $X->del('') ;
343 ok(40, $status == 0 );
345 # Make sure that the key deleted, cannot be retrieved
346 ok(41, ! defined $h{'q'}) ;
347 ok(42, ! defined $h{''}) ;
352 ok(43, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
354 # Attempting to delete a non-existant key should fail
356 $status = $X->del('joe') ;
357 ok(44, $status == 1 );
359 # Check the get interface
361 # First a non-existing key
362 $status = $X->get('aaaa', $value) ;
363 ok(45, $status == 1 );
365 # Next an existing key
366 $status = $X->get('a', $value) ;
367 ok(46, $status == 0 );
368 ok(47, $value eq 'A' );
373 # use seq to find an approximate match
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' );
381 # seq when the key does not match
384 $status = $X->seq($key, $value, R_CURSOR) ;
385 ok(51, $status == 1 );
388 # use seq to set the cursor, then delete the record @ the cursor.
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 );
401 # ditto, but use put to replace the key/value pair.
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' );
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
419 # use seq to walk forwards through a file
421 $status = $X->seq($key, $value, R_FIRST) ;
422 ok(64, $status == 0 );
423 my $previous = $key ;
426 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
428 ($ok = 0), last if ($previous cmp $key) == 1 ;
431 ok(65, $status == 1 );
434 # use seq to walk backwards through a file
435 $status = $X->seq($key, $value, R_LAST) ;
436 ok(67, $status == 0 );
440 while (($status = $X->seq($key, $value, R_PREV)) == 0)
442 ($ok = 0), last if ($previous cmp $key) == -1 ;
443 #print "key = [$key] value = [$value]\n" ;
446 ok(68, $status == 1 );
450 # check seq FIRST/LAST
456 ok(70, $status == 0 );
464 #ok(71, $status != 0 );
472 # Now try an in memory file
474 ok(72, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
476 # fd with an in memory file should return failure
478 ok(73, $status == -1 );
485 my $bt = new DB_File::BTREEINFO ;
486 $bt->{flags} = R_DUP ;
488 ok(74, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
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' ;
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 );
502 # now in list context
503 my @unknown = $YY->get_dup('Unknown') ;
504 ok(78, "@unknown" eq "" );
506 my @smith = $YY->get_dup('Smith') ;
507 ok(79, "@smith" eq "John" );
510 my @wall = $YY->get_dup('Wall') ;
512 @wall{@wall} = @wall ;
513 ok(80, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
517 my %unknown = $YY->get_dup('Unknown', 1) ;
518 ok(81, keys %unknown == 0 );
520 my %smith = $YY->get_dup('Smith', 1) ;
521 ok(82, keys %smith == 1 && $smith{'John'}) ;
523 my %wall = $YY->get_dup('Wall', 1) ;
524 ok(83, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
525 && $wall{'Brick'} == 2);
532 # test multiple callbacks
533 my $Dfile1 = "btree1" ;
534 my $Dfile2 = "btree2" ;
535 my $Dfile3 = "btree3" ;
537 my $dbh1 = new DB_File::BTREEINFO ;
538 $dbh1->{compare} = sub {
539 no warnings 'numeric' ;
542 my $dbh2 = new DB_File::BTREEINFO ;
543 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
545 my $dbh3 = new DB_File::BTREEINFO ;
546 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
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 $!;
554 my @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
555 my (@srt_1, @srt_2, @srt_3);
557 no warnings 'numeric' ;
558 @srt_1 = sort { $a <=> $b } @Keys ;
560 @srt_2 = sort { $a cmp $b } @Keys ;
561 @srt_3 = sort { length $a <=> length $b } @Keys ;
573 return 0 if @$a != @$b ;
575 foreach (1 .. length @$a)
577 return 0 unless $$a[$_] eq $$b[$_] ;
583 ok(84, ArrayCompare (\@srt_1, [keys %h]) );
584 ok(85, ArrayCompare (\@srt_2, [keys %g]) );
585 ok(86, ArrayCompare (\@srt_3, [keys %k]) );
590 unlink $Dfile1, $Dfile2, $Dfile3 ;
595 ok(87, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
597 { $h{$_} = $_ * 100 }
599 # check that there are 10 elements in the hash
601 while (($key,$value) = each(%h)) {
611 while (($key,$value) = each(%h)) {
620 # check that attempting to tie an array to a DB_BTREE will fail
622 my $filename = "xyz" ;
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/) ;
637 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
649 @EXPORT = @DB_File::EXPORT ;
655 $self->SUPER::STORE($key, $value * 2) ;
661 $self->SUPER::FETCH($key) - 1 ;
668 $self->SUPER::put($key, $value * 3) ;
673 $self->SUPER::get($_[0], $_[1]) ;
681 my $value = $self->FETCH($key) ;
682 return "[[$value]]" ;
690 BEGIN { push @INC, '.'; }
692 main::ok(91, $@ eq "") ;
696 $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE );
699 main::ok(92, $@ eq "") ;
701 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
702 main::ok(93, $@ eq "") ;
703 main::ok(94, $ret == 5) ;
706 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
707 main::ok(95, $@ eq "") ;
708 main::ok(96, $ret == 10) ;
710 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
711 main::ok(97, $@ eq "" ) ;
712 main::ok(98, $ret == 1) ;
714 $ret = eval '$X->A_new_method("joe") ' ;
715 main::ok(99, $@ eq "") ;
716 main::ok(100, $ret eq "[[11]]") ;
720 unlink "SubDB.pm", "dbbtree.tmp" ;
729 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
734 my($fk, $sk, $fv, $sv) = @_ ;
736 $fetch_key eq $fk && $store_key eq $sk &&
737 $fetch_value eq $fv && $store_value eq $sv &&
741 ok(101, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
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 = $_ }) ;
752 ok(102, checkOutput( "", "fred", "", "joe")) ;
754 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
755 ok(103, $h{"fred"} eq "joe");
757 ok(104, checkOutput( "", "fred", "joe", "")) ;
759 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
760 ok(105, $db->FIRSTKEY() eq "fred") ;
762 ok(106, checkOutput( "fred", "", "", "")) ;
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 = $_ }) ;
774 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
777 ok(107, checkOutput( "", "fred", "", "Jxe")) ;
779 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
780 ok(108, $h{"Fred"} eq "[Jxe]");
782 ok(109, checkOutput( "", "fred", "[Jxe]", "")) ;
784 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
785 ok(110, $db->FIRSTKEY() eq "FRED") ;
787 ok(111, checkOutput( "FRED", "", "", "")) ;
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);
795 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
797 ok(112, checkOutput( "", "fred", "", "joe")) ;
799 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
800 ok(113, $h{"fred"} eq "joe");
801 ok(114, checkOutput( "", "fred", "joe", "")) ;
803 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
804 ok(115, $db->FIRSTKEY() eq "fred") ;
805 ok(116, checkOutput( "fred", "", "", "")) ;
808 $db->filter_fetch_key (undef);
809 $db->filter_store_key (undef);
810 $db->filter_fetch_value (undef);
811 $db->filter_store_value (undef);
813 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
815 ok(117, checkOutput( "", "", "", "")) ;
817 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
818 ok(118, $h{"fred"} eq "joe");
819 ok(119, checkOutput( "", "", "", "")) ;
821 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
822 ok(120, $db->FIRSTKEY() eq "fred") ;
823 ok(121, checkOutput( "", "", "", "")) ;
831 # DBM Filter with a closure
838 ok(122, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
848 return sub { ++$count ;
850 $result{$name} = "$name - $count: [@kept]" ;
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")) ;
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") ;
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") ;
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") ;
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") ;
895 # DBM Filter recursion detection
901 ok(145, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
903 $db->filter_store_key (sub { $_ = $h{$_} }) ;
905 eval '$h{1} = 1234' ;
906 ok(146, $@ =~ /^recursion detected in filter_store_key at/ );
915 # Examples from the POD
920 my $redirect = new Redirect $file ;
925 use warnings FATAL => qw(all) ;
933 my ($key1, $key2) = @_ ;
934 "\L$key1" cmp "\L$key2" ;
937 # specify the Perl sub that will do the comparison
938 $DB_BTREE->{'compare'} = \&Compare ;
941 tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
942 or die "Cannot open file 'tree': $!\n" ;
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' ;
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.
964 delete $DB_BTREE->{'compare'} ;
966 ok(147, docat_del($file) eq <<'EOM') ;
973 my $redirect = new Redirect $file ;
978 use warnings FATAL => qw(all) ;
987 # Enable duplicate records
988 $DB_BTREE->{'flags'} = R_DUP ;
990 tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
991 or die "Cannot open $filename: $!\n";
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' ;
1000 # iterate through the associative array
1001 # and print each key/value pair.
1003 { print "$_ -> $h{$_}\n" }
1010 ok(148, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ;
1025 my $redirect = new Redirect $file ;
1030 use warnings FATAL => qw(all) ;
1034 my ($filename, $x, %h, $status, $key, $value);
1036 $filename = "tree" ;
1039 # Enable duplicate records
1040 $DB_BTREE->{'flags'} = R_DUP ;
1042 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1043 or die "Cannot open $filename: $!\n";
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' ;
1052 # iterate through the btree using seq
1053 # and print each key/value pair.
1055 for ($status = $x->seq($key, $value, R_FIRST) ;
1057 $status = $x->seq($key, $value, R_NEXT) )
1058 { print "$key -> $value\n" }
1065 ok(149, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ;
1081 my $redirect = new Redirect $file ;
1086 use warnings FATAL => qw(all) ;
1090 my ($filename, $x, %h);
1092 $filename = "tree" ;
1094 # Enable duplicate records
1095 $DB_BTREE->{'flags'} = R_DUP ;
1097 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1098 or die "Cannot open $filename: $!\n";
1100 my $cnt = $x->get_dup("Wall") ;
1101 print "Wall occurred $cnt times\n" ;
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" ;
1107 my @list = sort $x->get_dup("Wall") ;
1108 print "Wall => [@list]\n" ;
1110 @list = $x->get_dup("Smith") ;
1111 print "Smith => [@list]\n" ;
1113 @list = $x->get_dup("Dog") ;
1114 print "Dog => [@list]\n" ;
1120 ok(150, docat_del($file) eq <<'EOM') ;
1121 Wall occurred 3 times
1123 There are 2 Brick Walls
1124 Wall => [Brick Brick Larry]
1130 my $redirect = new Redirect $file ;
1135 use warnings FATAL => qw(all) ;
1139 my ($filename, $x, %h, $found);
1141 $filename = "tree" ;
1143 # Enable duplicate records
1144 $DB_BTREE->{'flags'} = R_DUP ;
1146 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1147 or die "Cannot open $filename: $!\n";
1149 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1150 print "Larry Wall is $found there\n" ;
1152 $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
1153 print "Harry Wall is $found there\n" ;
1159 ok(151, docat_del($file) eq <<'EOM') ;
1161 Harry Wall is not there
1165 my $redirect = new Redirect $file ;
1170 use warnings FATAL => qw(all) ;
1174 my ($filename, $x, %h, $found);
1176 $filename = "tree" ;
1178 # Enable duplicate records
1179 $DB_BTREE->{'flags'} = R_DUP ;
1181 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1182 or die "Cannot open $filename: $!\n";
1184 $x->del_dup("Wall", "Larry") ;
1186 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1187 print "Larry Wall is $found there\n" ;
1195 ok(152, docat_del($file) eq <<'EOM') ;
1196 Larry Wall is not there
1200 my $redirect = new Redirect $file ;
1205 use warnings FATAL => qw(all) ;
1210 my ($filename, $x, %h, $st, $key, $value);
1216 my $orig_key = $key ;
1217 $x->seq($key, $value, R_CURSOR) ;
1218 print "$orig_key\t-> $key\t-> $value\n" ;
1221 $filename = "tree" ;
1224 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1225 or die "Cannot open $filename: $!\n";
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' ;
1235 print "IN ORDER\n" ;
1236 for ($st = $x->seq($key, $value, R_FIRST) ;
1238 $st = $x->seq($key, $value, R_NEXT) )
1240 { print "$key -> $value\n" }
1242 print "\nPARTIAL MATCH\n" ;
1255 ok(153, docat_del($file) eq <<'EOM') ;
1265 a -> mouse -> mickey
1276 # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1279 # my $k = "newest" ;
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) ;
1288 # ok(159, $db->get($k, $v, R_CURSOR)) ;
1290 # ok(160, keys %h == 1) ;
1298 # Bug ID 20001013.009
1300 # test that $hash{KEY} = undef doesn't produce the warning
1301 # Use of uninitialized value in null operation
1309 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1311 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1312 or die "Can't open file: $!\n" ;
1320 # test that %hash = () doesn't produce the warning
1321 # Argument "" isn't numeric in entersub
1329 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1331 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1332 or die "Can't open file: $!\n" ;
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.
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_/}) ;
1358 $h{'Alpha_ABC'} = 2 ;
1359 $h{'Alpha_DEF'} = 5 ;
1361 ok(157, $h{'Alpha_ABC'} == 2);
1362 ok(158, $h{'Alpha_DEF'} == 5);
1364 my ($k, $v) = ("","");
1365 while (($k, $v) = each %h) {}
1366 ok(159, $bad_key == 0);
1369 foreach $k (keys %h) {}
1370 ok(160, $bad_key == 0);
1373 foreach $v (values %h) {}
1374 ok(161, $bad_key == 0);
1382 # now an error to pass 'compare' a non-code reference
1383 my $dbh = new DB_File::BTREEINFO ;
1385 eval { $dbh->{compare} = 2 };
1386 ok(162, $@ =~ /^Key 'compare' not associated with a code reference at/);
1388 eval { $dbh->{prefix} = 2 };
1389 ok(163, $@ =~ /^Key 'prefix' not associated with a code reference at/);
1395 # # recursion detection in btree
1398 # my $dbh = new DB_File::BTREEINFO ;
1399 # $dbh->{compare} = sub { $hash{3} = 4 ; length $_[0] } ;
1403 # ok(164, tie(%hash, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh ) );
1405 # eval { $hash{1} = 2;
1409 # ok(165, $@ =~ /^DB_File btree_compare: recursion detected/);
1420 # Check that two callbacks don't interact
1425 unlink $Dfile, $Dfile2;
1426 my $dbh1 = new DB_File::BTREEINFO ;
1427 $dbh1->{compare} = sub { ++ $h1_count ; $_[0] cmp $_[1] } ;
1429 my $dbh2 = new DB_File::BTREEINFO ;
1430 $dbh2->{compare} = sub { ;++ $h2_count ; $_[0] cmp $_[1] } ;
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 ) );
1446 ok(168, $h1_count > 0);
1447 ok(169, $h1_count == $h2_count);
1449 ok(170, safeUntie \%hash1);
1450 ok(171, safeUntie \%hash2);
1451 unlink $Dfile, $Dfile2;
1455 # Check that DBM Filter can cope with read-only $_
1462 ok(172, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1464 $db->filter_fetch_key (sub { }) ;
1465 $db->filter_store_key (sub { }) ;
1466 $db->filter_fetch_value (sub { }) ;
1467 $db->filter_store_value (sub { }) ;
1471 $h{"fred"} = "joe" ;
1472 ok(173, $h{"fred"} eq "joe");
1474 eval { grep { $h{$_} } (1, 2, 3) };
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);
1484 $h{"fred"} = "joe" ;
1486 ok(175, $h{"fred"} eq "joe");
1488 ok(176, $db->FIRSTKEY() eq "fred") ;
1490 eval { grep { $h{$_} } (1, 2, 3) };
1499 # Check low-level API works with filter
1504 my $Dfile = "xxy.db";
1507 ok(178, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
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", $_) } );
1520 $db->put($key, $value) ;
1522 ok 180, $value == 34 ;
1523 ok 181, $_ eq 'fred';
1524 #print "k [$key][$value]\n" ;
1527 $db->get($key, $val) ;
1529 ok 183, $val == 34 ;
1530 ok 184, $_ eq 'fred';
1536 ok 186, $value == 454 ;
1537 ok 187, $_ eq 'fred';
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
1551 # Use of uninitialized value in subroutine entry
1557 my $Dfile = "xxy.db";
1560 ok(188, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
1563 local $SIG{__WARN__} = sub {$warned = $_[0]} ;
1565 # db-put with substr of key
1567 for my $ix ( 10 .. 12 )
1569 my $key = $ix . "data" ;
1570 my $value = "value$ix" ;
1571 $remember{$key} = $value ;
1572 $db->put(substr($key,0), $value) ;
1575 ok 189, $warned eq ''
1576 or print "# Caught warning [$warned]\n" ;
1578 # db-put with substr of value
1580 for my $ix ( 20 .. 22 )
1582 my $key = $ix . "data" ;
1583 my $value = "value$ix" ;
1584 $remember{$key} = $value ;
1585 $db->put($key, substr($value,0)) ;
1588 ok 190, $warned eq ''
1589 or print "# Caught warning [$warned]\n" ;
1591 # via the tied hash is not a problem, but check anyway
1594 for my $ix ( 30 .. 32 )
1596 my $key = $ix . "data" ;
1597 my $value = "value$ix" ;
1598 $remember{$key} = $value ;
1599 $h{substr($key,0)} = $value ;
1602 ok 191, $warned eq ''
1603 or print "# Caught warning [$warned]\n" ;
1605 # via the tied hash is not a problem, but check anyway
1608 for my $ix ( 40 .. 42 )
1610 my $key = $ix . "data" ;
1611 my $value = "value$ix" ;
1612 $remember{$key} = $value ;
1613 $h{$key} = substr($value,0) ;
1616 ok 192, $warned eq ''
1617 or print "# Caught warning [$warned]\n" ;
1621 for ($status = $db->seq($key, $value, R_FIRST ) ;
1623 $status = $db->seq($key, $value, R_NEXT ) ) {
1625 #print "# key [$key] value [$value]\n" ;
1626 if (defined $remember{$key} && defined $value &&
1627 $remember{$key} eq $value) {
1628 delete $remember{$key} ;
1631 $bad{$key} = $value ;
1635 ok 193, keys %bad == 0 ;
1636 ok 194, keys %remember == 0 ;
1638 print "# missing -- $key $value\n" while ($key, $value) = each %remember;
1639 print "# bad -- $key $value\n" while ($key, $value) = each %bad;
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
1645 $db->put(undef, $value) ;
1646 ok 195, $warned eq ''
1647 or print "# Caught warning [$warned]\n" ;
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";
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" ;