6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bDB_File\b/) {
8 print "1..0 # Skip: DB_File was not built\n";
17 use vars qw($dbh $Dfile $bad_ones $FA) ;
19 # full tied array support started in Perl 5.004_57
20 # Double check to see if it is available.
23 sub try::TIEARRAY { bless [], "try" }
24 sub try::FETCHSIZE { $FA = 1 }
37 print "not " unless $result ;
50 my $filename = shift ;
52 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
53 my $real_stdout = select($fh) ;
54 return bless [$fh, $real_stdout ] ;
69 open(CAT,$file) || die "Cannot open $file:$!";
79 open(CAT,$file) || die "Cannot open $file: $!";
88 print STDERR <<EOM unless $bad_ones++ ;
90 # Some older versions of Berkeley DB version 1 will fail tests 51,
93 # You can safely ignore the errors if you're never going to use the
94 # broken functionality (recno databases with a modified bval).
95 # Otherwise you'll have to upgrade your DB library.
97 # If you want to use Berkeley DB version 1, then 1.85 and 1.86 are the
98 # last versions that were released. Berkeley DB version 2 is continually
99 # being updated -- Check out http://www.sleepycat.com/ for more details.
106 my $Dfile = "recno.tmp";
111 # Check the interface to RECNOINFO
113 my $dbh = new DB_File::RECNOINFO ;
114 ok(1, ! defined $dbh->{bval}) ;
115 ok(2, ! defined $dbh->{cachesize}) ;
116 ok(3, ! defined $dbh->{psize}) ;
117 ok(4, ! defined $dbh->{flags}) ;
118 ok(5, ! defined $dbh->{lorder}) ;
119 ok(6, ! defined $dbh->{reclen}) ;
120 ok(7, ! defined $dbh->{bfname}) ;
122 $dbh->{bval} = 3000 ;
123 ok(8, $dbh->{bval} == 3000 );
125 $dbh->{cachesize} = 9000 ;
126 ok(9, $dbh->{cachesize} == 9000 );
128 $dbh->{psize} = 400 ;
129 ok(10, $dbh->{psize} == 400 );
132 ok(11, $dbh->{flags} == 65 );
134 $dbh->{lorder} = 123 ;
135 ok(12, $dbh->{lorder} == 123 );
137 $dbh->{reclen} = 1234 ;
138 ok(13, $dbh->{reclen} == 1234 );
140 $dbh->{bfname} = 1234 ;
141 ok(14, $dbh->{bfname} == 1234 );
144 # Check that an invalid entry is caught both for store & fetch
145 eval '$dbh->{fred} = 1234' ;
146 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
147 eval 'my $q = $dbh->{fred}' ;
148 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
150 # Now check the interface to RECNOINFO
154 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
156 ok(18, ((stat($Dfile))[2] & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
157 || $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'amigaos') ;
161 ok(19, ($FA ? @h == 0 : !$l) );
163 my @data = qw( a b c d ever f g h i j k longername m n o p) ;
165 $h[0] = shift @data ;
166 ok(20, $h[0] eq 'a' );
172 unshift (@data, 'a') ;
174 ok(21, defined $h[1] );
175 ok(22, ! defined $h[16] );
176 ok(23, $FA ? @h == @data : $X->length == @data );
179 # Overwrite an entry & check fetch it
181 $data[3] = 'replaced' ;
182 ok(24, $h[3] eq 'replaced' );
185 my @push_data = qw(added to the end) ;
186 ($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
187 push (@data, @push_data) ;
188 ok(25, $h[++$i] eq 'added' );
189 ok(26, $h[++$i] eq 'to' );
190 ok(27, $h[++$i] eq 'the' );
191 ok(28, $h[++$i] eq 'end' );
194 my $popped = pop (@data) ;
195 my $value = ($FA ? pop @h : $X->pop) ;
196 ok(29, $value eq $popped) ;
199 $value = ($FA ? shift @h : $X->shift) ;
200 my $shifted = shift @data ;
201 ok(30, $value eq $shifted );
206 ($FA ? unshift @h,() : $X->unshift) ;
207 ok(31, ($FA ? @h == @data : $X->length == @data ));
209 my @new_data = qw(add this to the start of the array) ;
210 $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
211 unshift (@data, @new_data) ;
212 ok(32, $FA ? @h == @data : $X->length == @data );
213 ok(33, $h[0] eq "add") ;
214 ok(34, $h[1] eq "this") ;
215 ok(35, $h[2] eq "to") ;
216 ok(36, $h[3] eq "the") ;
217 ok(37, $h[4] eq "start") ;
218 ok(38, $h[5] eq "of") ;
219 ok(39, $h[6] eq "the") ;
220 ok(40, $h[7] eq "array") ;
221 ok(41, $h[8] eq $data[8]) ;
225 # Now both arrays should be identical
231 $ok = 0, last if $_ ne $h[$j ++] ;
235 # Neagtive subscripts
237 # get the last element of the array
238 ok(43, $h[-1] eq $data[-1] );
239 ok(44, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
241 # get the first element using a negative subscript
242 eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
244 ok(46, $h[0] eq "abcd" );
246 # now try to read before the start of the array
247 eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
248 ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
250 # IMPORTANT - $X must be undefined before the untie otherwise the
251 # underlying DB close routine will not get called.
259 # Check bval defaults to \n
262 my $dbh = new DB_File::RECNOINFO ;
263 ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
268 my $x = docat($Dfile) ;
270 ok(49, $x eq "abc\ndef\n\nghi\n") ;
277 my $dbh = new DB_File::RECNOINFO ;
279 ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
284 my $x = docat($Dfile) ;
286 my $ok = ($x eq "abc-def--ghi-") ;
287 bad_one() unless $ok ;
292 # Check R_FIXEDLEN with default bval (space)
295 my $dbh = new DB_File::RECNOINFO ;
296 $dbh->{flags} = R_FIXEDLEN ;
298 ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
303 my $x = docat($Dfile) ;
305 my $ok = ($x eq "abc def ghi ") ;
306 bad_one() unless $ok ;
311 # Check R_FIXEDLEN with user-defined bval
314 my $dbh = new DB_File::RECNOINFO ;
315 $dbh->{flags} = R_FIXEDLEN ;
318 ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
323 my $x = docat($Dfile) ;
325 my $ok = ($x eq "abc--def-------ghi--") ;
326 bad_one() unless $ok ;
331 # check that attempting to tie an associative array to a DB_RECNO will fail
333 my $filename = "xyz" ;
335 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
336 ok(56, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
348 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
355 use vars qw( @ISA @EXPORT) ;
360 @EXPORT = @DB_File::EXPORT ;
366 $self->SUPER::STORE($key, $value * 2) ;
372 $self->SUPER::FETCH($key) - 1 ;
379 $self->SUPER::put($key, $value * 3) ;
384 $self->SUPER::get($_[0], $_[1]) ;
392 my $value = $self->FETCH($key) ;
393 return "[[$value]]" ;
401 BEGIN { push @INC, '.'; }
403 main::ok(57, $@ eq "") ;
407 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
410 main::ok(58, $@ eq "") ;
412 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
413 main::ok(59, $@ eq "") ;
414 main::ok(60, $ret == 5) ;
417 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
418 main::ok(61, $@ eq "") ;
419 main::ok(62, $ret == 10) ;
421 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
422 main::ok(63, $@ eq "" ) ;
423 main::ok(64, $ret == 1) ;
425 $ret = eval '$X->A_new_method(1) ' ;
426 main::ok(65, $@ eq "") ;
427 main::ok(66, $ret eq "[[11]]") ;
431 unlink "SubDB.pm", "recno.tmp" ;
440 ok(67, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
445 ok(68, $FA ? $#h == 3 : $self->length() == 4) ;
448 my $x = docat($Dfile) ;
449 ok(69, $x eq "abc\ndef\nghi\njkl\n") ;
451 # $# sets array to same length
452 ok(70, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
456 { $self->STORESIZE(4) }
457 ok(71, $FA ? $#h == 3 : $self->length() == 4) ;
461 ok(72, $x eq "abc\ndef\nghi\njkl\n") ;
463 # $# sets array to bigger
464 ok(73, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
468 { $self->STORESIZE(7) }
469 ok(74, $FA ? $#h == 6 : $self->length() == 7) ;
473 ok(75, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
475 # $# sets array smaller
476 ok(76, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
480 { $self->STORESIZE(3) }
481 ok(77, $FA ? $#h == 2 : $self->length() == 3) ;
485 ok(78, $x eq "abc\ndef\nghi\n") ;
497 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
502 my($fk, $sk, $fv, $sv) = @_ ;
504 $fetch_key eq $fk && $store_key eq $sk &&
505 $fetch_value eq $fv && $store_value eq $sv &&
509 ok(79, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
511 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
512 $db->filter_store_key (sub { $store_key = $_ }) ;
513 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
514 $db->filter_store_value (sub { $store_value = $_ }) ;
520 ok(80, checkOutput( "", 0, "", "joe")) ;
522 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
523 ok(81, $h[0] eq "joe");
525 ok(82, checkOutput( "", 0, "joe", "")) ;
527 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
528 ok(83, $db->FIRSTKEY() == 0) ;
530 ok(84, checkOutput( 0, "", "", "")) ;
532 # replace the filters, but remember the previous set
533 my ($old_fk) = $db->filter_fetch_key
534 (sub { ++ $_ ; $fetch_key = $_ }) ;
535 my ($old_sk) = $db->filter_store_key
536 (sub { $_ *= 2 ; $store_key = $_ }) ;
537 my ($old_fv) = $db->filter_fetch_value
538 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
539 my ($old_sv) = $db->filter_store_value
540 (sub { s/o/x/g; $store_value = $_ }) ;
542 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
545 ok(85, checkOutput( "", 2, "", "Jxe")) ;
547 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
548 ok(86, $h[1] eq "[Jxe]");
550 ok(87, checkOutput( "", 2, "[Jxe]", "")) ;
552 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
553 ok(88, $db->FIRSTKEY() == 1) ;
555 ok(89, checkOutput( 1, "", "", "")) ;
557 # put the original filters back
558 $db->filter_fetch_key ($old_fk);
559 $db->filter_store_key ($old_sk);
560 $db->filter_fetch_value ($old_fv);
561 $db->filter_store_value ($old_sv);
563 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
565 ok(90, checkOutput( "", 0, "", "joe")) ;
567 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
568 ok(91, $h[0] eq "joe");
569 ok(92, checkOutput( "", 0, "joe", "")) ;
571 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
572 ok(93, $db->FIRSTKEY() == 0) ;
573 ok(94, checkOutput( 0, "", "", "")) ;
576 $db->filter_fetch_key (undef);
577 $db->filter_store_key (undef);
578 $db->filter_fetch_value (undef);
579 $db->filter_store_value (undef);
581 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
583 ok(95, checkOutput( "", "", "", "")) ;
585 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
586 ok(96, $h[0] eq "joe");
587 ok(97, checkOutput( "", "", "", "")) ;
589 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
590 ok(98, $db->FIRSTKEY() == 0) ;
591 ok(99, checkOutput( "", "", "", "")) ;
599 # DBM Filter with a closure
606 ok(100, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
616 return sub { ++$count ;
618 $result{$name} = "$name - $count: [@kept]" ;
622 $db->filter_store_key(Closure("store key")) ;
623 $db->filter_store_value(Closure("store value")) ;
624 $db->filter_fetch_key(Closure("fetch key")) ;
625 $db->filter_fetch_value(Closure("fetch value")) ;
630 ok(101, $result{"store key"} eq "store key - 1: [0]");
631 ok(102, $result{"store value"} eq "store value - 1: [joe]");
632 ok(103, ! defined $result{"fetch key"} );
633 ok(104, ! defined $result{"fetch value"} );
634 ok(105, $_ eq "original") ;
636 ok(106, $db->FIRSTKEY() == 0 ) ;
637 ok(107, $result{"store key"} eq "store key - 1: [0]");
638 ok(108, $result{"store value"} eq "store value - 1: [joe]");
639 ok(109, $result{"fetch key"} eq "fetch key - 1: [0]");
640 ok(110, ! defined $result{"fetch value"} );
641 ok(111, $_ eq "original") ;
644 ok(112, $result{"store key"} eq "store key - 2: [0 7]");
645 ok(113, $result{"store value"} eq "store value - 2: [joe john]");
646 ok(114, $result{"fetch key"} eq "fetch key - 1: [0]");
647 ok(115, ! defined $result{"fetch value"} );
648 ok(116, $_ eq "original") ;
650 ok(117, $h[0] eq "joe");
651 ok(118, $result{"store key"} eq "store key - 3: [0 7 0]");
652 ok(119, $result{"store value"} eq "store value - 2: [joe john]");
653 ok(120, $result{"fetch key"} eq "fetch key - 1: [0]");
654 ok(121, $result{"fetch value"} eq "fetch value - 1: [joe]");
655 ok(122, $_ eq "original") ;
663 # DBM Filter recursion detection
669 ok(123, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
671 $db->filter_store_key (sub { $_ = $h[0] }) ;
673 eval '$h[1] = 1234' ;
674 ok(124, $@ =~ /^recursion detected in filter_store_key at/ );
683 # Examples from the POD
687 my $redirect = new Redirect $file ;
689 use warnings FATAL => qw(all);
693 my $filename = "text" ;
697 my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
698 or die "Cannot open file 'text': $!\n" ;
700 # Add a few key/value pairs to the file
705 $FA ? push @h, "green", "black"
706 : $x->push("green", "black") ;
708 my $elements = $FA ? scalar @h : $x->length ;
709 print "The array contains $elements entries\n" ;
711 my $last = $FA ? pop @h : $x->pop ;
712 print "popped $last\n" ;
714 $FA ? unshift @h, "white"
715 : $x->unshift("white") ;
716 my $first = $FA ? shift @h : $x->shift ;
717 print "shifted $first\n" ;
719 # Check for existence of a key
720 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
722 # use a negative index
723 print "The last element is $h[-1]\n" ;
724 print "The 2nd last element is $h[-2]\n" ;
732 ok(125, docat_del($file) eq <<'EOM') ;
733 The array contains 5 entries
736 Element 1 Exists with value blue
737 The last element is green
738 The 2nd last element is yellow
741 my $save_output = "xyzt" ;
743 my $redirect = new Redirect $save_output ;
745 use warnings FATAL => qw(all);
747 use vars qw(@h $H $file $i) ;
755 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
756 or die "Cannot open file $file: $!\n" ;
758 # first create a text file to play with
766 # Print the records in order.
768 # The length method is needed here because evaluating a tied
769 # array in a scalar context does not return the number of
770 # elements in the array.
772 print "\nORIGINAL\n" ;
773 foreach $i (0 .. $H->length - 1) {
774 print "$i: $h[$i]\n" ;
777 # use the push & pop methods
780 print "\nThe last record was [$a]\n" ;
782 # and the shift & unshift methods
784 $H->unshift("first") ;
785 print "The first record was [$a]\n" ;
787 # Use the API to add a new record after record 2.
789 $H->put($i, "Newbie", R_IAFTER) ;
791 # and a new record before record 1.
793 $H->put($i, "New One", R_IBEFORE) ;
798 # now print the records in reverse order
799 print "\nREVERSE\n" ;
800 for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
801 { print "$i: $h[$i]\n" }
803 # same again, but use the API functions instead
804 print "\nREVERSE again\n" ;
805 my ($s, $k, $v) = (0, 0, 0) ;
806 for ($s = $H->seq($k, $v, R_LAST) ;
808 $s = $H->seq($k, $v, R_PREV))
817 ok(126, docat_del($save_output) eq <<'EOM') ;
826 The last record was [four]
827 The first record was [zero]
849 # Bug ID 20001013.009
851 # test that $hash{KEY} = undef doesn't produce the warning
852 # Use of uninitialized value in null operation
860 local $SIG{__WARN__} = sub {$a = $_[0]} ;
862 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
863 or die "Can't open file: $!\n" ;
871 # test that %hash = () doesn't produce the warning
872 # Argument "" isn't numeric in entersub
877 local $SIG{__WARN__} = sub {$a = $_[0]} ;
882 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
883 or die "Can't open file: $!\n" ;