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' || $^O eq 'MacOS') ? 0666 : 0640) ||
112 $^O eq 'amigaos' || $^O eq 'MSWin32' || $^O eq 'NetWare');
114 my ($key, $value, $i);
115 while (($key,$value) = each(%h)) {
120 $h{'goner1'} = 'snork';
123 ok(18, $h{'abc'} eq 'ABC' );
124 ok(19, !defined $h{'jimmy'} );
125 ok(20, !exists $h{'jimmy'} );
126 ok(21, exists $h{'abc'} );
129 $h{'jkl','mno'} = "JKL\034MNO";
130 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
134 $X->STORE('b', 'B') ;
147 $h{'goner2'} = 'snork';
151 # IMPORTANT - $X must be undefined before the untie otherwise the
152 # underlying DB close routine will not get called.
157 # tie to the same file again, do not supply a type - should default to HASH
158 ok(22, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640) );
160 # Modify an entry from the previous tie
181 $h{'goner3'} = 'snork';
184 $X->DELETE('goner3');
187 my @values = values(%h);
189 ok(23, $#keys == 29 && $#values == 29) ;
192 while (($key,$value) = each(%h)) {
193 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
195 $i++ if $key eq $value;
201 @keys = ('blurfl', keys(%h), 'dyick');
202 ok(25, $#keys == 31) ;
205 ok(26, $h{'foo'} eq '' );
207 # Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
208 # This feature was reenabled in version 3.1 of Berkeley DB.
210 if ($null_keys_allowed) {
212 $result = ( $h{''} eq 'bar' );
218 # check cache overflow and numeric keys and contents
220 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
221 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
224 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
225 $blksize,$blocks) = stat($Dfile);
228 @h{0..200} = 200..400;
229 my @foo = @h{0..200};
230 ok(30, join(':',200..400) eq join(':',@foo) );
233 # Now check all the non-tie specific stuff
235 # Check NOOVERWRITE will make put fail when attempting to overwrite
236 # an existing record.
238 my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
239 ok(31, $status == 1 );
241 # check that the value of the key 'x' has not been changed by the
243 ok(32, $h{'x'} eq 'X' );
246 $status = $X->put('key', 'value') ;
247 ok(33, $status == 0 );
249 #check that previous put can be retrieved
251 $status = $X->get('key', $value) ;
252 ok(34, $status == 0 );
253 ok(35, $value eq 'value' );
255 # Attempting to delete an existing key should work
257 $status = $X->del('q') ;
258 ok(36, $status == 0 );
260 # Make sure that the key deleted, cannot be retrieved
262 no warnings 'uninitialized' ;
263 ok(37, $h{'q'} eq undef );
266 # Attempting to delete a non-existant key should fail
268 $status = $X->del('joe') ;
269 ok(38, $status == 1 );
271 # Check the get interface
273 # First a non-existing key
274 $status = $X->get('aaaa', $value) ;
275 ok(39, $status == 1 );
277 # Next an existing key
278 $status = $X->get('a', $value) ;
279 ok(40, $status == 0 );
280 ok(41, $value eq 'A' );
285 # ditto, but use put to replace the key/value pair.
287 # use seq to walk backwards through a file - check that this reversed is
289 # check seq FIRST/LAST
295 ok(42, $status == 0 );
302 ok(43, $status != 0 );
312 ok(44, tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
314 { $h{$_} = $_ * 100 }
316 # check that there are 10 elements in the hash
318 while (($key,$value) = each(%h)) {
328 while (($key,$value) = each(%h)) {
337 # Now try an in memory file
338 ok(47, $X = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
340 # fd with an in memory file should return fail
342 ok(48, $status == -1 );
348 # check ability to override the default hashing
350 my $filename = "xyz" ;
351 my $hi = new DB_File::HASHINFO ;
353 $hi->{hash} = sub { ++$::count ; length $_[0] } ;
354 ok(49, tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $hi ) ;
356 ok(50, $h{"abc"} == 123) ;
359 ok(51, $::count >0) ;
363 # check that attempting to tie an array to a DB_HASH will fail
365 my $filename = "xyz" ;
367 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_HASH ; } ;
368 ok(52, $@ =~ /^DB_File can only tie an associative array to a DB_HASH database/) ;
380 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
387 use vars qw( @ISA @EXPORT) ;
392 @EXPORT = @DB_File::EXPORT ;
398 $self->SUPER::STORE($key, $value * 2) ;
404 $self->SUPER::FETCH($key) - 1 ;
411 $self->SUPER::put($key, $value * 3) ;
416 $self->SUPER::get($_[0], $_[1]) ;
424 my $value = $self->FETCH($key) ;
425 return "[[$value]]" ;
433 BEGIN { push @INC, '.'; }
435 main::ok(53, $@ eq "") ;
439 $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640, $DB_HASH );
442 main::ok(54, $@ eq "") ;
444 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
445 main::ok(55, $@ eq "") ;
446 main::ok(56, $ret == 5) ;
449 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
450 main::ok(57, $@ eq "") ;
451 main::ok(58, $ret == 10) ;
453 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
454 main::ok(59, $@ eq "" ) ;
455 main::ok(60, $ret == 1) ;
457 $ret = eval '$X->A_new_method("joe") ' ;
458 main::ok(61, $@ eq "") ;
459 main::ok(62, $ret eq "[[11]]") ;
463 unlink "SubDB.pm", "dbhash.tmp" ;
472 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
477 my($fk, $sk, $fv, $sv) = @_ ;
479 $fetch_key eq $fk && $store_key eq $sk &&
480 $fetch_value eq $fv && $store_value eq $sv &&
484 ok(63, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
486 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
487 $db->filter_store_key (sub { $store_key = $_ }) ;
488 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
489 $db->filter_store_value (sub { $store_value = $_ }) ;
495 ok(64, checkOutput( "", "fred", "", "joe")) ;
497 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
498 ok(65, $h{"fred"} eq "joe");
500 ok(66, checkOutput( "", "fred", "joe", "")) ;
502 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
503 ok(67, $db->FIRSTKEY() eq "fred") ;
505 ok(68, checkOutput( "fred", "", "", "")) ;
507 # replace the filters, but remember the previous set
508 my ($old_fk) = $db->filter_fetch_key
509 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
510 my ($old_sk) = $db->filter_store_key
511 (sub { $_ = lc $_ ; $store_key = $_ }) ;
512 my ($old_fv) = $db->filter_fetch_value
513 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
514 my ($old_sv) = $db->filter_store_value
515 (sub { s/o/x/g; $store_value = $_ }) ;
517 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
520 ok(69, checkOutput( "", "fred", "", "Jxe")) ;
522 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
523 ok(70, $h{"Fred"} eq "[Jxe]");
525 ok(71, checkOutput( "", "fred", "[Jxe]", "")) ;
527 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
528 ok(72, $db->FIRSTKEY() eq "FRED") ;
530 ok(73, checkOutput( "FRED", "", "", "")) ;
532 # put the original filters back
533 $db->filter_fetch_key ($old_fk);
534 $db->filter_store_key ($old_sk);
535 $db->filter_fetch_value ($old_fv);
536 $db->filter_store_value ($old_sv);
538 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
540 ok(74, checkOutput( "", "fred", "", "joe")) ;
542 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
543 ok(75, $h{"fred"} eq "joe");
544 ok(76, checkOutput( "", "fred", "joe", "")) ;
546 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
547 ok(77, $db->FIRSTKEY() eq "fred") ;
548 ok(78, checkOutput( "fred", "", "", "")) ;
551 $db->filter_fetch_key (undef);
552 $db->filter_store_key (undef);
553 $db->filter_fetch_value (undef);
554 $db->filter_store_value (undef);
556 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
558 ok(79, checkOutput( "", "", "", "")) ;
560 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
561 ok(80, $h{"fred"} eq "joe");
562 ok(81, checkOutput( "", "", "", "")) ;
564 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
565 ok(82, $db->FIRSTKEY() eq "fred") ;
566 ok(83, checkOutput( "", "", "", "")) ;
574 # DBM Filter with a closure
581 ok(84, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
591 return sub { ++$count ;
593 $result{$name} = "$name - $count: [@kept]" ;
597 $db->filter_store_key(Closure("store key")) ;
598 $db->filter_store_value(Closure("store value")) ;
599 $db->filter_fetch_key(Closure("fetch key")) ;
600 $db->filter_fetch_value(Closure("fetch value")) ;
605 ok(85, $result{"store key"} eq "store key - 1: [fred]");
606 ok(86, $result{"store value"} eq "store value - 1: [joe]");
607 ok(87, ! defined $result{"fetch key"} );
608 ok(88, ! defined $result{"fetch value"} );
609 ok(89, $_ eq "original") ;
611 ok(90, $db->FIRSTKEY() eq "fred") ;
612 ok(91, $result{"store key"} eq "store key - 1: [fred]");
613 ok(92, $result{"store value"} eq "store value - 1: [joe]");
614 ok(93, $result{"fetch key"} eq "fetch key - 1: [fred]");
615 ok(94, ! defined $result{"fetch value"} );
616 ok(95, $_ eq "original") ;
619 ok(96, $result{"store key"} eq "store key - 2: [fred jim]");
620 ok(97, $result{"store value"} eq "store value - 2: [joe john]");
621 ok(98, $result{"fetch key"} eq "fetch key - 1: [fred]");
622 ok(99, ! defined $result{"fetch value"} );
623 ok(100, $_ eq "original") ;
625 ok(101, $h{"fred"} eq "joe");
626 ok(102, $result{"store key"} eq "store key - 3: [fred jim fred]");
627 ok(103, $result{"store value"} eq "store value - 2: [joe john]");
628 ok(104, $result{"fetch key"} eq "fetch key - 1: [fred]");
629 ok(105, $result{"fetch value"} eq "fetch value - 1: [joe]");
630 ok(106, $_ eq "original") ;
638 # DBM Filter recursion detection
644 ok(107, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
646 $db->filter_store_key (sub { $_ = $h{$_} }) ;
648 eval '$h{1} = 1234' ;
649 ok(108, $@ =~ /^recursion detected in filter_store_key at/ );
658 # Examples from the POD
662 my $redirect = new Redirect $file ;
664 use warnings FATAL => qw(all);
667 use vars qw( %h $k $v ) ;
670 tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH
671 or die "Cannot open file 'fruit': $!\n";
673 # Add a few key/value pairs to the file
674 $h{"apple"} = "red" ;
675 $h{"orange"} = "orange" ;
676 $h{"banana"} = "yellow" ;
677 $h{"tomato"} = "red" ;
679 # Check for existence of a key
680 print "Banana Exists\n\n" if $h{"banana"} ;
682 # Delete a key/value pair.
685 # print the contents of the file
686 while (($k, $v) = each %h)
687 { print "$k -> $v\n" }
694 ok(109, docat_del($file) eq <<'EOM') ;
705 # Bug ID 20001013.009
707 # test that $hash{KEY} = undef doesn't produce the warning
708 # Use of uninitialized value in null operation
716 local $SIG{__WARN__} = sub {$a = $_[0]} ;
718 tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;
726 # test that %hash = () doesn't produce the warning
727 # Argument "" isn't numeric in entersub
735 local $SIG{__WARN__} = sub {$a = $_[0]} ;
737 tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;