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.2 (or earlier)
28 # because of the buggy Berkeley DB version included with the OS.
46 print "not " unless $result ;
52 my(@a) = unpack ("C*", $a) ;
53 my(@b) = unpack ("C*", $b) ;
55 my $len = (@a > @b ? @b : @a) ;
58 foreach $i ( 0 .. $len -1) {
59 return $a[$i] - $b[$i] if $a[$i] != $b[$i] ;
72 my $filename = shift ;
74 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
75 my $real_stdout = select($fh) ;
76 return bless [$fh, $real_stdout ] ;
91 open(CAT,$file) || die "Cannot open $file: $!";
94 $result = normalise($result) ;
101 my $result = docat($file);
117 my $hashref = shift ;
119 local $SIG{__WARN__} = sub {-- $no_inner } ;
126 my $db185mode = ($DB_File::db_version == 1 && ! $DB_File::db_185_compat) ;
127 my $null_keys_allowed = ($DB_File::db_ver < 2.004010
128 || $DB_File::db_ver >= 3.1 );
130 my $Dfile = "dbbtree.tmp";
135 # Check the interface to BTREEINFO
137 my $dbh = new DB_File::BTREEINFO ;
138 ok(1, ! defined $dbh->{flags}) ;
139 ok(2, ! defined $dbh->{cachesize}) ;
140 ok(3, ! defined $dbh->{psize}) ;
141 ok(4, ! defined $dbh->{lorder}) ;
142 ok(5, ! defined $dbh->{minkeypage}) ;
143 ok(6, ! defined $dbh->{maxkeypage}) ;
144 ok(7, ! defined $dbh->{compare}) ;
145 ok(8, ! defined $dbh->{prefix}) ;
147 $dbh->{flags} = 3000 ;
148 ok(9, $dbh->{flags} == 3000) ;
150 $dbh->{cachesize} = 9000 ;
151 ok(10, $dbh->{cachesize} == 9000);
153 $dbh->{psize} = 400 ;
154 ok(11, $dbh->{psize} == 400) ;
156 $dbh->{lorder} = 65 ;
157 ok(12, $dbh->{lorder} == 65) ;
159 $dbh->{minkeypage} = 123 ;
160 ok(13, $dbh->{minkeypage} == 123) ;
162 $dbh->{maxkeypage} = 1234 ;
163 ok(14, $dbh->{maxkeypage} == 1234 );
165 # Check that an invalid entry is caught both for store & fetch
166 eval '$dbh->{fred} = 1234' ;
167 ok(15, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
168 eval 'my $q = $dbh->{fred}' ;
169 ok(16, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
171 # Now check the interface to BTREE
174 ok(17, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
175 die "Could not tie: $!" unless $X;
177 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
178 $blksize,$blocks) = stat($Dfile);
180 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
182 ok(18, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
185 my ($key, $value, $i);
186 while (($key,$value) = each(%h)) {
191 $h{'goner1'} = 'snork';
194 ok(20, $h{'abc'} eq 'ABC' );
195 ok(21, ! defined $h{'jimmy'} ) ;
196 ok(22, ! exists $h{'jimmy'} ) ;
197 ok(23, defined $h{'abc'} ) ;
200 $h{'jkl','mno'} = "JKL\034MNO";
201 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
205 $X->STORE('b', 'B') ;
218 $h{'goner2'} = 'snork';
222 # IMPORTANT - $X must be undefined before the untie otherwise the
223 # underlying DB close routine will not get called.
227 # tie to the same file again
228 ok(24, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
230 # Modify an entry from the previous tie
251 $h{'goner3'} = 'snork';
254 $X->DELETE('goner3');
257 my @values = values(%h);
259 ok(25, $#keys == 29 && $#values == 29) ;
262 while (($key,$value) = each(%h)) {
263 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
265 $i++ if $key eq $value;
271 @keys = ('blurfl', keys(%h), 'dyick');
272 ok(27, $#keys == 31) ;
274 #Check that the keys can be retrieved in order
276 my @c = sort lexical @b ;
277 ok(28, ArrayCompare(\@b, \@c)) ;
280 ok(29, $h{'foo'} eq '' ) ;
282 # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
283 # This feature was reenabled in version 3.1 of Berkeley DB.
285 if ($null_keys_allowed) {
287 $result = ( $h{''} eq 'bar' );
293 # check cache overflow and numeric keys and contents
295 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
296 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
299 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
300 $blksize,$blocks) = stat($Dfile);
303 @h{0..200} = 200..400;
304 my @foo = @h{0..200};
305 ok(33, join(':',200..400) eq join(':',@foo) );
307 # Now check all the non-tie specific stuff
310 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
311 # an existing record.
313 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
314 ok(34, $status == 1 );
316 # check that the value of the key 'x' has not been changed by the
318 ok(35, $h{'x'} eq 'X' );
321 $status = $X->put('key', 'value') ;
322 ok(36, $status == 0 );
324 #check that previous put can be retrieved
326 $status = $X->get('key', $value) ;
327 ok(37, $status == 0 );
328 ok(38, $value eq 'value' );
330 # Attempting to delete an existing key should work
332 $status = $X->del('q') ;
333 ok(39, $status == 0 );
334 if ($null_keys_allowed) {
335 $status = $X->del('') ;
339 ok(40, $status == 0 );
341 # Make sure that the key deleted, cannot be retrieved
342 ok(41, ! defined $h{'q'}) ;
343 ok(42, ! defined $h{''}) ;
348 ok(43, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
350 # Attempting to delete a non-existant key should fail
352 $status = $X->del('joe') ;
353 ok(44, $status == 1 );
355 # Check the get interface
357 # First a non-existing key
358 $status = $X->get('aaaa', $value) ;
359 ok(45, $status == 1 );
361 # Next an existing key
362 $status = $X->get('a', $value) ;
363 ok(46, $status == 0 );
364 ok(47, $value eq 'A' );
369 # use seq to find an approximate match
372 $status = $X->seq($key, $value, R_CURSOR) ;
373 ok(48, $status == 0 );
374 ok(49, $key eq 'key' );
375 ok(50, $value eq 'value' );
377 # seq when the key does not match
380 $status = $X->seq($key, $value, R_CURSOR) ;
381 ok(51, $status == 1 );
384 # use seq to set the cursor, then delete the record @ the cursor.
388 $status = $X->seq($key, $value, R_CURSOR) ;
389 ok(52, $status == 0 );
390 ok(53, $key eq 'x' );
391 ok(54, $value eq 'X' );
392 $status = $X->del(0, R_CURSOR) ;
393 ok(55, $status == 0 );
394 $status = $X->get('x', $value) ;
395 ok(56, $status == 1 );
397 # ditto, but use put to replace the key/value pair.
400 $status = $X->seq($key, $value, R_CURSOR) ;
401 ok(57, $status == 0 );
402 ok(58, $key eq 'y' );
403 ok(59, $value eq 'Y' );
405 $key = "replace key" ;
406 $value = "replace value" ;
407 $status = $X->put($key, $value, R_CURSOR) ;
408 ok(60, $status == 0 );
409 ok(61, $key eq 'replace key' );
410 ok(62, $value eq 'replace value' );
411 $status = $X->get('y', $value) ;
412 ok(63, 1) ; # hard-wire to always pass. the previous test ($status == 1)
413 # only worked because of a bug in 1.85/6
415 # use seq to walk forwards through a file
417 $status = $X->seq($key, $value, R_FIRST) ;
418 ok(64, $status == 0 );
419 my $previous = $key ;
422 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
424 ($ok = 0), last if ($previous cmp $key) == 1 ;
427 ok(65, $status == 1 );
430 # use seq to walk backwards through a file
431 $status = $X->seq($key, $value, R_LAST) ;
432 ok(67, $status == 0 );
436 while (($status = $X->seq($key, $value, R_PREV)) == 0)
438 ($ok = 0), last if ($previous cmp $key) == -1 ;
439 #print "key = [$key] value = [$value]\n" ;
442 ok(68, $status == 1 );
446 # check seq FIRST/LAST
452 ok(70, $status == 0 );
459 ok(71, $status != 0 );
467 # Now try an in memory file
469 ok(72, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
471 # fd with an in memory file should return failure
473 ok(73, $status == -1 );
480 my $bt = new DB_File::BTREEINFO ;
481 $bt->{flags} = R_DUP ;
483 ok(74, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
485 $hh{'Wall'} = 'Larry' ;
486 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
487 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
488 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
489 $hh{'Smith'} = 'John' ;
490 $hh{'mouse'} = 'mickey' ;
492 # first work in scalar context
493 ok(75, scalar $YY->get_dup('Unknown') == 0 );
494 ok(76, scalar $YY->get_dup('Smith') == 1 );
495 ok(77, scalar $YY->get_dup('Wall') == 4 );
497 # now in list context
498 my @unknown = $YY->get_dup('Unknown') ;
499 ok(78, "@unknown" eq "" );
501 my @smith = $YY->get_dup('Smith') ;
502 ok(79, "@smith" eq "John" );
505 my @wall = $YY->get_dup('Wall') ;
507 @wall{@wall} = @wall ;
508 ok(80, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
512 my %unknown = $YY->get_dup('Unknown', 1) ;
513 ok(81, keys %unknown == 0 );
515 my %smith = $YY->get_dup('Smith', 1) ;
516 ok(82, keys %smith == 1 && $smith{'John'}) ;
518 my %wall = $YY->get_dup('Wall', 1) ;
519 ok(83, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
520 && $wall{'Brick'} == 2);
527 # test multiple callbacks
528 my $Dfile1 = "btree1" ;
529 my $Dfile2 = "btree2" ;
530 my $Dfile3 = "btree3" ;
532 my $dbh1 = new DB_File::BTREEINFO ;
533 $dbh1->{compare} = sub {
534 no warnings 'numeric' ;
537 my $dbh2 = new DB_File::BTREEINFO ;
538 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
540 my $dbh3 = new DB_File::BTREEINFO ;
541 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
545 tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) or die $!;
546 tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) or die $!;
547 tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) or die $!;
549 my @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
550 my (@srt_1, @srt_2, @srt_3);
552 no warnings 'numeric' ;
553 @srt_1 = sort { $a <=> $b } @Keys ;
555 @srt_2 = sort { $a cmp $b } @Keys ;
556 @srt_3 = sort { length $a <=> length $b } @Keys ;
568 return 0 if @$a != @$b ;
570 foreach (1 .. length @$a)
572 return 0 unless $$a[$_] eq $$b[$_] ;
578 ok(84, ArrayCompare (\@srt_1, [keys %h]) );
579 ok(85, ArrayCompare (\@srt_2, [keys %g]) );
580 ok(86, ArrayCompare (\@srt_3, [keys %k]) );
585 unlink $Dfile1, $Dfile2, $Dfile3 ;
590 ok(87, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
592 { $h{$_} = $_ * 100 }
594 # check that there are 10 elements in the hash
596 while (($key,$value) = each(%h)) {
606 while (($key,$value) = each(%h)) {
615 # check that attempting to tie an array to a DB_BTREE will fail
617 my $filename = "xyz" ;
619 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ;
620 ok(90, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ;
632 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
644 @EXPORT = @DB_File::EXPORT ;
650 $self->SUPER::STORE($key, $value * 2) ;
656 $self->SUPER::FETCH($key) - 1 ;
663 $self->SUPER::put($key, $value * 3) ;
668 $self->SUPER::get($_[0], $_[1]) ;
676 my $value = $self->FETCH($key) ;
677 return "[[$value]]" ;
685 BEGIN { push @INC, '.'; }
687 main::ok(91, $@ eq "") ;
691 $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE );
694 main::ok(92, $@ eq "") ;
696 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
697 main::ok(93, $@ eq "") ;
698 main::ok(94, $ret == 5) ;
701 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
702 main::ok(95, $@ eq "") ;
703 main::ok(96, $ret == 10) ;
705 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
706 main::ok(97, $@ eq "" ) ;
707 main::ok(98, $ret == 1) ;
709 $ret = eval '$X->A_new_method("joe") ' ;
710 main::ok(99, $@ eq "") ;
711 main::ok(100, $ret eq "[[11]]") ;
715 unlink "SubDB.pm", "dbbtree.tmp" ;
724 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
729 my($fk, $sk, $fv, $sv) = @_ ;
731 $fetch_key eq $fk && $store_key eq $sk &&
732 $fetch_value eq $fv && $store_value eq $sv &&
736 ok(101, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
738 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
739 $db->filter_store_key (sub { $store_key = $_ }) ;
740 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
741 $db->filter_store_value (sub { $store_value = $_ }) ;
747 ok(102, checkOutput( "", "fred", "", "joe")) ;
749 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
750 ok(103, $h{"fred"} eq "joe");
752 ok(104, checkOutput( "", "fred", "joe", "")) ;
754 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
755 ok(105, $db->FIRSTKEY() eq "fred") ;
757 ok(106, checkOutput( "fred", "", "", "")) ;
759 # replace the filters, but remember the previous set
760 my ($old_fk) = $db->filter_fetch_key
761 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
762 my ($old_sk) = $db->filter_store_key
763 (sub { $_ = lc $_ ; $store_key = $_ }) ;
764 my ($old_fv) = $db->filter_fetch_value
765 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
766 my ($old_sv) = $db->filter_store_value
767 (sub { s/o/x/g; $store_value = $_ }) ;
769 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
772 ok(107, checkOutput( "", "fred", "", "Jxe")) ;
774 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
775 ok(108, $h{"Fred"} eq "[Jxe]");
777 ok(109, checkOutput( "", "fred", "[Jxe]", "")) ;
779 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
780 ok(110, $db->FIRSTKEY() eq "FRED") ;
782 ok(111, checkOutput( "FRED", "", "", "")) ;
784 # put the original filters back
785 $db->filter_fetch_key ($old_fk);
786 $db->filter_store_key ($old_sk);
787 $db->filter_fetch_value ($old_fv);
788 $db->filter_store_value ($old_sv);
790 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
792 ok(112, checkOutput( "", "fred", "", "joe")) ;
794 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
795 ok(113, $h{"fred"} eq "joe");
796 ok(114, checkOutput( "", "fred", "joe", "")) ;
798 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
799 ok(115, $db->FIRSTKEY() eq "fred") ;
800 ok(116, checkOutput( "fred", "", "", "")) ;
803 $db->filter_fetch_key (undef);
804 $db->filter_store_key (undef);
805 $db->filter_fetch_value (undef);
806 $db->filter_store_value (undef);
808 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
810 ok(117, checkOutput( "", "", "", "")) ;
812 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
813 ok(118, $h{"fred"} eq "joe");
814 ok(119, checkOutput( "", "", "", "")) ;
816 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
817 ok(120, $db->FIRSTKEY() eq "fred") ;
818 ok(121, checkOutput( "", "", "", "")) ;
826 # DBM Filter with a closure
833 ok(122, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
843 return sub { ++$count ;
845 $result{$name} = "$name - $count: [@kept]" ;
849 $db->filter_store_key(Closure("store key")) ;
850 $db->filter_store_value(Closure("store value")) ;
851 $db->filter_fetch_key(Closure("fetch key")) ;
852 $db->filter_fetch_value(Closure("fetch value")) ;
857 ok(123, $result{"store key"} eq "store key - 1: [fred]");
858 ok(124, $result{"store value"} eq "store value - 1: [joe]");
859 ok(125, ! defined $result{"fetch key"} );
860 ok(126, ! defined $result{"fetch value"} );
861 ok(127, $_ eq "original") ;
863 ok(128, $db->FIRSTKEY() eq "fred") ;
864 ok(129, $result{"store key"} eq "store key - 1: [fred]");
865 ok(130, $result{"store value"} eq "store value - 1: [joe]");
866 ok(131, $result{"fetch key"} eq "fetch key - 1: [fred]");
867 ok(132, ! defined $result{"fetch value"} );
868 ok(133, $_ eq "original") ;
871 ok(134, $result{"store key"} eq "store key - 2: [fred jim]");
872 ok(135, $result{"store value"} eq "store value - 2: [joe john]");
873 ok(136, $result{"fetch key"} eq "fetch key - 1: [fred]");
874 ok(137, ! defined $result{"fetch value"} );
875 ok(138, $_ eq "original") ;
877 ok(139, $h{"fred"} eq "joe");
878 ok(140, $result{"store key"} eq "store key - 3: [fred jim fred]");
879 ok(141, $result{"store value"} eq "store value - 2: [joe john]");
880 ok(142, $result{"fetch key"} eq "fetch key - 1: [fred]");
881 ok(143, $result{"fetch value"} eq "fetch value - 1: [joe]");
882 ok(144, $_ eq "original") ;
890 # DBM Filter recursion detection
896 ok(145, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
898 $db->filter_store_key (sub { $_ = $h{$_} }) ;
900 eval '$h{1} = 1234' ;
901 ok(146, $@ =~ /^recursion detected in filter_store_key at/ );
910 # Examples from the POD
915 my $redirect = new Redirect $file ;
920 use warnings FATAL => qw(all) ;
928 my ($key1, $key2) = @_ ;
929 "\L$key1" cmp "\L$key2" ;
932 # specify the Perl sub that will do the comparison
933 $DB_BTREE->{'compare'} = \&Compare ;
936 tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
937 or die "Cannot open file 'tree': $!\n" ;
939 # Add a key/value pair to the file
940 $h{'Wall'} = 'Larry' ;
941 $h{'Smith'} = 'John' ;
942 $h{'mouse'} = 'mickey' ;
943 $h{'duck'} = 'donald' ;
948 # Cycle through the keys printing them in order.
949 # Note it is not necessary to sort the keys as
950 # the btree will have kept them in order automatically.
959 delete $DB_BTREE->{'compare'} ;
961 ok(147, docat_del($file) eq <<'EOM') ;
968 my $redirect = new Redirect $file ;
973 use warnings FATAL => qw(all) ;
982 # Enable duplicate records
983 $DB_BTREE->{'flags'} = R_DUP ;
985 tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
986 or die "Cannot open $filename: $!\n";
988 # Add some key/value pairs to the file
989 $h{'Wall'} = 'Larry' ;
990 $h{'Wall'} = 'Brick' ; # Note the duplicate key
991 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
992 $h{'Smith'} = 'John' ;
993 $h{'mouse'} = 'mickey' ;
995 # iterate through the associative array
996 # and print each key/value pair.
998 { print "$_ -> $h{$_}\n" }
1005 ok(148, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ;
1020 my $redirect = new Redirect $file ;
1025 use warnings FATAL => qw(all) ;
1029 my ($filename, $x, %h, $status, $key, $value);
1031 $filename = "tree" ;
1034 # Enable duplicate records
1035 $DB_BTREE->{'flags'} = R_DUP ;
1037 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1038 or die "Cannot open $filename: $!\n";
1040 # Add some key/value pairs to the file
1041 $h{'Wall'} = 'Larry' ;
1042 $h{'Wall'} = 'Brick' ; # Note the duplicate key
1043 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
1044 $h{'Smith'} = 'John' ;
1045 $h{'mouse'} = 'mickey' ;
1047 # iterate through the btree using seq
1048 # and print each key/value pair.
1050 for ($status = $x->seq($key, $value, R_FIRST) ;
1052 $status = $x->seq($key, $value, R_NEXT) )
1053 { print "$key -> $value\n" }
1060 ok(149, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ;
1076 my $redirect = new Redirect $file ;
1081 use warnings FATAL => qw(all) ;
1085 my ($filename, $x, %h);
1087 $filename = "tree" ;
1089 # Enable duplicate records
1090 $DB_BTREE->{'flags'} = R_DUP ;
1092 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1093 or die "Cannot open $filename: $!\n";
1095 my $cnt = $x->get_dup("Wall") ;
1096 print "Wall occurred $cnt times\n" ;
1098 my %hash = $x->get_dup("Wall", 1) ;
1099 print "Larry is there\n" if $hash{'Larry'} ;
1100 print "There are $hash{'Brick'} Brick Walls\n" ;
1102 my @list = sort $x->get_dup("Wall") ;
1103 print "Wall => [@list]\n" ;
1105 @list = $x->get_dup("Smith") ;
1106 print "Smith => [@list]\n" ;
1108 @list = $x->get_dup("Dog") ;
1109 print "Dog => [@list]\n" ;
1115 ok(150, docat_del($file) eq <<'EOM') ;
1116 Wall occurred 3 times
1118 There are 2 Brick Walls
1119 Wall => [Brick Brick Larry]
1125 my $redirect = new Redirect $file ;
1130 use warnings FATAL => qw(all) ;
1134 my ($filename, $x, %h, $found);
1136 $filename = "tree" ;
1138 # Enable duplicate records
1139 $DB_BTREE->{'flags'} = R_DUP ;
1141 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1142 or die "Cannot open $filename: $!\n";
1144 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1145 print "Larry Wall is $found there\n" ;
1147 $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
1148 print "Harry Wall is $found there\n" ;
1154 ok(151, docat_del($file) eq <<'EOM') ;
1156 Harry Wall is not there
1160 my $redirect = new Redirect $file ;
1165 use warnings FATAL => qw(all) ;
1169 my ($filename, $x, %h, $found);
1171 $filename = "tree" ;
1173 # Enable duplicate records
1174 $DB_BTREE->{'flags'} = R_DUP ;
1176 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1177 or die "Cannot open $filename: $!\n";
1179 $x->del_dup("Wall", "Larry") ;
1181 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1182 print "Larry Wall is $found there\n" ;
1190 ok(152, docat_del($file) eq <<'EOM') ;
1191 Larry Wall is not there
1195 my $redirect = new Redirect $file ;
1200 use warnings FATAL => qw(all) ;
1205 my ($filename, $x, %h, $st, $key, $value);
1211 my $orig_key = $key ;
1212 $x->seq($key, $value, R_CURSOR) ;
1213 print "$orig_key\t-> $key\t-> $value\n" ;
1216 $filename = "tree" ;
1219 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1220 or die "Cannot open $filename: $!\n";
1222 # Add some key/value pairs to the file
1223 $h{'mouse'} = 'mickey' ;
1224 $h{'Wall'} = 'Larry' ;
1225 $h{'Walls'} = 'Brick' ;
1226 $h{'Smith'} = 'John' ;
1230 print "IN ORDER\n" ;
1231 for ($st = $x->seq($key, $value, R_FIRST) ;
1233 $st = $x->seq($key, $value, R_NEXT) )
1235 { print "$key -> $value\n" }
1237 print "\nPARTIAL MATCH\n" ;
1250 ok(153, docat_del($file) eq <<'EOM') ;
1260 a -> mouse -> mickey
1271 # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1274 # my $k = "newest" ;
1276 # my $status = $db->put($k, $v, R_SETCURSOR) ;
1277 # print "status = [$status]\n" ;
1278 # ok(157, $status == 0) ;
1279 # $status = $db->del($k, R_CURSOR) ;
1280 # print "status = [$status]\n" ;
1281 # ok(158, $status == 0) ;
1283 # ok(159, $db->get($k, $v, R_CURSOR)) ;
1285 # ok(160, keys %h == 1) ;
1293 # Bug ID 20001013.009
1295 # test that $hash{KEY} = undef doesn't produce the warning
1296 # Use of uninitialized value in null operation
1304 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1306 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1307 or die "Can't open file: $!\n" ;
1315 # test that %hash = () doesn't produce the warning
1316 # Argument "" isn't numeric in entersub
1324 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1326 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1327 or die "Can't open file: $!\n" ;
1335 # When iterating over a tied hash using "each", the key passed to FETCH
1336 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
1337 # key in FETCH via a filter_fetch_key method we need to check that the
1338 # modified key doesn't get passed to NEXTKEY.
1339 # Also Test "keys" & "values" while we are at it.
1349 ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1350 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
1351 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
1353 $h{'Alpha_ABC'} = 2 ;
1354 $h{'Alpha_DEF'} = 5 ;
1356 ok(157, $h{'Alpha_ABC'} == 2);
1357 ok(158, $h{'Alpha_DEF'} == 5);
1359 my ($k, $v) = ("","");
1360 while (($k, $v) = each %h) {}
1361 ok(159, $bad_key == 0);
1364 foreach $k (keys %h) {}
1365 ok(160, $bad_key == 0);
1368 foreach $v (values %h) {}
1369 ok(161, $bad_key == 0);
1377 # now an error to pass 'compare' a non-code reference
1378 my $dbh = new DB_File::BTREEINFO ;
1380 eval { $dbh->{compare} = 2 };
1381 ok(162, $@ =~ /^Key 'compare' not associated with a code reference at/);
1383 eval { $dbh->{prefix} = 2 };
1384 ok(163, $@ =~ /^Key 'prefix' not associated with a code reference at/);
1390 # # recursion detection in btree
1393 # my $dbh = new DB_File::BTREEINFO ;
1394 # $dbh->{compare} = sub { $hash{3} = 4 ; length $_[0] } ;
1398 # ok(164, tie(%hash, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh ) );
1400 # eval { $hash{1} = 2;
1404 # ok(165, $@ =~ /^DB_File btree_compare: recursion detected/);
1415 # Check that two callbacks don't interact
1420 unlink $Dfile, $Dfile2;
1421 my $dbh1 = new DB_File::BTREEINFO ;
1422 $dbh1->{compare} = sub { ++ $h1_count ; $_[0] cmp $_[1] } ;
1424 my $dbh2 = new DB_File::BTREEINFO ;
1425 $dbh2->{compare} = sub { ;++ $h2_count ; $_[0] cmp $_[1] } ;
1430 ok(166, tie(%hash1, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh1 ) );
1431 ok(167, tie(%hash2, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) );
1441 ok(168, $h1_count > 0);
1442 ok(169, $h1_count == $h2_count);
1444 ok(170, safeUntie \%hash1);
1445 ok(171, safeUntie \%hash2);
1446 unlink $Dfile, $Dfile2;
1450 # Check that DBM Filter can cope with read-only $_
1457 ok(172, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1459 $db->filter_fetch_key (sub { }) ;
1460 $db->filter_store_key (sub { }) ;
1461 $db->filter_fetch_value (sub { }) ;
1462 $db->filter_store_value (sub { }) ;
1466 $h{"fred"} = "joe" ;
1467 ok(173, $h{"fred"} eq "joe");
1469 eval { grep { $h{$_} } (1, 2, 3) };
1473 # delete the filters
1474 $db->filter_fetch_key (undef);
1475 $db->filter_store_key (undef);
1476 $db->filter_fetch_value (undef);
1477 $db->filter_store_value (undef);
1479 $h{"fred"} = "joe" ;
1481 ok(175, $h{"fred"} eq "joe");
1483 ok(176, $db->FIRSTKEY() eq "fred") ;
1485 eval { grep { $h{$_} } (1, 2, 3) };