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.
104 my $splice_tests = 10 + 1; # ten regressions, plus the randoms
105 my $total_tests = 138 ;
106 $total_tests += $splice_tests if $FA ;
107 print "1..$total_tests\n";
109 my $Dfile = "recno.tmp";
114 # Check the interface to RECNOINFO
116 my $dbh = new DB_File::RECNOINFO ;
117 ok(1, ! defined $dbh->{bval}) ;
118 ok(2, ! defined $dbh->{cachesize}) ;
119 ok(3, ! defined $dbh->{psize}) ;
120 ok(4, ! defined $dbh->{flags}) ;
121 ok(5, ! defined $dbh->{lorder}) ;
122 ok(6, ! defined $dbh->{reclen}) ;
123 ok(7, ! defined $dbh->{bfname}) ;
125 $dbh->{bval} = 3000 ;
126 ok(8, $dbh->{bval} == 3000 );
128 $dbh->{cachesize} = 9000 ;
129 ok(9, $dbh->{cachesize} == 9000 );
131 $dbh->{psize} = 400 ;
132 ok(10, $dbh->{psize} == 400 );
135 ok(11, $dbh->{flags} == 65 );
137 $dbh->{lorder} = 123 ;
138 ok(12, $dbh->{lorder} == 123 );
140 $dbh->{reclen} = 1234 ;
141 ok(13, $dbh->{reclen} == 1234 );
143 $dbh->{bfname} = 1234 ;
144 ok(14, $dbh->{bfname} == 1234 );
147 # Check that an invalid entry is caught both for store & fetch
148 eval '$dbh->{fred} = 1234' ;
149 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
150 eval 'my $q = $dbh->{fred}' ;
151 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
153 # Now check the interface to RECNOINFO
157 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
159 ok(18, ((stat($Dfile))[2] & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
160 || $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'amigaos') ;
164 ok(19, ($FA ? @h == 0 : !$l) );
166 my @data = qw( a b c d ever f g h i j k longername m n o p) ;
168 $h[0] = shift @data ;
169 ok(20, $h[0] eq 'a' );
175 unshift (@data, 'a') ;
177 ok(21, defined $h[1] );
178 ok(22, ! defined $h[16] );
179 ok(23, $FA ? @h == @data : $X->length == @data );
182 # Overwrite an entry & check fetch it
184 $data[3] = 'replaced' ;
185 ok(24, $h[3] eq 'replaced' );
188 my @push_data = qw(added to the end) ;
189 ($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
190 push (@data, @push_data) ;
191 ok(25, $h[++$i] eq 'added' );
192 ok(26, $h[++$i] eq 'to' );
193 ok(27, $h[++$i] eq 'the' );
194 ok(28, $h[++$i] eq 'end' );
197 my $popped = pop (@data) ;
198 my $value = ($FA ? pop @h : $X->pop) ;
199 ok(29, $value eq $popped) ;
202 $value = ($FA ? shift @h : $X->shift) ;
203 my $shifted = shift @data ;
204 ok(30, $value eq $shifted );
209 ($FA ? unshift @h,() : $X->unshift) ;
210 ok(31, ($FA ? @h == @data : $X->length == @data ));
212 my @new_data = qw(add this to the start of the array) ;
213 $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
214 unshift (@data, @new_data) ;
215 ok(32, $FA ? @h == @data : $X->length == @data );
216 ok(33, $h[0] eq "add") ;
217 ok(34, $h[1] eq "this") ;
218 ok(35, $h[2] eq "to") ;
219 ok(36, $h[3] eq "the") ;
220 ok(37, $h[4] eq "start") ;
221 ok(38, $h[5] eq "of") ;
222 ok(39, $h[6] eq "the") ;
223 ok(40, $h[7] eq "array") ;
224 ok(41, $h[8] eq $data[8]) ;
226 # Brief test for SPLICE - more thorough 'soak test' is later.
229 @old = splice(@h, 1, 2, qw(bananas just before));
232 @old = $X->splice(1, 2, qw(bananas just before));
234 ok(42, $h[0] eq "add") ;
235 ok(43, $h[1] eq "bananas") ;
236 ok(44, $h[2] eq "just") ;
237 ok(45, $h[3] eq "before") ;
238 ok(46, $h[4] eq "the") ;
239 ok(47, $h[5] eq "start") ;
240 ok(48, $h[6] eq "of") ;
241 ok(49, $h[7] eq "the") ;
242 ok(50, $h[8] eq "array") ;
243 ok(51, $h[9] eq $data[8]) ;
244 $FA ? splice(@h, 1, 3, @old) : $X->splice(1, 3, @old);
246 # Now both arrays should be identical
252 $ok = 0, last if $_ ne $h[$j ++] ;
256 # Neagtive subscripts
258 # get the last element of the array
259 ok(53, $h[-1] eq $data[-1] );
260 ok(54, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
262 # get the first element using a negative subscript
263 eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
265 ok(56, $h[0] eq "abcd" );
267 # now try to read before the start of the array
268 eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
269 ok(57, $@ =~ '^Modification of non-creatable array value attempted' );
271 # IMPORTANT - $X must be undefined before the untie otherwise the
272 # underlying DB close routine will not get called.
280 # Check bval defaults to \n
283 my $dbh = new DB_File::RECNOINFO ;
284 ok(58, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
289 my $x = docat($Dfile) ;
291 ok(59, $x eq "abc\ndef\n\nghi\n") ;
298 my $dbh = new DB_File::RECNOINFO ;
300 ok(60, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
305 my $x = docat($Dfile) ;
307 my $ok = ($x eq "abc-def--ghi-") ;
308 bad_one() unless $ok ;
313 # Check R_FIXEDLEN with default bval (space)
316 my $dbh = new DB_File::RECNOINFO ;
317 $dbh->{flags} = R_FIXEDLEN ;
319 ok(62, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
324 my $x = docat($Dfile) ;
326 my $ok = ($x eq "abc def ghi ") ;
327 bad_one() unless $ok ;
332 # Check R_FIXEDLEN with user-defined bval
335 my $dbh = new DB_File::RECNOINFO ;
336 $dbh->{flags} = R_FIXEDLEN ;
339 ok(64, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
344 my $x = docat($Dfile) ;
346 my $ok = ($x eq "abc--def-------ghi--") ;
347 bad_one() unless $ok ;
352 # check that attempting to tie an associative array to a DB_RECNO will fail
354 my $filename = "xyz" ;
356 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
357 ok(66, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
369 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
376 use vars qw( @ISA @EXPORT) ;
381 @EXPORT = @DB_File::EXPORT ;
387 $self->SUPER::STORE($key, $value * 2) ;
393 $self->SUPER::FETCH($key) - 1 ;
400 $self->SUPER::put($key, $value * 3) ;
405 $self->SUPER::get($_[0], $_[1]) ;
413 my $value = $self->FETCH($key) ;
414 return "[[$value]]" ;
422 BEGIN { push @INC, '.'; }
424 main::ok(67, $@ eq "") ;
428 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
431 main::ok(68, $@ eq "") ;
433 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
434 main::ok(69, $@ eq "") ;
435 main::ok(70, $ret == 5) ;
438 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
439 main::ok(71, $@ eq "") ;
440 main::ok(72, $ret == 10) ;
442 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
443 main::ok(73, $@ eq "" ) ;
444 main::ok(74, $ret == 1) ;
446 $ret = eval '$X->A_new_method(1) ' ;
447 main::ok(75, $@ eq "") ;
448 main::ok(76, $ret eq "[[11]]") ;
452 unlink "SubDB.pm", "recno.tmp" ;
461 ok(77, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
466 ok(78, $FA ? $#h == 3 : $self->length() == 4) ;
469 my $x = docat($Dfile) ;
470 ok(79, $x eq "abc\ndef\nghi\njkl\n") ;
472 # $# sets array to same length
473 ok(80, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
477 { $self->STORESIZE(4) }
478 ok(81, $FA ? $#h == 3 : $self->length() == 4) ;
482 ok(82, $x eq "abc\ndef\nghi\njkl\n") ;
484 # $# sets array to bigger
485 ok(83, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
489 { $self->STORESIZE(7) }
490 ok(84, $FA ? $#h == 6 : $self->length() == 7) ;
494 ok(85, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
496 # $# sets array smaller
497 ok(86, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
501 { $self->STORESIZE(3) }
502 ok(87, $FA ? $#h == 2 : $self->length() == 3) ;
506 ok(88, $x eq "abc\ndef\nghi\n") ;
518 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
523 my($fk, $sk, $fv, $sv) = @_ ;
525 $fetch_key eq $fk && $store_key eq $sk &&
526 $fetch_value eq $fv && $store_value eq $sv &&
530 ok(89, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
532 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
533 $db->filter_store_key (sub { $store_key = $_ }) ;
534 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
535 $db->filter_store_value (sub { $store_value = $_ }) ;
541 ok(90, checkOutput( "", 0, "", "joe")) ;
543 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
544 ok(91, $h[0] eq "joe");
546 ok(92, checkOutput( "", 0, "joe", "")) ;
548 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
549 ok(93, $db->FIRSTKEY() == 0) ;
551 ok(94, checkOutput( 0, "", "", "")) ;
553 # replace the filters, but remember the previous set
554 my ($old_fk) = $db->filter_fetch_key
555 (sub { ++ $_ ; $fetch_key = $_ }) ;
556 my ($old_sk) = $db->filter_store_key
557 (sub { $_ *= 2 ; $store_key = $_ }) ;
558 my ($old_fv) = $db->filter_fetch_value
559 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
560 my ($old_sv) = $db->filter_store_value
561 (sub { s/o/x/g; $store_value = $_ }) ;
563 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
566 ok(95, checkOutput( "", 2, "", "Jxe")) ;
568 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
569 ok(96, $h[1] eq "[Jxe]");
571 ok(97, checkOutput( "", 2, "[Jxe]", "")) ;
573 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
574 ok(98, $db->FIRSTKEY() == 1) ;
576 ok(99, checkOutput( 1, "", "", "")) ;
578 # put the original filters back
579 $db->filter_fetch_key ($old_fk);
580 $db->filter_store_key ($old_sk);
581 $db->filter_fetch_value ($old_fv);
582 $db->filter_store_value ($old_sv);
584 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
586 ok(100, checkOutput( "", 0, "", "joe")) ;
588 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
589 ok(101, $h[0] eq "joe");
590 ok(102, checkOutput( "", 0, "joe", "")) ;
592 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
593 ok(103, $db->FIRSTKEY() == 0) ;
594 ok(104, checkOutput( 0, "", "", "")) ;
597 $db->filter_fetch_key (undef);
598 $db->filter_store_key (undef);
599 $db->filter_fetch_value (undef);
600 $db->filter_store_value (undef);
602 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
604 ok(105, checkOutput( "", "", "", "")) ;
606 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
607 ok(106, $h[0] eq "joe");
608 ok(107, checkOutput( "", "", "", "")) ;
610 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
611 ok(108, $db->FIRSTKEY() == 0) ;
612 ok(109, checkOutput( "", "", "", "")) ;
620 # DBM Filter with a closure
627 ok(110, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
637 return sub { ++$count ;
639 $result{$name} = "$name - $count: [@kept]" ;
643 $db->filter_store_key(Closure("store key")) ;
644 $db->filter_store_value(Closure("store value")) ;
645 $db->filter_fetch_key(Closure("fetch key")) ;
646 $db->filter_fetch_value(Closure("fetch value")) ;
651 ok(111, $result{"store key"} eq "store key - 1: [0]");
652 ok(112, $result{"store value"} eq "store value - 1: [joe]");
653 ok(113, ! defined $result{"fetch key"} );
654 ok(114, ! defined $result{"fetch value"} );
655 ok(115, $_ eq "original") ;
657 ok(116, $db->FIRSTKEY() == 0 ) ;
658 ok(117, $result{"store key"} eq "store key - 1: [0]");
659 ok(118, $result{"store value"} eq "store value - 1: [joe]");
660 ok(119, $result{"fetch key"} eq "fetch key - 1: [0]");
661 ok(120, ! defined $result{"fetch value"} );
662 ok(121, $_ eq "original") ;
665 ok(122, $result{"store key"} eq "store key - 2: [0 7]");
666 ok(123, $result{"store value"} eq "store value - 2: [joe john]");
667 ok(124, $result{"fetch key"} eq "fetch key - 1: [0]");
668 ok(125, ! defined $result{"fetch value"} );
669 ok(126, $_ eq "original") ;
671 ok(127, $h[0] eq "joe");
672 ok(128, $result{"store key"} eq "store key - 3: [0 7 0]");
673 ok(129, $result{"store value"} eq "store value - 2: [joe john]");
674 ok(130, $result{"fetch key"} eq "fetch key - 1: [0]");
675 ok(131, $result{"fetch value"} eq "fetch value - 1: [joe]");
676 ok(132, $_ eq "original") ;
684 # DBM Filter recursion detection
690 ok(133, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
692 $db->filter_store_key (sub { $_ = $h[0] }) ;
694 eval '$h[1] = 1234' ;
695 ok(134, $@ =~ /^recursion detected in filter_store_key at/ );
704 # Examples from the POD
708 my $redirect = new Redirect $file ;
710 use warnings FATAL => qw(all);
714 my $filename = "text" ;
718 my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
719 or die "Cannot open file 'text': $!\n" ;
721 # Add a few key/value pairs to the file
726 $FA ? push @h, "green", "black"
727 : $x->push("green", "black") ;
729 my $elements = $FA ? scalar @h : $x->length ;
730 print "The array contains $elements entries\n" ;
732 my $last = $FA ? pop @h : $x->pop ;
733 print "popped $last\n" ;
735 $FA ? unshift @h, "white"
736 : $x->unshift("white") ;
737 my $first = $FA ? shift @h : $x->shift ;
738 print "shifted $first\n" ;
740 # Check for existence of a key
741 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
743 # use a negative index
744 print "The last element is $h[-1]\n" ;
745 print "The 2nd last element is $h[-2]\n" ;
753 ok(135, docat_del($file) eq <<'EOM') ;
754 The array contains 5 entries
757 Element 1 Exists with value blue
758 The last element is green
759 The 2nd last element is yellow
762 my $save_output = "xyzt" ;
764 my $redirect = new Redirect $save_output ;
766 use warnings FATAL => qw(all);
768 use vars qw(@h $H $file $i) ;
776 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
777 or die "Cannot open file $file: $!\n" ;
779 # first create a text file to play with
787 # Print the records in order.
789 # The length method is needed here because evaluating a tied
790 # array in a scalar context does not return the number of
791 # elements in the array.
793 print "\nORIGINAL\n" ;
794 foreach $i (0 .. $H->length - 1) {
795 print "$i: $h[$i]\n" ;
798 # use the push & pop methods
801 print "\nThe last record was [$a]\n" ;
803 # and the shift & unshift methods
805 $H->unshift("first") ;
806 print "The first record was [$a]\n" ;
808 # Use the API to add a new record after record 2.
810 $H->put($i, "Newbie", R_IAFTER) ;
812 # and a new record before record 1.
814 $H->put($i, "New One", R_IBEFORE) ;
819 # now print the records in reverse order
820 print "\nREVERSE\n" ;
821 for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
822 { print "$i: $h[$i]\n" }
824 # same again, but use the API functions instead
825 print "\nREVERSE again\n" ;
826 my ($s, $k, $v) = (0, 0, 0) ;
827 for ($s = $H->seq($k, $v, R_LAST) ;
829 $s = $H->seq($k, $v, R_PREV))
838 ok(136, docat_del($save_output) eq <<'EOM') ;
847 The last record was [four]
848 The first record was [zero]
870 # Bug ID 20001013.009
872 # test that $hash{KEY} = undef doesn't produce the warning
873 # Use of uninitialized value in null operation
881 local $SIG{__WARN__} = sub {$a = $_[0]} ;
883 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
884 or die "Can't open file: $!\n" ;
892 # test that %hash = () doesn't produce the warning
893 # Argument "" isn't numeric in entersub
898 local $SIG{__WARN__} = sub {$a = $_[0]} ;
903 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
904 or die "Can't open file: $!\n" ;
911 # Only test splice if this is a newish version of Perl
916 # These are a few regression tests: bundles of five arguments to pass
917 # to test_splice(). The first four arguments correspond to those
918 # given to splice(), and the last says which context to call it in
919 # (scalar, list or void).
921 # The expected result is not needed because we get that by running
922 # Perl's built-in splice().
924 my @tests = ([ [ 'falsely', 'dinosaur', 'remedy', 'commotion',
925 'rarely', 'paleness' ],
927 [ 'redoubled', 'Taylorize', 'Zoe', 'halogen' ],
930 [ [ 'a' ], -2, 1, [ 'B' ], 'void' ],
932 [ [ 'Hartley', 'Islandia', 'assents', 'wishful' ],
937 [ [ 'visibility', 'pocketful', 'rectangles' ],
944 [ 'Margery', 'clearing', 'repercussion', 'clubs',
948 [ [ 'chastises', 'recalculates' ],
950 [ 'momentariness', 'mediates', 'accents', 'toils',
956 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
961 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
964 [ [ 'riheb' ], -8, undef, [], 'void' ],
966 [ [ 'uft', 'qnxs', '' ],
968 [ 'znp', 'mhnkh', 'bn' ],
974 require POSIX; my $tmp = POSIX::tmpnam();
975 foreach my $test (@tests) {
976 my $err = test_splice(@$test);
978 require Data::Dumper;
979 print STDERR "failed: ", Data::Dumper::Dumper($test);
980 print STDERR "error: $err\n";
984 else { ok($testnum++, 1) }
988 # Not worth running the random ones
989 print STDERR 'skipping ', $testnum++, "\n";
992 # A thousand randomly-generated tests
995 foreach (0 .. 1000 - 1) {
996 my $test = rand_test();
997 my $err = test_splice(@$test);
999 require Data::Dumper;
1000 print STDERR "failed: ", Data::Dumper::Dumper($test);
1001 print STDERR "error: $err\n";
1003 print STDERR "skipping any remaining random tests\n";
1008 ok($testnum++, not $failed);
1011 die if $testnum != $total_tests + 1;
1015 # Subroutines for SPLICE testing
1019 # Test the new splice() against Perl's built-in one. The first four
1020 # parameters are those passed to splice(), except that the lists must
1021 # be (explicitly) passed by reference, and are not actually modified.
1022 # (It's just a test!) The last argument specifies the context in
1023 # which to call the functions: 'list', 'scalar', or 'void'.
1026 # undef, if the two splices give the same results for the given
1027 # arguments and context;
1029 # an error message showing the difference, otherwise.
1031 # Reads global variable $tmp.
1034 die 'usage: test_splice(array, offset, length, list, context)' if @_ != 5;
1035 my ($array, $offset, $length, $list, $context) = @_;
1036 my @array = @$array;
1039 open(TEXT, ">$tmp") or die "cannot write to $tmp: $!";
1040 foreach (@array) { print TEXT "$_\n" }
1041 close TEXT or die "cannot close $tmp: $!";
1044 my $H = tie @h, 'DB_File', $tmp, O_RDWR, 0644, $DB_RECNO
1045 or die "cannot open $tmp: $!";
1047 return "basic DB_File sanity check failed"
1048 if list_diff(\@array, \@h);
1050 # Output from splice():
1051 # Returned value (munged a bit), error msg, warnings
1053 my ($s_r, $s_error, @s_warnings);
1055 my $gather_warning = sub { push @s_warnings, $_[0] };
1056 if ($context eq 'list') {
1059 local $SIG{__WARN__} = $gather_warning;
1060 @r = splice @array, $offset, $length, @list;
1065 elsif ($context eq 'scalar') {
1068 local $SIG{__WARN__} = $gather_warning;
1069 $r = splice @array, $offset, $length, @list;
1074 elsif ($context eq 'void') {
1076 local $SIG{__WARN__} = $gather_warning;
1077 splice @array, $offset, $length, @list;
1083 die "bad context $context";
1086 foreach ($s_error, @s_warnings) {
1088 s/ at \S+ line \d+\.$//;
1091 # Now do the same for DB_File's version of splice
1092 my ($ms_r, $ms_error, @ms_warnings);
1093 $gather_warning = sub { push @ms_warnings, $_[0] };
1094 if ($context eq 'list') {
1097 local $SIG{__WARN__} = $gather_warning;
1098 @r = splice @h, $offset, $length, @list;
1103 elsif ($context eq 'scalar') {
1106 local $SIG{__WARN__} = $gather_warning;
1107 $r = splice @h, $offset, $length, @list;
1112 elsif ($context eq 'void') {
1114 local $SIG{__WARN__} = $gather_warning;
1115 splice @h, $offset, $length, @list;
1121 die "bad context $context";
1124 foreach ($ms_error, @ms_warnings) {
1126 s/ at \S+ line \d+\.?$//;
1129 return "different errors: '$s_error' vs '$ms_error'"
1130 if $s_error ne $ms_error;
1131 return('different return values: ' . Dumper($s_r) . ' vs ' . Dumper($ms_r))
1132 if list_diff($s_r, $ms_r);
1133 return('different changed list: ' . Dumper(\@array) . ' vs ' . Dumper(\@h))
1134 if list_diff(\@array, \@h);
1136 if ((scalar @s_warnings) != (scalar @ms_warnings)) {
1137 return 'different number of warnings';
1140 while (@s_warnings) {
1141 my $sw = shift @s_warnings;
1142 my $msw = shift @ms_warnings;
1144 if (defined $sw and defined $msw) {
1145 $msw =~ s/ \(.+\)$//;
1146 $msw =~ s/ in splice$// if $] < 5.006;
1148 return "different warning: '$sw' vs '$msw'";
1151 elsif (not defined $sw and not defined $msw) {
1155 return "one warning defined, another undef";
1162 open(TEXT, $tmp) or die "cannot open $tmp: $!";
1163 @h = <TEXT>; chomp @h;
1164 close TEXT or die "cannot close $tmp: $!";
1165 return('list is different when re-read from disk: '
1166 . Dumper(\@array) . ' vs ' . Dumper(\@h))
1167 if list_diff(\@array, \@h);
1169 return undef; # success
1175 # Do two lists differ?
1178 # reference to first list
1179 # reference to second list
1181 # Returns true iff they differ. Only works for lists of (string or
1184 # Surely there is a better way to do this?
1187 die 'usage: list_diff(ref to first list, ref to second list)'
1190 my @a = @$a; my @b = @$b;
1191 return 1 if (scalar @a) != (scalar @b);
1192 for (my $i = 0; $i < @a; $i++) {
1193 my ($ae, $be) = ($a[$i], $b[$i]);
1194 if (defined $ae and defined $be) {
1195 return 1 if $ae ne $be;
1197 elsif (not defined $ae and not defined $be) {
1198 # Two undefined values are 'equal'
1210 # Think up a random ARRAY, OFFSET, LENGTH, LIST, and context.
1211 # ARRAY or LIST might be empty, and OFFSET or LENGTH might be
1212 # undefined. Return a 'test' - a listref of these five things.
1215 die 'usage: rand_test()' if @_;
1216 my @contexts = qw<list scalar void>;
1217 my $context = $contexts[int(rand @contexts)];
1218 return [ rand_list(),
1219 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1220 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1227 die 'usage: rand_list()' if @_;
1230 while (rand() > 0.1 * (scalar @r + 1)) {
1231 push @r, rand_word();
1238 die 'usage: rand_word()' if @_;
1240 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>;
1241 while (rand() > 0.1 * (length($r) + 1)) {
1242 $r .= $chars[int(rand(scalar @chars))];