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";
22 && $Config{db_version_major} == 1
23 && $Config{db_version_minor} == 0
24 && $Config{db_version_patch} == 0) {
27 # This test is known to crash in Mac OS X versions 10.1.4 (or earlier)
28 # 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-existant 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 );
457 ok(71, $status != 0 );
465 # Now try an in memory file
467 ok(72, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
469 # fd with an in memory file should return failure
471 ok(73, $status == -1 );
478 my $bt = new DB_File::BTREEINFO ;
479 $bt->{flags} = R_DUP ;
481 ok(74, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
483 $hh{'Wall'} = 'Larry' ;
484 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
485 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
486 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
487 $hh{'Smith'} = 'John' ;
488 $hh{'mouse'} = 'mickey' ;
490 # first work in scalar context
491 ok(75, scalar $YY->get_dup('Unknown') == 0 );
492 ok(76, scalar $YY->get_dup('Smith') == 1 );
493 ok(77, scalar $YY->get_dup('Wall') == 4 );
495 # now in list context
496 my @unknown = $YY->get_dup('Unknown') ;
497 ok(78, "@unknown" eq "" );
499 my @smith = $YY->get_dup('Smith') ;
500 ok(79, "@smith" eq "John" );
503 my @wall = $YY->get_dup('Wall') ;
505 @wall{@wall} = @wall ;
506 ok(80, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
510 my %unknown = $YY->get_dup('Unknown', 1) ;
511 ok(81, keys %unknown == 0 );
513 my %smith = $YY->get_dup('Smith', 1) ;
514 ok(82, keys %smith == 1 && $smith{'John'}) ;
516 my %wall = $YY->get_dup('Wall', 1) ;
517 ok(83, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
518 && $wall{'Brick'} == 2);
525 # test multiple callbacks
526 my $Dfile1 = "btree1" ;
527 my $Dfile2 = "btree2" ;
528 my $Dfile3 = "btree3" ;
530 my $dbh1 = new DB_File::BTREEINFO ;
531 $dbh1->{compare} = sub {
532 no warnings 'numeric' ;
535 my $dbh2 = new DB_File::BTREEINFO ;
536 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
538 my $dbh3 = new DB_File::BTREEINFO ;
539 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
543 tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) or die $!;
544 tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) or die $!;
545 tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) or die $!;
547 my @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
548 my (@srt_1, @srt_2, @srt_3);
550 no warnings 'numeric' ;
551 @srt_1 = sort { $a <=> $b } @Keys ;
553 @srt_2 = sort { $a cmp $b } @Keys ;
554 @srt_3 = sort { length $a <=> length $b } @Keys ;
566 return 0 if @$a != @$b ;
568 foreach (1 .. length @$a)
570 return 0 unless $$a[$_] eq $$b[$_] ;
576 ok(84, ArrayCompare (\@srt_1, [keys %h]) );
577 ok(85, ArrayCompare (\@srt_2, [keys %g]) );
578 ok(86, ArrayCompare (\@srt_3, [keys %k]) );
583 unlink $Dfile1, $Dfile2, $Dfile3 ;
588 ok(87, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
590 { $h{$_} = $_ * 100 }
592 # check that there are 10 elements in the hash
594 while (($key,$value) = each(%h)) {
604 while (($key,$value) = each(%h)) {
613 # check that attempting to tie an array to a DB_BTREE will fail
615 my $filename = "xyz" ;
617 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ;
618 ok(90, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ;
630 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
642 @EXPORT = @DB_File::EXPORT ;
648 $self->SUPER::STORE($key, $value * 2) ;
654 $self->SUPER::FETCH($key) - 1 ;
661 $self->SUPER::put($key, $value * 3) ;
666 $self->SUPER::get($_[0], $_[1]) ;
674 my $value = $self->FETCH($key) ;
675 return "[[$value]]" ;
683 BEGIN { push @INC, '.'; }
685 main::ok(91, $@ eq "") ;
689 $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE );
692 main::ok(92, $@ eq "") ;
694 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
695 main::ok(93, $@ eq "") ;
696 main::ok(94, $ret == 5) ;
699 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
700 main::ok(95, $@ eq "") ;
701 main::ok(96, $ret == 10) ;
703 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
704 main::ok(97, $@ eq "" ) ;
705 main::ok(98, $ret == 1) ;
707 $ret = eval '$X->A_new_method("joe") ' ;
708 main::ok(99, $@ eq "") ;
709 main::ok(100, $ret eq "[[11]]") ;
713 unlink "SubDB.pm", "dbbtree.tmp" ;
722 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
727 my($fk, $sk, $fv, $sv) = @_ ;
729 $fetch_key eq $fk && $store_key eq $sk &&
730 $fetch_value eq $fv && $store_value eq $sv &&
734 ok(101, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
736 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
737 $db->filter_store_key (sub { $store_key = $_ }) ;
738 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
739 $db->filter_store_value (sub { $store_value = $_ }) ;
745 ok(102, checkOutput( "", "fred", "", "joe")) ;
747 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
748 ok(103, $h{"fred"} eq "joe");
750 ok(104, checkOutput( "", "fred", "joe", "")) ;
752 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
753 ok(105, $db->FIRSTKEY() eq "fred") ;
755 ok(106, checkOutput( "fred", "", "", "")) ;
757 # replace the filters, but remember the previous set
758 my ($old_fk) = $db->filter_fetch_key
759 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
760 my ($old_sk) = $db->filter_store_key
761 (sub { $_ = lc $_ ; $store_key = $_ }) ;
762 my ($old_fv) = $db->filter_fetch_value
763 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
764 my ($old_sv) = $db->filter_store_value
765 (sub { s/o/x/g; $store_value = $_ }) ;
767 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
770 ok(107, checkOutput( "", "fred", "", "Jxe")) ;
772 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
773 ok(108, $h{"Fred"} eq "[Jxe]");
775 ok(109, checkOutput( "", "fred", "[Jxe]", "")) ;
777 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
778 ok(110, $db->FIRSTKEY() eq "FRED") ;
780 ok(111, checkOutput( "FRED", "", "", "")) ;
782 # put the original filters back
783 $db->filter_fetch_key ($old_fk);
784 $db->filter_store_key ($old_sk);
785 $db->filter_fetch_value ($old_fv);
786 $db->filter_store_value ($old_sv);
788 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
790 ok(112, checkOutput( "", "fred", "", "joe")) ;
792 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
793 ok(113, $h{"fred"} eq "joe");
794 ok(114, checkOutput( "", "fred", "joe", "")) ;
796 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
797 ok(115, $db->FIRSTKEY() eq "fred") ;
798 ok(116, checkOutput( "fred", "", "", "")) ;
801 $db->filter_fetch_key (undef);
802 $db->filter_store_key (undef);
803 $db->filter_fetch_value (undef);
804 $db->filter_store_value (undef);
806 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
808 ok(117, checkOutput( "", "", "", "")) ;
810 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
811 ok(118, $h{"fred"} eq "joe");
812 ok(119, checkOutput( "", "", "", "")) ;
814 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
815 ok(120, $db->FIRSTKEY() eq "fred") ;
816 ok(121, checkOutput( "", "", "", "")) ;
824 # DBM Filter with a closure
831 ok(122, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
841 return sub { ++$count ;
843 $result{$name} = "$name - $count: [@kept]" ;
847 $db->filter_store_key(Closure("store key")) ;
848 $db->filter_store_value(Closure("store value")) ;
849 $db->filter_fetch_key(Closure("fetch key")) ;
850 $db->filter_fetch_value(Closure("fetch value")) ;
855 ok(123, $result{"store key"} eq "store key - 1: [fred]");
856 ok(124, $result{"store value"} eq "store value - 1: [joe]");
857 ok(125, ! defined $result{"fetch key"} );
858 ok(126, ! defined $result{"fetch value"} );
859 ok(127, $_ eq "original") ;
861 ok(128, $db->FIRSTKEY() eq "fred") ;
862 ok(129, $result{"store key"} eq "store key - 1: [fred]");
863 ok(130, $result{"store value"} eq "store value - 1: [joe]");
864 ok(131, $result{"fetch key"} eq "fetch key - 1: [fred]");
865 ok(132, ! defined $result{"fetch value"} );
866 ok(133, $_ eq "original") ;
869 ok(134, $result{"store key"} eq "store key - 2: [fred jim]");
870 ok(135, $result{"store value"} eq "store value - 2: [joe john]");
871 ok(136, $result{"fetch key"} eq "fetch key - 1: [fred]");
872 ok(137, ! defined $result{"fetch value"} );
873 ok(138, $_ eq "original") ;
875 ok(139, $h{"fred"} eq "joe");
876 ok(140, $result{"store key"} eq "store key - 3: [fred jim fred]");
877 ok(141, $result{"store value"} eq "store value - 2: [joe john]");
878 ok(142, $result{"fetch key"} eq "fetch key - 1: [fred]");
879 ok(143, $result{"fetch value"} eq "fetch value - 1: [joe]");
880 ok(144, $_ eq "original") ;
888 # DBM Filter recursion detection
894 ok(145, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
896 $db->filter_store_key (sub { $_ = $h{$_} }) ;
898 eval '$h{1} = 1234' ;
899 ok(146, $@ =~ /^recursion detected in filter_store_key at/ );
908 # Examples from the POD
913 my $redirect = new Redirect $file ;
918 use warnings FATAL => qw(all) ;
926 my ($key1, $key2) = @_ ;
927 "\L$key1" cmp "\L$key2" ;
930 # specify the Perl sub that will do the comparison
931 $DB_BTREE->{'compare'} = \&Compare ;
934 tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
935 or die "Cannot open file 'tree': $!\n" ;
937 # Add a key/value pair to the file
938 $h{'Wall'} = 'Larry' ;
939 $h{'Smith'} = 'John' ;
940 $h{'mouse'} = 'mickey' ;
941 $h{'duck'} = 'donald' ;
946 # Cycle through the keys printing them in order.
947 # Note it is not necessary to sort the keys as
948 # the btree will have kept them in order automatically.
957 delete $DB_BTREE->{'compare'} ;
959 ok(147, docat_del($file) eq <<'EOM') ;
966 my $redirect = new Redirect $file ;
971 use warnings FATAL => qw(all) ;
980 # Enable duplicate records
981 $DB_BTREE->{'flags'} = R_DUP ;
983 tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
984 or die "Cannot open $filename: $!\n";
986 # Add some key/value pairs to the file
987 $h{'Wall'} = 'Larry' ;
988 $h{'Wall'} = 'Brick' ; # Note the duplicate key
989 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
990 $h{'Smith'} = 'John' ;
991 $h{'mouse'} = 'mickey' ;
993 # iterate through the associative array
994 # and print each key/value pair.
996 { print "$_ -> $h{$_}\n" }
1003 ok(148, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ;
1018 my $redirect = new Redirect $file ;
1023 use warnings FATAL => qw(all) ;
1027 my ($filename, $x, %h, $status, $key, $value);
1029 $filename = "tree" ;
1032 # Enable duplicate records
1033 $DB_BTREE->{'flags'} = R_DUP ;
1035 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1036 or die "Cannot open $filename: $!\n";
1038 # Add some key/value pairs to the file
1039 $h{'Wall'} = 'Larry' ;
1040 $h{'Wall'} = 'Brick' ; # Note the duplicate key
1041 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
1042 $h{'Smith'} = 'John' ;
1043 $h{'mouse'} = 'mickey' ;
1045 # iterate through the btree using seq
1046 # and print each key/value pair.
1048 for ($status = $x->seq($key, $value, R_FIRST) ;
1050 $status = $x->seq($key, $value, R_NEXT) )
1051 { print "$key -> $value\n" }
1058 ok(149, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ;
1074 my $redirect = new Redirect $file ;
1079 use warnings FATAL => qw(all) ;
1083 my ($filename, $x, %h);
1085 $filename = "tree" ;
1087 # Enable duplicate records
1088 $DB_BTREE->{'flags'} = R_DUP ;
1090 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1091 or die "Cannot open $filename: $!\n";
1093 my $cnt = $x->get_dup("Wall") ;
1094 print "Wall occurred $cnt times\n" ;
1096 my %hash = $x->get_dup("Wall", 1) ;
1097 print "Larry is there\n" if $hash{'Larry'} ;
1098 print "There are $hash{'Brick'} Brick Walls\n" ;
1100 my @list = sort $x->get_dup("Wall") ;
1101 print "Wall => [@list]\n" ;
1103 @list = $x->get_dup("Smith") ;
1104 print "Smith => [@list]\n" ;
1106 @list = $x->get_dup("Dog") ;
1107 print "Dog => [@list]\n" ;
1113 ok(150, docat_del($file) eq <<'EOM') ;
1114 Wall occurred 3 times
1116 There are 2 Brick Walls
1117 Wall => [Brick Brick Larry]
1123 my $redirect = new Redirect $file ;
1128 use warnings FATAL => qw(all) ;
1132 my ($filename, $x, %h, $found);
1134 $filename = "tree" ;
1136 # Enable duplicate records
1137 $DB_BTREE->{'flags'} = R_DUP ;
1139 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1140 or die "Cannot open $filename: $!\n";
1142 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1143 print "Larry Wall is $found there\n" ;
1145 $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
1146 print "Harry Wall is $found there\n" ;
1152 ok(151, docat_del($file) eq <<'EOM') ;
1154 Harry Wall is not there
1158 my $redirect = new Redirect $file ;
1163 use warnings FATAL => qw(all) ;
1167 my ($filename, $x, %h, $found);
1169 $filename = "tree" ;
1171 # Enable duplicate records
1172 $DB_BTREE->{'flags'} = R_DUP ;
1174 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1175 or die "Cannot open $filename: $!\n";
1177 $x->del_dup("Wall", "Larry") ;
1179 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1180 print "Larry Wall is $found there\n" ;
1188 ok(152, docat_del($file) eq <<'EOM') ;
1189 Larry Wall is not there
1193 my $redirect = new Redirect $file ;
1198 use warnings FATAL => qw(all) ;
1203 my ($filename, $x, %h, $st, $key, $value);
1209 my $orig_key = $key ;
1210 $x->seq($key, $value, R_CURSOR) ;
1211 print "$orig_key\t-> $key\t-> $value\n" ;
1214 $filename = "tree" ;
1217 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1218 or die "Cannot open $filename: $!\n";
1220 # Add some key/value pairs to the file
1221 $h{'mouse'} = 'mickey' ;
1222 $h{'Wall'} = 'Larry' ;
1223 $h{'Walls'} = 'Brick' ;
1224 $h{'Smith'} = 'John' ;
1228 print "IN ORDER\n" ;
1229 for ($st = $x->seq($key, $value, R_FIRST) ;
1231 $st = $x->seq($key, $value, R_NEXT) )
1233 { print "$key -> $value\n" }
1235 print "\nPARTIAL MATCH\n" ;
1248 ok(153, docat_del($file) eq <<'EOM') ;
1258 a -> mouse -> mickey
1269 # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1272 # my $k = "newest" ;
1274 # my $status = $db->put($k, $v, R_SETCURSOR) ;
1275 # print "status = [$status]\n" ;
1276 # ok(157, $status == 0) ;
1277 # $status = $db->del($k, R_CURSOR) ;
1278 # print "status = [$status]\n" ;
1279 # ok(158, $status == 0) ;
1281 # ok(159, $db->get($k, $v, R_CURSOR)) ;
1283 # ok(160, keys %h == 1) ;
1291 # Bug ID 20001013.009
1293 # test that $hash{KEY} = undef doesn't produce the warning
1294 # Use of uninitialized value in null operation
1302 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1304 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1305 or die "Can't open file: $!\n" ;
1313 # test that %hash = () doesn't produce the warning
1314 # Argument "" isn't numeric in entersub
1322 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1324 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1325 or die "Can't open file: $!\n" ;
1333 # When iterating over a tied hash using "each", the key passed to FETCH
1334 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
1335 # key in FETCH via a filter_fetch_key method we need to check that the
1336 # modified key doesn't get passed to NEXTKEY.
1337 # Also Test "keys" & "values" while we are at it.
1347 ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1348 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
1349 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
1351 $h{'Alpha_ABC'} = 2 ;
1352 $h{'Alpha_DEF'} = 5 ;
1354 ok(157, $h{'Alpha_ABC'} == 2);
1355 ok(158, $h{'Alpha_DEF'} == 5);
1357 my ($k, $v) = ("","");
1358 while (($k, $v) = each %h) {}
1359 ok(159, $bad_key == 0);
1362 foreach $k (keys %h) {}
1363 ok(160, $bad_key == 0);
1366 foreach $v (values %h) {}
1367 ok(161, $bad_key == 0);
1375 # now an error to pass 'compare' a non-code reference
1376 my $dbh = new DB_File::BTREEINFO ;
1378 eval { $dbh->{compare} = 2 };
1379 ok(162, $@ =~ /^Key 'compare' not associated with a code reference at/);
1381 eval { $dbh->{prefix} = 2 };
1382 ok(163, $@ =~ /^Key 'prefix' not associated with a code reference at/);
1388 # recursion detection in btree
1391 my $dbh = new DB_File::BTREEINFO ;
1392 $dbh->{compare} = sub { $hash{3} = 4 ; length $_[0] } ;
1396 ok(164, tie(%hash, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh ) );
1398 eval { $hash{1} = 2;
1402 ok(165, $@ =~ /^DB_File btree_compare: recursion detected/);
1411 # Check that two callbacks don't interact
1416 unlink $Dfile, $Dfile2;
1417 my $dbh1 = new DB_File::BTREEINFO ;
1418 $dbh1->{compare} = sub { ++ $h1_count ; $_[0] cmp $_[1] } ;
1420 my $dbh2 = new DB_File::BTREEINFO ;
1421 $dbh2->{compare} = sub { ;++ $h2_count ; $_[0] cmp $_[1] } ;
1426 ok(166, tie(%hash1, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh1 ) );
1427 ok(167, tie(%hash2, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) );
1437 ok(168, $h1_count > 0);
1438 ok(169, $h1_count == $h2_count);
1440 ok(170, safeUntie \%hash1);
1441 ok(171, safeUntie \%hash2);
1442 unlink $Dfile, $Dfile2;
1446 # Check that DBM Filter can cope with read-only $_
1453 ok(172, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1455 $db->filter_fetch_key (sub { }) ;
1456 $db->filter_store_key (sub { }) ;
1457 $db->filter_fetch_value (sub { }) ;
1458 $db->filter_store_value (sub { }) ;
1462 $h{"fred"} = "joe" ;
1463 ok(173, $h{"fred"} eq "joe");
1465 eval { grep { $h{$_} } (1, 2, 3) };
1469 # delete the filters
1470 $db->filter_fetch_key (undef);
1471 $db->filter_store_key (undef);
1472 $db->filter_fetch_value (undef);
1473 $db->filter_store_value (undef);
1475 $h{"fred"} = "joe" ;
1477 ok(175, $h{"fred"} eq "joe");
1479 ok(176, $db->FIRSTKEY() eq "fred") ;
1481 eval { grep { $h{$_} } (1, 2, 3) };