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 )) ;
160 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
161 $blksize,$blocks) = stat($Dfile);
163 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
165 ok(20, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
168 my ($key, $value, $i);
169 while (($key,$value) = each(%h)) {
174 $h{'goner1'} = 'snork';
177 ok(22, $h{'abc'} eq 'ABC' );
178 ok(23, ! defined $h{'jimmy'} ) ;
179 ok(24, ! exists $h{'jimmy'} ) ;
180 ok(25, defined $h{'abc'} ) ;
183 $h{'jkl','mno'} = "JKL\034MNO";
184 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
188 $X->STORE('b', 'B') ;
201 $h{'goner2'} = 'snork';
205 # IMPORTANT - $X must be undefined before the untie otherwise the
206 # underlying DB close routine will not get called.
210 # tie to the same file again
211 ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
213 # Modify an entry from the previous tie
234 $h{'goner3'} = 'snork';
237 $X->DELETE('goner3');
240 my @values = values(%h);
242 ok(27, $#keys == 29 && $#values == 29) ;
245 while (($key,$value) = each(%h)) {
246 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
248 $i++ if $key eq $value;
254 @keys = ('blurfl', keys(%h), 'dyick');
255 ok(29, $#keys == 31) ;
257 #Check that the keys can be retrieved in order
259 my @c = sort lexical @b ;
260 ok(30, ArrayCompare(\@b, \@c)) ;
263 ok(31, $h{'foo'} eq '' ) ;
265 # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
266 # This feature was reenabled in version 3.1 of Berkeley DB.
268 if ($null_keys_allowed) {
270 $result = ( $h{''} eq 'bar' );
276 # check cache overflow and numeric keys and contents
278 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
279 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
282 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
283 $blksize,$blocks) = stat($Dfile);
286 @h{0..200} = 200..400;
287 my @foo = @h{0..200};
288 ok(35, join(':',200..400) eq join(':',@foo) );
290 # Now check all the non-tie specific stuff
293 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
294 # an existing record.
296 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
297 ok(36, $status == 1 );
299 # check that the value of the key 'x' has not been changed by the
301 ok(37, $h{'x'} eq 'X' );
304 $status = $X->put('key', 'value') ;
305 ok(38, $status == 0 );
307 #check that previous put can be retrieved
309 $status = $X->get('key', $value) ;
310 ok(39, $status == 0 );
311 ok(40, $value eq 'value' );
313 # Attempting to delete an existing key should work
315 $status = $X->del('q') ;
316 ok(41, $status == 0 );
317 if ($null_keys_allowed) {
318 $status = $X->del('') ;
322 ok(42, $status == 0 );
324 # Make sure that the key deleted, cannot be retrieved
325 ok(43, ! defined $h{'q'}) ;
326 ok(44, ! defined $h{''}) ;
331 ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
333 # Attempting to delete a non-existant key should fail
335 $status = $X->del('joe') ;
336 ok(46, $status == 1 );
338 # Check the get interface
340 # First a non-existing key
341 $status = $X->get('aaaa', $value) ;
342 ok(47, $status == 1 );
344 # Next an existing key
345 $status = $X->get('a', $value) ;
346 ok(48, $status == 0 );
347 ok(49, $value eq 'A' );
352 # use seq to find an approximate match
355 $status = $X->seq($key, $value, R_CURSOR) ;
356 ok(50, $status == 0 );
357 ok(51, $key eq 'key' );
358 ok(52, $value eq 'value' );
360 # seq when the key does not match
363 $status = $X->seq($key, $value, R_CURSOR) ;
364 ok(53, $status == 1 );
367 # use seq to set the cursor, then delete the record @ the cursor.
371 $status = $X->seq($key, $value, R_CURSOR) ;
372 ok(54, $status == 0 );
373 ok(55, $key eq 'x' );
374 ok(56, $value eq 'X' );
375 $status = $X->del(0, R_CURSOR) ;
376 ok(57, $status == 0 );
377 $status = $X->get('x', $value) ;
378 ok(58, $status == 1 );
380 # ditto, but use put to replace the key/value pair.
383 $status = $X->seq($key, $value, R_CURSOR) ;
384 ok(59, $status == 0 );
385 ok(60, $key eq 'y' );
386 ok(61, $value eq 'Y' );
388 $key = "replace key" ;
389 $value = "replace value" ;
390 $status = $X->put($key, $value, R_CURSOR) ;
391 ok(62, $status == 0 );
392 ok(63, $key eq 'replace key' );
393 ok(64, $value eq 'replace value' );
394 $status = $X->get('y', $value) ;
395 ok(65, 1) ; # hard-wire to always pass. the previous test ($status == 1)
396 # only worked because of a bug in 1.85/6
398 # use seq to walk forwards through a file
400 $status = $X->seq($key, $value, R_FIRST) ;
401 ok(66, $status == 0 );
402 my $previous = $key ;
405 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
407 ($ok = 0), last if ($previous cmp $key) == 1 ;
410 ok(67, $status == 1 );
413 # use seq to walk backwards through a file
414 $status = $X->seq($key, $value, R_LAST) ;
415 ok(69, $status == 0 );
419 while (($status = $X->seq($key, $value, R_PREV)) == 0)
421 ($ok = 0), last if ($previous cmp $key) == -1 ;
422 #print "key = [$key] value = [$value]\n" ;
425 ok(70, $status == 1 );
429 # check seq FIRST/LAST
435 ok(72, $status == 0 );
442 ok(73, $status != 0 );
450 # Now try an in memory file
452 ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
454 # fd with an in memory file should return failure
456 ok(75, $status == -1 );
463 my $bt = new DB_File::BTREEINFO ;
464 $bt->{flags} = R_DUP ;
466 ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
468 $hh{'Wall'} = 'Larry' ;
469 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
470 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
471 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
472 $hh{'Smith'} = 'John' ;
473 $hh{'mouse'} = 'mickey' ;
475 # first work in scalar context
476 ok(77, scalar $YY->get_dup('Unknown') == 0 );
477 ok(78, scalar $YY->get_dup('Smith') == 1 );
478 ok(79, scalar $YY->get_dup('Wall') == 4 );
480 # now in list context
481 my @unknown = $YY->get_dup('Unknown') ;
482 ok(80, "@unknown" eq "" );
484 my @smith = $YY->get_dup('Smith') ;
485 ok(81, "@smith" eq "John" );
488 my @wall = $YY->get_dup('Wall') ;
490 @wall{@wall} = @wall ;
491 ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
495 my %unknown = $YY->get_dup('Unknown', 1) ;
496 ok(83, keys %unknown == 0 );
498 my %smith = $YY->get_dup('Smith', 1) ;
499 ok(84, keys %smith == 1 && $smith{'John'}) ;
501 my %wall = $YY->get_dup('Wall', 1) ;
502 ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
503 && $wall{'Brick'} == 2);
510 # test multiple callbacks
511 my $Dfile1 = "btree1" ;
512 my $Dfile2 = "btree2" ;
513 my $Dfile3 = "btree3" ;
515 my $dbh1 = new DB_File::BTREEINFO ;
516 $dbh1->{compare} = sub {
517 no warnings 'numeric' ;
520 my $dbh2 = new DB_File::BTREEINFO ;
521 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
523 my $dbh3 = new DB_File::BTREEINFO ;
524 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
528 tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) ;
529 tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) ;
530 tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) ;
532 my @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
533 my (@srt_1, @srt_2, @srt_3);
535 no warnings 'numeric' ;
536 @srt_1 = sort { $a <=> $b } @Keys ;
538 @srt_2 = sort { $a cmp $b } @Keys ;
539 @srt_3 = sort { length $a <=> length $b } @Keys ;
551 return 0 if @$a != @$b ;
553 foreach (1 .. length @$a)
555 return 0 unless $$a[$_] eq $$b[$_] ;
561 ok(86, ArrayCompare (\@srt_1, [keys %h]) );
562 ok(87, ArrayCompare (\@srt_2, [keys %g]) );
563 ok(88, ArrayCompare (\@srt_3, [keys %k]) );
568 unlink $Dfile1, $Dfile2, $Dfile3 ;
573 ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
575 { $h{$_} = $_ * 100 }
577 # check that there are 10 elements in the hash
579 while (($key,$value) = each(%h)) {
589 while (($key,$value) = each(%h)) {
598 # check that attempting to tie an array to a DB_BTREE will fail
600 my $filename = "xyz" ;
602 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ;
603 ok(92, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ;
615 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
622 use vars qw( @ISA @EXPORT) ;
627 @EXPORT = @DB_File::EXPORT ;
633 $self->SUPER::STORE($key, $value * 2) ;
639 $self->SUPER::FETCH($key) - 1 ;
646 $self->SUPER::put($key, $value * 3) ;
651 $self->SUPER::get($_[0], $_[1]) ;
659 my $value = $self->FETCH($key) ;
660 return "[[$value]]" ;
668 BEGIN { push @INC, '.'; }
670 main::ok(93, $@ eq "") ;
674 $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE );
677 main::ok(94, $@ eq "") ;
679 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
680 main::ok(95, $@ eq "") ;
681 main::ok(96, $ret == 5) ;
684 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
685 main::ok(97, $@ eq "") ;
686 main::ok(98, $ret == 10) ;
688 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
689 main::ok(99, $@ eq "" ) ;
690 main::ok(100, $ret == 1) ;
692 $ret = eval '$X->A_new_method("joe") ' ;
693 main::ok(101, $@ eq "") ;
694 main::ok(102, $ret eq "[[11]]") ;
698 unlink "SubDB.pm", "dbbtree.tmp" ;
707 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
712 my($fk, $sk, $fv, $sv) = @_ ;
714 $fetch_key eq $fk && $store_key eq $sk &&
715 $fetch_value eq $fv && $store_value eq $sv &&
719 ok(103, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
721 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
722 $db->filter_store_key (sub { $store_key = $_ }) ;
723 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
724 $db->filter_store_value (sub { $store_value = $_ }) ;
730 ok(104, checkOutput( "", "fred", "", "joe")) ;
732 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
733 ok(105, $h{"fred"} eq "joe");
735 ok(106, checkOutput( "", "fred", "joe", "")) ;
737 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
738 ok(107, $db->FIRSTKEY() eq "fred") ;
740 ok(108, checkOutput( "fred", "", "", "")) ;
742 # replace the filters, but remember the previous set
743 my ($old_fk) = $db->filter_fetch_key
744 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
745 my ($old_sk) = $db->filter_store_key
746 (sub { $_ = lc $_ ; $store_key = $_ }) ;
747 my ($old_fv) = $db->filter_fetch_value
748 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
749 my ($old_sv) = $db->filter_store_value
750 (sub { s/o/x/g; $store_value = $_ }) ;
752 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
755 ok(109, checkOutput( "", "fred", "", "Jxe")) ;
757 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
758 ok(110, $h{"Fred"} eq "[Jxe]");
760 ok(111, checkOutput( "", "fred", "[Jxe]", "")) ;
762 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
763 ok(112, $db->FIRSTKEY() eq "FRED") ;
765 ok(113, checkOutput( "FRED", "", "", "")) ;
767 # put the original filters back
768 $db->filter_fetch_key ($old_fk);
769 $db->filter_store_key ($old_sk);
770 $db->filter_fetch_value ($old_fv);
771 $db->filter_store_value ($old_sv);
773 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
775 ok(114, checkOutput( "", "fred", "", "joe")) ;
777 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
778 ok(115, $h{"fred"} eq "joe");
779 ok(116, checkOutput( "", "fred", "joe", "")) ;
781 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
782 ok(117, $db->FIRSTKEY() eq "fred") ;
783 ok(118, checkOutput( "fred", "", "", "")) ;
786 $db->filter_fetch_key (undef);
787 $db->filter_store_key (undef);
788 $db->filter_fetch_value (undef);
789 $db->filter_store_value (undef);
791 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
793 ok(119, checkOutput( "", "", "", "")) ;
795 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
796 ok(120, $h{"fred"} eq "joe");
797 ok(121, checkOutput( "", "", "", "")) ;
799 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
800 ok(122, $db->FIRSTKEY() eq "fred") ;
801 ok(123, checkOutput( "", "", "", "")) ;
809 # DBM Filter with a closure
816 ok(124, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
826 return sub { ++$count ;
828 $result{$name} = "$name - $count: [@kept]" ;
832 $db->filter_store_key(Closure("store key")) ;
833 $db->filter_store_value(Closure("store value")) ;
834 $db->filter_fetch_key(Closure("fetch key")) ;
835 $db->filter_fetch_value(Closure("fetch value")) ;
840 ok(125, $result{"store key"} eq "store key - 1: [fred]");
841 ok(126, $result{"store value"} eq "store value - 1: [joe]");
842 ok(127, ! defined $result{"fetch key"} );
843 ok(128, ! defined $result{"fetch value"} );
844 ok(129, $_ eq "original") ;
846 ok(130, $db->FIRSTKEY() eq "fred") ;
847 ok(131, $result{"store key"} eq "store key - 1: [fred]");
848 ok(132, $result{"store value"} eq "store value - 1: [joe]");
849 ok(133, $result{"fetch key"} eq "fetch key - 1: [fred]");
850 ok(134, ! defined $result{"fetch value"} );
851 ok(135, $_ eq "original") ;
854 ok(136, $result{"store key"} eq "store key - 2: [fred jim]");
855 ok(137, $result{"store value"} eq "store value - 2: [joe john]");
856 ok(138, $result{"fetch key"} eq "fetch key - 1: [fred]");
857 ok(139, ! defined $result{"fetch value"} );
858 ok(140, $_ eq "original") ;
860 ok(141, $h{"fred"} eq "joe");
861 ok(142, $result{"store key"} eq "store key - 3: [fred jim fred]");
862 ok(143, $result{"store value"} eq "store value - 2: [joe john]");
863 ok(144, $result{"fetch key"} eq "fetch key - 1: [fred]");
864 ok(145, $result{"fetch value"} eq "fetch value - 1: [joe]");
865 ok(146, $_ eq "original") ;
873 # DBM Filter recursion detection
879 ok(147, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
881 $db->filter_store_key (sub { $_ = $h{$_} }) ;
883 eval '$h{1} = 1234' ;
884 ok(148, $@ =~ /^recursion detected in filter_store_key at/ );
893 # Examples from the POD
898 my $redirect = new Redirect $file ;
903 use warnings FATAL => qw(all) ;
911 my ($key1, $key2) = @_ ;
912 "\L$key1" cmp "\L$key2" ;
915 # specify the Perl sub that will do the comparison
916 $DB_BTREE->{'compare'} = \&Compare ;
919 tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
920 or die "Cannot open file 'tree': $!\n" ;
922 # Add a key/value pair to the file
923 $h{'Wall'} = 'Larry' ;
924 $h{'Smith'} = 'John' ;
925 $h{'mouse'} = 'mickey' ;
926 $h{'duck'} = 'donald' ;
931 # Cycle through the keys printing them in order.
932 # Note it is not necessary to sort the keys as
933 # the btree will have kept them in order automatically.
942 delete $DB_BTREE->{'compare'} ;
944 ok(149, docat_del($file) eq <<'EOM') ;
951 my $redirect = new Redirect $file ;
956 use warnings FATAL => qw(all) ;
960 use vars qw($filename %h ) ;
965 # Enable duplicate records
966 $DB_BTREE->{'flags'} = R_DUP ;
968 tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
969 or die "Cannot open $filename: $!\n";
971 # Add some key/value pairs to the file
972 $h{'Wall'} = 'Larry' ;
973 $h{'Wall'} = 'Brick' ; # Note the duplicate key
974 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
975 $h{'Smith'} = 'John' ;
976 $h{'mouse'} = 'mickey' ;
978 # iterate through the associative array
979 # and print each key/value pair.
981 { print "$_ -> $h{$_}\n" }
988 ok(150, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ;
1003 my $redirect = new Redirect $file ;
1008 use warnings FATAL => qw(all) ;
1012 use vars qw($filename $x %h $status $key $value) ;
1014 $filename = "tree" ;
1017 # Enable duplicate records
1018 $DB_BTREE->{'flags'} = R_DUP ;
1020 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1021 or die "Cannot open $filename: $!\n";
1023 # Add some key/value pairs to the file
1024 $h{'Wall'} = 'Larry' ;
1025 $h{'Wall'} = 'Brick' ; # Note the duplicate key
1026 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
1027 $h{'Smith'} = 'John' ;
1028 $h{'mouse'} = 'mickey' ;
1030 # iterate through the btree using seq
1031 # and print each key/value pair.
1033 for ($status = $x->seq($key, $value, R_FIRST) ;
1035 $status = $x->seq($key, $value, R_NEXT) )
1036 { print "$key -> $value\n" }
1043 ok(151, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ;
1059 my $redirect = new Redirect $file ;
1064 use warnings FATAL => qw(all) ;
1068 use vars qw($filename $x %h ) ;
1070 $filename = "tree" ;
1072 # Enable duplicate records
1073 $DB_BTREE->{'flags'} = R_DUP ;
1075 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1076 or die "Cannot open $filename: $!\n";
1078 my $cnt = $x->get_dup("Wall") ;
1079 print "Wall occurred $cnt times\n" ;
1081 my %hash = $x->get_dup("Wall", 1) ;
1082 print "Larry is there\n" if $hash{'Larry'} ;
1083 print "There are $hash{'Brick'} Brick Walls\n" ;
1085 my @list = sort $x->get_dup("Wall") ;
1086 print "Wall => [@list]\n" ;
1088 @list = $x->get_dup("Smith") ;
1089 print "Smith => [@list]\n" ;
1091 @list = $x->get_dup("Dog") ;
1092 print "Dog => [@list]\n" ;
1098 ok(152, docat_del($file) eq <<'EOM') ;
1099 Wall occurred 3 times
1101 There are 2 Brick Walls
1102 Wall => [Brick Brick Larry]
1108 my $redirect = new Redirect $file ;
1113 use warnings FATAL => qw(all) ;
1117 use vars qw($filename $x %h $found) ;
1119 my $filename = "tree" ;
1121 # Enable duplicate records
1122 $DB_BTREE->{'flags'} = R_DUP ;
1124 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1125 or die "Cannot open $filename: $!\n";
1127 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1128 print "Larry Wall is $found there\n" ;
1130 $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
1131 print "Harry Wall is $found there\n" ;
1137 ok(153, docat_del($file) eq <<'EOM') ;
1139 Harry Wall is not there
1143 my $redirect = new Redirect $file ;
1148 use warnings FATAL => qw(all) ;
1152 use vars qw($filename $x %h $found) ;
1154 my $filename = "tree" ;
1156 # Enable duplicate records
1157 $DB_BTREE->{'flags'} = R_DUP ;
1159 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1160 or die "Cannot open $filename: $!\n";
1162 $x->del_dup("Wall", "Larry") ;
1164 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1165 print "Larry Wall is $found there\n" ;
1173 ok(154, docat_del($file) eq <<'EOM') ;
1174 Larry Wall is not there
1178 my $redirect = new Redirect $file ;
1183 use warnings FATAL => qw(all) ;
1188 use vars qw($filename $x %h $st $key $value) ;
1194 my $orig_key = $key ;
1195 $x->seq($key, $value, R_CURSOR) ;
1196 print "$orig_key\t-> $key\t-> $value\n" ;
1199 $filename = "tree" ;
1202 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1203 or die "Cannot open $filename: $!\n";
1205 # Add some key/value pairs to the file
1206 $h{'mouse'} = 'mickey' ;
1207 $h{'Wall'} = 'Larry' ;
1208 $h{'Walls'} = 'Brick' ;
1209 $h{'Smith'} = 'John' ;
1213 print "IN ORDER\n" ;
1214 for ($st = $x->seq($key, $value, R_FIRST) ;
1216 $st = $x->seq($key, $value, R_NEXT) )
1218 { print "$key -> $value\n" }
1220 print "\nPARTIAL MATCH\n" ;
1233 ok(155, docat_del($file) eq <<'EOM') ;
1243 a -> mouse -> mickey
1254 # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1257 # my $k = "newest" ;
1259 # my $status = $db->put($k, $v, R_SETCURSOR) ;
1260 # print "status = [$status]\n" ;
1261 # ok(157, $status == 0) ;
1262 # $status = $db->del($k, R_CURSOR) ;
1263 # print "status = [$status]\n" ;
1264 # ok(158, $status == 0) ;
1266 # ok(159, $db->get($k, $v, R_CURSOR)) ;
1268 # ok(160, keys %h == 1) ;
1276 # Bug ID 20001013.009
1278 # test that $hash{KEY} = undef doesn't produce the warning
1279 # Use of uninitialized value in null operation
1287 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1289 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1290 or die "Can't open file: $!\n" ;
1298 # test that %hash = () doesn't produce the warning
1299 # Argument "" isn't numeric in entersub
1307 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1309 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1310 or die "Can't open file: $!\n" ;
1318 # When iterating over a tied hash using "each", the key passed to FETCH
1319 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
1320 # key in FETCH via a filter_fetch_key method we need to check that the
1321 # modified key doesn't get passed to NEXTKEY.
1322 # Also Test "keys" & "values" while we are at it.
1332 ok(158, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1333 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
1334 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
1336 $h{'Alpha_ABC'} = 2 ;
1337 $h{'Alpha_DEF'} = 5 ;
1339 ok(159, $h{'Alpha_ABC'} == 2);
1340 ok(160, $h{'Alpha_DEF'} == 5);
1342 my ($k, $v) = ("","");
1343 while (($k, $v) = each %h) {}
1344 ok(161, $bad_key == 0);
1347 foreach $k (keys %h) {}
1348 ok(162, $bad_key == 0);
1351 foreach $v (values %h) {}
1352 ok(163, $bad_key == 0);