5 require Config; import Config;
6 if ($Config{'extensions'} !~ /\bDB_File\b/) {
7 print "1..0 # Skip: DB_File was not built\n";
15 use vars qw($dbh $Dfile $bad_ones $FA) ;
17 # full tied array support started in Perl 5.004_57
18 # Double check to see if it is available.
21 sub try::TIEARRAY { bless [], "try" }
22 sub try::FETCHSIZE { $FA = 1 }
35 print "not " unless $result ;
48 my $filename = shift ;
50 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
51 my $real_stdout = select($fh) ;
52 return bless [$fh, $real_stdout ] ;
67 open(CAT,$file) || die "Cannot open $file:$!";
77 open(CAT,$file) || die "Cannot open $file: $!";
86 print STDERR <<EOM unless $bad_ones++ ;
88 # Some older versions of Berkeley DB version 1 will fail tests 51,
91 # You can safely ignore the errors if you're never going to use the
92 # broken functionality (recno databases with a modified bval).
93 # Otherwise you'll have to upgrade your DB library.
95 # If you want to use Berkeley DB version 1, then 1.85 and 1.86 are the
96 # last versions that were released. Berkeley DB version 2 is continually
97 # being updated -- Check out http://www.sleepycat.com/ for more details.
104 my $Dfile = "recno.tmp";
109 # Check the interface to RECNOINFO
111 my $dbh = new DB_File::RECNOINFO ;
112 ok(1, ! defined $dbh->{bval}) ;
113 ok(2, ! defined $dbh->{cachesize}) ;
114 ok(3, ! defined $dbh->{psize}) ;
115 ok(4, ! defined $dbh->{flags}) ;
116 ok(5, ! defined $dbh->{lorder}) ;
117 ok(6, ! defined $dbh->{reclen}) ;
118 ok(7, ! defined $dbh->{bfname}) ;
120 $dbh->{bval} = 3000 ;
121 ok(8, $dbh->{bval} == 3000 );
123 $dbh->{cachesize} = 9000 ;
124 ok(9, $dbh->{cachesize} == 9000 );
126 $dbh->{psize} = 400 ;
127 ok(10, $dbh->{psize} == 400 );
130 ok(11, $dbh->{flags} == 65 );
132 $dbh->{lorder} = 123 ;
133 ok(12, $dbh->{lorder} == 123 );
135 $dbh->{reclen} = 1234 ;
136 ok(13, $dbh->{reclen} == 1234 );
138 $dbh->{bfname} = 1234 ;
139 ok(14, $dbh->{bfname} == 1234 );
142 # Check that an invalid entry is caught both for store & fetch
143 eval '$dbh->{fred} = 1234' ;
144 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
145 eval 'my $q = $dbh->{fred}' ;
146 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
148 # Now check the interface to RECNOINFO
152 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
154 ok(18, ((stat($Dfile))[2] & 0777) == ($^O eq 'os2' ? 0666 : 0640)
155 || $^O eq 'MSWin32' || $^O eq 'amigaos') ;
159 ok(19, ($FA ? @h == 0 : !$l) );
161 my @data = qw( a b c d ever f g h i j k longername m n o p) ;
163 $h[0] = shift @data ;
164 ok(20, $h[0] eq 'a' );
170 unshift (@data, 'a') ;
172 ok(21, defined $h[1] );
173 ok(22, ! defined $h[16] );
174 ok(23, $FA ? @h == @data : $X->length == @data );
177 # Overwrite an entry & check fetch it
179 $data[3] = 'replaced' ;
180 ok(24, $h[3] eq 'replaced' );
183 my @push_data = qw(added to the end) ;
184 ($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
185 push (@data, @push_data) ;
186 ok(25, $h[++$i] eq 'added' );
187 ok(26, $h[++$i] eq 'to' );
188 ok(27, $h[++$i] eq 'the' );
189 ok(28, $h[++$i] eq 'end' );
192 my $popped = pop (@data) ;
193 my $value = ($FA ? pop @h : $X->pop) ;
194 ok(29, $value eq $popped) ;
197 $value = ($FA ? shift @h : $X->shift) ;
198 my $shifted = shift @data ;
199 ok(30, $value eq $shifted );
204 ($FA ? unshift @h : $X->unshift) ;
205 ok(31, ($FA ? @h == @data : $X->length == @data ));
207 my @new_data = qw(add this to the start of the array) ;
208 $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
209 unshift (@data, @new_data) ;
210 ok(32, $FA ? @h == @data : $X->length == @data );
211 ok(33, $h[0] eq "add") ;
212 ok(34, $h[1] eq "this") ;
213 ok(35, $h[2] eq "to") ;
214 ok(36, $h[3] eq "the") ;
215 ok(37, $h[4] eq "start") ;
216 ok(38, $h[5] eq "of") ;
217 ok(39, $h[6] eq "the") ;
218 ok(40, $h[7] eq "array") ;
219 ok(41, $h[8] eq $data[8]) ;
223 # Now both arrays should be identical
229 $ok = 0, last if $_ ne $h[$j ++] ;
233 # Neagtive subscripts
235 # get the last element of the array
236 ok(43, $h[-1] eq $data[-1] );
237 ok(44, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
239 # get the first element using a negative subscript
240 eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
242 ok(46, $h[0] eq "abcd" );
244 # now try to read before the start of the array
245 eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
246 ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
248 # IMPORTANT - $X must be undefined before the untie otherwise the
249 # underlying DB close routine will not get called.
257 # Check bval defaults to \n
260 my $dbh = new DB_File::RECNOINFO ;
261 ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
266 my $x = docat($Dfile) ;
268 ok(49, $x eq "abc\ndef\n\nghi\n") ;
275 my $dbh = new DB_File::RECNOINFO ;
277 ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
282 my $x = docat($Dfile) ;
284 my $ok = ($x eq "abc-def--ghi-") ;
285 bad_one() unless $ok ;
290 # Check R_FIXEDLEN with default bval (space)
293 my $dbh = new DB_File::RECNOINFO ;
294 $dbh->{flags} = R_FIXEDLEN ;
296 ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
301 my $x = docat($Dfile) ;
303 my $ok = ($x eq "abc def ghi ") ;
304 bad_one() unless $ok ;
309 # Check R_FIXEDLEN with user-defined bval
312 my $dbh = new DB_File::RECNOINFO ;
313 $dbh->{flags} = R_FIXEDLEN ;
316 ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
321 my $x = docat($Dfile) ;
323 my $ok = ($x eq "abc--def-------ghi--") ;
324 bad_one() unless $ok ;
329 # check that attempting to tie an associative array to a DB_RECNO will fail
331 my $filename = "xyz" ;
333 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
334 ok(56, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
345 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
351 use vars qw( @ISA @EXPORT) ;
356 @EXPORT = @DB_File::EXPORT ;
362 $self->SUPER::STORE($key, $value * 2) ;
368 $self->SUPER::FETCH($key) - 1 ;
375 $self->SUPER::put($key, $value * 3) ;
380 $self->SUPER::get($_[0], $_[1]) ;
388 my $value = $self->FETCH($key) ;
389 return "[[$value]]" ;
397 BEGIN { push @INC, '.'; }
399 main::ok(57, $@ eq "") ;
403 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
406 main::ok(58, $@ eq "") ;
408 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
409 main::ok(59, $@ eq "") ;
410 main::ok(60, $ret == 5) ;
413 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
414 main::ok(61, $@ eq "") ;
415 main::ok(62, $ret == 10) ;
417 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
418 main::ok(63, $@ eq "" ) ;
419 main::ok(64, $ret == 1) ;
421 $ret = eval '$X->A_new_method(1) ' ;
422 main::ok(65, $@ eq "") ;
423 main::ok(66, $ret eq "[[11]]") ;
427 unlink "SubDB.pm", "recno.tmp" ;
436 ok(67, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
441 ok(68, $FA ? $#h == 3 : $self->length() == 4) ;
444 my $x = docat($Dfile) ;
445 ok(69, $x eq "abc\ndef\nghi\njkl\n") ;
447 # $# sets array to same length
448 ok(70, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
452 { $self->STORESIZE(4) }
453 ok(71, $FA ? $#h == 3 : $self->length() == 4) ;
457 ok(72, $x eq "abc\ndef\nghi\njkl\n") ;
459 # $# sets array to bigger
460 ok(73, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
464 { $self->STORESIZE(7) }
465 ok(74, $FA ? $#h == 6 : $self->length() == 7) ;
469 ok(75, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
471 # $# sets array smaller
472 ok(76, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
476 { $self->STORESIZE(3) }
477 ok(77, $FA ? $#h == 2 : $self->length() == 3) ;
481 ok(78, $x eq "abc\ndef\nghi\n") ;
492 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
497 my($fk, $sk, $fv, $sv) = @_ ;
499 $fetch_key eq $fk && $store_key eq $sk &&
500 $fetch_value eq $fv && $store_value eq $sv &&
504 ok(79, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
506 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
507 $db->filter_store_key (sub { $store_key = $_ }) ;
508 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
509 $db->filter_store_value (sub { $store_value = $_ }) ;
515 ok(80, checkOutput( "", 0, "", "joe")) ;
517 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
518 ok(81, $h[0] eq "joe");
520 ok(82, checkOutput( "", 0, "joe", "")) ;
522 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
523 ok(83, $db->FIRSTKEY() == 0) ;
525 ok(84, checkOutput( 0, "", "", "")) ;
527 # replace the filters, but remember the previous set
528 my ($old_fk) = $db->filter_fetch_key
529 (sub { ++ $_ ; $fetch_key = $_ }) ;
530 my ($old_sk) = $db->filter_store_key
531 (sub { $_ *= 2 ; $store_key = $_ }) ;
532 my ($old_fv) = $db->filter_fetch_value
533 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
534 my ($old_sv) = $db->filter_store_value
535 (sub { s/o/x/g; $store_value = $_ }) ;
537 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
540 ok(85, checkOutput( "", 2, "", "Jxe")) ;
542 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
543 ok(86, $h[1] eq "[Jxe]");
545 ok(87, checkOutput( "", 2, "[Jxe]", "")) ;
547 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
548 ok(88, $db->FIRSTKEY() == 1) ;
550 ok(89, checkOutput( 1, "", "", "")) ;
552 # put the original filters back
553 $db->filter_fetch_key ($old_fk);
554 $db->filter_store_key ($old_sk);
555 $db->filter_fetch_value ($old_fv);
556 $db->filter_store_value ($old_sv);
558 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
560 ok(90, checkOutput( "", 0, "", "joe")) ;
562 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
563 ok(91, $h[0] eq "joe");
564 ok(92, checkOutput( "", 0, "joe", "")) ;
566 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
567 ok(93, $db->FIRSTKEY() == 0) ;
568 ok(94, checkOutput( 0, "", "", "")) ;
571 $db->filter_fetch_key (undef);
572 $db->filter_store_key (undef);
573 $db->filter_fetch_value (undef);
574 $db->filter_store_value (undef);
576 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
578 ok(95, checkOutput( "", "", "", "")) ;
580 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
581 ok(96, $h[0] eq "joe");
582 ok(97, checkOutput( "", "", "", "")) ;
584 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
585 ok(98, $db->FIRSTKEY() == 0) ;
586 ok(99, checkOutput( "", "", "", "")) ;
594 # DBM Filter with a closure
600 ok(100, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
610 return sub { ++$count ;
612 $result{$name} = "$name - $count: [@kept]" ;
616 $db->filter_store_key(Closure("store key")) ;
617 $db->filter_store_value(Closure("store value")) ;
618 $db->filter_fetch_key(Closure("fetch key")) ;
619 $db->filter_fetch_value(Closure("fetch value")) ;
624 ok(101, $result{"store key"} eq "store key - 1: [0]");
625 ok(102, $result{"store value"} eq "store value - 1: [joe]");
626 ok(103, ! defined $result{"fetch key"} );
627 ok(104, ! defined $result{"fetch value"} );
628 ok(105, $_ eq "original") ;
630 ok(106, $db->FIRSTKEY() == 0 ) ;
631 ok(107, $result{"store key"} eq "store key - 1: [0]");
632 ok(108, $result{"store value"} eq "store value - 1: [joe]");
633 ok(109, $result{"fetch key"} eq "fetch key - 1: [0]");
634 ok(110, ! defined $result{"fetch value"} );
635 ok(111, $_ eq "original") ;
638 ok(112, $result{"store key"} eq "store key - 2: [0 7]");
639 ok(113, $result{"store value"} eq "store value - 2: [joe john]");
640 ok(114, $result{"fetch key"} eq "fetch key - 1: [0]");
641 ok(115, ! defined $result{"fetch value"} );
642 ok(116, $_ eq "original") ;
644 ok(117, $h[0] eq "joe");
645 ok(118, $result{"store key"} eq "store key - 3: [0 7 0]");
646 ok(119, $result{"store value"} eq "store value - 2: [joe john]");
647 ok(120, $result{"fetch key"} eq "fetch key - 1: [0]");
648 ok(121, $result{"fetch value"} eq "fetch value - 1: [joe]");
649 ok(122, $_ eq "original") ;
657 # DBM Filter recursion detection
662 ok(123, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
664 $db->filter_store_key (sub { $_ = $h[0] }) ;
666 eval '$h[1] = 1234' ;
667 ok(124, $@ =~ /^recursion detected in filter_store_key at/ );
676 # Examples from the POD
680 my $redirect = new Redirect $file ;
685 my $filename = "text" ;
689 my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
690 or die "Cannot open file 'text': $!\n" ;
692 # Add a few key/value pairs to the file
697 $FA ? push @h, "green", "black"
698 : $x->push("green", "black") ;
700 my $elements = $FA ? scalar @h : $x->length ;
701 print "The array contains $elements entries\n" ;
703 my $last = $FA ? pop @h : $x->pop ;
704 print "popped $last\n" ;
706 $FA ? unshift @h, "white"
707 : $x->unshift("white") ;
708 my $first = $FA ? shift @h : $x->shift ;
709 print "shifted $first\n" ;
711 # Check for existence of a key
712 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
714 # use a negative index
715 print "The last element is $h[-1]\n" ;
716 print "The 2nd last element is $h[-2]\n" ;
724 ok(125, docat_del($file) eq <<'EOM') ;
725 The array contains 5 entries
728 Element 1 Exists with value blue
729 The last element is green
730 The 2nd last element is yellow
733 my $save_output = "xyzt" ;
735 my $redirect = new Redirect $save_output ;
738 use vars qw(@h $H $file $i) ;
746 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
747 or die "Cannot open file $file: $!\n" ;
749 # first create a text file to play with
757 # Print the records in order.
759 # The length method is needed here because evaluating a tied
760 # array in a scalar context does not return the number of
761 # elements in the array.
763 print "\nORIGINAL\n" ;
764 foreach $i (0 .. $H->length - 1) {
765 print "$i: $h[$i]\n" ;
768 # use the push & pop methods
771 print "\nThe last record was [$a]\n" ;
773 # and the shift & unshift methods
775 $H->unshift("first") ;
776 print "The first record was [$a]\n" ;
778 # Use the API to add a new record after record 2.
780 $H->put($i, "Newbie", R_IAFTER) ;
782 # and a new record before record 1.
784 $H->put($i, "New One", R_IBEFORE) ;
789 # now print the records in reverse order
790 print "\nREVERSE\n" ;
791 for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
792 { print "$i: $h[$i]\n" }
794 # same again, but use the API functions instead
795 print "\nREVERSE again\n" ;
796 my ($s, $k, $v) = (0, 0, 0) ;
797 for ($s = $H->seq($k, $v, R_LAST) ;
799 $s = $H->seq($k, $v, R_PREV))
808 ok(126, docat_del($save_output) eq <<'EOM') ;
817 The last record was [four]
818 The first record was [zero]