5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
7 print "1..0 # Skip: DB_File was not built\n";
16 use vars qw($dbh $Dfile $bad_ones $FA) ;
18 # full tied array support started in Perl 5.004_57
19 # Double check to see if it is available.
22 sub try::TIEARRAY { bless [], "try" }
23 sub try::FETCHSIZE { $FA = 1 }
36 print "not " unless $result ;
49 my $filename = shift ;
51 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
52 my $real_stdout = select($fh) ;
53 return bless [$fh, $real_stdout ] ;
68 open(CAT,$file) || die "Cannot open $file:$!";
78 open(CAT,$file) || die "Cannot open $file: $!";
87 print STDERR <<EOM unless $bad_ones++ ;
89 # Some older versions of Berkeley DB version 1 will fail tests 51,
92 # You can safely ignore the errors if you're never going to use the
93 # broken functionality (recno databases with a modified bval).
94 # Otherwise you'll have to upgrade your DB library.
96 # If you want to use Berkeley DB version 1, then 1.85 and 1.86 are the
97 # last versions that were released. Berkeley DB version 2 is continually
98 # being updated -- Check out http://www.sleepycat.com/ for more details.
105 my $Dfile = "recno.tmp";
110 # Check the interface to RECNOINFO
112 my $dbh = new DB_File::RECNOINFO ;
113 ok(1, ! defined $dbh->{bval}) ;
114 ok(2, ! defined $dbh->{cachesize}) ;
115 ok(3, ! defined $dbh->{psize}) ;
116 ok(4, ! defined $dbh->{flags}) ;
117 ok(5, ! defined $dbh->{lorder}) ;
118 ok(6, ! defined $dbh->{reclen}) ;
119 ok(7, ! defined $dbh->{bfname}) ;
121 $dbh->{bval} = 3000 ;
122 ok(8, $dbh->{bval} == 3000 );
124 $dbh->{cachesize} = 9000 ;
125 ok(9, $dbh->{cachesize} == 9000 );
127 $dbh->{psize} = 400 ;
128 ok(10, $dbh->{psize} == 400 );
131 ok(11, $dbh->{flags} == 65 );
133 $dbh->{lorder} = 123 ;
134 ok(12, $dbh->{lorder} == 123 );
136 $dbh->{reclen} = 1234 ;
137 ok(13, $dbh->{reclen} == 1234 );
139 $dbh->{bfname} = 1234 ;
140 ok(14, $dbh->{bfname} == 1234 );
143 # Check that an invalid entry is caught both for store & fetch
144 eval '$dbh->{fred} = 1234' ;
145 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
146 eval 'my $q = $dbh->{fred}' ;
147 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
149 # Now check the interface to RECNOINFO
153 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
155 ok(18, ((stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)
156 || $^O eq 'MSWin32' || $^O eq 'amigaos') ;
160 ok(19, ($FA ? @h == 0 : !$l) );
162 my @data = qw( a b c d ever f g h i j k longername m n o p) ;
164 $h[0] = shift @data ;
165 ok(20, $h[0] eq 'a' );
171 unshift (@data, 'a') ;
173 ok(21, defined $h[1] );
174 ok(22, ! defined $h[16] );
175 ok(23, $FA ? @h == @data : $X->length == @data );
178 # Overwrite an entry & check fetch it
180 $data[3] = 'replaced' ;
181 ok(24, $h[3] eq 'replaced' );
184 my @push_data = qw(added to the end) ;
185 ($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
186 push (@data, @push_data) ;
187 ok(25, $h[++$i] eq 'added' );
188 ok(26, $h[++$i] eq 'to' );
189 ok(27, $h[++$i] eq 'the' );
190 ok(28, $h[++$i] eq 'end' );
193 my $popped = pop (@data) ;
194 my $value = ($FA ? pop @h : $X->pop) ;
195 ok(29, $value eq $popped) ;
198 $value = ($FA ? shift @h : $X->shift) ;
199 my $shifted = shift @data ;
200 ok(30, $value eq $shifted );
205 ($FA ? unshift @h : $X->unshift) ;
206 ok(31, ($FA ? @h == @data : $X->length == @data ));
208 my @new_data = qw(add this to the start of the array) ;
209 $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
210 unshift (@data, @new_data) ;
211 ok(32, $FA ? @h == @data : $X->length == @data );
212 ok(33, $h[0] eq "add") ;
213 ok(34, $h[1] eq "this") ;
214 ok(35, $h[2] eq "to") ;
215 ok(36, $h[3] eq "the") ;
216 ok(37, $h[4] eq "start") ;
217 ok(38, $h[5] eq "of") ;
218 ok(39, $h[6] eq "the") ;
219 ok(40, $h[7] eq "array") ;
220 ok(41, $h[8] eq $data[8]) ;
224 # Now both arrays should be identical
230 $ok = 0, last if $_ ne $h[$j ++] ;
234 # Neagtive subscripts
236 # get the last element of the array
237 ok(43, $h[-1] eq $data[-1] );
238 ok(44, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
240 # get the first element using a negative subscript
241 eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
243 ok(46, $h[0] eq "abcd" );
245 # now try to read before the start of the array
246 eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
247 ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
249 # IMPORTANT - $X must be undefined before the untie otherwise the
250 # underlying DB close routine will not get called.
258 # Check bval defaults to \n
261 my $dbh = new DB_File::RECNOINFO ;
262 ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
267 my $x = docat($Dfile) ;
269 ok(49, $x eq "abc\ndef\n\nghi\n") ;
276 my $dbh = new DB_File::RECNOINFO ;
278 ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
283 my $x = docat($Dfile) ;
285 my $ok = ($x eq "abc-def--ghi-") ;
286 bad_one() unless $ok ;
291 # Check R_FIXEDLEN with default bval (space)
294 my $dbh = new DB_File::RECNOINFO ;
295 $dbh->{flags} = R_FIXEDLEN ;
297 ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
302 my $x = docat($Dfile) ;
304 my $ok = ($x eq "abc def ghi ") ;
305 bad_one() unless $ok ;
310 # Check R_FIXEDLEN with user-defined bval
313 my $dbh = new DB_File::RECNOINFO ;
314 $dbh->{flags} = R_FIXEDLEN ;
317 ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
322 my $x = docat($Dfile) ;
324 my $ok = ($x eq "abc--def-------ghi--") ;
325 bad_one() unless $ok ;
330 # check that attempting to tie an associative array to a DB_RECNO will fail
332 my $filename = "xyz" ;
334 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
335 ok(56, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
347 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
354 use vars qw( @ISA @EXPORT) ;
359 @EXPORT = @DB_File::EXPORT ;
365 $self->SUPER::STORE($key, $value * 2) ;
371 $self->SUPER::FETCH($key) - 1 ;
378 $self->SUPER::put($key, $value * 3) ;
383 $self->SUPER::get($_[0], $_[1]) ;
391 my $value = $self->FETCH($key) ;
392 return "[[$value]]" ;
400 BEGIN { push @INC, '.'; }
402 main::ok(57, $@ eq "") ;
406 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
409 main::ok(58, $@ eq "") ;
411 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
412 main::ok(59, $@ eq "") ;
413 main::ok(60, $ret == 5) ;
416 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
417 main::ok(61, $@ eq "") ;
418 main::ok(62, $ret == 10) ;
420 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
421 main::ok(63, $@ eq "" ) ;
422 main::ok(64, $ret == 1) ;
424 $ret = eval '$X->A_new_method(1) ' ;
425 main::ok(65, $@ eq "") ;
426 main::ok(66, $ret eq "[[11]]") ;
430 unlink "SubDB.pm", "recno.tmp" ;
439 ok(67, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
444 ok(68, $FA ? $#h == 3 : $self->length() == 4) ;
447 my $x = docat($Dfile) ;
448 ok(69, $x eq "abc\ndef\nghi\njkl\n") ;
450 # $# sets array to same length
451 ok(70, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
455 { $self->STORESIZE(4) }
456 ok(71, $FA ? $#h == 3 : $self->length() == 4) ;
460 ok(72, $x eq "abc\ndef\nghi\njkl\n") ;
462 # $# sets array to bigger
463 ok(73, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
467 { $self->STORESIZE(7) }
468 ok(74, $FA ? $#h == 6 : $self->length() == 7) ;
472 ok(75, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
474 # $# sets array smaller
475 ok(76, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
479 { $self->STORESIZE(3) }
480 ok(77, $FA ? $#h == 2 : $self->length() == 3) ;
484 ok(78, $x eq "abc\ndef\nghi\n") ;
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(79, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
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(80, checkOutput( "", 0, "", "joe")) ;
521 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
522 ok(81, $h[0] eq "joe");
524 ok(82, checkOutput( "", 0, "joe", "")) ;
526 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
527 ok(83, $db->FIRSTKEY() == 0) ;
529 ok(84, checkOutput( 0, "", "", "")) ;
531 # replace the filters, but remember the previous set
532 my ($old_fk) = $db->filter_fetch_key
533 (sub { ++ $_ ; $fetch_key = $_ }) ;
534 my ($old_sk) = $db->filter_store_key
535 (sub { $_ *= 2 ; $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(85, checkOutput( "", 2, "", "Jxe")) ;
546 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
547 ok(86, $h[1] eq "[Jxe]");
549 ok(87, checkOutput( "", 2, "[Jxe]", "")) ;
551 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
552 ok(88, $db->FIRSTKEY() == 1) ;
554 ok(89, checkOutput( 1, "", "", "")) ;
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(90, checkOutput( "", 0, "", "joe")) ;
566 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
567 ok(91, $h[0] eq "joe");
568 ok(92, checkOutput( "", 0, "joe", "")) ;
570 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
571 ok(93, $db->FIRSTKEY() == 0) ;
572 ok(94, checkOutput( 0, "", "", "")) ;
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(95, checkOutput( "", "", "", "")) ;
584 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
585 ok(96, $h[0] eq "joe");
586 ok(97, checkOutput( "", "", "", "")) ;
588 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
589 ok(98, $db->FIRSTKEY() == 0) ;
590 ok(99, checkOutput( "", "", "", "")) ;
598 # DBM Filter with a closure
605 ok(100, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
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(101, $result{"store key"} eq "store key - 1: [0]");
630 ok(102, $result{"store value"} eq "store value - 1: [joe]");
631 ok(103, ! defined $result{"fetch key"} );
632 ok(104, ! defined $result{"fetch value"} );
633 ok(105, $_ eq "original") ;
635 ok(106, $db->FIRSTKEY() == 0 ) ;
636 ok(107, $result{"store key"} eq "store key - 1: [0]");
637 ok(108, $result{"store value"} eq "store value - 1: [joe]");
638 ok(109, $result{"fetch key"} eq "fetch key - 1: [0]");
639 ok(110, ! defined $result{"fetch value"} );
640 ok(111, $_ eq "original") ;
643 ok(112, $result{"store key"} eq "store key - 2: [0 7]");
644 ok(113, $result{"store value"} eq "store value - 2: [joe john]");
645 ok(114, $result{"fetch key"} eq "fetch key - 1: [0]");
646 ok(115, ! defined $result{"fetch value"} );
647 ok(116, $_ eq "original") ;
649 ok(117, $h[0] eq "joe");
650 ok(118, $result{"store key"} eq "store key - 3: [0 7 0]");
651 ok(119, $result{"store value"} eq "store value - 2: [joe john]");
652 ok(120, $result{"fetch key"} eq "fetch key - 1: [0]");
653 ok(121, $result{"fetch value"} eq "fetch value - 1: [joe]");
654 ok(122, $_ eq "original") ;
662 # DBM Filter recursion detection
668 ok(123, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
670 $db->filter_store_key (sub { $_ = $h[0] }) ;
672 eval '$h[1] = 1234' ;
673 ok(124, $@ =~ /^recursion detected in filter_store_key at/ );
682 # Examples from the POD
686 my $redirect = new Redirect $file ;
688 use warnings FATAL => qw(all);
692 my $filename = "text" ;
696 my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
697 or die "Cannot open file 'text': $!\n" ;
699 # Add a few key/value pairs to the file
704 $FA ? push @h, "green", "black"
705 : $x->push("green", "black") ;
707 my $elements = $FA ? scalar @h : $x->length ;
708 print "The array contains $elements entries\n" ;
710 my $last = $FA ? pop @h : $x->pop ;
711 print "popped $last\n" ;
713 $FA ? unshift @h, "white"
714 : $x->unshift("white") ;
715 my $first = $FA ? shift @h : $x->shift ;
716 print "shifted $first\n" ;
718 # Check for existence of a key
719 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
721 # use a negative index
722 print "The last element is $h[-1]\n" ;
723 print "The 2nd last element is $h[-2]\n" ;
731 ok(125, docat_del($file) eq <<'EOM') ;
732 The array contains 5 entries
735 Element 1 Exists with value blue
736 The last element is green
737 The 2nd last element is yellow
740 my $save_output = "xyzt" ;
742 my $redirect = new Redirect $save_output ;
744 use warnings FATAL => qw(all);
746 use vars qw(@h $H $file $i) ;
754 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
755 or die "Cannot open file $file: $!\n" ;
757 # first create a text file to play with
765 # Print the records in order.
767 # The length method is needed here because evaluating a tied
768 # array in a scalar context does not return the number of
769 # elements in the array.
771 print "\nORIGINAL\n" ;
772 foreach $i (0 .. $H->length - 1) {
773 print "$i: $h[$i]\n" ;
776 # use the push & pop methods
779 print "\nThe last record was [$a]\n" ;
781 # and the shift & unshift methods
783 $H->unshift("first") ;
784 print "The first record was [$a]\n" ;
786 # Use the API to add a new record after record 2.
788 $H->put($i, "Newbie", R_IAFTER) ;
790 # and a new record before record 1.
792 $H->put($i, "New One", R_IBEFORE) ;
797 # now print the records in reverse order
798 print "\nREVERSE\n" ;
799 for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
800 { print "$i: $h[$i]\n" }
802 # same again, but use the API functions instead
803 print "\nREVERSE again\n" ;
804 my ($s, $k, $v) = (0, 0, 0) ;
805 for ($s = $H->seq($k, $v, R_LAST) ;
807 $s = $H->seq($k, $v, R_PREV))
816 ok(126, docat_del($save_output) eq <<'EOM') ;
825 The last record was [four]
826 The first record was [zero]
848 # Bug ID 20001013.009
850 # test that $hash{KEY} = undef doesn't produce the warning
851 # Use of uninitialized value in null operation
859 local $SIG{__WARN__} = sub {$a = $_[0]} ;
861 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
862 or die "Can't open file: $!\n" ;
870 # test that %hash = () doesn't produce the warning
871 # Argument "" isn't numeric in entersub
876 local $SIG{__WARN__} = sub {$a = $_[0]} ;
881 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
882 or die "Can't open file: $!\n" ;