5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
7 print "1..0 # Skip: DB_File was not built\n";
24 print "not " unless $result ;
35 my $filename = shift ;
37 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
38 my $real_stdout = select($fh) ;
39 return bless [$fh, $real_stdout ] ;
54 open(CAT,$file) || die "Cannot open $file: $!";
61 my $Dfile = "dbhash.tmp";
62 my $null_keys_allowed = ($DB_File::db_ver < 2.004010
63 || $DB_File::db_ver >= 3.1 );
69 # Check the interface to HASHINFO
71 my $dbh = new DB_File::HASHINFO ;
73 ok(1, ! defined $dbh->{bsize}) ;
74 ok(2, ! defined $dbh->{ffactor}) ;
75 ok(3, ! defined $dbh->{nelem}) ;
76 ok(4, ! defined $dbh->{cachesize}) ;
77 ok(5, ! defined $dbh->{hash}) ;
78 ok(6, ! defined $dbh->{lorder}) ;
80 $dbh->{bsize} = 3000 ;
81 ok(7, $dbh->{bsize} == 3000 );
83 $dbh->{ffactor} = 9000 ;
84 ok(8, $dbh->{ffactor} == 9000 );
87 ok(9, $dbh->{nelem} == 400 );
89 $dbh->{cachesize} = 65 ;
90 ok(10, $dbh->{cachesize} == 65 );
92 $dbh->{hash} = "abc" ;
93 ok(11, $dbh->{hash} eq "abc" );
95 $dbh->{lorder} = 1234 ;
96 ok(12, $dbh->{lorder} == 1234 );
98 # Check that an invalid entry is caught both for store & fetch
99 eval '$dbh->{fred} = 1234' ;
100 ok(13, $@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ );
101 eval 'my $q = $dbh->{fred}' ;
102 ok(14, $@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ );
105 # Now check the interface to HASH
107 ok(15, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
109 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
110 $blksize,$blocks) = stat($Dfile);
111 ok(16, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) || $^O eq 'amigaos' || $^O eq 'MSWin32' || $^O eq 'NetWare');
113 my ($key, $value, $i);
114 while (($key,$value) = each(%h)) {
119 $h{'goner1'} = 'snork';
122 ok(18, $h{'abc'} eq 'ABC' );
123 ok(19, !defined $h{'jimmy'} );
124 ok(20, !exists $h{'jimmy'} );
125 ok(21, exists $h{'abc'} );
128 $h{'jkl','mno'} = "JKL\034MNO";
129 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
133 $X->STORE('b', 'B') ;
146 $h{'goner2'} = 'snork';
150 # IMPORTANT - $X must be undefined before the untie otherwise the
151 # underlying DB close routine will not get called.
156 # tie to the same file again, do not supply a type - should default to HASH
157 ok(22, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640) );
159 # Modify an entry from the previous tie
180 $h{'goner3'} = 'snork';
183 $X->DELETE('goner3');
186 my @values = values(%h);
188 ok(23, $#keys == 29 && $#values == 29) ;
191 while (($key,$value) = each(%h)) {
192 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
194 $i++ if $key eq $value;
200 @keys = ('blurfl', keys(%h), 'dyick');
201 ok(25, $#keys == 31) ;
204 ok(26, $h{'foo'} eq '' );
206 # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
207 # This feature was reenabled in version 3.1 of Berkeley DB.
209 if ($null_keys_allowed) {
211 $result = ( $h{''} eq 'bar' );
217 # check cache overflow and numeric keys and contents
219 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
220 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
223 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
224 $blksize,$blocks) = stat($Dfile);
227 @h{0..200} = 200..400;
228 my @foo = @h{0..200};
229 ok(30, join(':',200..400) eq join(':',@foo) );
232 # Now check all the non-tie specific stuff
234 # Check NOOVERWRITE will make put fail when attempting to overwrite
235 # an existing record.
237 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
238 ok(31, $status == 1 );
240 # check that the value of the key 'x' has not been changed by the
242 ok(32, $h{'x'} eq 'X' );
245 $status = $X->put('key', 'value') ;
246 ok(33, $status == 0 );
248 #check that previous put can be retrieved
250 $status = $X->get('key', $value) ;
251 ok(34, $status == 0 );
252 ok(35, $value eq 'value' );
254 # Attempting to delete an existing key should work
256 $status = $X->del('q') ;
257 ok(36, $status == 0 );
259 # Make sure that the key deleted, cannot be retrieved
261 no warnings 'uninitialized' ;
262 ok(37, $h{'q'} eq undef );
265 # Attempting to delete a non-existant key should fail
267 $status = $X->del('joe') ;
268 ok(38, $status == 1 );
270 # Check the get interface
272 # First a non-existing key
273 $status = $X->get('aaaa', $value) ;
274 ok(39, $status == 1 );
276 # Next an existing key
277 $status = $X->get('a', $value) ;
278 ok(40, $status == 0 );
279 ok(41, $value eq 'A' );
284 # ditto, but use put to replace the key/value pair.
286 # use seq to walk backwards through a file - check that this reversed is
288 # check seq FIRST/LAST
294 ok(42, $status == 0 );
301 ok(43, $status != 0 );
311 ok(44, tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
313 { $h{$_} = $_ * 100 }
315 # check that there are 10 elements in the hash
317 while (($key,$value) = each(%h)) {
327 while (($key,$value) = each(%h)) {
336 # Now try an in memory file
337 ok(47, $X = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
339 # fd with an in memory file should return fail
341 ok(48, $status == -1 );
347 # check ability to override the default hashing
349 my $filename = "xyz" ;
350 my $hi = new DB_File::HASHINFO ;
352 $hi->{hash} = sub { ++$::count ; length $_[0] } ;
353 ok(49, tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $hi ) ;
355 ok(50, $h{"abc"} == 123) ;
358 ok(51, $::count >0) ;
362 # check that attempting to tie an array to a DB_HASH will fail
364 my $filename = "xyz" ;
366 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_HASH ; } ;
367 ok(52, $@ =~ /^DB_File can only tie an associative array to a DB_HASH database/) ;
379 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
386 use vars qw( @ISA @EXPORT) ;
391 @EXPORT = @DB_File::EXPORT ;
397 $self->SUPER::STORE($key, $value * 2) ;
403 $self->SUPER::FETCH($key) - 1 ;
410 $self->SUPER::put($key, $value * 3) ;
415 $self->SUPER::get($_[0], $_[1]) ;
423 my $value = $self->FETCH($key) ;
424 return "[[$value]]" ;
432 BEGIN { push @INC, '.'; }
434 main::ok(53, $@ eq "") ;
438 $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640, $DB_HASH );
441 main::ok(54, $@ eq "") ;
443 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
444 main::ok(55, $@ eq "") ;
445 main::ok(56, $ret == 5) ;
448 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
449 main::ok(57, $@ eq "") ;
450 main::ok(58, $ret == 10) ;
452 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
453 main::ok(59, $@ eq "" ) ;
454 main::ok(60, $ret == 1) ;
456 $ret = eval '$X->A_new_method("joe") ' ;
457 main::ok(61, $@ eq "") ;
458 main::ok(62, $ret eq "[[11]]") ;
462 unlink "SubDB.pm", "dbhash.tmp" ;
471 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
476 my($fk, $sk, $fv, $sv) = @_ ;
478 $fetch_key eq $fk && $store_key eq $sk &&
479 $fetch_value eq $fv && $store_value eq $sv &&
483 ok(63, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
485 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
486 $db->filter_store_key (sub { $store_key = $_ }) ;
487 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
488 $db->filter_store_value (sub { $store_value = $_ }) ;
494 ok(64, checkOutput( "", "fred", "", "joe")) ;
496 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
497 ok(65, $h{"fred"} eq "joe");
499 ok(66, checkOutput( "", "fred", "joe", "")) ;
501 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
502 ok(67, $db->FIRSTKEY() eq "fred") ;
504 ok(68, checkOutput( "fred", "", "", "")) ;
506 # replace the filters, but remember the previous set
507 my ($old_fk) = $db->filter_fetch_key
508 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
509 my ($old_sk) = $db->filter_store_key
510 (sub { $_ = lc $_ ; $store_key = $_ }) ;
511 my ($old_fv) = $db->filter_fetch_value
512 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
513 my ($old_sv) = $db->filter_store_value
514 (sub { s/o/x/g; $store_value = $_ }) ;
516 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
519 ok(69, checkOutput( "", "fred", "", "Jxe")) ;
521 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
522 ok(70, $h{"Fred"} eq "[Jxe]");
524 ok(71, checkOutput( "", "fred", "[Jxe]", "")) ;
526 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
527 ok(72, $db->FIRSTKEY() eq "FRED") ;
529 ok(73, checkOutput( "FRED", "", "", "")) ;
531 # put the original filters back
532 $db->filter_fetch_key ($old_fk);
533 $db->filter_store_key ($old_sk);
534 $db->filter_fetch_value ($old_fv);
535 $db->filter_store_value ($old_sv);
537 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
539 ok(74, checkOutput( "", "fred", "", "joe")) ;
541 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
542 ok(75, $h{"fred"} eq "joe");
543 ok(76, checkOutput( "", "fred", "joe", "")) ;
545 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
546 ok(77, $db->FIRSTKEY() eq "fred") ;
547 ok(78, checkOutput( "fred", "", "", "")) ;
550 $db->filter_fetch_key (undef);
551 $db->filter_store_key (undef);
552 $db->filter_fetch_value (undef);
553 $db->filter_store_value (undef);
555 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
557 ok(79, checkOutput( "", "", "", "")) ;
559 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
560 ok(80, $h{"fred"} eq "joe");
561 ok(81, checkOutput( "", "", "", "")) ;
563 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
564 ok(82, $db->FIRSTKEY() eq "fred") ;
565 ok(83, checkOutput( "", "", "", "")) ;
573 # DBM Filter with a closure
580 ok(84, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
590 return sub { ++$count ;
592 $result{$name} = "$name - $count: [@kept]" ;
596 $db->filter_store_key(Closure("store key")) ;
597 $db->filter_store_value(Closure("store value")) ;
598 $db->filter_fetch_key(Closure("fetch key")) ;
599 $db->filter_fetch_value(Closure("fetch value")) ;
604 ok(85, $result{"store key"} eq "store key - 1: [fred]");
605 ok(86, $result{"store value"} eq "store value - 1: [joe]");
606 ok(87, ! defined $result{"fetch key"} );
607 ok(88, ! defined $result{"fetch value"} );
608 ok(89, $_ eq "original") ;
610 ok(90, $db->FIRSTKEY() eq "fred") ;
611 ok(91, $result{"store key"} eq "store key - 1: [fred]");
612 ok(92, $result{"store value"} eq "store value - 1: [joe]");
613 ok(93, $result{"fetch key"} eq "fetch key - 1: [fred]");
614 ok(94, ! defined $result{"fetch value"} );
615 ok(95, $_ eq "original") ;
618 ok(96, $result{"store key"} eq "store key - 2: [fred jim]");
619 ok(97, $result{"store value"} eq "store value - 2: [joe john]");
620 ok(98, $result{"fetch key"} eq "fetch key - 1: [fred]");
621 ok(99, ! defined $result{"fetch value"} );
622 ok(100, $_ eq "original") ;
624 ok(101, $h{"fred"} eq "joe");
625 ok(102, $result{"store key"} eq "store key - 3: [fred jim fred]");
626 ok(103, $result{"store value"} eq "store value - 2: [joe john]");
627 ok(104, $result{"fetch key"} eq "fetch key - 1: [fred]");
628 ok(105, $result{"fetch value"} eq "fetch value - 1: [joe]");
629 ok(106, $_ eq "original") ;
637 # DBM Filter recursion detection
643 ok(107, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
645 $db->filter_store_key (sub { $_ = $h{$_} }) ;
647 eval '$h{1} = 1234' ;
648 ok(108, $@ =~ /^recursion detected in filter_store_key at/ );
657 # Examples from the POD
661 my $redirect = new Redirect $file ;
663 use warnings FATAL => qw(all);
666 use vars qw( %h $k $v ) ;
669 tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH
670 or die "Cannot open file 'fruit': $!\n";
672 # Add a few key/value pairs to the file
673 $h{"apple"} = "red" ;
674 $h{"orange"} = "orange" ;
675 $h{"banana"} = "yellow" ;
676 $h{"tomato"} = "red" ;
678 # Check for existence of a key
679 print "Banana Exists\n\n" if $h{"banana"} ;
681 # Delete a key/value pair.
684 # print the contents of the file
685 while (($k, $v) = each %h)
686 { print "$k -> $v\n" }
693 ok(109, docat_del($file) eq <<'EOM') ;
704 # Bug ID 20001013.009
706 # test that $hash{KEY} = undef doesn't produce the warning
707 # Use of uninitialized value in null operation
715 local $SIG{__WARN__} = sub {$a = $_[0]} ;
717 tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;
725 # test that %hash = () doesn't produce the warning
726 # Argument "" isn't numeric in entersub
734 local $SIG{__WARN__} = sub {$a = $_[0]} ;
736 tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;