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 ;
44 my $filename = shift ;
46 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
47 my $real_stdout = select($fh) ;
48 return bless [$fh, $real_stdout ] ;
63 open(CAT,$file) || die "Cannot open $file: $!";
66 $result = normalise($result) ;
81 my $Dfile = "dbhash.tmp";
82 my $null_keys_allowed = ($DB_File::db_ver < 2.004010
83 || $DB_File::db_ver >= 3.1 );
89 # Check the interface to HASHINFO
91 my $dbh = new DB_File::HASHINFO ;
93 ok(1, ! defined $dbh->{bsize}) ;
94 ok(2, ! defined $dbh->{ffactor}) ;
95 ok(3, ! defined $dbh->{nelem}) ;
96 ok(4, ! defined $dbh->{cachesize}) ;
97 ok(5, ! defined $dbh->{hash}) ;
98 ok(6, ! defined $dbh->{lorder}) ;
100 $dbh->{bsize} = 3000 ;
101 ok(7, $dbh->{bsize} == 3000 );
103 $dbh->{ffactor} = 9000 ;
104 ok(8, $dbh->{ffactor} == 9000 );
106 $dbh->{nelem} = 400 ;
107 ok(9, $dbh->{nelem} == 400 );
109 $dbh->{cachesize} = 65 ;
110 ok(10, $dbh->{cachesize} == 65 );
112 $dbh->{hash} = "abc" ;
113 ok(11, $dbh->{hash} eq "abc" );
115 $dbh->{lorder} = 1234 ;
116 ok(12, $dbh->{lorder} == 1234 );
118 # Check that an invalid entry is caught both for store & fetch
119 eval '$dbh->{fred} = 1234' ;
120 ok(13, $@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ );
121 eval 'my $q = $dbh->{fred}' ;
122 ok(14, $@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ );
125 # Now check the interface to HASH
127 ok(15, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
128 die "Could not tie: $!" unless $X;
130 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
131 $blksize,$blocks) = stat($Dfile);
133 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
135 ok(16, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640) ||
138 my ($key, $value, $i);
139 while (($key,$value) = each(%h)) {
144 $h{'goner1'} = 'snork';
147 ok(18, $h{'abc'} eq 'ABC' );
148 ok(19, !defined $h{'jimmy'} );
149 ok(20, !exists $h{'jimmy'} );
150 ok(21, exists $h{'abc'} );
153 $h{'jkl','mno'} = "JKL\034MNO";
154 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
158 $X->STORE('b', 'B') ;
171 $h{'goner2'} = 'snork';
175 # IMPORTANT - $X must be undefined before the untie otherwise the
176 # underlying DB close routine will not get called.
181 # tie to the same file again, do not supply a type - should default to HASH
182 ok(22, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640) );
184 # Modify an entry from the previous tie
205 $h{'goner3'} = 'snork';
208 $X->DELETE('goner3');
211 my @values = values(%h);
213 ok(23, $#keys == 29 && $#values == 29) ;
216 while (($key,$value) = each(%h)) {
217 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
219 $i++ if $key eq $value;
225 @keys = ('blurfl', keys(%h), 'dyick');
226 ok(25, $#keys == 31) ;
229 ok(26, $h{'foo'} eq '' );
231 # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
232 # This feature was reenabled in version 3.1 of Berkeley DB.
234 if ($null_keys_allowed) {
236 $result = ( $h{''} eq 'bar' );
242 # check cache overflow and numeric keys and contents
244 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
245 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
248 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
249 $blksize,$blocks) = stat($Dfile);
252 @h{0..200} = 200..400;
253 my @foo = @h{0..200};
254 ok(30, join(':',200..400) eq join(':',@foo) );
257 # Now check all the non-tie specific stuff
259 # Check NOOVERWRITE will make put fail when attempting to overwrite
260 # an existing record.
262 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
263 ok(31, $status == 1 );
265 # check that the value of the key 'x' has not been changed by the
267 ok(32, $h{'x'} eq 'X' );
270 $status = $X->put('key', 'value') ;
271 ok(33, $status == 0 );
273 #check that previous put can be retrieved
275 $status = $X->get('key', $value) ;
276 ok(34, $status == 0 );
277 ok(35, $value eq 'value' );
279 # Attempting to delete an existing key should work
281 $status = $X->del('q') ;
282 ok(36, $status == 0 );
284 # Make sure that the key deleted, cannot be retrieved
286 no warnings 'uninitialized' ;
287 ok(37, $h{'q'} eq undef );
290 # Attempting to delete a non-existant key should fail
292 $status = $X->del('joe') ;
293 ok(38, $status == 1 );
295 # Check the get interface
297 # First a non-existing key
298 $status = $X->get('aaaa', $value) ;
299 ok(39, $status == 1 );
301 # Next an existing key
302 $status = $X->get('a', $value) ;
303 ok(40, $status == 0 );
304 ok(41, $value eq 'A' );
309 # ditto, but use put to replace the key/value pair.
311 # use seq to walk backwards through a file - check that this reversed is
313 # check seq FIRST/LAST
319 ok(42, $status == 0 );
326 ok(43, $status != 0 );
336 ok(44, tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
338 { $h{$_} = $_ * 100 }
340 # check that there are 10 elements in the hash
342 while (($key,$value) = each(%h)) {
352 while (($key,$value) = each(%h)) {
361 # Now try an in memory file
362 ok(47, $X = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
364 # fd with an in memory file should return fail
366 ok(48, $status == -1 );
372 # check ability to override the default hashing
374 my $filename = "xyz" ;
375 my $hi = new DB_File::HASHINFO ;
377 $hi->{hash} = sub { ++$::count ; length $_[0] } ;
378 ok(49, tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $hi ) ;
380 ok(50, $h{"abc"} == 123) ;
383 ok(51, $::count >0) ;
387 # check that attempting to tie an array to a DB_HASH will fail
389 my $filename = "xyz" ;
391 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_HASH ; } ;
392 ok(52, $@ =~ /^DB_File can only tie an associative array to a DB_HASH database/) ;
404 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
416 @EXPORT = @DB_File::EXPORT ;
422 $self->SUPER::STORE($key, $value * 2) ;
428 $self->SUPER::FETCH($key) - 1 ;
435 $self->SUPER::put($key, $value * 3) ;
440 $self->SUPER::get($_[0], $_[1]) ;
448 my $value = $self->FETCH($key) ;
449 return "[[$value]]" ;
457 BEGIN { push @INC, '.'; }
459 main::ok(53, $@ eq "") ;
463 $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640, $DB_HASH );
466 main::ok(54, $@ eq "") ;
468 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
469 main::ok(55, $@ eq "") ;
470 main::ok(56, $ret == 5) ;
473 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
474 main::ok(57, $@ eq "") ;
475 main::ok(58, $ret == 10) ;
477 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
478 main::ok(59, $@ eq "" ) ;
479 main::ok(60, $ret == 1) ;
481 $ret = eval '$X->A_new_method("joe") ' ;
482 main::ok(61, $@ eq "") ;
483 main::ok(62, $ret eq "[[11]]") ;
487 unlink "SubDB.pm", "dbhash.tmp" ;
496 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
501 my($fk, $sk, $fv, $sv) = @_ ;
503 $fetch_key eq $fk && $store_key eq $sk &&
504 $fetch_value eq $fv && $store_value eq $sv &&
508 ok(63, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
510 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
511 $db->filter_store_key (sub { $store_key = $_ }) ;
512 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
513 $db->filter_store_value (sub { $store_value = $_ }) ;
519 ok(64, checkOutput( "", "fred", "", "joe")) ;
521 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
522 ok(65, $h{"fred"} eq "joe");
524 ok(66, checkOutput( "", "fred", "joe", "")) ;
526 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
527 ok(67, $db->FIRSTKEY() eq "fred") ;
529 ok(68, checkOutput( "fred", "", "", "")) ;
531 # replace the filters, but remember the previous set
532 my ($old_fk) = $db->filter_fetch_key
533 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
534 my ($old_sk) = $db->filter_store_key
535 (sub { $_ = lc $_ ; $store_key = $_ }) ;
536 my ($old_fv) = $db->filter_fetch_value
537 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
538 my ($old_sv) = $db->filter_store_value
539 (sub { s/o/x/g; $store_value = $_ }) ;
541 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
544 ok(69, checkOutput( "", "fred", "", "Jxe")) ;
546 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
547 ok(70, $h{"Fred"} eq "[Jxe]");
549 ok(71, checkOutput( "", "fred", "[Jxe]", "")) ;
551 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
552 ok(72, $db->FIRSTKEY() eq "FRED") ;
554 ok(73, checkOutput( "FRED", "", "", "")) ;
556 # put the original filters back
557 $db->filter_fetch_key ($old_fk);
558 $db->filter_store_key ($old_sk);
559 $db->filter_fetch_value ($old_fv);
560 $db->filter_store_value ($old_sv);
562 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
564 ok(74, checkOutput( "", "fred", "", "joe")) ;
566 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
567 ok(75, $h{"fred"} eq "joe");
568 ok(76, checkOutput( "", "fred", "joe", "")) ;
570 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
571 ok(77, $db->FIRSTKEY() eq "fred") ;
572 ok(78, checkOutput( "fred", "", "", "")) ;
575 $db->filter_fetch_key (undef);
576 $db->filter_store_key (undef);
577 $db->filter_fetch_value (undef);
578 $db->filter_store_value (undef);
580 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
582 ok(79, checkOutput( "", "", "", "")) ;
584 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
585 ok(80, $h{"fred"} eq "joe");
586 ok(81, checkOutput( "", "", "", "")) ;
588 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
589 ok(82, $db->FIRSTKEY() eq "fred") ;
590 ok(83, checkOutput( "", "", "", "")) ;
598 # DBM Filter with a closure
605 ok(84, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
615 return sub { ++$count ;
617 $result{$name} = "$name - $count: [@kept]" ;
621 $db->filter_store_key(Closure("store key")) ;
622 $db->filter_store_value(Closure("store value")) ;
623 $db->filter_fetch_key(Closure("fetch key")) ;
624 $db->filter_fetch_value(Closure("fetch value")) ;
629 ok(85, $result{"store key"} eq "store key - 1: [fred]");
630 ok(86, $result{"store value"} eq "store value - 1: [joe]");
631 ok(87, ! defined $result{"fetch key"} );
632 ok(88, ! defined $result{"fetch value"} );
633 ok(89, $_ eq "original") ;
635 ok(90, $db->FIRSTKEY() eq "fred") ;
636 ok(91, $result{"store key"} eq "store key - 1: [fred]");
637 ok(92, $result{"store value"} eq "store value - 1: [joe]");
638 ok(93, $result{"fetch key"} eq "fetch key - 1: [fred]");
639 ok(94, ! defined $result{"fetch value"} );
640 ok(95, $_ eq "original") ;
643 ok(96, $result{"store key"} eq "store key - 2: [fred jim]");
644 ok(97, $result{"store value"} eq "store value - 2: [joe john]");
645 ok(98, $result{"fetch key"} eq "fetch key - 1: [fred]");
646 ok(99, ! defined $result{"fetch value"} );
647 ok(100, $_ eq "original") ;
649 ok(101, $h{"fred"} eq "joe");
650 ok(102, $result{"store key"} eq "store key - 3: [fred jim fred]");
651 ok(103, $result{"store value"} eq "store value - 2: [joe john]");
652 ok(104, $result{"fetch key"} eq "fetch key - 1: [fred]");
653 ok(105, $result{"fetch value"} eq "fetch value - 1: [joe]");
654 ok(106, $_ eq "original") ;
662 # DBM Filter recursion detection
668 ok(107, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
670 $db->filter_store_key (sub { $_ = $h{$_} }) ;
672 eval '$h{1} = 1234' ;
673 ok(108, $@ =~ /^recursion detected in filter_store_key at/ );
682 # Examples from the POD
686 my $redirect = new Redirect $file ;
688 use warnings FATAL => qw(all);
694 tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH
695 or die "Cannot open file 'fruit': $!\n";
697 # Add a few key/value pairs to the file
698 $h{"apple"} = "red" ;
699 $h{"orange"} = "orange" ;
700 $h{"banana"} = "yellow" ;
701 $h{"tomato"} = "red" ;
703 # Check for existence of a key
704 print "Banana Exists\n\n" if $h{"banana"} ;
706 # Delete a key/value pair.
709 # print the contents of the file
710 while (($k, $v) = each %h)
711 { print "$k -> $v\n" }
718 ok(109, docat_del($file) eq <<'EOM') ;
729 # Bug ID 20001013.009
731 # test that $hash{KEY} = undef doesn't produce the warning
732 # Use of uninitialized value in null operation
740 local $SIG{__WARN__} = sub {$a = $_[0]} ;
742 tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;
750 # test that %hash = () doesn't produce the warning
751 # Argument "" isn't numeric in entersub
759 local $SIG{__WARN__} = sub {$a = $_[0]} ;
761 tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;
769 # When iterating over a tied hash using "each", the key passed to FETCH
770 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
771 # key in FETCH via a filter_fetch_key method we need to check that the
772 # modified key doesn't get passed to NEXTKEY.
773 # Also Test "keys" & "values" while we are at it.
783 ok(112, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
784 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
785 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
787 $h{'Alpha_ABC'} = 2 ;
788 $h{'Alpha_DEF'} = 5 ;
790 ok(113, $h{'Alpha_ABC'} == 2);
791 ok(114, $h{'Alpha_DEF'} == 5);
793 my ($k, $v) = ("","");
794 while (($k, $v) = each %h) {}
795 ok(115, $bad_key == 0);
798 foreach $k (keys %h) {}
799 ok(116, $bad_key == 0);
802 foreach $v (values %h) {}
803 ok(117, $bad_key == 0);