4 unshift @INC, '../lib' if -d '../lib' ;
5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
7 print "1..0 # Skip: DB_File was not built\n";
22 print "not " unless $result ;
33 my $filename = shift ;
35 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
36 my $real_stdout = select($fh) ;
37 return bless [$fh, $real_stdout ] ;
52 open(CAT,$file) || die "Cannot open $file: $!";
59 my $Dfile = "dbhash.tmp";
64 # Check the interface to HASHINFO
66 my $dbh = new DB_File::HASHINFO ;
68 ok(1, ! defined $dbh->{bsize}) ;
69 ok(2, ! defined $dbh->{ffactor}) ;
70 ok(3, ! defined $dbh->{nelem}) ;
71 ok(4, ! defined $dbh->{cachesize}) ;
72 ok(5, ! defined $dbh->{hash}) ;
73 ok(6, ! defined $dbh->{lorder}) ;
75 $dbh->{bsize} = 3000 ;
76 ok(7, $dbh->{bsize} == 3000 );
78 $dbh->{ffactor} = 9000 ;
79 ok(8, $dbh->{ffactor} == 9000 );
82 ok(9, $dbh->{nelem} == 400 );
84 $dbh->{cachesize} = 65 ;
85 ok(10, $dbh->{cachesize} == 65 );
87 $dbh->{hash} = "abc" ;
88 ok(11, $dbh->{hash} eq "abc" );
90 $dbh->{lorder} = 1234 ;
91 ok(12, $dbh->{lorder} == 1234 );
93 # Check that an invalid entry is caught both for store & fetch
94 eval '$dbh->{fred} = 1234' ;
95 ok(13, $@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ );
96 eval 'my $q = $dbh->{fred}' ;
97 ok(14, $@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ );
100 # Now check the interface to HASH
102 ok(15, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
104 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
105 $blksize,$blocks) = stat($Dfile);
106 ok(16, ($mode & 0777) == ($^O eq 'os2' ? 0666 : 0640) || $^O eq 'amigaos' || $^O eq 'MSWin32');
108 while (($key,$value) = each(%h)) {
113 $h{'goner1'} = 'snork';
116 ok(18, $h{'abc'} eq 'ABC' );
117 ok(19, !defined $h{'jimmy'} );
118 ok(20, !exists $h{'jimmy'} );
119 ok(21, exists $h{'abc'} );
122 $h{'jkl','mno'} = "JKL\034MNO";
123 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
127 $X->STORE('b', 'B') ;
140 $h{'goner2'} = 'snork';
144 # IMPORTANT - $X must be undefined before the untie otherwise the
145 # underlying DB close routine will not get called.
150 # tie to the same file again, do not supply a type - should default to HASH
151 ok(22, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640) );
153 # Modify an entry from the previous tie
174 $h{'goner3'} = 'snork';
177 $X->DELETE('goner3');
180 @values = values(%h);
182 ok(23, $#keys == 29 && $#values == 29) ;
185 while (($key,$value) = each(%h)) {
186 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
188 $i++ if $key eq $value;
194 @keys = ('blurfl', keys(%h), 'dyick');
195 ok(25, $#keys == 31) ;
198 ok(26, $h{'foo'} eq '' );
201 #ok(27, $h{''} eq 'bar' );
204 # check cache overflow and numeric keys and contents
206 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
207 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
210 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
211 $blksize,$blocks) = stat($Dfile);
214 @h{0..200} = 200..400;
216 ok(30, join(':',200..400) eq join(':',@foo) );
219 # Now check all the non-tie specific stuff
221 # Check NOOVERWRITE will make put fail when attempting to overwrite
222 # an existing record.
224 $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
225 ok(31, $status == 1 );
227 # check that the value of the key 'x' has not been changed by the
229 ok(32, $h{'x'} eq 'X' );
232 $status = $X->put('key', 'value') ;
233 ok(33, $status == 0 );
235 #check that previous put can be retrieved
237 $status = $X->get('key', $value) ;
238 ok(34, $status == 0 );
239 ok(35, $value eq 'value' );
241 # Attempting to delete an existing key should work
243 $status = $X->del('q') ;
244 ok(36, $status == 0 );
246 # Make sure that the key deleted, cannot be retrieved
248 ok(37, $h{'q'} eq undef );
251 # Attempting to delete a non-existant key should fail
253 $status = $X->del('joe') ;
254 ok(38, $status == 1 );
256 # Check the get interface
258 # First a non-existing key
259 $status = $X->get('aaaa', $value) ;
260 ok(39, $status == 1 );
262 # Next an existing key
263 $status = $X->get('a', $value) ;
264 ok(40, $status == 0 );
265 ok(41, $value eq 'A' );
270 # ditto, but use put to replace the key/value pair.
272 # use seq to walk backwards through a file - check that this reversed is
274 # check seq FIRST/LAST
280 ok(42, $status == 0 );
287 ok(43, $status != 0 );
297 ok(44, tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
299 { $h{$_} = $_ * 100 }
301 # check that there are 10 elements in the hash
303 while (($key,$value) = each(%h)) {
313 while (($key,$value) = each(%h)) {
322 # Now try an in memory file
323 ok(47, $X = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
325 # fd with an in memory file should return fail
327 ok(48, $status == -1 );
333 # check ability to override the default hashing
335 my $filename = "xyz" ;
336 my $hi = new DB_File::HASHINFO ;
338 $hi->{hash} = sub { ++$::count ; length $_[0] } ;
339 ok(49, tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $hi ) ;
341 ok(50, $h{"abc"} == 123) ;
344 ok(51, $::count >0) ;
348 # check that attempting to tie an array to a DB_HASH will fail
350 my $filename = "xyz" ;
352 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_HASH ; } ;
353 ok(52, $@ =~ /^DB_File can only tie an associative array to a DB_HASH database/) ;
364 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
370 use vars qw( @ISA @EXPORT) ;
375 @EXPORT = @DB_File::EXPORT ;
381 $self->SUPER::STORE($key, $value * 2) ;
387 $self->SUPER::FETCH($key) - 1 ;
394 $self->SUPER::put($key, $value * 3) ;
399 $self->SUPER::get($_[0], $_[1]) ;
407 my $value = $self->FETCH($key) ;
408 return "[[$value]]" ;
416 BEGIN { push @INC, '.'; }
418 main::ok(53, $@ eq "") ;
422 $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640, $DB_HASH );
425 main::ok(54, $@ eq "") ;
427 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
428 main::ok(55, $@ eq "") ;
429 main::ok(56, $ret == 5) ;
432 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
433 main::ok(57, $@ eq "") ;
434 main::ok(58, $ret == 10) ;
436 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
437 main::ok(59, $@ eq "" ) ;
438 main::ok(60, $ret == 1) ;
440 $ret = eval '$X->A_new_method("joe") ' ;
441 main::ok(61, $@ eq "") ;
442 main::ok(62, $ret eq "[[11]]") ;
446 unlink "SubDB.pm", "dbhash.tmp" ;
454 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
459 my($fk, $sk, $fv, $sv) = @_ ;
461 $fetch_key eq $fk && $store_key eq $sk &&
462 $fetch_value eq $fv && $store_value eq $sv &&
466 ok(63, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
468 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
469 $db->filter_store_key (sub { $store_key = $_ }) ;
470 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
471 $db->filter_store_value (sub { $store_value = $_ }) ;
477 ok(64, checkOutput( "", "fred", "", "joe")) ;
479 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
480 ok(65, $h{"fred"} eq "joe");
482 ok(66, checkOutput( "", "fred", "joe", "")) ;
484 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
485 ok(67, $db->FIRSTKEY() eq "fred") ;
487 ok(68, checkOutput( "fred", "", "", "")) ;
489 # replace the filters, but remember the previous set
490 my ($old_fk) = $db->filter_fetch_key
491 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
492 my ($old_sk) = $db->filter_store_key
493 (sub { $_ = lc $_ ; $store_key = $_ }) ;
494 my ($old_fv) = $db->filter_fetch_value
495 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
496 my ($old_sv) = $db->filter_store_value
497 (sub { s/o/x/g; $store_value = $_ }) ;
499 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
502 ok(69, checkOutput( "", "fred", "", "Jxe")) ;
504 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
505 ok(70, $h{"Fred"} eq "[Jxe]");
507 ok(71, checkOutput( "", "fred", "[Jxe]", "")) ;
509 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
510 ok(72, $db->FIRSTKEY() eq "FRED") ;
512 ok(73, checkOutput( "FRED", "", "", "")) ;
514 # put the original filters back
515 $db->filter_fetch_key ($old_fk);
516 $db->filter_store_key ($old_sk);
517 $db->filter_fetch_value ($old_fv);
518 $db->filter_store_value ($old_sv);
520 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
522 ok(74, checkOutput( "", "fred", "", "joe")) ;
524 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
525 ok(75, $h{"fred"} eq "joe");
526 ok(76, checkOutput( "", "fred", "joe", "")) ;
528 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
529 ok(77, $db->FIRSTKEY() eq "fred") ;
530 ok(78, checkOutput( "fred", "", "", "")) ;
533 $db->filter_fetch_key (undef);
534 $db->filter_store_key (undef);
535 $db->filter_fetch_value (undef);
536 $db->filter_store_value (undef);
538 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
540 ok(79, checkOutput( "", "", "", "")) ;
542 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
543 ok(80, $h{"fred"} eq "joe");
544 ok(81, checkOutput( "", "", "", "")) ;
546 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
547 ok(82, $db->FIRSTKEY() eq "fred") ;
548 ok(83, checkOutput( "", "", "", "")) ;
556 # DBM Filter with a closure
562 ok(84, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
572 return sub { ++$count ;
574 $result{$name} = "$name - $count: [@kept]" ;
578 $db->filter_store_key(Closure("store key")) ;
579 $db->filter_store_value(Closure("store value")) ;
580 $db->filter_fetch_key(Closure("fetch key")) ;
581 $db->filter_fetch_value(Closure("fetch value")) ;
586 ok(85, $result{"store key"} eq "store key - 1: [fred]");
587 ok(86, $result{"store value"} eq "store value - 1: [joe]");
588 ok(87, ! defined $result{"fetch key"} );
589 ok(88, ! defined $result{"fetch value"} );
590 ok(89, $_ eq "original") ;
592 ok(90, $db->FIRSTKEY() eq "fred") ;
593 ok(91, $result{"store key"} eq "store key - 1: [fred]");
594 ok(92, $result{"store value"} eq "store value - 1: [joe]");
595 ok(93, $result{"fetch key"} eq "fetch key - 1: [fred]");
596 ok(94, ! defined $result{"fetch value"} );
597 ok(95, $_ eq "original") ;
600 ok(96, $result{"store key"} eq "store key - 2: [fred jim]");
601 ok(97, $result{"store value"} eq "store value - 2: [joe john]");
602 ok(98, $result{"fetch key"} eq "fetch key - 1: [fred]");
603 ok(99, ! defined $result{"fetch value"} );
604 ok(100, $_ eq "original") ;
606 ok(101, $h{"fred"} eq "joe");
607 ok(102, $result{"store key"} eq "store key - 3: [fred jim fred]");
608 ok(103, $result{"store value"} eq "store value - 2: [joe john]");
609 ok(104, $result{"fetch key"} eq "fetch key - 1: [fred]");
610 ok(105, $result{"fetch value"} eq "fetch value - 1: [joe]");
611 ok(106, $_ eq "original") ;
619 # DBM Filter recursion detection
624 ok(107, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
626 $db->filter_store_key (sub { $_ = $h{$_} }) ;
628 eval '$h{1} = 1234' ;
629 ok(108, $@ =~ /^recursion detected in filter_store_key at/ );
638 # Examples from the POD
642 my $redirect = new Redirect $file ;
646 use vars qw( %h $k $v ) ;
649 tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH
650 or die "Cannot open file 'fruit': $!\n";
652 # Add a few key/value pairs to the file
653 $h{"apple"} = "red" ;
654 $h{"orange"} = "orange" ;
655 $h{"banana"} = "yellow" ;
656 $h{"tomato"} = "red" ;
658 # Check for existence of a key
659 print "Banana Exists\n\n" if $h{"banana"} ;
661 # Delete a key/value pair.
664 # print the contents of the file
665 while (($k, $v) = each %h)
666 { print "$k -> $v\n" }
673 ok(109, docat_del($file) eq <<'EOM') ;