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) ;
83
local $SIG{__WARN__} = sub {-- $no_inner } ;
89
my $Dfile = "dbhash.tmp";
90
my $Dfile2 = "dbhash2.tmp";
91
my $null_keys_allowed = ($DB_File::db_ver < 2.004010
92
|| $DB_File::db_ver >= 3.1 );
98
# Check the interface to HASHINFO
100
my $dbh = new DB_File::HASHINFO ;
102
ok(1, ! defined $dbh->{bsize}) ;
103
ok(2, ! defined $dbh->{ffactor}) ;
104
ok(3, ! defined $dbh->{nelem}) ;
105
ok(4, ! defined $dbh->{cachesize}) ;
106
ok(5, ! defined $dbh->{hash}) ;
107
ok(6, ! defined $dbh->{lorder}) ;
109
$dbh->{bsize} = 3000 ;
110
ok(7, $dbh->{bsize} == 3000 );
112
$dbh->{ffactor} = 9000 ;
113
ok(8, $dbh->{ffactor} == 9000 );
115
$dbh->{nelem} = 400 ;
116
ok(9, $dbh->{nelem} == 400 );
118
$dbh->{cachesize} = 65 ;
119
ok(10, $dbh->{cachesize} == 65 );
121
my $some_sub = sub {} ;
122
$dbh->{hash} = $some_sub;
123
ok(11, $dbh->{hash} eq $some_sub );
125
$dbh->{lorder} = 1234 ;
126
ok(12, $dbh->{lorder} == 1234 );
128
# Check that an invalid entry is caught both for store & fetch
129
eval '$dbh->{fred} = 1234' ;
130
ok(13, $@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ );
131
eval 'my $q = $dbh->{fred}' ;
132
ok(14, $@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ );
135
# Now check the interface to HASH
137
ok(15, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
138
die "Could not tie: $!" unless $X;
140
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
141
$blksize,$blocks) = stat($Dfile);
143
my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
145
ok(16, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640) ||
148
my ($key, $value, $i);
149
while (($key,$value) = each(%h)) {
154
$h{'goner1'} = 'snork';
157
ok(18, $h{'abc'} eq 'ABC' );
158
ok(19, !defined $h{'jimmy'} );
159
ok(20, !exists $h{'jimmy'} );
160
ok(21, exists $h{'abc'} );
163
$h{'jkl','mno'} = "JKL\034MNO";
164
$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
168
$X->STORE('b', 'B') ;
181
$h{'goner2'} = 'snork';
185
# IMPORTANT - $X must be undefined before the untie otherwise the
186
# underlying DB close routine will not get called.
191
# tie to the same file again, do not supply a type - should default to HASH
192
ok(22, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640) );
194
# Modify an entry from the previous tie
215
$h{'goner3'} = 'snork';
218
$X->DELETE('goner3');
221
my @values = values(%h);
223
ok(23, $#keys == 29 && $#values == 29) ;
226
while (($key,$value) = each(%h)) {
227
if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
229
$i++ if $key eq $value;
235
@keys = ('blurfl', keys(%h), 'dyick');
236
ok(25, $#keys == 31) ;
239
ok(26, $h{'foo'} eq '' );
241
# Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
242
# This feature was reenabled in version 3.1 of Berkeley DB.
244
if ($null_keys_allowed) {
246
$result = ( $h{''} eq 'bar' );
252
# check cache overflow and numeric keys and contents
254
for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
255
for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
258
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
259
$blksize,$blocks) = stat($Dfile);
262
@h{0..200} = 200..400;
263
my @foo = @h{0..200};
264
ok(30, join(':',200..400) eq join(':',@foo) );
267
# Now check all the non-tie specific stuff
269
# Check NOOVERWRITE will make put fail when attempting to overwrite
270
# an existing record.
272
my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
273
ok(31, $status == 1 );
275
# check that the value of the key 'x' has not been changed by the
277
ok(32, $h{'x'} eq 'X' );
280
$status = $X->put('key', 'value') ;
281
ok(33, $status == 0 );
283
#check that previous put can be retrieved
285
$status = $X->get('key', $value) ;
286
ok(34, $status == 0 );
287
ok(35, $value eq 'value' );
289
# Attempting to delete an existing key should work
291
$status = $X->del('q') ;
292
ok(36, $status == 0 );
294
# Make sure that the key deleted, cannot be retrieved
296
no warnings 'uninitialized' ;
297
ok(37, $h{'q'} eq undef );
300
# Attempting to delete a non-existant key should fail
302
$status = $X->del('joe') ;
303
ok(38, $status == 1 );
305
# Check the get interface
307
# First a non-existing key
308
$status = $X->get('aaaa', $value) ;
309
ok(39, $status == 1 );
311
# Next an existing key
312
$status = $X->get('a', $value) ;
313
ok(40, $status == 0 );
314
ok(41, $value eq 'A' );
319
# ditto, but use put to replace the key/value pair.
321
# use seq to walk backwards through a file - check that this reversed is
323
# check seq FIRST/LAST
329
ok(42, $status == 0 );
336
ok(43, $status != 0 );
346
ok(44, tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
348
{ $h{$_} = $_ * 100 }
350
# check that there are 10 elements in the hash
352
while (($key,$value) = each(%h)) {
362
while (($key,$value) = each(%h)) {
371
# Now try an in memory file
372
ok(47, $X = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
374
# fd with an in memory file should return fail
376
ok(48, $status == -1 );
382
# check ability to override the default hashing
384
my $filename = "xyz" ;
385
my $hi = new DB_File::HASHINFO ;
387
$hi->{hash} = sub { ++$::count ; length $_[0] } ;
388
ok(49, tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $hi ) ;
390
ok(50, $h{"abc"} == 123) ;
393
ok(51, $::count >0) ;
397
# check that attempting to tie an array to a DB_HASH will fail
399
my $filename = "xyz" ;
401
eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_HASH ; } ;
402
ok(52, $@ =~ /^DB_File can only tie an associative array to a DB_HASH database/) ;
414
open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
426
@EXPORT = @DB_File::EXPORT ;
432
$self->SUPER::STORE($key, $value * 2) ;
438
$self->SUPER::FETCH($key) - 1 ;
445
$self->SUPER::put($key, $value * 3) ;
450
$self->SUPER::get($_[0], $_[1]) ;
458
my $value = $self->FETCH($key) ;
459
return "[[$value]]" ;
467
BEGIN { push @INC, '.'; }
469
main::ok(53, $@ eq "") ;
473
$X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640, $DB_HASH );
476
main::ok(54, $@ eq "") ;
478
my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
479
main::ok(55, $@ eq "") ;
480
main::ok(56, $ret == 5) ;
483
$ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
484
main::ok(57, $@ eq "") ;
485
main::ok(58, $ret == 10) ;
487
$ret = eval ' R_NEXT eq main::R_NEXT ' ;
488
main::ok(59, $@ eq "" ) ;
489
main::ok(60, $ret == 1) ;
491
$ret = eval '$X->A_new_method("joe") ' ;
492
main::ok(61, $@ eq "") ;
493
main::ok(62, $ret eq "[[11]]") ;
497
unlink "SubDB.pm", "dbhash.tmp" ;
506
my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
511
no warnings 'uninitialized';
512
my($fk, $sk, $fv, $sv) = @_ ;
514
print "# Fetch Key : expected '$fk' got '$fetch_key'\n"
515
if $fetch_key ne $fk ;
516
print "# Fetch Value : expected '$fv' got '$fetch_value'\n"
517
if $fetch_value ne $fv ;
518
print "# Store Key : expected '$sk' got '$store_key'\n"
519
if $store_key ne $sk ;
520
print "# Store Value : expected '$sv' got '$store_value'\n"
521
if $store_value ne $sv ;
522
print "# \$_ : expected 'original' got '$_'\n"
523
if $_ ne 'original' ;
526
$fetch_key eq $fk && $store_key eq $sk &&
527
$fetch_value eq $fv && $store_value eq $sv &&
531
ok(63, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
533
$db->filter_fetch_key (sub { $fetch_key = $_ }) ;
534
$db->filter_store_key (sub { $store_key = $_ }) ;
535
$db->filter_fetch_value (sub { $fetch_value = $_}) ;
536
$db->filter_store_value (sub { $store_value = $_ }) ;
542
ok(64, checkOutput( "", "fred", "", "joe")) ;
544
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
545
ok(65, $h{"fred"} eq "joe");
547
ok(66, checkOutput( "", "fred", "joe", "")) ;
549
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
552
ok(67, ! $db->seq($k, $v, R_FIRST) ) ;
553
ok(68, $k eq "fred") ;
554
ok(69, $v eq "joe") ;
556
ok(70, checkOutput( "fred", "fred", "joe", "")) ;
558
# replace the filters, but remember the previous set
559
my ($old_fk) = $db->filter_fetch_key
560
(sub { $_ = uc $_ ; $fetch_key = $_ }) ;
561
my ($old_sk) = $db->filter_store_key
562
(sub { $_ = lc $_ ; $store_key = $_ }) ;
563
my ($old_fv) = $db->filter_fetch_value
564
(sub { $_ = "[$_]"; $fetch_value = $_ }) ;
565
my ($old_sv) = $db->filter_store_value
566
(sub { s/o/x/g; $store_value = $_ }) ;
568
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
571
ok(71, checkOutput( "", "fred", "", "Jxe")) ;
573
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
574
ok(72, $h{"Fred"} eq "[Jxe]");
576
ok(73, checkOutput( "", "fred", "[Jxe]", "")) ;
578
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
580
ok(74, ! $db->seq($k, $v, R_FIRST) ) ;
581
ok(75, $k eq "FRED") ;
582
ok(76, $v eq "[Jxe]") ;
584
ok(77, checkOutput( "FRED", "fred", "[Jxe]", "")) ;
586
# put the original filters back
587
$db->filter_fetch_key ($old_fk);
588
$db->filter_store_key ($old_sk);
589
$db->filter_fetch_value ($old_fv);
590
$db->filter_store_value ($old_sv);
592
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
594
ok(78, checkOutput( "", "fred", "", "joe")) ;
596
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
597
ok(79, $h{"fred"} eq "joe");
598
ok(80, checkOutput( "", "fred", "joe", "")) ;
600
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
601
#ok(77, $db->FIRSTKEY() eq "fred") ;
603
ok(81, ! $db->seq($k, $v, R_FIRST) ) ;
604
ok(82, $k eq "fred") ;
605
ok(83, $v eq "joe") ;
607
ok(84, checkOutput( "fred", "fred", "joe", "")) ;
610
$db->filter_fetch_key (undef);
611
$db->filter_store_key (undef);
612
$db->filter_fetch_value (undef);
613
$db->filter_store_value (undef);
615
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
617
ok(85, checkOutput( "", "", "", "")) ;
619
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
620
ok(86, $h{"fred"} eq "joe");
621
ok(87, checkOutput( "", "", "", "")) ;
623
($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
625
ok(88, ! $db->seq($k, $v, R_FIRST) ) ;
626
ok(89, $k eq "fred") ;
627
ok(90, $v eq "joe") ;
628
ok(91, checkOutput( "", "", "", "")) ;
636
# DBM Filter with a closure
643
ok(92, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
653
return sub { ++$count ;
655
$result{$name} = "$name - $count: [@kept]" ;
659
$db->filter_store_key(Closure("store key")) ;
660
$db->filter_store_value(Closure("store value")) ;
661
$db->filter_fetch_key(Closure("fetch key")) ;
662
$db->filter_fetch_value(Closure("fetch value")) ;
667
ok(93, $result{"store key"} eq "store key - 1: [fred]");
668
ok(94, $result{"store value"} eq "store value - 1: [joe]");
669
ok(95, ! defined $result{"fetch key"} );
670
ok(96, ! defined $result{"fetch value"} );
671
ok(97, $_ eq "original") ;
673
ok(98, $db->FIRSTKEY() eq "fred") ;
674
ok(99, $result{"store key"} eq "store key - 1: [fred]");
675
ok(100, $result{"store value"} eq "store value - 1: [joe]");
676
ok(101, $result{"fetch key"} eq "fetch key - 1: [fred]");
677
ok(102, ! defined $result{"fetch value"} );
678
ok(103, $_ eq "original") ;
681
ok(104, $result{"store key"} eq "store key - 2: [fred jim]");
682
ok(105, $result{"store value"} eq "store value - 2: [joe john]");
683
ok(106, $result{"fetch key"} eq "fetch key - 1: [fred]");
684
ok(107, ! defined $result{"fetch value"} );
685
ok(108, $_ eq "original") ;
687
ok(109, $h{"fred"} eq "joe");
688
ok(110, $result{"store key"} eq "store key - 3: [fred jim fred]");
689
ok(111, $result{"store value"} eq "store value - 2: [joe john]");
690
ok(112, $result{"fetch key"} eq "fetch key - 1: [fred]");
691
ok(113, $result{"fetch value"} eq "fetch value - 1: [joe]");
692
ok(114, $_ eq "original") ;
700
# DBM Filter recursion detection
706
ok(115, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
708
$db->filter_store_key (sub { $_ = $h{$_} }) ;
710
eval '$h{1} = 1234' ;
711
ok(116, $@ =~ /^recursion detected in filter_store_key at/ );
720
# Examples from the POD
724
my $redirect = new Redirect $file ;
726
use warnings FATAL => qw(all);
732
tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH
733
or die "Cannot open file 'fruit': $!\n";
735
# Add a few key/value pairs to the file
736
$h{"apple"} = "red" ;
737
$h{"orange"} = "orange" ;
738
$h{"banana"} = "yellow" ;
739
$h{"tomato"} = "red" ;
741
# Check for existence of a key
742
print "Banana Exists\n\n" if $h{"banana"} ;
744
# Delete a key/value pair.
747
# print the contents of the file
748
while (($k, $v) = each %h)
749
{ print "$k -> $v\n" }
756
ok(117, docat_del($file) eq <<'EOM') ;
767
# Bug ID 20001013.009
769
# test that $hash{KEY} = undef doesn't produce the warning
770
# Use of uninitialized value in null operation
778
local $SIG{__WARN__} = sub {$a = $_[0]} ;
780
tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;
788
# test that %hash = () doesn't produce the warning
789
# Argument "" isn't numeric in entersub
797
local $SIG{__WARN__} = sub {$a = $_[0]} ;
799
tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;
807
# When iterating over a tied hash using "each", the key passed to FETCH
808
# will be recycled and passed to NEXTKEY. If a Source Filter modifies the
809
# key in FETCH via a filter_fetch_key method we need to check that the
810
# modified key doesn't get passed to NEXTKEY.
811
# Also Test "keys" & "values" while we are at it.
821
ok(120, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
822
$db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
823
$db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
825
$h{'Alpha_ABC'} = 2 ;
826
$h{'Alpha_DEF'} = 5 ;
828
ok(121, $h{'Alpha_ABC'} == 2);
829
ok(122, $h{'Alpha_DEF'} == 5);
831
my ($k, $v) = ("","");
832
while (($k, $v) = each %h) {}
833
ok(123, $bad_key == 0);
836
foreach $k (keys %h) {}
837
ok(124, $bad_key == 0);
840
foreach $v (values %h) {}
841
ok(125, $bad_key == 0);
849
# now an error to pass 'hash' a non-code reference
850
my $dbh = new DB_File::HASHINFO ;
852
eval { $dbh->{hash} = 2 };
853
ok(126, $@ =~ /^Key 'hash' not associated with a code reference at/);
858
# recursion detection in hash
861
my $dbh = new DB_File::HASHINFO ;
862
$dbh->{hash} = sub { $hash{3} = 4 ; length $_[0] } ;
866
ok(127, tie(%hash, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh ) );
872
ok(128, $@ =~ /^DB_File hash callback: recursion detected/);
881
# Check that two hash's don't interact
886
unlink $Dfile, $Dfile2;
887
my $dbh1 = new DB_File::HASHINFO ;
888
$dbh1->{hash} = sub { ++ $h1_count ; length $_[0] } ;
890
my $dbh2 = new DB_File::HASHINFO ;
891
$dbh2->{hash} = sub { ++ $h2_count ; length $_[0] } ;
896
ok(129, tie(%hash1, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh1 ) );
897
ok(130, tie(%hash2, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) );
907
ok(131, $h1_count > 0);
908
ok(132, $h1_count == $h2_count);
910
ok(133, safeUntie \%hash1);
911
ok(134, safeUntie \%hash2);
912
unlink $Dfile, $Dfile2;
916
# Passing undef for flags and/or mode when calling tie could cause
917
# Use of uninitialized value in subroutine entry
921
#local $SIG{__WARN__} = sub { ++ $warn_count };
925
tie %hash1, 'DB_File',$Dfile, undef;
926
ok(135, $warn_count == 0);
928
tie %hash1, 'DB_File',$Dfile, O_RDWR|O_CREAT, undef;
929
ok(136, $warn_count == 0);
930
tie %hash1, 'DB_File',$Dfile, undef, undef;
931
ok(137, $warn_count == 0);
938
# Check that DBM Filter can cope with read-only $_
945
ok(138, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
947
$db->filter_fetch_key (sub { }) ;
948
$db->filter_store_key (sub { }) ;
949
$db->filter_fetch_value (sub { }) ;
950
$db->filter_store_value (sub { }) ;
955
ok(139, $h{"fred"} eq "joe");
957
eval { grep { $h{$_} } (1, 2, 3) };
962
$db->filter_fetch_key (undef);
963
$db->filter_store_key (undef);
964
$db->filter_fetch_value (undef);
965
$db->filter_store_value (undef);
969
ok(141, $h{"fred"} eq "joe");
971
ok(142, $db->FIRSTKEY() eq "fred") ;
973
eval { grep { $h{$_} } (1, 2, 3) };