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";
35 print "not " unless $result ;
46 my $filename = shift ;
48 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
49 my $real_stdout = select($fh) ;
50 return bless [$fh, $real_stdout ] ;
65 open(CAT,$file) || die "Cannot open $file: $!";
68 $result = normalise($result) ;
85 local $SIG{__WARN__} = sub {-- $no_inner } ;
91 my $Dfile = "dbhash.tmp";
92 my $Dfile2 = "dbhash2.tmp";
93 my $null_keys_allowed = ($DB_File::db_ver < 2.004010
94 || $DB_File::db_ver >= 3.1 );
100 # Check the interface to HASHINFO
102 my $dbh = new DB_File::HASHINFO ;
104 ok(1, ! defined $dbh->{bsize}) ;
105 ok(2, ! defined $dbh->{ffactor}) ;
106 ok(3, ! defined $dbh->{nelem}) ;
107 ok(4, ! defined $dbh->{cachesize}) ;
108 ok(5, ! defined $dbh->{hash}) ;
109 ok(6, ! defined $dbh->{lorder}) ;
111 $dbh->{bsize} = 3000 ;
112 ok(7, $dbh->{bsize} == 3000 );
114 $dbh->{ffactor} = 9000 ;
115 ok(8, $dbh->{ffactor} == 9000 );
117 $dbh->{nelem} = 400 ;
118 ok(9, $dbh->{nelem} == 400 );
120 $dbh->{cachesize} = 65 ;
121 ok(10, $dbh->{cachesize} == 65 );
123 my $some_sub = sub {} ;
124 $dbh->{hash} = $some_sub;
125 ok(11, $dbh->{hash} eq $some_sub );
127 $dbh->{lorder} = 1234 ;
128 ok(12, $dbh->{lorder} == 1234 );
130 # Check that an invalid entry is caught both for store & fetch
131 eval '$dbh->{fred} = 1234' ;
132 ok(13, $@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ );
133 eval 'my $q = $dbh->{fred}' ;
134 ok(14, $@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ );
137 # Now check the interface to HASH
139 ok(15, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
140 die "Could not tie: $!" unless $X;
142 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
143 $blksize,$blocks) = stat($Dfile);
145 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
147 ok(16, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640) ||
150 my ($key, $value, $i);
151 while (($key,$value) = each(%h)) {
156 $h{'goner1'} = 'snork';
159 ok(18, $h{'abc'} eq 'ABC' );
160 ok(19, !defined $h{'jimmy'} );
161 ok(20, !exists $h{'jimmy'} );
162 ok(21, exists $h{'abc'} );
165 $h{'jkl','mno'} = "JKL\034MNO";
166 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
170 $X->STORE('b', 'B') ;
183 $h{'goner2'} = 'snork';
187 # IMPORTANT - $X must be undefined before the untie otherwise the
188 # underlying DB close routine will not get called.
193 # tie to the same file again, do not supply a type - should default to HASH
194 ok(22, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640) );
196 # Modify an entry from the previous tie
217 $h{'goner3'} = 'snork';
220 $X->DELETE('goner3');
223 my @values = values(%h);
225 ok(23, $#keys == 29 && $#values == 29) ;
228 while (($key,$value) = each(%h)) {
229 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
231 $i++ if $key eq $value;
237 @keys = ('blurfl', keys(%h), 'dyick');
238 ok(25, $#keys == 31) ;
241 ok(26, $h{'foo'} eq '' );
243 # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
244 # This feature was reenabled in version 3.1 of Berkeley DB.
246 if ($null_keys_allowed) {
248 $result = ( $h{''} eq 'bar' );
254 # check cache overflow and numeric keys and contents
256 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
257 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
260 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
261 $blksize,$blocks) = stat($Dfile);
264 @h{0..200} = 200..400;
265 my @foo = @h{0..200};
266 ok(30, join(':',200..400) eq join(':',@foo) );
269 # Now check all the non-tie specific stuff
271 # Check NOOVERWRITE will make put fail when attempting to overwrite
272 # an existing record.
274 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
275 ok(31, $status == 1 );
277 # check that the value of the key 'x' has not been changed by the
279 ok(32, $h{'x'} eq 'X' );
282 $status = $X->put('key', 'value') ;
283 ok(33, $status == 0 );
285 #check that previous put can be retrieved
287 $status = $X->get('key', $value) ;
288 ok(34, $status == 0 );
289 ok(35, $value eq 'value' );
291 # Attempting to delete an existing key should work
293 $status = $X->del('q') ;
294 ok(36, $status == 0 );
296 # Make sure that the key deleted, cannot be retrieved
298 no warnings 'uninitialized' ;
299 ok(37, $h{'q'} eq undef );
302 # Attempting to delete a non-existant key should fail
304 $status = $X->del('joe') ;
305 ok(38, $status == 1 );
307 # Check the get interface
309 # First a non-existing key
310 $status = $X->get('aaaa', $value) ;
311 ok(39, $status == 1 );
313 # Next an existing key
314 $status = $X->get('a', $value) ;
315 ok(40, $status == 0 );
316 ok(41, $value eq 'A' );
321 # ditto, but use put to replace the key/value pair.
323 # use seq to walk backwards through a file - check that this reversed is
325 # check seq FIRST/LAST
331 ok(42, $status == 0 );
338 ok(43, $status != 0 );
348 ok(44, tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
350 { $h{$_} = $_ * 100 }
352 # check that there are 10 elements in the hash
354 while (($key,$value) = each(%h)) {
364 while (($key,$value) = each(%h)) {
373 # Now try an in memory file
374 ok(47, $X = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
376 # fd with an in memory file should return fail
378 ok(48, $status == -1 );
384 # check ability to override the default hashing
386 my $filename = "xyz" ;
387 my $hi = new DB_File::HASHINFO ;
389 $hi->{hash} = sub { ++$::count ; length $_[0] } ;
390 ok(49, tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $hi ) ;
392 ok(50, $h{"abc"} == 123) ;
395 ok(51, $::count >0) ;
399 # check that attempting to tie an array to a DB_HASH will fail
401 my $filename = "xyz" ;
403 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_HASH ; } ;
404 ok(52, $@ =~ /^DB_File can only tie an associative array to a DB_HASH database/) ;
416 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
428 @EXPORT = @DB_File::EXPORT ;
434 $self->SUPER::STORE($key, $value * 2) ;
440 $self->SUPER::FETCH($key) - 1 ;
447 $self->SUPER::put($key, $value * 3) ;
452 $self->SUPER::get($_[0], $_[1]) ;
460 my $value = $self->FETCH($key) ;
461 return "[[$value]]" ;
469 BEGIN { push @INC, '.'; }
471 main::ok(53, $@ eq "") ;
475 $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640, $DB_HASH );
478 main::ok(54, $@ eq "") ;
480 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
481 main::ok(55, $@ eq "") ;
482 main::ok(56, $ret == 5) ;
485 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
486 main::ok(57, $@ eq "") ;
487 main::ok(58, $ret == 10) ;
489 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
490 main::ok(59, $@ eq "" ) ;
491 main::ok(60, $ret == 1) ;
493 $ret = eval '$X->A_new_method("joe") ' ;
494 main::ok(61, $@ eq "") ;
495 main::ok(62, $ret eq "[[11]]") ;
499 unlink "SubDB.pm", "dbhash.tmp" ;
508 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
513 no warnings 'uninitialized';
514 my($fk, $sk, $fv, $sv) = @_ ;
516 print "# Fetch Key : expected '$fk' got '$fetch_key'\n"
517 if $fetch_key ne $fk ;
518 print "# Fetch Value : expected '$fv' got '$fetch_value'\n"
519 if $fetch_value ne $fv ;
520 print "# Store Key : expected '$sk' got '$store_key'\n"
521 if $store_key ne $sk ;
522 print "# Store Value : expected '$sv' got '$store_value'\n"
523 if $store_value ne $sv ;
524 print "# \$_ : expected 'original' got '$_'\n"
525 if $_ ne 'original' ;
528 $fetch_key eq $fk && $store_key eq $sk &&
529 $fetch_value eq $fv && $store_value eq $sv &&
533 ok(63, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
535 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
536 $db->filter_store_key (sub { $store_key = $_ }) ;
537 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
538 $db->filter_store_value (sub { $store_value = $_ }) ;
544 ok(64, checkOutput( "", "fred", "", "joe")) ;
546 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
547 ok(65, $h{"fred"} eq "joe");
549 ok(66, checkOutput( "", "fred", "joe", "")) ;
551 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
554 ok(67, ! $db->seq($k, $v, R_FIRST) ) ;
555 ok(68, $k eq "fred") ;
556 ok(69, $v eq "joe") ;
558 ok(70, checkOutput( "fred", "fred", "joe", "")) ;
560 # replace the filters, but remember the previous set
561 my ($old_fk) = $db->filter_fetch_key
562 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
563 my ($old_sk) = $db->filter_store_key
564 (sub { $_ = lc $_ ; $store_key = $_ }) ;
565 my ($old_fv) = $db->filter_fetch_value
566 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
567 my ($old_sv) = $db->filter_store_value
568 (sub { s/o/x/g; $store_value = $_ }) ;
570 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
573 ok(71, checkOutput( "", "fred", "", "Jxe")) ;
575 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
576 ok(72, $h{"Fred"} eq "[Jxe]");
578 ok(73, checkOutput( "", "fred", "[Jxe]", "")) ;
580 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
582 ok(74, ! $db->seq($k, $v, R_FIRST) ) ;
583 ok(75, $k eq "FRED") ;
584 ok(76, $v eq "[Jxe]") ;
586 ok(77, checkOutput( "FRED", "fred", "[Jxe]", "")) ;
588 # put the original filters back
589 $db->filter_fetch_key ($old_fk);
590 $db->filter_store_key ($old_sk);
591 $db->filter_fetch_value ($old_fv);
592 $db->filter_store_value ($old_sv);
594 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
596 ok(78, checkOutput( "", "fred", "", "joe")) ;
598 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
599 ok(79, $h{"fred"} eq "joe");
600 ok(80, checkOutput( "", "fred", "joe", "")) ;
602 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
603 #ok(77, $db->FIRSTKEY() eq "fred") ;
605 ok(81, ! $db->seq($k, $v, R_FIRST) ) ;
606 ok(82, $k eq "fred") ;
607 ok(83, $v eq "joe") ;
609 ok(84, checkOutput( "fred", "fred", "joe", "")) ;
612 $db->filter_fetch_key (undef);
613 $db->filter_store_key (undef);
614 $db->filter_fetch_value (undef);
615 $db->filter_store_value (undef);
617 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
619 ok(85, checkOutput( "", "", "", "")) ;
621 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
622 ok(86, $h{"fred"} eq "joe");
623 ok(87, checkOutput( "", "", "", "")) ;
625 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
627 ok(88, ! $db->seq($k, $v, R_FIRST) ) ;
628 ok(89, $k eq "fred") ;
629 ok(90, $v eq "joe") ;
630 ok(91, checkOutput( "", "", "", "")) ;
638 # DBM Filter with a closure
645 ok(92, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
655 return sub { ++$count ;
657 $result{$name} = "$name - $count: [@kept]" ;
661 $db->filter_store_key(Closure("store key")) ;
662 $db->filter_store_value(Closure("store value")) ;
663 $db->filter_fetch_key(Closure("fetch key")) ;
664 $db->filter_fetch_value(Closure("fetch value")) ;
669 ok(93, $result{"store key"} eq "store key - 1: [fred]");
670 ok(94, $result{"store value"} eq "store value - 1: [joe]");
671 ok(95, ! defined $result{"fetch key"} );
672 ok(96, ! defined $result{"fetch value"} );
673 ok(97, $_ eq "original") ;
675 ok(98, $db->FIRSTKEY() eq "fred") ;
676 ok(99, $result{"store key"} eq "store key - 1: [fred]");
677 ok(100, $result{"store value"} eq "store value - 1: [joe]");
678 ok(101, $result{"fetch key"} eq "fetch key - 1: [fred]");
679 ok(102, ! defined $result{"fetch value"} );
680 ok(103, $_ eq "original") ;
683 ok(104, $result{"store key"} eq "store key - 2: [fred jim]");
684 ok(105, $result{"store value"} eq "store value - 2: [joe john]");
685 ok(106, $result{"fetch key"} eq "fetch key - 1: [fred]");
686 ok(107, ! defined $result{"fetch value"} );
687 ok(108, $_ eq "original") ;
689 ok(109, $h{"fred"} eq "joe");
690 ok(110, $result{"store key"} eq "store key - 3: [fred jim fred]");
691 ok(111, $result{"store value"} eq "store value - 2: [joe john]");
692 ok(112, $result{"fetch key"} eq "fetch key - 1: [fred]");
693 ok(113, $result{"fetch value"} eq "fetch value - 1: [joe]");
694 ok(114, $_ eq "original") ;
702 # DBM Filter recursion detection
708 ok(115, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
710 $db->filter_store_key (sub { $_ = $h{$_} }) ;
712 eval '$h{1} = 1234' ;
713 ok(116, $@ =~ /^recursion detected in filter_store_key at/ );
722 # Examples from the POD
726 my $redirect = new Redirect $file ;
728 use warnings FATAL => qw(all);
734 tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH
735 or die "Cannot open file 'fruit': $!\n";
737 # Add a few key/value pairs to the file
738 $h{"apple"} = "red" ;
739 $h{"orange"} = "orange" ;
740 $h{"banana"} = "yellow" ;
741 $h{"tomato"} = "red" ;
743 # Check for existence of a key
744 print "Banana Exists\n\n" if $h{"banana"} ;
746 # Delete a key/value pair.
749 # print the contents of the file
750 while (($k, $v) = each %h)
751 { print "$k -> $v\n" }
758 ok(117, docat_del($file) eq <<'EOM') ;
769 # Bug ID 20001013.009
771 # test that $hash{KEY} = undef doesn't produce the warning
772 # Use of uninitialized value in null operation
780 local $SIG{__WARN__} = sub {$a = $_[0]} ;
782 tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;
790 # test that %hash = () doesn't produce the warning
791 # Argument "" isn't numeric in entersub
799 local $SIG{__WARN__} = sub {$a = $_[0]} ;
801 tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;
809 # When iterating over a tied hash using "each", the key passed to FETCH
810 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
811 # key in FETCH via a filter_fetch_key method we need to check that the
812 # modified key doesn't get passed to NEXTKEY.
813 # Also Test "keys" & "values" while we are at it.
823 ok(120, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
824 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
825 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
827 $h{'Alpha_ABC'} = 2 ;
828 $h{'Alpha_DEF'} = 5 ;
830 ok(121, $h{'Alpha_ABC'} == 2);
831 ok(122, $h{'Alpha_DEF'} == 5);
833 my ($k, $v) = ("","");
834 while (($k, $v) = each %h) {}
835 ok(123, $bad_key == 0);
838 foreach $k (keys %h) {}
839 ok(124, $bad_key == 0);
842 foreach $v (values %h) {}
843 ok(125, $bad_key == 0);
851 # now an error to pass 'hash' a non-code reference
852 my $dbh = new DB_File::HASHINFO ;
854 eval { $dbh->{hash} = 2 };
855 ok(126, $@ =~ /^Key 'hash' not associated with a code reference at/);
861 # # recursion detection in hash
863 # my $Dfile = "xxx.db";
865 # my $dbh = new DB_File::HASHINFO ;
866 # $dbh->{hash} = sub { $hash{3} = 4 ; length $_[0] } ;
869 # ok(127, tie(%hash, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh ) );
871 # eval { $hash{1} = 2;
875 # ok(128, $@ =~ /^DB_File hash callback: recursion detected/);
887 # Check that two hash's don't interact
892 unlink $Dfile, $Dfile2;
893 my $dbh1 = new DB_File::HASHINFO ;
894 $dbh1->{hash} = sub { ++ $h1_count ; length $_[0] } ;
896 my $dbh2 = new DB_File::HASHINFO ;
897 $dbh2->{hash} = sub { ++ $h2_count ; length $_[0] } ;
902 ok(129, tie(%hash1, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh1 ) );
903 ok(130, tie(%hash2, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) );
913 ok(131, $h1_count > 0);
914 ok(132, $h1_count == $h2_count);
916 ok(133, safeUntie \%hash1);
917 ok(134, safeUntie \%hash2);
918 unlink $Dfile, $Dfile2;
922 # Passing undef for flags and/or mode when calling tie could cause
923 # Use of uninitialized value in subroutine entry
927 #local $SIG{__WARN__} = sub { ++ $warn_count };
931 tie %hash1, 'DB_File',$Dfile, undef;
932 ok(135, $warn_count == 0);
934 tie %hash1, 'DB_File',$Dfile, O_RDWR|O_CREAT, undef;
935 ok(136, $warn_count == 0);
936 tie %hash1, 'DB_File',$Dfile, undef, undef;
937 ok(137, $warn_count == 0);
944 # Check that DBM Filter can cope with read-only $_
949 my $Dfile = "xxy.db";
952 ok(138, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
954 $db->filter_fetch_key (sub { }) ;
955 $db->filter_store_key (sub { }) ;
956 $db->filter_fetch_value (sub { }) ;
957 $db->filter_store_value (sub { }) ;
962 ok(139, $h{"fred"} eq "joe");
964 eval { grep { $h{$_} } (1, 2, 3) };
969 $db->filter_fetch_key (undef);
970 $db->filter_store_key (undef);
971 $db->filter_fetch_value (undef);
972 $db->filter_store_value (undef);
976 ok(141, $h{"fred"} eq "joe");
978 ok(142, $db->FIRSTKEY() eq "fred") ;
980 eval { grep { $h{$_} } (1, 2, 3) };