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.3 (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 $db185mode = ($DB_File::db_version == 1 && ! $DB_File::db_185_compat) ;
116 my $null_keys_allowed = ($DB_File::db_ver < 2.004010
117 || $DB_File::db_ver >= 3.1 );
119 my $Dfile = "dbbtree.tmp";
124 # Check the interface to BTREEINFO
126 my $dbh = new DB_File::BTREEINFO ;
127 ok(1, ! defined $dbh->{flags}) ;
128 ok(2, ! defined $dbh->{cachesize}) ;
129 ok(3, ! defined $dbh->{psize}) ;
130 ok(4, ! defined $dbh->{lorder}) ;
131 ok(5, ! defined $dbh->{minkeypage}) ;
132 ok(6, ! defined $dbh->{maxkeypage}) ;
133 ok(7, ! defined $dbh->{compare}) ;
134 ok(8, ! defined $dbh->{prefix}) ;
136 $dbh->{flags} = 3000 ;
137 ok(9, $dbh->{flags} == 3000) ;
139 $dbh->{cachesize} = 9000 ;
140 ok(10, $dbh->{cachesize} == 9000);
142 $dbh->{psize} = 400 ;
143 ok(11, $dbh->{psize} == 400) ;
145 $dbh->{lorder} = 65 ;
146 ok(12, $dbh->{lorder} == 65) ;
148 $dbh->{minkeypage} = 123 ;
149 ok(13, $dbh->{minkeypage} == 123) ;
151 $dbh->{maxkeypage} = 1234 ;
152 ok(14, $dbh->{maxkeypage} == 1234 );
154 $dbh->{compare} = 1234 ;
155 ok(15, $dbh->{compare} == 1234) ;
157 $dbh->{prefix} = 1234 ;
158 ok(16, $dbh->{prefix} == 1234 );
160 # Check that an invalid entry is caught both for store & fetch
161 eval '$dbh->{fred} = 1234' ;
162 ok(17, $@ =~ /^DB_File::BTREEINFO::STORE - Unknown element 'fred' at/ ) ;
163 eval 'my $q = $dbh->{fred}' ;
164 ok(18, $@ =~ /^DB_File::BTREEINFO::FETCH - Unknown element 'fred' at/ ) ;
166 # Now check the interface to BTREE
169 ok(19, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE )) ;
170 die "Could not tie: $!" unless $X;
172 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
173 $blksize,$blocks) = stat($Dfile);
175 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
177 ok(20, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
180 my ($key, $value, $i);
181 while (($key,$value) = each(%h)) {
186 $h{'goner1'} = 'snork';
189 ok(22, $h{'abc'} eq 'ABC' );
190 ok(23, ! defined $h{'jimmy'} ) ;
191 ok(24, ! exists $h{'jimmy'} ) ;
192 ok(25, defined $h{'abc'} ) ;
195 $h{'jkl','mno'} = "JKL\034MNO";
196 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
200 $X->STORE('b', 'B') ;
213 $h{'goner2'} = 'snork';
217 # IMPORTANT - $X must be undefined before the untie otherwise the
218 # underlying DB close routine will not get called.
222 # tie to the same file again
223 ok(26, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE)) ;
225 # Modify an entry from the previous tie
246 $h{'goner3'} = 'snork';
249 $X->DELETE('goner3');
252 my @values = values(%h);
254 ok(27, $#keys == 29 && $#values == 29) ;
257 while (($key,$value) = each(%h)) {
258 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
260 $i++ if $key eq $value;
266 @keys = ('blurfl', keys(%h), 'dyick');
267 ok(29, $#keys == 31) ;
269 #Check that the keys can be retrieved in order
271 my @c = sort lexical @b ;
272 ok(30, ArrayCompare(\@b, \@c)) ;
275 ok(31, $h{'foo'} eq '' ) ;
277 # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
278 # This feature was reenabled in version 3.1 of Berkeley DB.
280 if ($null_keys_allowed) {
282 $result = ( $h{''} eq 'bar' );
288 # check cache overflow and numeric keys and contents
290 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
291 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
294 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
295 $blksize,$blocks) = stat($Dfile);
298 @h{0..200} = 200..400;
299 my @foo = @h{0..200};
300 ok(35, join(':',200..400) eq join(':',@foo) );
302 # Now check all the non-tie specific stuff
305 # Check R_NOOVERWRITE flag will make put fail when attempting to overwrite
306 # an existing record.
308 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
309 ok(36, $status == 1 );
311 # check that the value of the key 'x' has not been changed by the
313 ok(37, $h{'x'} eq 'X' );
316 $status = $X->put('key', 'value') ;
317 ok(38, $status == 0 );
319 #check that previous put can be retrieved
321 $status = $X->get('key', $value) ;
322 ok(39, $status == 0 );
323 ok(40, $value eq 'value' );
325 # Attempting to delete an existing key should work
327 $status = $X->del('q') ;
328 ok(41, $status == 0 );
329 if ($null_keys_allowed) {
330 $status = $X->del('') ;
334 ok(42, $status == 0 );
336 # Make sure that the key deleted, cannot be retrieved
337 ok(43, ! defined $h{'q'}) ;
338 ok(44, ! defined $h{''}) ;
343 ok(45, $X = tie(%h, 'DB_File',$Dfile, O_RDWR, 0640, $DB_BTREE ));
345 # Attempting to delete a non-existant key should fail
347 $status = $X->del('joe') ;
348 ok(46, $status == 1 );
350 # Check the get interface
352 # First a non-existing key
353 $status = $X->get('aaaa', $value) ;
354 ok(47, $status == 1 );
356 # Next an existing key
357 $status = $X->get('a', $value) ;
358 ok(48, $status == 0 );
359 ok(49, $value eq 'A' );
364 # use seq to find an approximate match
367 $status = $X->seq($key, $value, R_CURSOR) ;
368 ok(50, $status == 0 );
369 ok(51, $key eq 'key' );
370 ok(52, $value eq 'value' );
372 # seq when the key does not match
375 $status = $X->seq($key, $value, R_CURSOR) ;
376 ok(53, $status == 1 );
379 # use seq to set the cursor, then delete the record @ the cursor.
383 $status = $X->seq($key, $value, R_CURSOR) ;
384 ok(54, $status == 0 );
385 ok(55, $key eq 'x' );
386 ok(56, $value eq 'X' );
387 $status = $X->del(0, R_CURSOR) ;
388 ok(57, $status == 0 );
389 $status = $X->get('x', $value) ;
390 ok(58, $status == 1 );
392 # ditto, but use put to replace the key/value pair.
395 $status = $X->seq($key, $value, R_CURSOR) ;
396 ok(59, $status == 0 );
397 ok(60, $key eq 'y' );
398 ok(61, $value eq 'Y' );
400 $key = "replace key" ;
401 $value = "replace value" ;
402 $status = $X->put($key, $value, R_CURSOR) ;
403 ok(62, $status == 0 );
404 ok(63, $key eq 'replace key' );
405 ok(64, $value eq 'replace value' );
406 $status = $X->get('y', $value) ;
407 ok(65, 1) ; # hard-wire to always pass. the previous test ($status == 1)
408 # only worked because of a bug in 1.85/6
410 # use seq to walk forwards through a file
412 $status = $X->seq($key, $value, R_FIRST) ;
413 ok(66, $status == 0 );
414 my $previous = $key ;
417 while (($status = $X->seq($key, $value, R_NEXT)) == 0)
419 ($ok = 0), last if ($previous cmp $key) == 1 ;
422 ok(67, $status == 1 );
425 # use seq to walk backwards through a file
426 $status = $X->seq($key, $value, R_LAST) ;
427 ok(69, $status == 0 );
431 while (($status = $X->seq($key, $value, R_PREV)) == 0)
433 ($ok = 0), last if ($previous cmp $key) == -1 ;
434 #print "key = [$key] value = [$value]\n" ;
437 ok(70, $status == 1 );
441 # check seq FIRST/LAST
447 ok(72, $status == 0 );
454 ok(73, $status != 0 );
462 # Now try an in memory file
464 ok(74, $Y = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_BTREE ));
466 # fd with an in memory file should return failure
468 ok(75, $status == -1 );
475 my $bt = new DB_File::BTREEINFO ;
476 $bt->{flags} = R_DUP ;
478 ok(76, $YY = tie(%hh, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $bt )) ;
480 $hh{'Wall'} = 'Larry' ;
481 $hh{'Wall'} = 'Stone' ; # Note the duplicate key
482 $hh{'Wall'} = 'Brick' ; # Note the duplicate key
483 $hh{'Wall'} = 'Brick' ; # Note the duplicate key and value
484 $hh{'Smith'} = 'John' ;
485 $hh{'mouse'} = 'mickey' ;
487 # first work in scalar context
488 ok(77, scalar $YY->get_dup('Unknown') == 0 );
489 ok(78, scalar $YY->get_dup('Smith') == 1 );
490 ok(79, scalar $YY->get_dup('Wall') == 4 );
492 # now in list context
493 my @unknown = $YY->get_dup('Unknown') ;
494 ok(80, "@unknown" eq "" );
496 my @smith = $YY->get_dup('Smith') ;
497 ok(81, "@smith" eq "John" );
500 my @wall = $YY->get_dup('Wall') ;
502 @wall{@wall} = @wall ;
503 ok(82, (@wall == 4 && $wall{'Larry'} && $wall{'Stone'} && $wall{'Brick'}) );
507 my %unknown = $YY->get_dup('Unknown', 1) ;
508 ok(83, keys %unknown == 0 );
510 my %smith = $YY->get_dup('Smith', 1) ;
511 ok(84, keys %smith == 1 && $smith{'John'}) ;
513 my %wall = $YY->get_dup('Wall', 1) ;
514 ok(85, keys %wall == 3 && $wall{'Larry'} == 1 && $wall{'Stone'} == 1
515 && $wall{'Brick'} == 2);
522 # test multiple callbacks
523 my $Dfile1 = "btree1" ;
524 my $Dfile2 = "btree2" ;
525 my $Dfile3 = "btree3" ;
527 my $dbh1 = new DB_File::BTREEINFO ;
528 $dbh1->{compare} = sub {
529 no warnings 'numeric' ;
532 my $dbh2 = new DB_File::BTREEINFO ;
533 $dbh2->{compare} = sub { $_[0] cmp $_[1] } ;
535 my $dbh3 = new DB_File::BTREEINFO ;
536 $dbh3->{compare} = sub { length $_[0] <=> length $_[1] } ;
540 tie(%h, 'DB_File',$Dfile1, O_RDWR|O_CREAT, 0640, $dbh1 ) or die $!;
541 tie(%g, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) or die $!;
542 tie(%k, 'DB_File',$Dfile3, O_RDWR|O_CREAT, 0640, $dbh3 ) or die $!;
544 my @Keys = qw( 0123 12 -1234 9 987654321 def ) ;
545 my (@srt_1, @srt_2, @srt_3);
547 no warnings 'numeric' ;
548 @srt_1 = sort { $a <=> $b } @Keys ;
550 @srt_2 = sort { $a cmp $b } @Keys ;
551 @srt_3 = sort { length $a <=> length $b } @Keys ;
563 return 0 if @$a != @$b ;
565 foreach (1 .. length @$a)
567 return 0 unless $$a[$_] eq $$b[$_] ;
573 ok(86, ArrayCompare (\@srt_1, [keys %h]) );
574 ok(87, ArrayCompare (\@srt_2, [keys %g]) );
575 ok(88, ArrayCompare (\@srt_3, [keys %k]) );
580 unlink $Dfile1, $Dfile2, $Dfile3 ;
585 ok(89, tie(%h, 'DB_File', $Dfile1, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
587 { $h{$_} = $_ * 100 }
589 # check that there are 10 elements in the hash
591 while (($key,$value) = each(%h)) {
601 while (($key,$value) = each(%h)) {
610 # check that attempting to tie an array to a DB_BTREE will fail
612 my $filename = "xyz" ;
614 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE ; } ;
615 ok(92, $@ =~ /^DB_File can only tie an associative array to a DB_BTREE database/) ;
627 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
639 @EXPORT = @DB_File::EXPORT ;
645 $self->SUPER::STORE($key, $value * 2) ;
651 $self->SUPER::FETCH($key) - 1 ;
658 $self->SUPER::put($key, $value * 3) ;
663 $self->SUPER::get($_[0], $_[1]) ;
671 my $value = $self->FETCH($key) ;
672 return "[[$value]]" ;
680 BEGIN { push @INC, '.'; }
682 main::ok(93, $@ eq "") ;
686 $X = tie(%h, "SubDB","dbbtree.tmp", O_RDWR|O_CREAT, 0640, $DB_BTREE );
689 main::ok(94, $@ eq "") ;
691 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
692 main::ok(95, $@ eq "") ;
693 main::ok(96, $ret == 5) ;
696 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
697 main::ok(97, $@ eq "") ;
698 main::ok(98, $ret == 10) ;
700 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
701 main::ok(99, $@ eq "" ) ;
702 main::ok(100, $ret == 1) ;
704 $ret = eval '$X->A_new_method("joe") ' ;
705 main::ok(101, $@ eq "") ;
706 main::ok(102, $ret eq "[[11]]") ;
710 unlink "SubDB.pm", "dbbtree.tmp" ;
719 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
724 my($fk, $sk, $fv, $sv) = @_ ;
726 $fetch_key eq $fk && $store_key eq $sk &&
727 $fetch_value eq $fv && $store_value eq $sv &&
731 ok(103, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
733 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
734 $db->filter_store_key (sub { $store_key = $_ }) ;
735 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
736 $db->filter_store_value (sub { $store_value = $_ }) ;
742 ok(104, checkOutput( "", "fred", "", "joe")) ;
744 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
745 ok(105, $h{"fred"} eq "joe");
747 ok(106, checkOutput( "", "fred", "joe", "")) ;
749 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
750 ok(107, $db->FIRSTKEY() eq "fred") ;
752 ok(108, checkOutput( "fred", "", "", "")) ;
754 # replace the filters, but remember the previous set
755 my ($old_fk) = $db->filter_fetch_key
756 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
757 my ($old_sk) = $db->filter_store_key
758 (sub { $_ = lc $_ ; $store_key = $_ }) ;
759 my ($old_fv) = $db->filter_fetch_value
760 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
761 my ($old_sv) = $db->filter_store_value
762 (sub { s/o/x/g; $store_value = $_ }) ;
764 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
767 ok(109, checkOutput( "", "fred", "", "Jxe")) ;
769 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
770 ok(110, $h{"Fred"} eq "[Jxe]");
772 ok(111, checkOutput( "", "fred", "[Jxe]", "")) ;
774 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
775 ok(112, $db->FIRSTKEY() eq "FRED") ;
777 ok(113, checkOutput( "FRED", "", "", "")) ;
779 # put the original filters back
780 $db->filter_fetch_key ($old_fk);
781 $db->filter_store_key ($old_sk);
782 $db->filter_fetch_value ($old_fv);
783 $db->filter_store_value ($old_sv);
785 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
787 ok(114, checkOutput( "", "fred", "", "joe")) ;
789 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
790 ok(115, $h{"fred"} eq "joe");
791 ok(116, checkOutput( "", "fred", "joe", "")) ;
793 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
794 ok(117, $db->FIRSTKEY() eq "fred") ;
795 ok(118, checkOutput( "fred", "", "", "")) ;
798 $db->filter_fetch_key (undef);
799 $db->filter_store_key (undef);
800 $db->filter_fetch_value (undef);
801 $db->filter_store_value (undef);
803 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
805 ok(119, checkOutput( "", "", "", "")) ;
807 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
808 ok(120, $h{"fred"} eq "joe");
809 ok(121, checkOutput( "", "", "", "")) ;
811 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
812 ok(122, $db->FIRSTKEY() eq "fred") ;
813 ok(123, checkOutput( "", "", "", "")) ;
821 # DBM Filter with a closure
828 ok(124, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
838 return sub { ++$count ;
840 $result{$name} = "$name - $count: [@kept]" ;
844 $db->filter_store_key(Closure("store key")) ;
845 $db->filter_store_value(Closure("store value")) ;
846 $db->filter_fetch_key(Closure("fetch key")) ;
847 $db->filter_fetch_value(Closure("fetch value")) ;
852 ok(125, $result{"store key"} eq "store key - 1: [fred]");
853 ok(126, $result{"store value"} eq "store value - 1: [joe]");
854 ok(127, ! defined $result{"fetch key"} );
855 ok(128, ! defined $result{"fetch value"} );
856 ok(129, $_ eq "original") ;
858 ok(130, $db->FIRSTKEY() eq "fred") ;
859 ok(131, $result{"store key"} eq "store key - 1: [fred]");
860 ok(132, $result{"store value"} eq "store value - 1: [joe]");
861 ok(133, $result{"fetch key"} eq "fetch key - 1: [fred]");
862 ok(134, ! defined $result{"fetch value"} );
863 ok(135, $_ eq "original") ;
866 ok(136, $result{"store key"} eq "store key - 2: [fred jim]");
867 ok(137, $result{"store value"} eq "store value - 2: [joe john]");
868 ok(138, $result{"fetch key"} eq "fetch key - 1: [fred]");
869 ok(139, ! defined $result{"fetch value"} );
870 ok(140, $_ eq "original") ;
872 ok(141, $h{"fred"} eq "joe");
873 ok(142, $result{"store key"} eq "store key - 3: [fred jim fred]");
874 ok(143, $result{"store value"} eq "store value - 2: [joe john]");
875 ok(144, $result{"fetch key"} eq "fetch key - 1: [fred]");
876 ok(145, $result{"fetch value"} eq "fetch value - 1: [joe]");
877 ok(146, $_ eq "original") ;
885 # DBM Filter recursion detection
891 ok(147, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
893 $db->filter_store_key (sub { $_ = $h{$_} }) ;
895 eval '$h{1} = 1234' ;
896 ok(148, $@ =~ /^recursion detected in filter_store_key at/ );
905 # Examples from the POD
910 my $redirect = new Redirect $file ;
915 use warnings FATAL => qw(all) ;
923 my ($key1, $key2) = @_ ;
924 "\L$key1" cmp "\L$key2" ;
927 # specify the Perl sub that will do the comparison
928 $DB_BTREE->{'compare'} = \&Compare ;
931 tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
932 or die "Cannot open file 'tree': $!\n" ;
934 # Add a key/value pair to the file
935 $h{'Wall'} = 'Larry' ;
936 $h{'Smith'} = 'John' ;
937 $h{'mouse'} = 'mickey' ;
938 $h{'duck'} = 'donald' ;
943 # Cycle through the keys printing them in order.
944 # Note it is not necessary to sort the keys as
945 # the btree will have kept them in order automatically.
954 delete $DB_BTREE->{'compare'} ;
956 ok(149, docat_del($file) eq <<'EOM') ;
963 my $redirect = new Redirect $file ;
968 use warnings FATAL => qw(all) ;
977 # Enable duplicate records
978 $DB_BTREE->{'flags'} = R_DUP ;
980 tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
981 or die "Cannot open $filename: $!\n";
983 # Add some key/value pairs to the file
984 $h{'Wall'} = 'Larry' ;
985 $h{'Wall'} = 'Brick' ; # Note the duplicate key
986 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
987 $h{'Smith'} = 'John' ;
988 $h{'mouse'} = 'mickey' ;
990 # iterate through the associative array
991 # and print each key/value pair.
993 { print "$_ -> $h{$_}\n" }
1000 ok(150, docat_del($file) eq ($db185mode ? <<'EOM' : <<'EOM') ) ;
1015 my $redirect = new Redirect $file ;
1020 use warnings FATAL => qw(all) ;
1024 my ($filename, $x, %h, $status, $key, $value);
1026 $filename = "tree" ;
1029 # Enable duplicate records
1030 $DB_BTREE->{'flags'} = R_DUP ;
1032 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1033 or die "Cannot open $filename: $!\n";
1035 # Add some key/value pairs to the file
1036 $h{'Wall'} = 'Larry' ;
1037 $h{'Wall'} = 'Brick' ; # Note the duplicate key
1038 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
1039 $h{'Smith'} = 'John' ;
1040 $h{'mouse'} = 'mickey' ;
1042 # iterate through the btree using seq
1043 # and print each key/value pair.
1045 for ($status = $x->seq($key, $value, R_FIRST) ;
1047 $status = $x->seq($key, $value, R_NEXT) )
1048 { print "$key -> $value\n" }
1055 ok(151, docat_del($file) eq ($db185mode == 1 ? <<'EOM' : <<'EOM') ) ;
1071 my $redirect = new Redirect $file ;
1076 use warnings FATAL => qw(all) ;
1080 my ($filename, $x, %h);
1082 $filename = "tree" ;
1084 # Enable duplicate records
1085 $DB_BTREE->{'flags'} = R_DUP ;
1087 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1088 or die "Cannot open $filename: $!\n";
1090 my $cnt = $x->get_dup("Wall") ;
1091 print "Wall occurred $cnt times\n" ;
1093 my %hash = $x->get_dup("Wall", 1) ;
1094 print "Larry is there\n" if $hash{'Larry'} ;
1095 print "There are $hash{'Brick'} Brick Walls\n" ;
1097 my @list = sort $x->get_dup("Wall") ;
1098 print "Wall => [@list]\n" ;
1100 @list = $x->get_dup("Smith") ;
1101 print "Smith => [@list]\n" ;
1103 @list = $x->get_dup("Dog") ;
1104 print "Dog => [@list]\n" ;
1110 ok(152, docat_del($file) eq <<'EOM') ;
1111 Wall occurred 3 times
1113 There are 2 Brick Walls
1114 Wall => [Brick Brick Larry]
1120 my $redirect = new Redirect $file ;
1125 use warnings FATAL => qw(all) ;
1129 my ($filename, $x, %h, $found);
1131 $filename = "tree" ;
1133 # Enable duplicate records
1134 $DB_BTREE->{'flags'} = R_DUP ;
1136 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1137 or die "Cannot open $filename: $!\n";
1139 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1140 print "Larry Wall is $found there\n" ;
1142 $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
1143 print "Harry Wall is $found there\n" ;
1149 ok(153, docat_del($file) eq <<'EOM') ;
1151 Harry Wall is not there
1155 my $redirect = new Redirect $file ;
1160 use warnings FATAL => qw(all) ;
1164 my ($filename, $x, %h, $found);
1166 $filename = "tree" ;
1168 # Enable duplicate records
1169 $DB_BTREE->{'flags'} = R_DUP ;
1171 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1172 or die "Cannot open $filename: $!\n";
1174 $x->del_dup("Wall", "Larry") ;
1176 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1177 print "Larry Wall is $found there\n" ;
1185 ok(154, docat_del($file) eq <<'EOM') ;
1186 Larry Wall is not there
1190 my $redirect = new Redirect $file ;
1195 use warnings FATAL => qw(all) ;
1200 my ($filename, $x, %h, $st, $key, $value);
1206 my $orig_key = $key ;
1207 $x->seq($key, $value, R_CURSOR) ;
1208 print "$orig_key\t-> $key\t-> $value\n" ;
1211 $filename = "tree" ;
1214 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1215 or die "Cannot open $filename: $!\n";
1217 # Add some key/value pairs to the file
1218 $h{'mouse'} = 'mickey' ;
1219 $h{'Wall'} = 'Larry' ;
1220 $h{'Walls'} = 'Brick' ;
1221 $h{'Smith'} = 'John' ;
1225 print "IN ORDER\n" ;
1226 for ($st = $x->seq($key, $value, R_FIRST) ;
1228 $st = $x->seq($key, $value, R_NEXT) )
1230 { print "$key -> $value\n" }
1232 print "\nPARTIAL MATCH\n" ;
1245 ok(155, docat_del($file) eq <<'EOM') ;
1255 a -> mouse -> mickey
1266 # ok(156, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1269 # my $k = "newest" ;
1271 # my $status = $db->put($k, $v, R_SETCURSOR) ;
1272 # print "status = [$status]\n" ;
1273 # ok(157, $status == 0) ;
1274 # $status = $db->del($k, R_CURSOR) ;
1275 # print "status = [$status]\n" ;
1276 # ok(158, $status == 0) ;
1278 # ok(159, $db->get($k, $v, R_CURSOR)) ;
1280 # ok(160, keys %h == 1) ;
1288 # Bug ID 20001013.009
1290 # test that $hash{KEY} = undef doesn't produce the warning
1291 # Use of uninitialized value in null operation
1299 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1301 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1302 or die "Can't open file: $!\n" ;
1310 # test that %hash = () doesn't produce the warning
1311 # Argument "" isn't numeric in entersub
1319 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1321 tie %h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_BTREE
1322 or die "Can't open file: $!\n" ;
1330 # When iterating over a tied hash using "each", the key passed to FETCH
1331 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
1332 # key in FETCH via a filter_fetch_key method we need to check that the
1333 # modified key doesn't get passed to NEXTKEY.
1334 # Also Test "keys" & "values" while we are at it.
1344 ok(158, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_BTREE ) );
1345 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
1346 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
1348 $h{'Alpha_ABC'} = 2 ;
1349 $h{'Alpha_DEF'} = 5 ;
1351 ok(159, $h{'Alpha_ABC'} == 2);
1352 ok(160, $h{'Alpha_DEF'} == 5);
1354 my ($k, $v) = ("","");
1355 while (($k, $v) = each %h) {}
1356 ok(161, $bad_key == 0);
1359 foreach $k (keys %h) {}
1360 ok(162, $bad_key == 0);
1363 foreach $v (values %h) {}
1364 ok(163, $bad_key == 0);