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";
33 print "not " unless $result ;
39 my(@a) = unpack ("C*", $a) ;
40 my(@b) = unpack ("C*", $b) ;
42 my $len = (@a > @b ? @b : @a) ;
45 foreach $i ( 0 .. $len -1) {
46 return $a[$i] - $b[$i] if $a[$i] != $b[$i] ;
59 my $filename = shift ;
61 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
62 my $real_stdout = select($fh) ;
63 return bless [$fh, $real_stdout ] ;
78 open(CAT,$file) || die "Cannot open $file: $!";
81 $result = normalise($result) ;
88 my $result = docat($file);
104 my $db185mode = ($DB_File::db_version == 1 && ! $DB_File::db_185_compat) ;
105 my $null_keys_allowed = ($DB_File::db_ver < 2.004010
106 || $DB_File::db_ver >= 3.1 );
108 my $Dfile = "dbbtree.tmp";
113 # Check the interface to BTREEINFO
115 my $dbh = new DB_File::BTREEINFO ;
116 ok(1, ! defined $dbh->{flags}) ;
117 ok(2, ! defined $dbh->{cachesize}) ;
118 ok(3, ! defined $dbh->{psize}) ;
119 ok(4, ! defined $dbh->{lorder}) ;
120 ok(5, ! defined $dbh->{minkeypage}) ;
121 ok(6, ! defined $dbh->{maxkeypage}) ;
122 ok(7, ! defined $dbh->{compare}) ;
123 ok(8, ! defined $dbh->{prefix}) ;
125 $dbh->{flags} = 3000 ;
126 ok(9, $dbh->{flags} == 3000) ;
128 $dbh->{cachesize} = 9000 ;
129 ok(10, $dbh->{cachesize} == 9000);
131 $dbh->{psize} = 400 ;
132 ok(11, $dbh->{psize} == 400) ;
134 $dbh->{lorder} = 65 ;
135 ok(12, $dbh->{lorder} == 65) ;
137 $dbh->{minkeypage} = 123 ;
138 ok(13, $dbh->{minkeypage} == 123) ;
140 $dbh->{maxkeypage} = 1234 ;
141 ok(14, $dbh->{maxkeypage} == 1234 );
143 $dbh->{compare} = 1234 ;
144 ok(15, $dbh->{compare} == 1234) ;
146 $dbh->{prefix} = 1234 ;
147 ok(16, $dbh->{prefix} == 1234 );
149 # Check that an invalid entry is caught both for store & fetch
150 eval '$dbh->{fred} = 1234' ;
151 ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
152 eval 'my $q = $dbh->{fred}' ;
153 ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
155 # Now check the interface to BTREE
158 ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
159 die "Could not tie: $!" unless $X;
161 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
162 $blksize,$blocks) = stat($Dfile);
164 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
166 ok(20, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
169 my ($key, $value, $i);
170 while (($key,$value) = each(%h)) {
175 $h{'goner1'} = 'snork';
178 ok(22, $h{'abc'} eq 'ABC' );
179 ok(23, ! defined $h{'jimmy'} ) ;
180 ok(24, ! exists $h{'jimmy'} ) ;
181 ok(25, defined $h{'abc'} ) ;
184 $h{'jkl','mno'} = "JKL\034MNO";
185 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
189 $X->STORE('b', 'B') ;
202 $h{'goner2'} = 'snork';
206 # IMPORTANT - $X must be undefined before the untie otherwise the
207 # underlying DB close routine will not get called.
211 # tie to the same file again
212 ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
214 # Modify an entry from the previous tie
235 $h{'goner3'} = 'snork';
238 $X->DELETE('goner3');
241 my @values = values(%h);
243 ok(27, $#keys == 29 && $#values == 29) ;
246 while (($key,$value) = each(%h)) {
247 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
249 $i++ if $key eq $value;
255 @keys = ('blurfl', keys(%h), 'dyick');
256 ok(29, $#keys == 31) ;
258 #Check that the keys can be retrieved in order
260 my @c = sort lexical @b ;
261 ok(30, ArrayCompare(\@b, \@c)) ;
264 ok(31, $h{'foo'} eq '' ) ;
266 # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
267 # This feature was reenabled in version 3.1 of Berkeley DB.
269 if ($null_keys_allowed) {
271 $result = ( $h{''} eq 'bar' );
277 # check cache overflow and numeric keys and contents
279 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
280 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
283 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
284 $blksize,$blocks) = stat($Dfile);
287 @h{0..200} = 200..400;
288 my @foo = @h{0..200};
289 ok(35, join(':',200..400) eq join(':',@foo) );
291 # Now check all the non-tie specific stuff
294 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
295 # an existing record.
297 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
298 ok(36, $status == 1 );
300 # check that the value of the key 'x' has not been changed by the
302 ok(37, $h{'x'} eq 'X' );
305 $status = $X->put('key', 'value') ;
306 ok(38, $status == 0 );
308 #check that previous put can be retrieved
310 $status = $X->get('key', $value) ;
311 ok(39, $status == 0 );
312 ok(40, $value eq 'value' );
314 # Attempting to delete an existing key should work
316 $status = $X->del('q') ;
317 ok(41, $status == 0 );
318 if ($null_keys_allowed) {
319 $status = $X->del('') ;
323 ok(42, $status == 0 );
325 # Make sure that the key deleted, cannot be retrieved
326 ok(43, ! defined $h{'q'}) ;
327 ok(44, ! defined $h{''}) ;
332 ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
334 # Attempting to delete a non-existant key should fail
336 $status = $X->del('joe') ;
337 ok(46, $status == 1 );
339 # Check the get interface
341 # First a non-existing key
342 $status = $X->get('aaaa', $value) ;
343 ok(47, $status == 1 );
345 # Next an existing key
346 $status = $X->get('a', $value) ;
347 ok(48, $status == 0 );
348 ok(49, $value eq 'A' );
353 # use seq to find an approximate match
356 $status = $X->seq($key, $value, R_CURSOR) ;
357 ok(50, $status == 0 );
358 ok(51, $key eq 'key' );
359 ok(52, $value eq 'value' );
361 # seq when the key does not match
364 $status = $X->seq($key, $value, R_CURSOR) ;
365 ok(53, $status == 1 );
368 # use seq to set the cursor, then delete the record @ the cursor.
372 $status = $X->seq($key, $value, R_CURSOR) ;
373 ok(54, $status == 0 );
374 ok(55, $key eq 'x' );
375 ok(56, $value eq 'X' );
376 $status = $X->del(0, R_CURSOR) ;
377 ok(57, $status == 0 );
378 $status = $X->get('x', $value) ;
379 ok(58, $status == 1 );
381 # ditto, but use put to replace the key/value pair.
384 $status = $X->seq($key, $value, R_CURSOR) ;
385 ok(59, $status == 0 );
386 ok(60, $key eq 'y' );
387 ok(61, $value eq 'Y' );
389 $key = "replace key" ;
390 $value = "replace value" ;
391 $status = $X->put($key, $value, R_CURSOR) ;
392 ok(62, $status == 0 );
393 ok(63, $key eq 'replace key' );
394 ok(64, $value eq 'replace value' );
395 $status = $X->get('y', $value) ;
396 ok(65, 1) ; # hard-wire to always pass. the previous test ($status == 1)
397 # only worked because of a bug in 1.85/6
399 # use seq to walk forwards through a file
401 $status = $X->seq($key, $value, R_FIRST) ;
402 ok(66, $status == 0 );
403 my $previous = $key ;
406 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
408 ($ok = 0), last if ($previous cmp $key) == 1 ;
411 ok(67, $status == 1 );
414 # use seq to walk backwards through a file
415 $status = $X->seq($key, $value, R_LAST) ;
416 ok(69, $status == 0 );
420 while (($status = $X->seq($key, $value, R_PREV)) == 0)
422 ($ok = 0), last if ($previous cmp $key) == -1 ;
423 #print "key = [$key] value = [$value]\n" ;
426 ok(70, $status == 1 );
430 # check seq FIRST/LAST
436 ok(72, $status == 0 );
443 ok(73, $status != 0 );
451 # Now try an in memory file
453 ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
455 # fd with an in memory file should return failure
457 ok(75, $status == -1 );
464 my $bt = new DB_File::BTREEINFO ;
465 $bt->{flags} = R_DUP ;
467 ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
469 $hh{'Wall'} = 'Larry' ;
470 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
471 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
472 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
473 $hh{'Smith'} = 'John' ;
474 $hh{'mouse'} = 'mickey' ;
476 # first work in scalar context
477 ok(77, scalar $YY->get_dup('Unknown') == 0 );
478 ok(78, scalar $YY->get_dup('Smith') == 1 );
479 ok(79, scalar $YY->get_dup('Wall') == 4 );
481 # now in list context
482 my @unknown = $YY->get_dup('Unknown') ;
483 ok(80, "@unknown" eq "" );
485 my @smith = $YY->get_dup('Smith') ;
486 ok(81, "@smith" eq "John" );
489 my @wall = $YY->get_dup('Wall') ;
491 @wall{@wall} = @wall ;
492 ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
496 my %unknown = $YY->get_dup('Unknown', 1) ;
497 ok(83, keys %unknown == 0 );
499 my %smith = $YY->get_dup('Smith', 1) ;
500 ok(84, keys %smith == 1 && $smith{'John'}) ;
502 my %wall = $YY->get_dup('Wall', 1) ;
503 ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
504 && $wall{'Brick'} == 2);
511 # test multiple callbacks
512 my $Dfile1 = "btree1" ;
513 my $Dfile2 = "btree2" ;
514 my $Dfile3 = "btree3" ;
516 my $dbh1 = new DB_File::BTREEINFO ;
517 $dbh1->{compare} = sub {
518 no warnings 'numeric' ;
521 my $dbh2 = new DB_File::BTREEINFO ;
522 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
524 my $dbh3 = new DB_File::BTREEINFO ;
525 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
529 tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) or die $!;
530 tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) or die $!;
531 tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) or die $!;
533 my @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
534 my (@srt_1, @srt_2, @srt_3);
536 no warnings 'numeric' ;
537 @srt_1 = sort { $a <=> $b } @Keys ;
539 @srt_2 = sort { $a cmp $b } @Keys ;
540 @srt_3 = sort { length $a <=> length $b } @Keys ;
552 return 0 if @$a != @$b ;
554 foreach (1 .. length @$a)
556 return 0 unless $$a[$_] eq $$b[$_] ;
562 ok(86, ArrayCompare (\@srt_1, [keys %h]) );
563 ok(87, ArrayCompare (\@srt_2, [keys %g]) );
564 ok(88, ArrayCompare (\@srt_3, [keys %k]) );
569 unlink $Dfile1, $Dfile2, $Dfile3 ;
574 ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
576 { $h{$_} = $_ * 100 }
578 # check that there are 10 elements in the hash
580 while (($key,$value) = each(%h)) {
590 while (($key,$value) = each(%h)) {
599 # check that attempting to tie an array to a DB_BTREE will fail
601 my $filename = "xyz" ;
603 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ;
604 ok(92, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ;
616 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
628 @EXPORT = @DB_File::EXPORT ;
634 $self->SUPER::STORE($key, $value * 2) ;
640 $self->SUPER::FETCH($key) - 1 ;
647 $self->SUPER::put($key, $value * 3) ;
652 $self->SUPER::get($_[0], $_[1]) ;
660 my $value = $self->FETCH($key) ;
661 return "[[$value]]" ;
669 BEGIN { push @INC, '.'; }
671 main::ok(93, $@ eq "") ;
675 $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE );
678 main::ok(94, $@ eq "") ;
680 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
681 main::ok(95, $@ eq "") ;
682 main::ok(96, $ret == 5) ;
685 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
686 main::ok(97, $@ eq "") ;
687 main::ok(98, $ret == 10) ;
689 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
690 main::ok(99, $@ eq "" ) ;
691 main::ok(100, $ret == 1) ;
693 $ret = eval '$X->A_new_method("joe") ' ;
694 main::ok(101, $@ eq "") ;
695 main::ok(102, $ret eq "[[11]]") ;
699 unlink "SubDB.pm", "dbbtree.tmp" ;
708 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
713 my($fk, $sk, $fv, $sv) = @_ ;
715 $fetch_key eq $fk && $store_key eq $sk &&
716 $fetch_value eq $fv && $store_value eq $sv &&
720 ok(103, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
722 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
723 $db->filter_store_key (sub { $store_key = $_ }) ;
724 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
725 $db->filter_store_value (sub { $store_value = $_ }) ;
731 ok(104, checkOutput( "", "fred", "", "joe")) ;
733 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
734 ok(105, $h{"fred"} eq "joe");
736 ok(106, checkOutput( "", "fred", "joe", "")) ;
738 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
739 ok(107, $db->FIRSTKEY() eq "fred") ;
741 ok(108, checkOutput( "fred", "", "", "")) ;
743 # replace the filters, but remember the previous set
744 my ($old_fk) = $db->filter_fetch_key
745 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
746 my ($old_sk) = $db->filter_store_key
747 (sub { $_ = lc $_ ; $store_key = $_ }) ;
748 my ($old_fv) = $db->filter_fetch_value
749 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
750 my ($old_sv) = $db->filter_store_value
751 (sub { s/o/x/g; $store_value = $_ }) ;
753 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
756 ok(109, checkOutput( "", "fred", "", "Jxe")) ;
758 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
759 ok(110, $h{"Fred"} eq "[Jxe]");
761 ok(111, checkOutput( "", "fred", "[Jxe]", "")) ;
763 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
764 ok(112, $db->FIRSTKEY() eq "FRED") ;
766 ok(113, checkOutput( "FRED", "", "", "")) ;
768 # put the original filters back
769 $db->filter_fetch_key ($old_fk);
770 $db->filter_store_key ($old_sk);
771 $db->filter_fetch_value ($old_fv);
772 $db->filter_store_value ($old_sv);
774 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
776 ok(114, checkOutput( "", "fred", "", "joe")) ;
778 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
779 ok(115, $h{"fred"} eq "joe");
780 ok(116, checkOutput( "", "fred", "joe", "")) ;
782 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
783 ok(117, $db->FIRSTKEY() eq "fred") ;
784 ok(118, checkOutput( "fred", "", "", "")) ;
787 $db->filter_fetch_key (undef);
788 $db->filter_store_key (undef);
789 $db->filter_fetch_value (undef);
790 $db->filter_store_value (undef);
792 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
794 ok(119, checkOutput( "", "", "", "")) ;
796 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
797 ok(120, $h{"fred"} eq "joe");
798 ok(121, checkOutput( "", "", "", "")) ;
800 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
801 ok(122, $db->FIRSTKEY() eq "fred") ;
802 ok(123, checkOutput( "", "", "", "")) ;
810 # DBM Filter with a closure
817 ok(124, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
827 return sub { ++$count ;
829 $result{$name} = "$name - $count: [@kept]" ;
833 $db->filter_store_key(Closure("store key")) ;
834 $db->filter_store_value(Closure("store value")) ;
835 $db->filter_fetch_key(Closure("fetch key")) ;
836 $db->filter_fetch_value(Closure("fetch value")) ;
841 ok(125, $result{"store key"} eq "store key - 1: [fred]");
842 ok(126, $result{"store value"} eq "store value - 1: [joe]");
843 ok(127, ! defined $result{"fetch key"} );
844 ok(128, ! defined $result{"fetch value"} );
845 ok(129, $_ eq "original") ;
847 ok(130, $db->FIRSTKEY() eq "fred") ;
848 ok(131, $result{"store key"} eq "store key - 1: [fred]");
849 ok(132, $result{"store value"} eq "store value - 1: [joe]");
850 ok(133, $result{"fetch key"} eq "fetch key - 1: [fred]");
851 ok(134, ! defined $result{"fetch value"} );
852 ok(135, $_ eq "original") ;
855 ok(136, $result{"store key"} eq "store key - 2: [fred jim]");
856 ok(137, $result{"store value"} eq "store value - 2: [joe john]");
857 ok(138, $result{"fetch key"} eq "fetch key - 1: [fred]");
858 ok(139, ! defined $result{"fetch value"} );
859 ok(140, $_ eq "original") ;
861 ok(141, $h{"fred"} eq "joe");
862 ok(142, $result{"store key"} eq "store key - 3: [fred jim fred]");
863 ok(143, $result{"store value"} eq "store value - 2: [joe john]");
864 ok(144, $result{"fetch key"} eq "fetch key - 1: [fred]");
865 ok(145, $result{"fetch value"} eq "fetch value - 1: [joe]");
866 ok(146, $_ eq "original") ;
874 # DBM Filter recursion detection
880 ok(147, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
882 $db->filter_store_key (sub { $_ = $h{$_} }) ;
884 eval '$h{1} = 1234' ;
885 ok(148, $@ =~ /^recursion detected in filter_store_key at/ );
894 # Examples from the POD
899 my $redirect = new Redirect $file ;
904 use warnings FATAL => qw(all) ;
912 my ($key1, $key2) = @_ ;
913 "\L$key1" cmp "\L$key2" ;
916 # specify the Perl sub that will do the comparison
917 $DB_BTREE->{'compare'} = \&Compare ;
920 tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
921 or die "Cannot open file 'tree': $!\n" ;
923 # Add a key/value pair to the file
924 $h{'Wall'} = 'Larry' ;
925 $h{'Smith'} = 'John' ;
926 $h{'mouse'} = 'mickey' ;
927 $h{'duck'} = 'donald' ;
932 # Cycle through the keys printing them in order.
933 # Note it is not necessary to sort the keys as
934 # the btree will have kept them in order automatically.
943 delete $DB_BTREE->{'compare'} ;
945 ok(149, docat_del($file) eq <<'EOM') ;
952 my $redirect = new Redirect $file ;
957 use warnings FATAL => qw(all) ;
966 # Enable duplicate records
967 $DB_BTREE->{'flags'} = R_DUP ;
969 tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
970 or die "Cannot open $filename: $!\n";
972 # Add some key/value pairs to the file
973 $h{'Wall'} = 'Larry' ;
974 $h{'Wall'} = 'Brick' ; # Note the duplicate key
975 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
976 $h{'Smith'} = 'John' ;
977 $h{'mouse'} = 'mickey' ;
979 # iterate through the associative array
980 # and print each key/value pair.
982 { print "$_ -> $h{$_}\n" }
989 ok(150, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ;
1004 my $redirect = new Redirect $file ;
1009 use warnings FATAL => qw(all) ;
1013 my ($filename, $x, %h, $status, $key, $value);
1015 $filename = "tree" ;
1018 # Enable duplicate records
1019 $DB_BTREE->{'flags'} = R_DUP ;
1021 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1022 or die "Cannot open $filename: $!\n";
1024 # Add some key/value pairs to the file
1025 $h{'Wall'} = 'Larry' ;
1026 $h{'Wall'} = 'Brick' ; # Note the duplicate key
1027 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
1028 $h{'Smith'} = 'John' ;
1029 $h{'mouse'} = 'mickey' ;
1031 # iterate through the btree using seq
1032 # and print each key/value pair.
1034 for ($status = $x->seq($key, $value, R_FIRST) ;
1036 $status = $x->seq($key, $value, R_NEXT) )
1037 { print "$key -> $value\n" }
1044 ok(151, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ;
1060 my $redirect = new Redirect $file ;
1065 use warnings FATAL => qw(all) ;
1069 my ($filename, $x, %h);
1071 $filename = "tree" ;
1073 # Enable duplicate records
1074 $DB_BTREE->{'flags'} = R_DUP ;
1076 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1077 or die "Cannot open $filename: $!\n";
1079 my $cnt = $x->get_dup("Wall") ;
1080 print "Wall occurred $cnt times\n" ;
1082 my %hash = $x->get_dup("Wall", 1) ;
1083 print "Larry is there\n" if $hash{'Larry'} ;
1084 print "There are $hash{'Brick'} Brick Walls\n" ;
1086 my @list = sort $x->get_dup("Wall") ;
1087 print "Wall => [@list]\n" ;
1089 @list = $x->get_dup("Smith") ;
1090 print "Smith => [@list]\n" ;
1092 @list = $x->get_dup("Dog") ;
1093 print "Dog => [@list]\n" ;
1099 ok(152, docat_del($file) eq <<'EOM') ;
1100 Wall occurred 3 times
1102 There are 2 Brick Walls
1103 Wall => [Brick Brick Larry]
1109 my $redirect = new Redirect $file ;
1114 use warnings FATAL => qw(all) ;
1118 my ($filename, $x, %h, $found);
1120 $filename = "tree" ;
1122 # Enable duplicate records
1123 $DB_BTREE->{'flags'} = R_DUP ;
1125 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1126 or die "Cannot open $filename: $!\n";
1128 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1129 print "Larry Wall is $found there\n" ;
1131 $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
1132 print "Harry Wall is $found there\n" ;
1138 ok(153, docat_del($file) eq <<'EOM') ;
1140 Harry Wall is not there
1144 my $redirect = new Redirect $file ;
1149 use warnings FATAL => qw(all) ;
1153 my ($filename, $x, %h, $found);
1155 $filename = "tree" ;
1157 # Enable duplicate records
1158 $DB_BTREE->{'flags'} = R_DUP ;
1160 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1161 or die "Cannot open $filename: $!\n";
1163 $x->del_dup("Wall", "Larry") ;
1165 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1166 print "Larry Wall is $found there\n" ;
1174 ok(154, docat_del($file) eq <<'EOM') ;
1175 Larry Wall is not there
1179 my $redirect = new Redirect $file ;
1184 use warnings FATAL => qw(all) ;
1189 my ($filename, $x, %h, $st, $key, $value);
1195 my $orig_key = $key ;
1196 $x->seq($key, $value, R_CURSOR) ;
1197 print "$orig_key\t-> $key\t-> $value\n" ;
1200 $filename = "tree" ;
1203 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1204 or die "Cannot open $filename: $!\n";
1206 # Add some key/value pairs to the file
1207 $h{'mouse'} = 'mickey' ;
1208 $h{'Wall'} = 'Larry' ;
1209 $h{'Walls'} = 'Brick' ;
1210 $h{'Smith'} = 'John' ;
1214 print "IN ORDER\n" ;
1215 for ($st = $x->seq($key, $value, R_FIRST) ;
1217 $st = $x->seq($key, $value, R_NEXT) )
1219 { print "$key -> $value\n" }
1221 print "\nPARTIAL MATCH\n" ;
1234 ok(155, docat_del($file) eq <<'EOM') ;
1244 a -> mouse -> mickey
1255 # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1258 # my $k = "newest" ;
1260 # my $status = $db->put($k, $v, R_SETCURSOR) ;
1261 # print "status = [$status]\n" ;
1262 # ok(157, $status == 0) ;
1263 # $status = $db->del($k, R_CURSOR) ;
1264 # print "status = [$status]\n" ;
1265 # ok(158, $status == 0) ;
1267 # ok(159, $db->get($k, $v, R_CURSOR)) ;
1269 # ok(160, keys %h == 1) ;
1277 # Bug ID 20001013.009
1279 # test that $hash{KEY} = undef doesn't produce the warning
1280 # Use of uninitialized value in null operation
1288 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1290 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1291 or die "Can't open file: $!\n" ;
1299 # test that %hash = () doesn't produce the warning
1300 # Argument "" isn't numeric in entersub
1308 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1310 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1311 or die "Can't open file: $!\n" ;
1319 # When iterating over a tied hash using "each", the key passed to FETCH
1320 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
1321 # key in FETCH via a filter_fetch_key method we need to check that the
1322 # modified key doesn't get passed to NEXTKEY.
1323 # Also Test "keys" & "values" while we are at it.
1333 ok(158, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1334 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
1335 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
1337 $h{'Alpha_ABC'} = 2 ;
1338 $h{'Alpha_DEF'} = 5 ;
1340 ok(159, $h{'Alpha_ABC'} == 2);
1341 ok(160, $h{'Alpha_DEF'} == 5);
1343 my ($k, $v) = ("","");
1344 while (($k, $v) = each %h) {}
1345 ok(161, $bad_key == 0);
1348 foreach $k (keys %h) {}
1349 ok(162, $bad_key == 0);
1352 foreach $v (values %h) {}
1353 ok(163, $bad_key == 0);