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 use vars qw($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 print STDERR <<EOM unless $bad_ones++ ;
96 # Some older versions of Berkeley DB version 1 will fail tests 51,
99 # You can safely ignore the errors if you're never going to use the
100 # broken functionality (recno databases with a modified bval).
101 # Otherwise you'll have to upgrade your DB library.
103 # If you want to use Berkeley DB version 1, then 1.85 and 1.86 are the
104 # last versions that were released. Berkeley DB version 2 is continually
105 # being updated -- Check out http://www.sleepycat.com/ for more details.
112 return unless $^O eq 'cygwin' ;
120 local $SIG{__DIE__} ;
121 eval { require Data::Dumper ; import Data::Dumper } ;
125 *Dumper = sub { my $a = shift; return "[ @{ $a } ]" } ;
129 my $splice_tests = 10 + 1; # ten regressions, plus the randoms
130 my $total_tests = 138 ;
131 $total_tests += $splice_tests if $FA ;
132 print "1..$total_tests\n";
134 my $Dfile = "recno.tmp";
139 # Check the interface to RECNOINFO
141 my $dbh = new DB_File::RECNOINFO ;
142 ok(1, ! defined $dbh->{bval}) ;
143 ok(2, ! defined $dbh->{cachesize}) ;
144 ok(3, ! defined $dbh->{psize}) ;
145 ok(4, ! defined $dbh->{flags}) ;
146 ok(5, ! defined $dbh->{lorder}) ;
147 ok(6, ! defined $dbh->{reclen}) ;
148 ok(7, ! defined $dbh->{bfname}) ;
150 $dbh->{bval} = 3000 ;
151 ok(8, $dbh->{bval} == 3000 );
153 $dbh->{cachesize} = 9000 ;
154 ok(9, $dbh->{cachesize} == 9000 );
156 $dbh->{psize} = 400 ;
157 ok(10, $dbh->{psize} == 400 );
160 ok(11, $dbh->{flags} == 65 );
162 $dbh->{lorder} = 123 ;
163 ok(12, $dbh->{lorder} == 123 );
165 $dbh->{reclen} = 1234 ;
166 ok(13, $dbh->{reclen} == 1234 );
168 $dbh->{bfname} = 1234 ;
169 ok(14, $dbh->{bfname} == 1234 );
172 # Check that an invalid entry is caught both for store & fetch
173 eval '$dbh->{fred} = 1234' ;
174 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
175 eval 'my $q = $dbh->{fred}' ;
176 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
178 # Now check the interface to RECNOINFO
182 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
184 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
186 ok(18, ((stat($Dfile))[2] & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
191 ok(19, ($FA ? @h == 0 : !$l) );
193 my @data = qw( a b c d ever f g h i j k longername m n o p) ;
195 $h[0] = shift @data ;
196 ok(20, $h[0] eq 'a' );
202 unshift (@data, 'a') ;
204 ok(21, defined $h[1] );
205 ok(22, ! defined $h[16] );
206 ok(23, $FA ? @h == @data : $X->length == @data );
209 # Overwrite an entry & check fetch it
211 $data[3] = 'replaced' ;
212 ok(24, $h[3] eq 'replaced' );
215 my @push_data = qw(added to the end) ;
216 ($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
217 push (@data, @push_data) ;
218 ok(25, $h[++$i] eq 'added' );
219 ok(26, $h[++$i] eq 'to' );
220 ok(27, $h[++$i] eq 'the' );
221 ok(28, $h[++$i] eq 'end' );
224 my $popped = pop (@data) ;
225 my $value = ($FA ? pop @h : $X->pop) ;
226 ok(29, $value eq $popped) ;
229 $value = ($FA ? shift @h : $X->shift) ;
230 my $shifted = shift @data ;
231 ok(30, $value eq $shifted );
236 ($FA ? unshift @h,() : $X->unshift) ;
237 ok(31, ($FA ? @h == @data : $X->length == @data ));
239 my @new_data = qw(add this to the start of the array) ;
240 $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
241 unshift (@data, @new_data) ;
242 ok(32, $FA ? @h == @data : $X->length == @data );
243 ok(33, $h[0] eq "add") ;
244 ok(34, $h[1] eq "this") ;
245 ok(35, $h[2] eq "to") ;
246 ok(36, $h[3] eq "the") ;
247 ok(37, $h[4] eq "start") ;
248 ok(38, $h[5] eq "of") ;
249 ok(39, $h[6] eq "the") ;
250 ok(40, $h[7] eq "array") ;
251 ok(41, $h[8] eq $data[8]) ;
253 # Brief test for SPLICE - more thorough 'soak test' is later.
256 @old = splice(@h, 1, 2, qw(bananas just before));
259 @old = $X->splice(1, 2, qw(bananas just before));
261 ok(42, $h[0] eq "add") ;
262 ok(43, $h[1] eq "bananas") ;
263 ok(44, $h[2] eq "just") ;
264 ok(45, $h[3] eq "before") ;
265 ok(46, $h[4] eq "the") ;
266 ok(47, $h[5] eq "start") ;
267 ok(48, $h[6] eq "of") ;
268 ok(49, $h[7] eq "the") ;
269 ok(50, $h[8] eq "array") ;
270 ok(51, $h[9] eq $data[8]) ;
271 $FA ? splice(@h, 1, 3, @old) : $X->splice(1, 3, @old);
273 # Now both arrays should be identical
279 $ok = 0, last if $_ ne $h[$j ++] ;
283 # Neagtive subscripts
285 # get the last element of the array
286 ok(53, $h[-1] eq $data[-1] );
287 ok(54, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
289 # get the first element using a negative subscript
290 eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
292 ok(56, $h[0] eq "abcd" );
294 # now try to read before the start of the array
295 eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
296 ok(57, $@ =~ '^Modification of non-creatable array value attempted' );
298 # IMPORTANT - $X must be undefined before the untie otherwise the
299 # underlying DB close routine will not get called.
307 # Check bval defaults to \n
310 my $dbh = new DB_File::RECNOINFO ;
311 ok(58, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
316 my $x = docat($Dfile) ;
318 ok(59, $x eq "abc\ndef\n\nghi\n") ;
325 my $dbh = new DB_File::RECNOINFO ;
327 ok(60, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
332 my $x = docat($Dfile) ;
334 my $ok = ($x eq "abc-def--ghi-") ;
335 bad_one() unless $ok ;
340 # Check R_FIXEDLEN with default bval (space)
343 my $dbh = new DB_File::RECNOINFO ;
344 $dbh->{flags} = R_FIXEDLEN ;
346 ok(62, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
351 my $x = docat($Dfile) ;
353 my $ok = ($x eq "abc def ghi ") ;
354 bad_one() unless $ok ;
359 # Check R_FIXEDLEN with user-defined bval
362 my $dbh = new DB_File::RECNOINFO ;
363 $dbh->{flags} = R_FIXEDLEN ;
366 ok(64, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
371 my $x = docat($Dfile) ;
373 my $ok = ($x eq "abc--def-------ghi--") ;
374 bad_one() unless $ok ;
379 # check that attempting to tie an associative array to a DB_RECNO will fail
381 my $filename = "xyz" ;
383 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
384 ok(66, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
396 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
403 use vars qw( @ISA @EXPORT) ;
408 @EXPORT = @DB_File::EXPORT ;
414 $self->SUPER::STORE($key, $value * 2) ;
420 $self->SUPER::FETCH($key) - 1 ;
427 $self->SUPER::put($key, $value * 3) ;
432 $self->SUPER::get($_[0], $_[1]) ;
440 my $value = $self->FETCH($key) ;
441 return "[[$value]]" ;
449 BEGIN { push @INC, '.'; }
451 main::ok(67, $@ eq "") ;
455 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
458 main::ok(68, $@ eq "") ;
460 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
461 main::ok(69, $@ eq "") ;
462 main::ok(70, $ret == 5) ;
465 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
466 main::ok(71, $@ eq "") ;
467 main::ok(72, $ret == 10) ;
469 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
470 main::ok(73, $@ eq "" ) ;
471 main::ok(74, $ret == 1) ;
473 $ret = eval '$X->A_new_method(1) ' ;
474 main::ok(75, $@ eq "") ;
475 main::ok(76, $ret eq "[[11]]") ;
479 unlink "SubDB.pm", "recno.tmp" ;
488 ok(77, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
493 ok(78, $FA ? $#h == 3 : $self->length() == 4) ;
496 my $x = docat($Dfile) ;
497 ok(79, $x eq "abc\ndef\nghi\njkl\n") ;
499 # $# sets array to same length
500 ok(80, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
504 { $self->STORESIZE(4) }
505 ok(81, $FA ? $#h == 3 : $self->length() == 4) ;
509 ok(82, $x eq "abc\ndef\nghi\njkl\n") ;
511 # $# sets array to bigger
512 ok(83, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
516 { $self->STORESIZE(7) }
517 ok(84, $FA ? $#h == 6 : $self->length() == 7) ;
521 ok(85, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
523 # $# sets array smaller
524 ok(86, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
528 { $self->STORESIZE(3) }
529 ok(87, $FA ? $#h == 2 : $self->length() == 3) ;
533 ok(88, $x eq "abc\ndef\nghi\n") ;
545 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
550 my($fk, $sk, $fv, $sv) = @_ ;
552 $fetch_key eq $fk && $store_key eq $sk &&
553 $fetch_value eq $fv && $store_value eq $sv &&
557 ok(89, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
559 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
560 $db->filter_store_key (sub { $store_key = $_ }) ;
561 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
562 $db->filter_store_value (sub { $store_value = $_ }) ;
568 ok(90, checkOutput( "", 0, "", "joe")) ;
570 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
571 ok(91, $h[0] eq "joe");
573 ok(92, checkOutput( "", 0, "joe", "")) ;
575 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
576 ok(93, $db->FIRSTKEY() == 0) ;
578 ok(94, checkOutput( 0, "", "", "")) ;
580 # replace the filters, but remember the previous set
581 my ($old_fk) = $db->filter_fetch_key
582 (sub { ++ $_ ; $fetch_key = $_ }) ;
583 my ($old_sk) = $db->filter_store_key
584 (sub { $_ *= 2 ; $store_key = $_ }) ;
585 my ($old_fv) = $db->filter_fetch_value
586 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
587 my ($old_sv) = $db->filter_store_value
588 (sub { s/o/x/g; $store_value = $_ }) ;
590 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
593 ok(95, checkOutput( "", 2, "", "Jxe")) ;
595 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
596 ok(96, $h[1] eq "[Jxe]");
598 ok(97, checkOutput( "", 2, "[Jxe]", "")) ;
600 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
601 ok(98, $db->FIRSTKEY() == 1) ;
603 ok(99, checkOutput( 1, "", "", "")) ;
605 # put the original filters back
606 $db->filter_fetch_key ($old_fk);
607 $db->filter_store_key ($old_sk);
608 $db->filter_fetch_value ($old_fv);
609 $db->filter_store_value ($old_sv);
611 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
613 ok(100, checkOutput( "", 0, "", "joe")) ;
615 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
616 ok(101, $h[0] eq "joe");
617 ok(102, checkOutput( "", 0, "joe", "")) ;
619 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
620 ok(103, $db->FIRSTKEY() == 0) ;
621 ok(104, checkOutput( 0, "", "", "")) ;
624 $db->filter_fetch_key (undef);
625 $db->filter_store_key (undef);
626 $db->filter_fetch_value (undef);
627 $db->filter_store_value (undef);
629 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
631 ok(105, checkOutput( "", "", "", "")) ;
633 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
634 ok(106, $h[0] eq "joe");
635 ok(107, checkOutput( "", "", "", "")) ;
637 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
638 ok(108, $db->FIRSTKEY() == 0) ;
639 ok(109, checkOutput( "", "", "", "")) ;
647 # DBM Filter with a closure
654 ok(110, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
664 return sub { ++$count ;
666 $result{$name} = "$name - $count: [@kept]" ;
670 $db->filter_store_key(Closure("store key")) ;
671 $db->filter_store_value(Closure("store value")) ;
672 $db->filter_fetch_key(Closure("fetch key")) ;
673 $db->filter_fetch_value(Closure("fetch value")) ;
678 ok(111, $result{"store key"} eq "store key - 1: [0]");
679 ok(112, $result{"store value"} eq "store value - 1: [joe]");
680 ok(113, ! defined $result{"fetch key"} );
681 ok(114, ! defined $result{"fetch value"} );
682 ok(115, $_ eq "original") ;
684 ok(116, $db->FIRSTKEY() == 0 ) ;
685 ok(117, $result{"store key"} eq "store key - 1: [0]");
686 ok(118, $result{"store value"} eq "store value - 1: [joe]");
687 ok(119, $result{"fetch key"} eq "fetch key - 1: [0]");
688 ok(120, ! defined $result{"fetch value"} );
689 ok(121, $_ eq "original") ;
692 ok(122, $result{"store key"} eq "store key - 2: [0 7]");
693 ok(123, $result{"store value"} eq "store value - 2: [joe john]");
694 ok(124, $result{"fetch key"} eq "fetch key - 1: [0]");
695 ok(125, ! defined $result{"fetch value"} );
696 ok(126, $_ eq "original") ;
698 ok(127, $h[0] eq "joe");
699 ok(128, $result{"store key"} eq "store key - 3: [0 7 0]");
700 ok(129, $result{"store value"} eq "store value - 2: [joe john]");
701 ok(130, $result{"fetch key"} eq "fetch key - 1: [0]");
702 ok(131, $result{"fetch value"} eq "fetch value - 1: [joe]");
703 ok(132, $_ eq "original") ;
711 # DBM Filter recursion detection
717 ok(133, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
719 $db->filter_store_key (sub { $_ = $h[0] }) ;
721 eval '$h[1] = 1234' ;
722 ok(134, $@ =~ /^recursion detected in filter_store_key at/ );
731 # Examples from the POD
735 my $redirect = new Redirect $file ;
737 use warnings FATAL => qw(all);
741 my $filename = "text" ;
745 my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
746 or die "Cannot open file 'text': $!\n" ;
748 # Add a few key/value pairs to the file
753 $FA ? push @h, "green", "black"
754 : $x->push("green", "black") ;
756 my $elements = $FA ? scalar @h : $x->length ;
757 print "The array contains $elements entries\n" ;
759 my $last = $FA ? pop @h : $x->pop ;
760 print "popped $last\n" ;
762 $FA ? unshift @h, "white"
763 : $x->unshift("white") ;
764 my $first = $FA ? shift @h : $x->shift ;
765 print "shifted $first\n" ;
767 # Check for existence of a key
768 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
770 # use a negative index
771 print "The last element is $h[-1]\n" ;
772 print "The 2nd last element is $h[-2]\n" ;
780 ok(135, docat_del($file) eq <<'EOM') ;
781 The array contains 5 entries
784 Element 1 Exists with value blue
785 The last element is green
786 The 2nd last element is yellow
789 my $save_output = "xyzt" ;
791 my $redirect = new Redirect $save_output ;
793 use warnings FATAL => qw(all);
795 use vars qw(@h $H $file $i) ;
803 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
804 or die "Cannot open file $file: $!\n" ;
806 # first create a text file to play with
814 # Print the records in order.
816 # The length method is needed here because evaluating a tied
817 # array in a scalar context does not return the number of
818 # elements in the array.
820 print "\nORIGINAL\n" ;
821 foreach $i (0 .. $H->length - 1) {
822 print "$i: $h[$i]\n" ;
825 # use the push & pop methods
828 print "\nThe last record was [$a]\n" ;
830 # and the shift & unshift methods
832 $H->unshift("first") ;
833 print "The first record was [$a]\n" ;
835 # Use the API to add a new record after record 2.
837 $H->put($i, "Newbie", R_IAFTER) ;
839 # and a new record before record 1.
841 $H->put($i, "New One", R_IBEFORE) ;
846 # now print the records in reverse order
847 print "\nREVERSE\n" ;
848 for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
849 { print "$i: $h[$i]\n" }
851 # same again, but use the API functions instead
852 print "\nREVERSE again\n" ;
853 my ($s, $k, $v) = (0, 0, 0) ;
854 for ($s = $H->seq($k, $v, R_LAST) ;
856 $s = $H->seq($k, $v, R_PREV))
865 ok(136, docat_del($save_output) eq <<'EOM') ;
874 The last record was [four]
875 The first record was [zero]
897 # Bug ID 20001013.009
899 # test that $hash{KEY} = undef doesn't produce the warning
900 # Use of uninitialized value in null operation
908 local $SIG{__WARN__} = sub {$a = $_[0]} ;
910 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
911 or die "Can't open file: $!\n" ;
919 # test that %hash = () doesn't produce the warning
920 # Argument "" isn't numeric in entersub
925 local $SIG{__WARN__} = sub {$a = $_[0]} ;
930 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
931 or die "Can't open file: $!\n" ;
938 # Only test splice if this is a newish version of Perl
943 # These are a few regression tests: bundles of five arguments to pass
944 # to test_splice(). The first four arguments correspond to those
945 # given to splice(), and the last says which context to call it in
946 # (scalar, list or void).
948 # The expected result is not needed because we get that by running
949 # Perl's built-in splice().
951 my @tests = ([ [ 'falsely', 'dinosaur', 'remedy', 'commotion',
952 'rarely', 'paleness' ],
954 [ 'redoubled', 'Taylorize', 'Zoe', 'halogen' ],
957 [ [ 'a' ], -2, 1, [ 'B' ], 'void' ],
959 [ [ 'Hartley', 'Islandia', 'assents', 'wishful' ],
964 [ [ 'visibility', 'pocketful', 'rectangles' ],
971 [ 'Margery', 'clearing', 'repercussion', 'clubs',
975 [ [ 'chastises', 'recalculates' ],
977 [ 'momentariness', 'mediates', 'accents', 'toils',
983 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
988 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
991 [ [ 'riheb' ], -8, undef, [], 'void' ],
993 [ [ 'uft', 'qnxs', '' ],
995 [ 'znp', 'mhnkh', 'bn' ],
1001 require POSIX; my $tmp = POSIX::tmpnam();
1002 foreach my $test (@tests) {
1003 my $err = test_splice(@$test);
1005 print STDERR "# failed: ", Dumper($test);
1006 print STDERR "# error: $err\n";
1010 else { ok($testnum++, 1) }
1014 # Not worth running the random ones
1015 print STDERR '# skipping ', $testnum++, "\n";
1018 # A thousand randomly-generated tests
1021 foreach (0 .. 1000 - 1) {
1022 my $test = rand_test();
1023 my $err = test_splice(@$test);
1025 print STDERR "# failed: ", Dumper($test);
1026 print STDERR "# error: $err\n";
1028 print STDERR "# skipping any remaining random tests\n";
1033 ok($testnum++, not $failed);
1036 die if $testnum != $total_tests + 1;
1040 # Subroutines for SPLICE testing
1044 # Test the new splice() against Perl's built-in one. The first four
1045 # parameters are those passed to splice(), except that the lists must
1046 # be (explicitly) passed by reference, and are not actually modified.
1047 # (It's just a test!) The last argument specifies the context in
1048 # which to call the functions: 'list', 'scalar', or 'void'.
1051 # undef, if the two splices give the same results for the given
1052 # arguments and context;
1054 # an error message showing the difference, otherwise.
1056 # Reads global variable $tmp.
1059 die 'usage: test_splice(array, offset, length, list, context)' if @_ != 5;
1060 my ($array, $offset, $length, $list, $context) = @_;
1061 my @array = @$array;
1064 open(TEXT, ">$tmp") or die "cannot write to $tmp: $!";
1065 foreach (@array) { print TEXT "$_\n" }
1066 close TEXT or die "cannot close $tmp: $!";
1069 my $H = tie @h, 'DB_File', $tmp, O_RDWR, 0644, $DB_RECNO
1070 or die "cannot open $tmp: $!";
1072 return "basic DB_File sanity check failed"
1073 if list_diff(\@array, \@h);
1075 # Output from splice():
1076 # Returned value (munged a bit), error msg, warnings
1078 my ($s_r, $s_error, @s_warnings);
1080 my $gather_warning = sub { push @s_warnings, $_[0] };
1081 if ($context eq 'list') {
1084 local $SIG{__WARN__} = $gather_warning;
1085 @r = splice @array, $offset, $length, @list;
1090 elsif ($context eq 'scalar') {
1093 local $SIG{__WARN__} = $gather_warning;
1094 $r = splice @array, $offset, $length, @list;
1099 elsif ($context eq 'void') {
1101 local $SIG{__WARN__} = $gather_warning;
1102 splice @array, $offset, $length, @list;
1108 die "bad context $context";
1111 foreach ($s_error, @s_warnings) {
1113 s/ at \S+ line \d+\.$//;
1116 # Now do the same for DB_File's version of splice
1117 my ($ms_r, $ms_error, @ms_warnings);
1118 $gather_warning = sub { push @ms_warnings, $_[0] };
1119 if ($context eq 'list') {
1122 local $SIG{__WARN__} = $gather_warning;
1123 @r = splice @h, $offset, $length, @list;
1128 elsif ($context eq 'scalar') {
1131 local $SIG{__WARN__} = $gather_warning;
1132 $r = splice @h, $offset, $length, @list;
1137 elsif ($context eq 'void') {
1139 local $SIG{__WARN__} = $gather_warning;
1140 splice @h, $offset, $length, @list;
1146 die "bad context $context";
1149 foreach ($ms_error, @ms_warnings) {
1151 s/ at \S+ line \d+\.?$//;
1154 return "different errors: '$s_error' vs '$ms_error'"
1155 if $s_error ne $ms_error;
1156 return('different return values: ' . Dumper($s_r) . ' vs ' . Dumper($ms_r))
1157 if list_diff($s_r, $ms_r);
1158 return('different changed list: ' . Dumper(\@array) . ' vs ' . Dumper(\@h))
1159 if list_diff(\@array, \@h);
1161 if ((scalar @s_warnings) != (scalar @ms_warnings)) {
1162 return 'different number of warnings';
1165 while (@s_warnings) {
1166 my $sw = shift @s_warnings;
1167 my $msw = shift @ms_warnings;
1169 if (defined $sw and defined $msw) {
1170 $msw =~ s/ \(.+\)$//;
1171 $msw =~ s/ in splice$// if $] < 5.006;
1173 return "different warning: '$sw' vs '$msw'";
1176 elsif (not defined $sw and not defined $msw) {
1180 return "one warning defined, another undef";
1187 open(TEXT, $tmp) or die "cannot open $tmp: $!";
1188 @h = <TEXT>; normalise @h; chomp @h;
1189 close TEXT or die "cannot close $tmp: $!";
1190 return('list is different when re-read from disk: '
1191 . Dumper(\@array) . ' vs ' . Dumper(\@h))
1192 if list_diff(\@array, \@h);
1194 return undef; # success
1200 # Do two lists differ?
1203 # reference to first list
1204 # reference to second list
1206 # Returns true iff they differ. Only works for lists of (string or
1209 # Surely there is a better way to do this?
1212 die 'usage: list_diff(ref to first list, ref to second list)'
1215 my @a = @$a; my @b = @$b;
1216 return 1 if (scalar @a) != (scalar @b);
1217 for (my $i = 0; $i < @a; $i++) {
1218 my ($ae, $be) = ($a[$i], $b[$i]);
1219 if (defined $ae and defined $be) {
1220 return 1 if $ae ne $be;
1222 elsif (not defined $ae and not defined $be) {
1223 # Two undefined values are 'equal'
1235 # Think up a random ARRAY, OFFSET, LENGTH, LIST, and context.
1236 # ARRAY or LIST might be empty, and OFFSET or LENGTH might be
1237 # undefined. Return a 'test' - a listref of these five things.
1240 die 'usage: rand_test()' if @_;
1241 my @contexts = qw<list scalar void>;
1242 my $context = $contexts[int(rand @contexts)];
1243 return [ rand_list(),
1244 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1245 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1252 die 'usage: rand_list()' if @_;
1255 while (rand() > 0.1 * (scalar @r + 1)) {
1256 push @r, rand_word();
1263 die 'usage: rand_word()' if @_;
1265 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>;
1266 while (rand() > 0.1 * (length($r) + 1)) {
1267 $r .= $chars[int(rand(scalar @chars))];