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";
25 our ($dbh, $Dfile, $bad_ones, $FA);
27 # full tied array support started in Perl 5.004_57
28 # Double check to see if it is available.
31 sub try::TIEARRAY { bless [], "try" }
32 sub try::FETCHSIZE { $FA = 1 }
45 print "not " unless $result ;
58 my $filename = shift ;
60 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
61 my $real_stdout = select($fh) ;
62 return bless [$fh, $real_stdout ] ;
77 open(CAT,$file) || die "Cannot open $file:$!";
87 my $result = docat($file);
94 unless ($bad_ones++) {
97 # Some older versions of Berkeley DB version 1 will fail db-recno
98 # tests 61, 63 and 65.
101 && $Config{db_version_major} == 1
102 && $Config{db_version_minor} == 0
103 && $Config{db_version_patch} == 0) {
106 # For example Mac OS X 10.1.4 (or earlier) has such an old
107 # version of Berkeley DB.
113 # You can safely ignore the errors if you're never going to use the
114 # broken functionality (recno databases with a modified bval).
115 # Otherwise you'll have to upgrade your DB library.
117 # If you want to use Berkeley DB version 1, then 1.85 and 1.86 are the
118 # last versions that were released. Berkeley DB version 2 is continually
119 # being updated -- Check out http://www.sleepycat.com/ for more details.
127 return unless $^O eq 'cygwin' ;
135 local $SIG{__DIE__} ;
136 eval { require Data::Dumper ; import Data::Dumper } ;
140 *Dumper = sub { my $a = shift; return "[ @{ $a } ]" } ;
144 my $splice_tests = 10 + 11 + 1; # ten regressions, 11 warnings, plus the randoms
145 my $total_tests = 138 ;
146 $total_tests += $splice_tests if $FA ;
147 print "1..$total_tests\n";
149 $Dfile = "recno.tmp";
154 # Check the interface to RECNOINFO
156 $dbh = new DB_File::RECNOINFO ;
157 ok(1, ! defined $dbh->{bval}) ;
158 ok(2, ! defined $dbh->{cachesize}) ;
159 ok(3, ! defined $dbh->{psize}) ;
160 ok(4, ! defined $dbh->{flags}) ;
161 ok(5, ! defined $dbh->{lorder}) ;
162 ok(6, ! defined $dbh->{reclen}) ;
163 ok(7, ! defined $dbh->{bfname}) ;
165 $dbh->{bval} = 3000 ;
166 ok(8, $dbh->{bval} == 3000 );
168 $dbh->{cachesize} = 9000 ;
169 ok(9, $dbh->{cachesize} == 9000 );
171 $dbh->{psize} = 400 ;
172 ok(10, $dbh->{psize} == 400 );
175 ok(11, $dbh->{flags} == 65 );
177 $dbh->{lorder} = 123 ;
178 ok(12, $dbh->{lorder} == 123 );
180 $dbh->{reclen} = 1234 ;
181 ok(13, $dbh->{reclen} == 1234 );
183 $dbh->{bfname} = 1234 ;
184 ok(14, $dbh->{bfname} == 1234 );
187 # Check that an invalid entry is caught both for store & fetch
188 eval '$dbh->{fred} = 1234' ;
189 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
190 eval 'my $q = $dbh->{fred}' ;
191 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
193 # Now check the interface to RECNOINFO
197 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
199 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
201 ok(18, ((stat($Dfile))[2] & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
206 ok(19, ($FA ? @h == 0 : !$l) );
208 my @data = qw( a b c d ever f g h i j k longername m n o p) ;
210 $h[0] = shift @data ;
211 ok(20, $h[0] eq 'a' );
217 unshift (@data, 'a') ;
219 ok(21, defined $h[1] );
220 ok(22, ! defined $h[16] );
221 ok(23, $FA ? @h == @data : $X->length == @data );
224 # Overwrite an entry & check fetch it
226 $data[3] = 'replaced' ;
227 ok(24, $h[3] eq 'replaced' );
230 my @push_data = qw(added to the end) ;
231 ($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
232 push (@data, @push_data) ;
233 ok(25, $h[++$i] eq 'added' );
234 ok(26, $h[++$i] eq 'to' );
235 ok(27, $h[++$i] eq 'the' );
236 ok(28, $h[++$i] eq 'end' );
239 my $popped = pop (@data) ;
240 my $value = ($FA ? pop @h : $X->pop) ;
241 ok(29, $value eq $popped) ;
244 $value = ($FA ? shift @h : $X->shift) ;
245 my $shifted = shift @data ;
246 ok(30, $value eq $shifted );
251 ($FA ? unshift @h,() : $X->unshift) ;
252 ok(31, ($FA ? @h == @data : $X->length == @data ));
254 my @new_data = qw(add this to the start of the array) ;
255 $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
256 unshift (@data, @new_data) ;
257 ok(32, $FA ? @h == @data : $X->length == @data );
258 ok(33, $h[0] eq "add") ;
259 ok(34, $h[1] eq "this") ;
260 ok(35, $h[2] eq "to") ;
261 ok(36, $h[3] eq "the") ;
262 ok(37, $h[4] eq "start") ;
263 ok(38, $h[5] eq "of") ;
264 ok(39, $h[6] eq "the") ;
265 ok(40, $h[7] eq "array") ;
266 ok(41, $h[8] eq $data[8]) ;
268 # Brief test for SPLICE - more thorough 'soak test' is later.
271 @old = splice(@h, 1, 2, qw(bananas just before));
274 @old = $X->splice(1, 2, qw(bananas just before));
276 ok(42, $h[0] eq "add") ;
277 ok(43, $h[1] eq "bananas") ;
278 ok(44, $h[2] eq "just") ;
279 ok(45, $h[3] eq "before") ;
280 ok(46, $h[4] eq "the") ;
281 ok(47, $h[5] eq "start") ;
282 ok(48, $h[6] eq "of") ;
283 ok(49, $h[7] eq "the") ;
284 ok(50, $h[8] eq "array") ;
285 ok(51, $h[9] eq $data[8]) ;
286 $FA ? splice(@h, 1, 3, @old) : $X->splice(1, 3, @old);
288 # Now both arrays should be identical
294 $ok = 0, last if $_ ne $h[$j ++] ;
298 # Neagtive subscripts
300 # get the last element of the array
301 ok(53, $h[-1] eq $data[-1] );
302 ok(54, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
304 # get the first element using a negative subscript
305 eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
307 ok(56, $h[0] eq "abcd" );
309 # now try to read before the start of the array
310 eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
311 ok(57, $@ =~ '^Modification of non-creatable array value attempted' );
313 # IMPORTANT - $X must be undefined before the untie otherwise the
314 # underlying DB close routine will not get called.
322 # Check bval defaults to \n
325 my $dbh = new DB_File::RECNOINFO ;
326 ok(58, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
331 my $x = docat($Dfile) ;
333 ok(59, $x eq "abc\ndef\n\nghi\n") ;
340 my $dbh = new DB_File::RECNOINFO ;
342 ok(60, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
347 my $x = docat($Dfile) ;
349 my $ok = ($x eq "abc-def--ghi-") ;
350 bad_one() unless $ok ;
355 # Check R_FIXEDLEN with default bval (space)
358 my $dbh = new DB_File::RECNOINFO ;
359 $dbh->{flags} = R_FIXEDLEN ;
361 ok(62, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
366 my $x = docat($Dfile) ;
368 my $ok = ($x eq "abc def ghi ") ;
369 bad_one() unless $ok ;
374 # Check R_FIXEDLEN with user-defined bval
377 my $dbh = new DB_File::RECNOINFO ;
378 $dbh->{flags} = R_FIXEDLEN ;
381 ok(64, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
386 my $x = docat($Dfile) ;
388 my $ok = ($x eq "abc--def-------ghi--") ;
389 bad_one() unless $ok ;
394 # check that attempting to tie an associative array to a DB_RECNO will fail
396 my $filename = "xyz" ;
398 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
399 ok(66, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
411 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
423 @EXPORT = @DB_File::EXPORT ;
429 $self->SUPER::STORE($key, $value * 2) ;
435 $self->SUPER::FETCH($key) - 1 ;
442 $self->SUPER::put($key, $value * 3) ;
447 $self->SUPER::get($_[0], $_[1]) ;
455 my $value = $self->FETCH($key) ;
456 return "[[$value]]" ;
462 close FILE or die "Could not close: $!";
464 BEGIN { push @INC, '.'; }
466 main::ok(67, $@ eq "") ;
470 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
472 die "Could not tie: $!" unless $X;
474 main::ok(68, $@ eq "") ;
476 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
477 main::ok(69, $@ eq "") ;
478 main::ok(70, $ret == 5) ;
481 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
482 main::ok(71, $@ eq "") ;
483 main::ok(72, $ret == 10) ;
485 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
486 main::ok(73, $@ eq "" ) ;
487 main::ok(74, $ret == 1) ;
489 $ret = eval '$X->A_new_method(1) ' ;
490 main::ok(75, $@ eq "") ;
491 main::ok(76, $ret eq "[[11]]") ;
495 unlink "SubDB.pm", "recno.tmp" ;
504 ok(77, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
509 ok(78, $FA ? $#h == 3 : $self->length() == 4) ;
512 my $x = docat($Dfile) ;
513 ok(79, $x eq "abc\ndef\nghi\njkl\n") ;
515 # $# sets array to same length
516 ok(80, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
520 { $self->STORESIZE(4) }
521 ok(81, $FA ? $#h == 3 : $self->length() == 4) ;
525 ok(82, $x eq "abc\ndef\nghi\njkl\n") ;
527 # $# sets array to bigger
528 ok(83, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
532 { $self->STORESIZE(7) }
533 ok(84, $FA ? $#h == 6 : $self->length() == 7) ;
537 ok(85, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
539 # $# sets array smaller
540 ok(86, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
544 { $self->STORESIZE(3) }
545 ok(87, $FA ? $#h == 2 : $self->length() == 3) ;
549 ok(88, $x eq "abc\ndef\nghi\n") ;
561 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
566 my($fk, $sk, $fv, $sv) = @_ ;
568 $fetch_key eq $fk && $store_key eq $sk &&
569 $fetch_value eq $fv && $store_value eq $sv &&
573 ok(89, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
575 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
576 $db->filter_store_key (sub { $store_key = $_ }) ;
577 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
578 $db->filter_store_value (sub { $store_value = $_ }) ;
584 ok(90, checkOutput( "", 0, "", "joe")) ;
586 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
587 ok(91, $h[0] eq "joe");
589 ok(92, checkOutput( "", 0, "joe", "")) ;
591 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
592 ok(93, $db->FIRSTKEY() == 0) ;
594 ok(94, checkOutput( 0, "", "", "")) ;
596 # replace the filters, but remember the previous set
597 my ($old_fk) = $db->filter_fetch_key
598 (sub { ++ $_ ; $fetch_key = $_ }) ;
599 my ($old_sk) = $db->filter_store_key
600 (sub { $_ *= 2 ; $store_key = $_ }) ;
601 my ($old_fv) = $db->filter_fetch_value
602 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
603 my ($old_sv) = $db->filter_store_value
604 (sub { s/o/x/g; $store_value = $_ }) ;
606 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
609 ok(95, checkOutput( "", 2, "", "Jxe")) ;
611 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
612 ok(96, $h[1] eq "[Jxe]");
614 ok(97, checkOutput( "", 2, "[Jxe]", "")) ;
616 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
617 ok(98, $db->FIRSTKEY() == 1) ;
619 ok(99, checkOutput( 1, "", "", "")) ;
621 # put the original filters back
622 $db->filter_fetch_key ($old_fk);
623 $db->filter_store_key ($old_sk);
624 $db->filter_fetch_value ($old_fv);
625 $db->filter_store_value ($old_sv);
627 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
629 ok(100, checkOutput( "", 0, "", "joe")) ;
631 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
632 ok(101, $h[0] eq "joe");
633 ok(102, checkOutput( "", 0, "joe", "")) ;
635 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
636 ok(103, $db->FIRSTKEY() == 0) ;
637 ok(104, checkOutput( 0, "", "", "")) ;
640 $db->filter_fetch_key (undef);
641 $db->filter_store_key (undef);
642 $db->filter_fetch_value (undef);
643 $db->filter_store_value (undef);
645 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
647 ok(105, checkOutput( "", "", "", "")) ;
649 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
650 ok(106, $h[0] eq "joe");
651 ok(107, checkOutput( "", "", "", "")) ;
653 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
654 ok(108, $db->FIRSTKEY() == 0) ;
655 ok(109, checkOutput( "", "", "", "")) ;
663 # DBM Filter with a closure
670 ok(110, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
680 return sub { ++$count ;
682 $result{$name} = "$name - $count: [@kept]" ;
686 $db->filter_store_key(Closure("store key")) ;
687 $db->filter_store_value(Closure("store value")) ;
688 $db->filter_fetch_key(Closure("fetch key")) ;
689 $db->filter_fetch_value(Closure("fetch value")) ;
694 ok(111, $result{"store key"} eq "store key - 1: [0]");
695 ok(112, $result{"store value"} eq "store value - 1: [joe]");
696 ok(113, ! defined $result{"fetch key"} );
697 ok(114, ! defined $result{"fetch value"} );
698 ok(115, $_ eq "original") ;
700 ok(116, $db->FIRSTKEY() == 0 ) ;
701 ok(117, $result{"store key"} eq "store key - 1: [0]");
702 ok(118, $result{"store value"} eq "store value - 1: [joe]");
703 ok(119, $result{"fetch key"} eq "fetch key - 1: [0]");
704 ok(120, ! defined $result{"fetch value"} );
705 ok(121, $_ eq "original") ;
708 ok(122, $result{"store key"} eq "store key - 2: [0 7]");
709 ok(123, $result{"store value"} eq "store value - 2: [joe john]");
710 ok(124, $result{"fetch key"} eq "fetch key - 1: [0]");
711 ok(125, ! defined $result{"fetch value"} );
712 ok(126, $_ eq "original") ;
714 ok(127, $h[0] eq "joe");
715 ok(128, $result{"store key"} eq "store key - 3: [0 7 0]");
716 ok(129, $result{"store value"} eq "store value - 2: [joe john]");
717 ok(130, $result{"fetch key"} eq "fetch key - 1: [0]");
718 ok(131, $result{"fetch value"} eq "fetch value - 1: [joe]");
719 ok(132, $_ eq "original") ;
727 # DBM Filter recursion detection
733 ok(133, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
735 $db->filter_store_key (sub { $_ = $h[0] }) ;
737 eval '$h[1] = 1234' ;
738 ok(134, $@ =~ /^recursion detected in filter_store_key at/ );
747 # Examples from the POD
751 my $redirect = new Redirect $file ;
753 use warnings FATAL => qw(all);
757 my $filename = "text" ;
761 my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
762 or die "Cannot open file 'text': $!\n" ;
764 # Add a few key/value pairs to the file
769 $FA ? push @h, "green", "black"
770 : $x->push("green", "black") ;
772 my $elements = $FA ? scalar @h : $x->length ;
773 print "The array contains $elements entries\n" ;
775 my $last = $FA ? pop @h : $x->pop ;
776 print "popped $last\n" ;
778 $FA ? unshift @h, "white"
779 : $x->unshift("white") ;
780 my $first = $FA ? shift @h : $x->shift ;
781 print "shifted $first\n" ;
783 # Check for existence of a key
784 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
786 # use a negative index
787 print "The last element is $h[-1]\n" ;
788 print "The 2nd last element is $h[-2]\n" ;
796 ok(135, docat_del($file) eq <<'EOM') ;
797 The array contains 5 entries
800 Element 1 Exists with value blue
801 The last element is green
802 The 2nd last element is yellow
805 my $save_output = "xyzt" ;
807 my $redirect = new Redirect $save_output ;
809 use warnings FATAL => qw(all);
811 our (@h, $H, $file, $i);
819 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
820 or die "Cannot open file $file: $!\n" ;
822 # first create a text file to play with
830 # Print the records in order.
832 # The length method is needed here because evaluating a tied
833 # array in a scalar context does not return the number of
834 # elements in the array.
836 print "\nORIGINAL\n" ;
837 foreach $i (0 .. $H->length - 1) {
838 print "$i: $h[$i]\n" ;
841 # use the push & pop methods
844 print "\nThe last record was [$a]\n" ;
846 # and the shift & unshift methods
848 $H->unshift("first") ;
849 print "The first record was [$a]\n" ;
851 # Use the API to add a new record after record 2.
853 $H->put($i, "Newbie", R_IAFTER) ;
855 # and a new record before record 1.
857 $H->put($i, "New One", R_IBEFORE) ;
862 # now print the records in reverse order
863 print "\nREVERSE\n" ;
864 for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
865 { print "$i: $h[$i]\n" }
867 # same again, but use the API functions instead
868 print "\nREVERSE again\n" ;
869 my ($s, $k, $v) = (0, 0, 0) ;
870 for ($s = $H->seq($k, $v, R_LAST) ;
872 $s = $H->seq($k, $v, R_PREV))
881 ok(136, docat_del($save_output) eq <<'EOM') ;
890 The last record was [four]
891 The first record was [zero]
913 # Bug ID 20001013.009
915 # test that $hash{KEY} = undef doesn't produce the warning
916 # Use of uninitialized value in null operation
924 local $SIG{__WARN__} = sub {$a = $_[0]} ;
926 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
927 or die "Can't open file: $!\n" ;
935 # test that %hash = () doesn't produce the warning
936 # Argument "" isn't numeric in entersub
941 local $SIG{__WARN__} = sub {$a = $_[0]} ;
946 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
947 or die "Can't open file: $!\n" ;
954 # Only test splice if this is a newish version of Perl
960 # check that the splice warnings are under the same lexical control
961 # as their non-tied counterparts.
968 local $SIG{__WARN__} = sub {$a = $_[0]} ;
973 tie @tied, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
974 or die "Can't open file: $!\n" ;
976 # uninitialized offset
981 ok(139, $a =~ /^Use of uninitialized value /);
983 splice(@tied, $offset);
984 ok(140, $a =~ /^Use of uninitialized value in splice/);
986 no warnings 'uninitialized';
991 splice(@tied, $offset);
994 # uninitialized length
998 splice(@a, 0, $length);
999 ok(143, $a =~ /^Use of uninitialized value /);
1001 splice(@tied, 0, $length);
1002 ok(144, $a =~ /^Use of uninitialized value in splice/);
1004 no warnings 'uninitialized';
1006 splice(@a, 0, $length);
1009 splice(@tied, 0, $length);
1012 # offset past end of array
1016 my $splice_end_array = ($a =~ /^splice\(\) offset past end of array/);
1019 ok(147, !$splice_end_array || $a =~ /^splice\(\) offset past end of array/);
1034 # These are a few regression tests: bundles of five arguments to pass
1035 # to test_splice(). The first four arguments correspond to those
1036 # given to splice(), and the last says which context to call it in
1037 # (scalar, list or void).
1039 # The expected result is not needed because we get that by running
1040 # Perl's built-in splice().
1042 my @tests = ([ [ 'falsely', 'dinosaur', 'remedy', 'commotion',
1043 'rarely', 'paleness' ],
1045 [ 'redoubled', 'Taylorize', 'Zoe', 'halogen' ],
1048 [ [ 'a' ], -2, 1, [ 'B' ], 'void' ],
1050 [ [ 'Hartley', 'Islandia', 'assents', 'wishful' ],
1055 [ [ 'visibility', 'pocketful', 'rectangles' ],
1060 [ [ 'sleeplessly' ],
1062 [ 'Margery', 'clearing', 'repercussion', 'clubs',
1066 [ [ 'chastises', 'recalculates' ],
1068 [ 'momentariness', 'mediates', 'accents', 'toils',
1074 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
1079 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
1082 [ [ 'riheb' ], -8, undef, [], 'void' ],
1084 [ [ 'uft', 'qnxs', '' ],
1086 [ 'znp', 'mhnkh', 'bn' ],
1092 require POSIX; my $tmp = POSIX::tmpnam();
1093 foreach my $test (@tests) {
1094 my $err = test_splice(@$test);
1096 print STDERR "# failed: ", Dumper($test);
1097 print STDERR "# error: $err\n";
1101 else { ok($testnum++, 1) }
1105 # Not worth running the random ones
1106 print STDERR '# skipping ', $testnum++, "\n";
1109 # A thousand randomly-generated tests
1112 foreach (0 .. 1000 - 1) {
1113 my $test = rand_test();
1114 my $err = test_splice(@$test);
1116 print STDERR "# failed: ", Dumper($test);
1117 print STDERR "# error: $err\n";
1119 print STDERR "# skipping any remaining random tests\n";
1124 ok($testnum++, not $failed);
1127 die if $testnum != $total_tests + 1;
1131 # Subroutines for SPLICE testing
1135 # Test the new splice() against Perl's built-in one. The first four
1136 # parameters are those passed to splice(), except that the lists must
1137 # be (explicitly) passed by reference, and are not actually modified.
1138 # (It's just a test!) The last argument specifies the context in
1139 # which to call the functions: 'list', 'scalar', or 'void'.
1142 # undef, if the two splices give the same results for the given
1143 # arguments and context;
1145 # an error message showing the difference, otherwise.
1147 # Reads global variable $tmp.
1150 die 'usage: test_splice(array, offset, length, list, context)' if @_ != 5;
1151 my ($array, $offset, $length, $list, $context) = @_;
1152 my @array = @$array;
1158 my $H = tie @h, 'DB_File', $tmp, O_CREAT|O_RDWR, 0644, $DB_RECNO
1159 or die "cannot open $tmp: $!";
1162 foreach ( @array ) { $h[$i++] = $_ }
1164 return "basic DB_File sanity check failed"
1165 if list_diff(\@array, \@h);
1167 # Output from splice():
1168 # Returned value (munged a bit), error msg, warnings
1170 my ($s_r, $s_error, @s_warnings);
1172 my $gather_warning = sub { push @s_warnings, $_[0] };
1173 if ($context eq 'list') {
1176 local $SIG{__WARN__} = $gather_warning;
1177 @r = splice @array, $offset, $length, @list;
1182 elsif ($context eq 'scalar') {
1185 local $SIG{__WARN__} = $gather_warning;
1186 $r = splice @array, $offset, $length, @list;
1191 elsif ($context eq 'void') {
1193 local $SIG{__WARN__} = $gather_warning;
1194 splice @array, $offset, $length, @list;
1200 die "bad context $context";
1203 foreach ($s_error, @s_warnings) {
1205 s/ at \S+ line \d+\.$//;
1208 # Now do the same for DB_File's version of splice
1209 my ($ms_r, $ms_error, @ms_warnings);
1210 $gather_warning = sub { push @ms_warnings, $_[0] };
1211 if ($context eq 'list') {
1214 local $SIG{__WARN__} = $gather_warning;
1215 @r = splice @h, $offset, $length, @list;
1220 elsif ($context eq 'scalar') {
1223 local $SIG{__WARN__} = $gather_warning;
1224 $r = splice @h, $offset, $length, @list;
1229 elsif ($context eq 'void') {
1231 local $SIG{__WARN__} = $gather_warning;
1232 splice @h, $offset, $length, @list;
1238 die "bad context $context";
1241 foreach ($ms_error, @ms_warnings) {
1243 s/ at \S+ line \d+\.?.*//s;
1246 return "different errors: '$s_error' vs '$ms_error'"
1247 if $s_error ne $ms_error;
1248 return('different return values: ' . Dumper($s_r) . ' vs ' . Dumper($ms_r))
1249 if list_diff($s_r, $ms_r);
1250 return('different changed list: ' . Dumper(\@array) . ' vs ' . Dumper(\@h))
1251 if list_diff(\@array, \@h);
1253 if ((scalar @s_warnings) != (scalar @ms_warnings)) {
1254 return 'different number of warnings';
1257 while (@s_warnings) {
1258 my $sw = shift @s_warnings;
1259 my $msw = shift @ms_warnings;
1261 if (defined $sw and defined $msw) {
1262 $msw =~ s/ \(.+\)$//;
1263 $msw =~ s/ in splice$// if $] < 5.006;
1265 return "different warning: '$sw' vs '$msw'";
1268 elsif (not defined $sw and not defined $msw) {
1272 return "one warning defined, another undef";
1279 open(TEXT, $tmp) or die "cannot open $tmp: $!";
1280 @h = <TEXT>; normalise @h; chomp @h;
1281 close TEXT or die "cannot close $tmp: $!";
1282 return('list is different when re-read from disk: '
1283 . Dumper(\@array) . ' vs ' . Dumper(\@h))
1284 if list_diff(\@array, \@h);
1286 return undef; # success
1292 # Do two lists differ?
1295 # reference to first list
1296 # reference to second list
1298 # Returns true iff they differ. Only works for lists of (string or
1301 # Surely there is a better way to do this?
1304 die 'usage: list_diff(ref to first list, ref to second list)'
1307 my @a = @$a; my @b = @$b;
1308 return 1 if (scalar @a) != (scalar @b);
1309 for (my $i = 0; $i < @a; $i++) {
1310 my ($ae, $be) = ($a[$i], $b[$i]);
1311 if (defined $ae and defined $be) {
1312 return 1 if $ae ne $be;
1314 elsif (not defined $ae and not defined $be) {
1315 # Two undefined values are 'equal'
1327 # Think up a random ARRAY, OFFSET, LENGTH, LIST, and context.
1328 # ARRAY or LIST might be empty, and OFFSET or LENGTH might be
1329 # undefined. Return a 'test' - a listref of these five things.
1332 die 'usage: rand_test()' if @_;
1333 my @contexts = qw<list scalar void>;
1334 my $context = $contexts[int(rand @contexts)];
1335 return [ rand_list(),
1336 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1337 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1344 die 'usage: rand_list()' if @_;
1347 while (rand() > 0.1 * (scalar @r + 1)) {
1348 push @r, rand_word();
1355 die 'usage: rand_word()' if @_;
1357 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>;
1358 while (rand() > 0.1 * (length($r) + 1)) {
1359 $r .= $chars[int(rand(scalar @chars))];