9 if ($Config{'extensions'} !~ /\bDB_File\b/ ) {
10 print "1..0 # Skip: DB_File was not built\n";
18 our ($dbh, $Dfile, $bad_ones, $FA);
20 # full tied array support started in Perl 5.004_57
21 # Double check to see if it is available.
24 sub try::TIEARRAY { bless [], "try" }
25 sub try::FETCHSIZE { $FA = 1 }
38 print "not " unless $result ;
51 my $filename = shift ;
53 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
54 my $real_stdout = select($fh) ;
55 return bless [$fh, $real_stdout ] ;
70 open(CAT,$file) || die "Cannot open $file:$!";
80 my $result = docat($file);
89 local $SIG{__WARN__} = sub {-- $no_inner } ;
96 unless ($bad_ones++) {
99 # Some older versions of Berkeley DB version 1 will fail db-recno
100 # tests 61, 63, 64 and 65.
103 && $Config{db_version_major} == 1
104 && $Config{db_version_minor} == 0
105 && $Config{db_version_patch} == 0) {
108 # For example Mac OS X 10.2 (or earlier) has such an old
109 # version of Berkeley DB.
115 # You can safely ignore the errors if you're never going to use the
116 # broken functionality (recno databases with a modified bval).
117 # Otherwise you'll have to upgrade your DB library.
119 # If you want to use Berkeley DB version 1, then 1.85 and 1.86 are the
120 # last versions that were released. Berkeley DB version 2 is continually
121 # being updated -- Check out http://www.sleepycat.com/ for more details.
129 return unless $^O eq 'cygwin' ;
137 local $SIG{__DIE__} ;
138 eval { require Data::Dumper ; import Data::Dumper } ;
142 *Dumper = sub { my $a = shift; return "[ @{ $a } ]" } ;
146 my $splice_tests = 10 + 12 + 1; # ten regressions, plus the randoms
147 my $total_tests = 181 ;
148 $total_tests += $splice_tests if $FA ;
149 print "1..$total_tests\n";
151 $Dfile = "recno.tmp";
156 # Check the interface to RECNOINFO
158 $dbh = new DB_File::RECNOINFO ;
159 ok(1, ! defined $dbh->{bval}) ;
160 ok(2, ! defined $dbh->{cachesize}) ;
161 ok(3, ! defined $dbh->{psize}) ;
162 ok(4, ! defined $dbh->{flags}) ;
163 ok(5, ! defined $dbh->{lorder}) ;
164 ok(6, ! defined $dbh->{reclen}) ;
165 ok(7, ! defined $dbh->{bfname}) ;
167 $dbh->{bval} = 3000 ;
168 ok(8, $dbh->{bval} == 3000 );
170 $dbh->{cachesize} = 9000 ;
171 ok(9, $dbh->{cachesize} == 9000 );
173 $dbh->{psize} = 400 ;
174 ok(10, $dbh->{psize} == 400 );
177 ok(11, $dbh->{flags} == 65 );
179 $dbh->{lorder} = 123 ;
180 ok(12, $dbh->{lorder} == 123 );
182 $dbh->{reclen} = 1234 ;
183 ok(13, $dbh->{reclen} == 1234 );
185 $dbh->{bfname} = 1234 ;
186 ok(14, $dbh->{bfname} == 1234 );
189 # Check that an invalid entry is caught both for store & fetch
190 eval '$dbh->{fred} = 1234' ;
191 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
192 eval 'my $q = $dbh->{fred}' ;
193 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
195 # Now check the interface to RECNOINFO
199 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
201 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
203 ok(18, ((stat($Dfile))[2] & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
208 ok(19, ($FA ? @h == 0 : !$l) );
210 my @data = qw( a b c d ever f g h i j k longername m n o p) ;
212 $h[0] = shift @data ;
213 ok(20, $h[0] eq 'a' );
219 unshift (@data, 'a') ;
221 ok(21, defined $h[1] );
222 ok(22, ! defined $h[16] );
223 ok(23, $FA ? @h == @data : $X->length == @data );
226 # Overwrite an entry & check fetch it
228 $data[3] = 'replaced' ;
229 ok(24, $h[3] eq 'replaced' );
232 my @push_data = qw(added to the end) ;
233 ($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
234 push (@data, @push_data) ;
235 ok(25, $h[++$i] eq 'added' );
236 ok(26, $h[++$i] eq 'to' );
237 ok(27, $h[++$i] eq 'the' );
238 ok(28, $h[++$i] eq 'end' );
241 my $popped = pop (@data) ;
242 my $value = ($FA ? pop @h : $X->pop) ;
243 ok(29, $value eq $popped) ;
246 $value = ($FA ? shift @h : $X->shift) ;
247 my $shifted = shift @data ;
248 ok(30, $value eq $shifted );
253 ($FA ? unshift @h,() : $X->unshift) ;
254 ok(31, ($FA ? @h == @data : $X->length == @data ));
256 my @new_data = qw(add this to the start of the array) ;
257 $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
258 unshift (@data, @new_data) ;
259 ok(32, $FA ? @h == @data : $X->length == @data );
260 ok(33, $h[0] eq "add") ;
261 ok(34, $h[1] eq "this") ;
262 ok(35, $h[2] eq "to") ;
263 ok(36, $h[3] eq "the") ;
264 ok(37, $h[4] eq "start") ;
265 ok(38, $h[5] eq "of") ;
266 ok(39, $h[6] eq "the") ;
267 ok(40, $h[7] eq "array") ;
268 ok(41, $h[8] eq $data[8]) ;
270 # Brief test for SPLICE - more thorough 'soak test' is later.
273 @old = splice(@h, 1, 2, qw(bananas just before));
276 @old = $X->splice(1, 2, qw(bananas just before));
278 ok(42, $h[0] eq "add") ;
279 ok(43, $h[1] eq "bananas") ;
280 ok(44, $h[2] eq "just") ;
281 ok(45, $h[3] eq "before") ;
282 ok(46, $h[4] eq "the") ;
283 ok(47, $h[5] eq "start") ;
284 ok(48, $h[6] eq "of") ;
285 ok(49, $h[7] eq "the") ;
286 ok(50, $h[8] eq "array") ;
287 ok(51, $h[9] eq $data[8]) ;
288 $FA ? splice(@h, 1, 3, @old) : $X->splice(1, 3, @old);
290 # Now both arrays should be identical
296 $ok = 0, last if $_ ne $h[$j ++] ;
300 # Neagtive subscripts
302 # get the last element of the array
303 ok(53, $h[-1] eq $data[-1] );
304 ok(54, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
306 # get the first element using a negative subscript
307 eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
309 ok(56, $h[0] eq "abcd" );
311 # now try to read before the start of the array
312 eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
313 ok(57, $@ =~ '^Modification of non-creatable array value attempted' );
315 # IMPORTANT - $X must be undefined before the untie otherwise the
316 # underlying DB close routine will not get called.
318 ok(58, safeUntie \@h);
324 # Check bval defaults to \n
327 my $dbh = new DB_File::RECNOINFO ;
328 ok(59, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
332 ok(60, safeUntie \@h);
333 my $x = docat($Dfile) ;
335 ok(61, $x eq "abc\ndef\n\nghi\n") ;
342 my $dbh = new DB_File::RECNOINFO ;
344 ok(62, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
348 ok(63, safeUntie \@h);
349 my $x = docat($Dfile) ;
351 my $ok = ($x eq "abc-def--ghi-") ;
352 bad_one() unless $ok ;
357 # Check R_FIXEDLEN with default bval (space)
360 my $dbh = new DB_File::RECNOINFO ;
361 $dbh->{flags} = R_FIXEDLEN ;
363 ok(65, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
367 ok(66, safeUntie \@h);
368 my $x = docat($Dfile) ;
370 my $ok = ($x eq "abc def ghi ") ;
371 bad_one() unless $ok ;
376 # Check R_FIXEDLEN with user-defined bval
379 my $dbh = new DB_File::RECNOINFO ;
380 $dbh->{flags} = R_FIXEDLEN ;
383 ok(68, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
387 ok(69, safeUntie \@h);
388 my $x = docat($Dfile) ;
390 my $ok = ($x eq "abc--def-------ghi--") ;
391 bad_one() unless $ok ;
396 # check that attempting to tie an associative array to a DB_RECNO will fail
398 my $filename = "xyz" ;
400 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
401 ok(71, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
413 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
425 @EXPORT = @DB_File::EXPORT ;
431 $self->SUPER::STORE($key, $value * 2) ;
437 $self->SUPER::FETCH($key) - 1 ;
444 $self->SUPER::put($key, $value * 3) ;
449 $self->SUPER::get($_[0], $_[1]) ;
457 my $value = $self->FETCH($key) ;
458 return "[[$value]]" ;
464 close FILE or die "Could not close: $!";
466 BEGIN { push @INC, '.'; }
468 main::ok(72, $@ eq "") ;
472 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
474 die "Could not tie: $!" unless $X;
476 main::ok(73, $@ eq "") ;
478 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
479 main::ok(74, $@ eq "") ;
480 main::ok(75, $ret == 5) ;
483 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
484 main::ok(76, $@ eq "") ;
485 main::ok(77, $ret == 10) ;
487 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
488 main::ok(78, $@ eq "" ) ;
489 main::ok(79, $ret == 1) ;
491 $ret = eval '$X->A_new_method(1) ' ;
492 main::ok(80, $@ eq "") ;
493 main::ok(81, $ret eq "[[11]]") ;
496 main::ok(82, main::safeUntie \@h);
497 unlink "SubDB.pm", "recno.tmp" ;
506 ok(83, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
511 ok(84, $FA ? $#h == 3 : $self->length() == 4) ;
513 ok(85, safeUntie \@h);
514 my $x = docat($Dfile) ;
515 ok(86, $x eq "abc\ndef\nghi\njkl\n") ;
517 # $# sets array to same length
518 $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ;
520 or warn "# $DB_File::Error\n";
524 { $self->STORESIZE(4) }
525 ok(88, $FA ? $#h == 3 : $self->length() == 4) ;
527 ok(89, safeUntie \@h);
529 ok(90, $x eq "abc\ndef\nghi\njkl\n") ;
531 # $# sets array to bigger
532 ok(91, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
536 { $self->STORESIZE(7) }
537 ok(92, $FA ? $#h == 6 : $self->length() == 7) ;
539 ok(93, safeUntie \@h);
541 ok(94, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
543 # $# sets array smaller
544 ok(95, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
548 { $self->STORESIZE(3) }
549 ok(96, $FA ? $#h == 2 : $self->length() == 3) ;
551 ok(97, safeUntie \@h);
553 ok(98, $x eq "abc\ndef\nghi\n") ;
565 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
570 my($fk, $sk, $fv, $sv) = @_ ;
572 print "# Fetch Key : expected '$fk' got '$fetch_key'\n"
573 if $fetch_key ne $fk ;
574 print "# Fetch Value : expected '$fv' got '$fetch_value'\n"
575 if $fetch_value ne $fv ;
576 print "# Store Key : expected '$sk' got '$store_key'\n"
577 if $store_key ne $sk ;
578 print "# Store Value : expected '$sv' got '$store_value'\n"
579 if $store_value ne $sv ;
580 print "# \$_ : expected 'original' got '$_'\n"
581 if $_ ne 'original' ;
584 $fetch_key eq $fk && $store_key eq $sk &&
585 $fetch_value eq $fv && $store_value eq $sv &&
589 ok(99, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
591 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
592 $db->filter_store_key (sub { $store_key = $_ }) ;
593 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
594 $db->filter_store_value (sub { $store_value = $_ }) ;
600 ok(100, checkOutput( "", 0, "", "joe")) ;
602 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
603 ok(101, $h[0] eq "joe");
605 ok(102, checkOutput( "", 0, "joe", "")) ;
607 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
608 ok(103, $db->FIRSTKEY() == 0) ;
610 ok(104, checkOutput( 0, "", "", "")) ;
612 # replace the filters, but remember the previous set
613 my ($old_fk) = $db->filter_fetch_key
614 (sub { ++ $_ ; $fetch_key = $_ }) ;
615 my ($old_sk) = $db->filter_store_key
616 (sub { $_ *= 2 ; $store_key = $_ }) ;
617 my ($old_fv) = $db->filter_fetch_value
618 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
619 my ($old_sv) = $db->filter_store_value
620 (sub { s/o/x/g; $store_value = $_ }) ;
622 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
625 ok(105, checkOutput( "", 2, "", "Jxe")) ;
627 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
628 ok(106, $h[1] eq "[Jxe]");
630 ok(107, checkOutput( "", 2, "[Jxe]", "")) ;
632 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
633 ok(108, $db->FIRSTKEY() == 1) ;
635 ok(109, checkOutput( 1, "", "", "")) ;
637 # put the original filters back
638 $db->filter_fetch_key ($old_fk);
639 $db->filter_store_key ($old_sk);
640 $db->filter_fetch_value ($old_fv);
641 $db->filter_store_value ($old_sv);
643 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
645 ok(110, checkOutput( "", 0, "", "joe")) ;
647 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
648 ok(111, $h[0] eq "joe");
649 ok(112, checkOutput( "", 0, "joe", "")) ;
651 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
652 ok(113, $db->FIRSTKEY() == 0) ;
653 ok(114, checkOutput( 0, "", "", "")) ;
656 $db->filter_fetch_key (undef);
657 $db->filter_store_key (undef);
658 $db->filter_fetch_value (undef);
659 $db->filter_store_value (undef);
661 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
663 ok(115, checkOutput( "", "", "", "")) ;
665 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
666 ok(116, $h[0] eq "joe");
667 ok(117, checkOutput( "", "", "", "")) ;
669 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
670 ok(118, $db->FIRSTKEY() == 0) ;
671 ok(119, checkOutput( "", "", "", "")) ;
674 ok(120, safeUntie \@h);
679 # DBM Filter with a closure
686 ok(121, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
696 return sub { ++$count ;
698 $result{$name} = "$name - $count: [@kept]" ;
702 $db->filter_store_key(Closure("store key")) ;
703 $db->filter_store_value(Closure("store value")) ;
704 $db->filter_fetch_key(Closure("fetch key")) ;
705 $db->filter_fetch_value(Closure("fetch value")) ;
710 ok(122, $result{"store key"} eq "store key - 1: [0]");
711 ok(123, $result{"store value"} eq "store value - 1: [joe]");
712 ok(124, ! defined $result{"fetch key"} );
713 ok(125, ! defined $result{"fetch value"} );
714 ok(126, $_ eq "original") ;
716 ok(127, $db->FIRSTKEY() == 0 ) ;
717 ok(128, $result{"store key"} eq "store key - 1: [0]");
718 ok(129, $result{"store value"} eq "store value - 1: [joe]");
719 ok(130, $result{"fetch key"} eq "fetch key - 1: [0]");
720 ok(131, ! defined $result{"fetch value"} );
721 ok(132, $_ eq "original") ;
724 ok(133, $result{"store key"} eq "store key - 2: [0 7]");
725 ok(134, $result{"store value"} eq "store value - 2: [joe john]");
726 ok(135, $result{"fetch key"} eq "fetch key - 1: [0]");
727 ok(136, ! defined $result{"fetch value"} );
728 ok(137, $_ eq "original") ;
730 ok(138, $h[0] eq "joe");
731 ok(139, $result{"store key"} eq "store key - 3: [0 7 0]");
732 ok(140, $result{"store value"} eq "store value - 2: [joe john]");
733 ok(141, $result{"fetch key"} eq "fetch key - 1: [0]");
734 ok(142, $result{"fetch value"} eq "fetch value - 1: [joe]");
735 ok(143, $_ eq "original") ;
738 ok(144, safeUntie \@h);
743 # DBM Filter recursion detection
749 ok(145, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
751 $db->filter_store_key (sub { $_ = $h[0] }) ;
753 eval '$h[1] = 1234' ;
754 ok(146, $@ =~ /^recursion detected in filter_store_key at/ );
757 ok(147, safeUntie \@h);
763 # Examples from the POD
767 my $redirect = new Redirect $file ;
769 use warnings FATAL => qw(all);
773 my $filename = "text" ;
777 my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
778 or die "Cannot open file 'text': $!\n" ;
780 # Add a few key/value pairs to the file
785 $FA ? push @h, "green", "black"
786 : $x->push("green", "black") ;
788 my $elements = $FA ? scalar @h : $x->length ;
789 print "The array contains $elements entries\n" ;
791 my $last = $FA ? pop @h : $x->pop ;
792 print "popped $last\n" ;
794 $FA ? unshift @h, "white"
795 : $x->unshift("white") ;
796 my $first = $FA ? shift @h : $x->shift ;
797 print "shifted $first\n" ;
799 # Check for existence of a key
800 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
802 # use a negative index
803 print "The last element is $h[-1]\n" ;
804 print "The 2nd last element is $h[-2]\n" ;
812 ok(148, docat_del($file) eq <<'EOM') ;
813 The array contains 5 entries
816 Element 1 Exists with value blue
817 The last element is green
818 The 2nd last element is yellow
821 my $save_output = "xyzt" ;
823 my $redirect = new Redirect $save_output ;
825 use warnings FATAL => qw(all);
827 our (@h, $H, $file, $i);
835 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
836 or die "Cannot open file $file: $!\n" ;
838 # first create a text file to play with
846 # Print the records in order.
848 # The length method is needed here because evaluating a tied
849 # array in a scalar context does not return the number of
850 # elements in the array.
852 print "\nORIGINAL\n" ;
853 foreach $i (0 .. $H->length - 1) {
854 print "$i: $h[$i]\n" ;
857 # use the push & pop methods
860 print "\nThe last record was [$a]\n" ;
862 # and the shift & unshift methods
864 $H->unshift("first") ;
865 print "The first record was [$a]\n" ;
867 # Use the API to add a new record after record 2.
869 $H->put($i, "Newbie", R_IAFTER) ;
871 # and a new record before record 1.
873 $H->put($i, "New One", R_IBEFORE) ;
878 # now print the records in reverse order
879 print "\nREVERSE\n" ;
880 for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
881 { print "$i: $h[$i]\n" }
883 # same again, but use the API functions instead
884 print "\nREVERSE again\n" ;
885 my ($s, $k, $v) = (0, 0, 0) ;
886 for ($s = $H->seq($k, $v, R_LAST) ;
888 $s = $H->seq($k, $v, R_PREV))
897 ok(149, docat_del($save_output) eq <<'EOM') ;
906 The last record was [four]
907 The first record was [zero]
929 # Bug ID 20001013.009
931 # test that $hash{KEY} = undef doesn't produce the warning
932 # Use of uninitialized value in null operation
940 local $SIG{__WARN__} = sub {$a = $_[0]} ;
942 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
943 or die "Can't open file: $!\n" ;
946 ok(151, safeUntie \@h);
951 # test that %hash = () doesn't produce the warning
952 # Argument "" isn't numeric in entersub
957 local $SIG{__WARN__} = sub {$a = $_[0]} ;
962 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
963 or die "Can't open file: $!\n" ;
966 ok(153, safeUntie \@h);
971 # Check that DBM Filter can cope with read-only $_
978 ok(154, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
980 $db->filter_fetch_key (sub { }) ;
981 $db->filter_store_key (sub { }) ;
982 $db->filter_fetch_value (sub { }) ;
983 $db->filter_store_value (sub { }) ;
988 ok(155, $h[0] eq "joe");
990 eval { my @r= grep { $h[$_] } (1, 2, 3) };
995 $db->filter_fetch_key (undef);
996 $db->filter_store_key (undef);
997 $db->filter_fetch_value (undef);
998 $db->filter_store_value (undef);
1002 ok(157, $h[1] eq "joe");
1004 eval { my @r= grep { $h[$_] } (1, 2, 3) };
1013 # Check low-level API works with filter
1018 my $Dfile = "xxy.db";
1021 ok(159, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
1024 $db->filter_fetch_key (sub { ++ $_ } );
1025 $db->filter_store_key (sub { -- $_ } );
1026 $db->filter_fetch_value (sub { $_ = unpack("i", $_) } );
1027 $db->filter_store_value (sub { $_ = pack("i", $_) } );
1034 $db->put($key, $value) ;
1036 ok 161, $value == 34 ;
1037 ok 162, $_ eq 'fred';
1038 #print "k [$key][$value]\n" ;
1041 $db->get($key, $val) ;
1043 ok 164, $val == 34 ;
1044 ok 165, $_ eq 'fred';
1050 ok 167, $value == 454 ;
1051 ok 168, $_ eq 'fred';
1060 # Regression Test for bug 30237
1061 # Check that substr can be used in the key to db_put
1062 # and that db_put does not trigger the warning
1064 # Use of uninitialized value in subroutine entry
1071 my $Dfile = "xxy.db";
1074 ok(169, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO) );
1077 local $SIG{__WARN__} = sub {$warned = $_[0]} ;
1079 # db-put with substr of key
1081 for my $ix ( 0 .. 2 )
1083 my $key = $ix . "data" ;
1084 my $value = "value$ix" ;
1085 $remember{substr($key,0, 1)} = $value ;
1086 $db->put(substr($key,0, 1), $value) ;
1089 ok 170, $warned eq ''
1090 or print "# Caught warning [$warned]\n" ;
1092 # db-put with substr of value
1094 for my $ix ( 3 .. 5 )
1096 my $key = $ix . "data" ;
1097 my $value = "value$ix" ;
1098 $remember{$ix} = $value ;
1099 $db->put($ix, substr($value,0)) ;
1102 ok 171, $warned eq ''
1103 or print "# Caught warning [$warned]\n" ;
1105 # via the tied array is not a problem, but check anyway
1108 for my $ix ( 6 .. 8 )
1110 my $key = $ix . "data" ;
1111 my $value = "value$ix" ;
1112 $remember{substr($key,0,1)} = $value ;
1113 $h[substr($key,0,1)] = $value ;
1116 ok 172, $warned eq ''
1117 or print "# Caught warning [$warned]\n" ;
1119 # via the tied array is not a problem, but check anyway
1122 for my $ix ( 9 .. 10 )
1124 my $key = $ix . "data" ;
1125 my $value = "value$ix" ;
1126 $remember{$ix} = $value ;
1127 $h[$ix] = substr($value,0) ;
1130 ok 173, $warned eq ''
1131 or print "# Caught warning [$warned]\n" ;
1135 for (my $status = $db->seq($key, $value, R_FIRST ) ;
1137 $status = $db->seq($key, $value, R_NEXT ) ) {
1139 #print "# key [$key] value [$value]\n" ;
1140 if (defined $remember{$key} && defined $value &&
1141 $remember{$key} eq $value) {
1142 delete $remember{$key} ;
1145 $bad{$key} = $value ;
1149 ok 174, keys %bad == 0 ;
1150 ok 175, keys %remember == 0 ;
1152 print "# missing -- $key $value\n" while ($key, $value) = each %remember;
1153 print "# bad -- $key $value\n" while ($key, $value) = each %bad;
1155 # Make sure this fix does not break code to handle an undef key
1158 $status = $db->put(undef, $value) ;
1159 ok 176, $status == 0
1160 or print "# put failed - status $status\n";
1161 ok 177, $warned eq ''
1162 or print "# Caught warning [$warned]\n" ;
1165 print "# db_ver $DB_File::db_ver\n";
1167 $status = $db->get(undef, $value) ;
1168 ok 178, $status == 0
1169 or print "# get failed - status $status\n" ;
1170 ok(179, $db->get(undef, $value) == 0) or print "# get failed\n" ;
1171 ok 180, $value eq 'fred' or print "# got [$value]\n" ;
1172 ok 181, $warned eq ''
1173 or print "# Caught warning [$warned]\n" ;
1181 # Only test splice if this is a newish version of Perl
1187 # check that the splice warnings are under the same lexical control
1188 # as their non-tied counterparts.
1195 local $SIG{__WARN__} = sub {$a = $_[0]} ;
1200 tie @tied, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
1201 or die "Can't open file: $!\n" ;
1203 # uninitialized offset
1207 splice(@a, $offset);
1208 ok(182, $a =~ /^Use of uninitialized value /);
1210 splice(@tied, $offset);
1211 ok(183, $a =~ /^Use of uninitialized value in splice/);
1213 no warnings 'uninitialized';
1215 splice(@a, $offset);
1218 splice(@tied, $offset);
1221 # uninitialized length
1225 splice(@a, 0, $length);
1226 ok(186, $a =~ /^Use of uninitialized value /);
1228 splice(@tied, 0, $length);
1229 ok(187, $a =~ /^Use of uninitialized value in splice/);
1231 no warnings 'uninitialized';
1233 splice(@a, 0, $length);
1236 splice(@tied, 0, $length);
1239 # offset past end of array
1243 my $splice_end_array = ($a =~ /^splice\(\) offset past end of array/);
1246 ok(190, !$splice_end_array || $a =~ /^splice\(\) offset past end of array/);
1256 ok(193, safeUntie \@tied);
1261 # These are a few regression tests: bundles of five arguments to pass
1262 # to test_splice(). The first four arguments correspond to those
1263 # given to splice(), and the last says which context to call it in
1264 # (scalar, list or void).
1266 # The expected result is not needed because we get that by running
1267 # Perl's built-in splice().
1269 my @tests = ([ [ 'falsely', 'dinosaur', 'remedy', 'commotion',
1270 'rarely', 'paleness' ],
1272 [ 'redoubled', 'Taylorize', 'Zoe', 'halogen' ],
1275 [ [ 'a' ], -2, 1, [ 'B' ], 'void' ],
1277 [ [ 'Hartley', 'Islandia', 'assents', 'wishful' ],
1282 [ [ 'visibility', 'pocketful', 'rectangles' ],
1287 [ [ 'sleeplessly' ],
1289 [ 'Margery', 'clearing', 'repercussion', 'clubs',
1293 [ [ 'chastises', 'recalculates' ],
1295 [ 'momentariness', 'mediates', 'accents', 'toils',
1301 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
1306 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
1309 [ [ 'riheb' ], -8, undef, [], 'void' ],
1311 [ [ 'uft', 'qnxs', '' ],
1313 [ 'znp', 'mhnkh', 'bn' ],
1320 foreach my $test (@tests) {
1321 my $err = test_splice(@$test);
1323 print STDERR "# failed: ", Dumper($test);
1324 print STDERR "# error: $err\n";
1328 else { ok($testnum++, 1) }
1332 # Not worth running the random ones
1333 print STDERR '# skipping ', $testnum++, "\n";
1336 # A thousand randomly-generated tests
1339 foreach (0 .. 1000 - 1) {
1340 my $test = rand_test();
1341 my $err = test_splice(@$test);
1343 print STDERR "# failed: ", Dumper($test);
1344 print STDERR "# error: $err\n";
1346 print STDERR "# skipping any remaining random tests\n";
1351 ok($testnum++, not $failed);
1354 die "testnum ($testnum) != total_tests ($total_tests) + 1"
1355 if $testnum != $total_tests + 1;
1359 # Subroutines for SPLICE testing
1363 # Test the new splice() against Perl's built-in one. The first four
1364 # parameters are those passed to splice(), except that the lists must
1365 # be (explicitly) passed by reference, and are not actually modified.
1366 # (It's just a test!) The last argument specifies the context in
1367 # which to call the functions: 'list', 'scalar', or 'void'.
1370 # undef, if the two splices give the same results for the given
1371 # arguments and context;
1373 # an error message showing the difference, otherwise.
1375 # Reads global variable $tmp.
1378 die 'usage: test_splice(array, offset, length, list, context)' if @_ != 5;
1379 my ($array, $offset, $length, $list, $context) = @_;
1380 my @array = @$array;
1386 my $H = tie @h, 'DB_File', $tmp, O_CREAT|O_RDWR, 0644, $DB_RECNO
1387 or die "cannot open $tmp: $!";
1390 foreach ( @array ) { $h[$i++] = $_ }
1392 return "basic DB_File sanity check failed"
1393 if list_diff(\@array, \@h);
1395 # Output from splice():
1396 # Returned value (munged a bit), error msg, warnings
1398 my ($s_r, $s_error, @s_warnings);
1400 my $gather_warning = sub { push @s_warnings, $_[0] };
1401 if ($context eq 'list') {
1404 local $SIG{__WARN__} = $gather_warning;
1405 @r = splice @array, $offset, $length, @list;
1410 elsif ($context eq 'scalar') {
1413 local $SIG{__WARN__} = $gather_warning;
1414 $r = splice @array, $offset, $length, @list;
1419 elsif ($context eq 'void') {
1421 local $SIG{__WARN__} = $gather_warning;
1422 splice @array, $offset, $length, @list;
1428 die "bad context $context";
1431 foreach ($s_error, @s_warnings) {
1433 s/ at \S+ line \d+\.$//;
1434 # only built-in splice identifies name of uninit value
1435 s/(uninitialized value) \$\w+/$1/;
1438 # Now do the same for DB_File's version of splice
1439 my ($ms_r, $ms_error, @ms_warnings);
1440 $gather_warning = sub { push @ms_warnings, $_[0] };
1441 if ($context eq 'list') {
1444 local $SIG{__WARN__} = $gather_warning;
1445 @r = splice @h, $offset, $length, @list;
1450 elsif ($context eq 'scalar') {
1453 local $SIG{__WARN__} = $gather_warning;
1454 $r = splice @h, $offset, $length, @list;
1459 elsif ($context eq 'void') {
1461 local $SIG{__WARN__} = $gather_warning;
1462 splice @h, $offset, $length, @list;
1468 die "bad context $context";
1471 foreach ($ms_error, @ms_warnings) {
1473 s/ at \S+(\s+\S+)*? line \d+\.?.*//s;
1476 return "different errors: '$s_error' vs '$ms_error'"
1477 if $s_error ne $ms_error;
1478 return('different return values: ' . Dumper($s_r) . ' vs ' . Dumper($ms_r))
1479 if list_diff($s_r, $ms_r);
1480 return('different changed list: ' . Dumper(\@array) . ' vs ' . Dumper(\@h))
1481 if list_diff(\@array, \@h);
1483 if ((scalar @s_warnings) != (scalar @ms_warnings)) {
1484 return 'different number of warnings';
1487 while (@s_warnings) {
1488 my $sw = shift @s_warnings;
1489 my $msw = shift @ms_warnings;
1491 if (defined $sw and defined $msw) {
1492 $msw =~ s/ \(.+\)$//;
1493 $msw =~ s/ in splice$// if $] < 5.006;
1495 return "different warning: '$sw' vs '$msw'";
1498 elsif (not defined $sw and not defined $msw) {
1502 return "one warning defined, another undef";
1509 open(TEXT, $tmp) or die "cannot open $tmp: $!";
1510 @h = <TEXT>; normalise @h; chomp @h;
1511 close TEXT or die "cannot close $tmp: $!";
1512 return('list is different when re-read from disk: '
1513 . Dumper(\@array) . ' vs ' . Dumper(\@h))
1514 if list_diff(\@array, \@h);
1518 return undef; # success
1524 # Do two lists differ?
1527 # reference to first list
1528 # reference to second list
1530 # Returns true iff they differ. Only works for lists of (string or
1533 # Surely there is a better way to do this?
1536 die 'usage: list_diff(ref to first list, ref to second list)'
1539 my @a = @$a; my @b = @$b;
1540 return 1 if (scalar @a) != (scalar @b);
1541 for (my $i = 0; $i < @a; $i++) {
1542 my ($ae, $be) = ($a[$i], $b[$i]);
1543 if (defined $ae and defined $be) {
1544 return 1 if $ae ne $be;
1546 elsif (not defined $ae and not defined $be) {
1547 # Two undefined values are 'equal'
1559 # Think up a random ARRAY, OFFSET, LENGTH, LIST, and context.
1560 # ARRAY or LIST might be empty, and OFFSET or LENGTH might be
1561 # undefined. Return a 'test' - a listref of these five things.
1564 die 'usage: rand_test()' if @_;
1565 my @contexts = qw<list scalar void>;
1566 my $context = $contexts[int(rand @contexts)];
1567 return [ rand_list(),
1568 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1569 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1576 die 'usage: rand_list()' if @_;
1579 while (rand() > 0.1 * (scalar @r + 1)) {
1580 push @r, rand_word();
1587 die 'usage: rand_word()' if @_;
1589 my @chars = qw<a b c d e f g h i j k l m n o p q r s t u v w x y z>;
1590 while (rand() > 0.1 * (length($r) + 1)) {
1591 $r .= $chars[int(rand(scalar @chars))];