7 unless(grep /blib/, @INC) {
9 @INC = '../lib' if -d '../lib';
16 if(-d "lib" && -f "TEST") {
17 if ($Config{'extensions'} !~ /\bDB_File\b/ ) {
26 use vars qw($dbh $Dfile $bad_ones $FA) ;
28 # full tied array support started in Perl 5.004_57
29 # Double check to see if it is available.
32 sub try::TIEARRAY { bless [], "try" }
33 sub try::FETCHSIZE { $FA = 1 }
46 print "not " unless $result ;
59 my $filename = shift ;
61 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
62 my $real_stdout = select($fh) ;
63 return bless [$fh, $real_stdout ] ;
78 open(CAT,$file) || die "Cannot open $file:$!";
88 my $result = docat($file);
95 print STDERR <<EOM unless $bad_ones++ ;
97 # Some older versions of Berkeley DB version 1 will fail tests 51,
100 # You can safely ignore the errors if you're never going to use the
101 # broken functionality (recno databases with a modified bval).
102 # Otherwise you'll have to upgrade your DB library.
104 # If you want to use Berkeley DB version 1, then 1.85 and 1.86 are the
105 # last versions that were released. Berkeley DB version 2 is continually
106 # being updated -- Check out http://www.sleepycat.com/ for more details.
113 return unless $^O eq 'cygwin' ;
121 local $SIG{__DIE__} ;
122 eval { require Data::Dumper ; import Data::Dumper } ;
126 *Dumper = sub { my $a = shift; return "[ @{ $a } ]" } ;
130 my $splice_tests = 10 + 1; # ten regressions, plus the randoms
131 my $total_tests = 138 ;
132 $total_tests += $splice_tests if $FA ;
133 print "1..$total_tests\n";
135 my $Dfile = "recno.tmp";
140 # Check the interface to RECNOINFO
142 my $dbh = new DB_File::RECNOINFO ;
143 ok(1, ! defined $dbh->{bval}) ;
144 ok(2, ! defined $dbh->{cachesize}) ;
145 ok(3, ! defined $dbh->{psize}) ;
146 ok(4, ! defined $dbh->{flags}) ;
147 ok(5, ! defined $dbh->{lorder}) ;
148 ok(6, ! defined $dbh->{reclen}) ;
149 ok(7, ! defined $dbh->{bfname}) ;
151 $dbh->{bval} = 3000 ;
152 ok(8, $dbh->{bval} == 3000 );
154 $dbh->{cachesize} = 9000 ;
155 ok(9, $dbh->{cachesize} == 9000 );
157 $dbh->{psize} = 400 ;
158 ok(10, $dbh->{psize} == 400 );
161 ok(11, $dbh->{flags} == 65 );
163 $dbh->{lorder} = 123 ;
164 ok(12, $dbh->{lorder} == 123 );
166 $dbh->{reclen} = 1234 ;
167 ok(13, $dbh->{reclen} == 1234 );
169 $dbh->{bfname} = 1234 ;
170 ok(14, $dbh->{bfname} == 1234 );
173 # Check that an invalid entry is caught both for store & fetch
174 eval '$dbh->{fred} = 1234' ;
175 ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
176 eval 'my $q = $dbh->{fred}' ;
177 ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
179 # Now check the interface to RECNOINFO
183 ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
185 my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
187 ok(18, ((stat($Dfile))[2] & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
192 ok(19, ($FA ? @h == 0 : !$l) );
194 my @data = qw( a b c d ever f g h i j k longername m n o p) ;
196 $h[0] = shift @data ;
197 ok(20, $h[0] eq 'a' );
203 unshift (@data, 'a') ;
205 ok(21, defined $h[1] );
206 ok(22, ! defined $h[16] );
207 ok(23, $FA ? @h == @data : $X->length == @data );
210 # Overwrite an entry & check fetch it
212 $data[3] = 'replaced' ;
213 ok(24, $h[3] eq 'replaced' );
216 my @push_data = qw(added to the end) ;
217 ($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
218 push (@data, @push_data) ;
219 ok(25, $h[++$i] eq 'added' );
220 ok(26, $h[++$i] eq 'to' );
221 ok(27, $h[++$i] eq 'the' );
222 ok(28, $h[++$i] eq 'end' );
225 my $popped = pop (@data) ;
226 my $value = ($FA ? pop @h : $X->pop) ;
227 ok(29, $value eq $popped) ;
230 $value = ($FA ? shift @h : $X->shift) ;
231 my $shifted = shift @data ;
232 ok(30, $value eq $shifted );
237 ($FA ? unshift @h,() : $X->unshift) ;
238 ok(31, ($FA ? @h == @data : $X->length == @data ));
240 my @new_data = qw(add this to the start of the array) ;
241 $FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
242 unshift (@data, @new_data) ;
243 ok(32, $FA ? @h == @data : $X->length == @data );
244 ok(33, $h[0] eq "add") ;
245 ok(34, $h[1] eq "this") ;
246 ok(35, $h[2] eq "to") ;
247 ok(36, $h[3] eq "the") ;
248 ok(37, $h[4] eq "start") ;
249 ok(38, $h[5] eq "of") ;
250 ok(39, $h[6] eq "the") ;
251 ok(40, $h[7] eq "array") ;
252 ok(41, $h[8] eq $data[8]) ;
254 # Brief test for SPLICE - more thorough 'soak test' is later.
257 @old = splice(@h, 1, 2, qw(bananas just before));
260 @old = $X->splice(1, 2, qw(bananas just before));
262 ok(42, $h[0] eq "add") ;
263 ok(43, $h[1] eq "bananas") ;
264 ok(44, $h[2] eq "just") ;
265 ok(45, $h[3] eq "before") ;
266 ok(46, $h[4] eq "the") ;
267 ok(47, $h[5] eq "start") ;
268 ok(48, $h[6] eq "of") ;
269 ok(49, $h[7] eq "the") ;
270 ok(50, $h[8] eq "array") ;
271 ok(51, $h[9] eq $data[8]) ;
272 $FA ? splice(@h, 1, 3, @old) : $X->splice(1, 3, @old);
274 # Now both arrays should be identical
280 $ok = 0, last if $_ ne $h[$j ++] ;
284 # Neagtive subscripts
286 # get the last element of the array
287 ok(53, $h[-1] eq $data[-1] );
288 ok(54, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
290 # get the first element using a negative subscript
291 eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
293 ok(56, $h[0] eq "abcd" );
295 # now try to read before the start of the array
296 eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
297 ok(57, $@ =~ '^Modification of non-creatable array value attempted' );
299 # IMPORTANT - $X must be undefined before the untie otherwise the
300 # underlying DB close routine will not get called.
308 # Check bval defaults to \n
311 my $dbh = new DB_File::RECNOINFO ;
312 ok(58, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
317 my $x = docat($Dfile) ;
319 ok(59, $x eq "abc\ndef\n\nghi\n") ;
326 my $dbh = new DB_File::RECNOINFO ;
328 ok(60, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
333 my $x = docat($Dfile) ;
335 my $ok = ($x eq "abc-def--ghi-") ;
336 bad_one() unless $ok ;
341 # Check R_FIXEDLEN with default bval (space)
344 my $dbh = new DB_File::RECNOINFO ;
345 $dbh->{flags} = R_FIXEDLEN ;
347 ok(62, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
352 my $x = docat($Dfile) ;
354 my $ok = ($x eq "abc def ghi ") ;
355 bad_one() unless $ok ;
360 # Check R_FIXEDLEN with user-defined bval
363 my $dbh = new DB_File::RECNOINFO ;
364 $dbh->{flags} = R_FIXEDLEN ;
367 ok(64, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
372 my $x = docat($Dfile) ;
374 my $ok = ($x eq "abc--def-------ghi--") ;
375 bad_one() unless $ok ;
380 # check that attempting to tie an associative array to a DB_RECNO will fail
382 my $filename = "xyz" ;
384 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
385 ok(66, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
397 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
404 use vars qw( @ISA @EXPORT) ;
409 @EXPORT = @DB_File::EXPORT ;
415 $self->SUPER::STORE($key, $value * 2) ;
421 $self->SUPER::FETCH($key) - 1 ;
428 $self->SUPER::put($key, $value * 3) ;
433 $self->SUPER::get($_[0], $_[1]) ;
441 my $value = $self->FETCH($key) ;
442 return "[[$value]]" ;
450 BEGIN { push @INC, '.'; }
452 main::ok(67, $@ eq "") ;
456 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
459 main::ok(68, $@ eq "") ;
461 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
462 main::ok(69, $@ eq "") ;
463 main::ok(70, $ret == 5) ;
466 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
467 main::ok(71, $@ eq "") ;
468 main::ok(72, $ret == 10) ;
470 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
471 main::ok(73, $@ eq "" ) ;
472 main::ok(74, $ret == 1) ;
474 $ret = eval '$X->A_new_method(1) ' ;
475 main::ok(75, $@ eq "") ;
476 main::ok(76, $ret eq "[[11]]") ;
480 unlink "SubDB.pm", "recno.tmp" ;
489 ok(77, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
494 ok(78, $FA ? $#h == 3 : $self->length() == 4) ;
497 my $x = docat($Dfile) ;
498 ok(79, $x eq "abc\ndef\nghi\njkl\n") ;
500 # $# sets array to same length
501 ok(80, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
505 { $self->STORESIZE(4) }
506 ok(81, $FA ? $#h == 3 : $self->length() == 4) ;
510 ok(82, $x eq "abc\ndef\nghi\njkl\n") ;
512 # $# sets array to bigger
513 ok(83, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
517 { $self->STORESIZE(7) }
518 ok(84, $FA ? $#h == 6 : $self->length() == 7) ;
522 ok(85, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
524 # $# sets array smaller
525 ok(86, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
529 { $self->STORESIZE(3) }
530 ok(87, $FA ? $#h == 2 : $self->length() == 3) ;
534 ok(88, $x eq "abc\ndef\nghi\n") ;
546 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
551 my($fk, $sk, $fv, $sv) = @_ ;
553 $fetch_key eq $fk && $store_key eq $sk &&
554 $fetch_value eq $fv && $store_value eq $sv &&
558 ok(89, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
560 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
561 $db->filter_store_key (sub { $store_key = $_ }) ;
562 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
563 $db->filter_store_value (sub { $store_value = $_ }) ;
569 ok(90, checkOutput( "", 0, "", "joe")) ;
571 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
572 ok(91, $h[0] eq "joe");
574 ok(92, checkOutput( "", 0, "joe", "")) ;
576 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
577 ok(93, $db->FIRSTKEY() == 0) ;
579 ok(94, checkOutput( 0, "", "", "")) ;
581 # replace the filters, but remember the previous set
582 my ($old_fk) = $db->filter_fetch_key
583 (sub { ++ $_ ; $fetch_key = $_ }) ;
584 my ($old_sk) = $db->filter_store_key
585 (sub { $_ *= 2 ; $store_key = $_ }) ;
586 my ($old_fv) = $db->filter_fetch_value
587 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
588 my ($old_sv) = $db->filter_store_value
589 (sub { s/o/x/g; $store_value = $_ }) ;
591 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
594 ok(95, checkOutput( "", 2, "", "Jxe")) ;
596 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
597 ok(96, $h[1] eq "[Jxe]");
599 ok(97, checkOutput( "", 2, "[Jxe]", "")) ;
601 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
602 ok(98, $db->FIRSTKEY() == 1) ;
604 ok(99, checkOutput( 1, "", "", "")) ;
606 # put the original filters back
607 $db->filter_fetch_key ($old_fk);
608 $db->filter_store_key ($old_sk);
609 $db->filter_fetch_value ($old_fv);
610 $db->filter_store_value ($old_sv);
612 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
614 ok(100, checkOutput( "", 0, "", "joe")) ;
616 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
617 ok(101, $h[0] eq "joe");
618 ok(102, checkOutput( "", 0, "joe", "")) ;
620 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
621 ok(103, $db->FIRSTKEY() == 0) ;
622 ok(104, checkOutput( 0, "", "", "")) ;
625 $db->filter_fetch_key (undef);
626 $db->filter_store_key (undef);
627 $db->filter_fetch_value (undef);
628 $db->filter_store_value (undef);
630 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
632 ok(105, checkOutput( "", "", "", "")) ;
634 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
635 ok(106, $h[0] eq "joe");
636 ok(107, checkOutput( "", "", "", "")) ;
638 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
639 ok(108, $db->FIRSTKEY() == 0) ;
640 ok(109, checkOutput( "", "", "", "")) ;
648 # DBM Filter with a closure
655 ok(110, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
665 return sub { ++$count ;
667 $result{$name} = "$name - $count: [@kept]" ;
671 $db->filter_store_key(Closure("store key")) ;
672 $db->filter_store_value(Closure("store value")) ;
673 $db->filter_fetch_key(Closure("fetch key")) ;
674 $db->filter_fetch_value(Closure("fetch value")) ;
679 ok(111, $result{"store key"} eq "store key - 1: [0]");
680 ok(112, $result{"store value"} eq "store value - 1: [joe]");
681 ok(113, ! defined $result{"fetch key"} );
682 ok(114, ! defined $result{"fetch value"} );
683 ok(115, $_ eq "original") ;
685 ok(116, $db->FIRSTKEY() == 0 ) ;
686 ok(117, $result{"store key"} eq "store key - 1: [0]");
687 ok(118, $result{"store value"} eq "store value - 1: [joe]");
688 ok(119, $result{"fetch key"} eq "fetch key - 1: [0]");
689 ok(120, ! defined $result{"fetch value"} );
690 ok(121, $_ eq "original") ;
693 ok(122, $result{"store key"} eq "store key - 2: [0 7]");
694 ok(123, $result{"store value"} eq "store value - 2: [joe john]");
695 ok(124, $result{"fetch key"} eq "fetch key - 1: [0]");
696 ok(125, ! defined $result{"fetch value"} );
697 ok(126, $_ eq "original") ;
699 ok(127, $h[0] eq "joe");
700 ok(128, $result{"store key"} eq "store key - 3: [0 7 0]");
701 ok(129, $result{"store value"} eq "store value - 2: [joe john]");
702 ok(130, $result{"fetch key"} eq "fetch key - 1: [0]");
703 ok(131, $result{"fetch value"} eq "fetch value - 1: [joe]");
704 ok(132, $_ eq "original") ;
712 # DBM Filter recursion detection
718 ok(133, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
720 $db->filter_store_key (sub { $_ = $h[0] }) ;
722 eval '$h[1] = 1234' ;
723 ok(134, $@ =~ /^recursion detected in filter_store_key at/ );
732 # Examples from the POD
736 my $redirect = new Redirect $file ;
738 use warnings FATAL => qw(all);
742 my $filename = "text" ;
746 my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
747 or die "Cannot open file 'text': $!\n" ;
749 # Add a few key/value pairs to the file
754 $FA ? push @h, "green", "black"
755 : $x->push("green", "black") ;
757 my $elements = $FA ? scalar @h : $x->length ;
758 print "The array contains $elements entries\n" ;
760 my $last = $FA ? pop @h : $x->pop ;
761 print "popped $last\n" ;
763 $FA ? unshift @h, "white"
764 : $x->unshift("white") ;
765 my $first = $FA ? shift @h : $x->shift ;
766 print "shifted $first\n" ;
768 # Check for existence of a key
769 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
771 # use a negative index
772 print "The last element is $h[-1]\n" ;
773 print "The 2nd last element is $h[-2]\n" ;
781 ok(135, docat_del($file) eq <<'EOM') ;
782 The array contains 5 entries
785 Element 1 Exists with value blue
786 The last element is green
787 The 2nd last element is yellow
790 my $save_output = "xyzt" ;
792 my $redirect = new Redirect $save_output ;
794 use warnings FATAL => qw(all);
796 use vars qw(@h $H $file $i) ;
804 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
805 or die "Cannot open file $file: $!\n" ;
807 # first create a text file to play with
815 # Print the records in order.
817 # The length method is needed here because evaluating a tied
818 # array in a scalar context does not return the number of
819 # elements in the array.
821 print "\nORIGINAL\n" ;
822 foreach $i (0 .. $H->length - 1) {
823 print "$i: $h[$i]\n" ;
826 # use the push & pop methods
829 print "\nThe last record was [$a]\n" ;
831 # and the shift & unshift methods
833 $H->unshift("first") ;
834 print "The first record was [$a]\n" ;
836 # Use the API to add a new record after record 2.
838 $H->put($i, "Newbie", R_IAFTER) ;
840 # and a new record before record 1.
842 $H->put($i, "New One", R_IBEFORE) ;
847 # now print the records in reverse order
848 print "\nREVERSE\n" ;
849 for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
850 { print "$i: $h[$i]\n" }
852 # same again, but use the API functions instead
853 print "\nREVERSE again\n" ;
854 my ($s, $k, $v) = (0, 0, 0) ;
855 for ($s = $H->seq($k, $v, R_LAST) ;
857 $s = $H->seq($k, $v, R_PREV))
866 ok(136, docat_del($save_output) eq <<'EOM') ;
875 The last record was [four]
876 The first record was [zero]
898 # Bug ID 20001013.009
900 # test that $hash{KEY} = undef doesn't produce the warning
901 # Use of uninitialized value in null operation
909 local $SIG{__WARN__} = sub {$a = $_[0]} ;
911 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
912 or die "Can't open file: $!\n" ;
920 # test that %hash = () doesn't produce the warning
921 # Argument "" isn't numeric in entersub
926 local $SIG{__WARN__} = sub {$a = $_[0]} ;
931 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
932 or die "Can't open file: $!\n" ;
939 # Only test splice if this is a newish version of Perl
944 # These are a few regression tests: bundles of five arguments to pass
945 # to test_splice(). The first four arguments correspond to those
946 # given to splice(), and the last says which context to call it in
947 # (scalar, list or void).
949 # The expected result is not needed because we get that by running
950 # Perl's built-in splice().
952 my @tests = ([ [ 'falsely', 'dinosaur', 'remedy', 'commotion',
953 'rarely', 'paleness' ],
955 [ 'redoubled', 'Taylorize', 'Zoe', 'halogen' ],
958 [ [ 'a' ], -2, 1, [ 'B' ], 'void' ],
960 [ [ 'Hartley', 'Islandia', 'assents', 'wishful' ],
965 [ [ 'visibility', 'pocketful', 'rectangles' ],
972 [ 'Margery', 'clearing', 'repercussion', 'clubs',
976 [ [ 'chastises', 'recalculates' ],
978 [ 'momentariness', 'mediates', 'accents', 'toils',
984 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
989 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
992 [ [ 'riheb' ], -8, undef, [], 'void' ],
994 [ [ 'uft', 'qnxs', '' ],
996 [ 'znp', 'mhnkh', 'bn' ],
1002 require POSIX; my $tmp = POSIX::tmpnam();
1003 foreach my $test (@tests) {
1004 my $err = test_splice(@$test);
1006 print STDERR "# failed: ", Dumper($test);
1007 print STDERR "# error: $err\n";
1011 else { ok($testnum++, 1) }
1015 # Not worth running the random ones
1016 print STDERR '# skipping ', $testnum++, "\n";
1019 # A thousand randomly-generated tests
1022 foreach (0 .. 1000 - 1) {
1023 my $test = rand_test();
1024 my $err = test_splice(@$test);
1026 print STDERR "# failed: ", Dumper($test);
1027 print STDERR "# error: $err\n";
1029 print STDERR "# skipping any remaining random tests\n";
1034 ok($testnum++, not $failed);
1037 die if $testnum != $total_tests + 1;
1041 # Subroutines for SPLICE testing
1045 # Test the new splice() against Perl's built-in one. The first four
1046 # parameters are those passed to splice(), except that the lists must
1047 # be (explicitly) passed by reference, and are not actually modified.
1048 # (It's just a test!) The last argument specifies the context in
1049 # which to call the functions: 'list', 'scalar', or 'void'.
1052 # undef, if the two splices give the same results for the given
1053 # arguments and context;
1055 # an error message showing the difference, otherwise.
1057 # Reads global variable $tmp.
1060 die 'usage: test_splice(array, offset, length, list, context)' if @_ != 5;
1061 my ($array, $offset, $length, $list, $context) = @_;
1062 my @array = @$array;
1065 open(TEXT, ">$tmp") or die "cannot write to $tmp: $!";
1066 foreach (@array) { print TEXT "$_\n" }
1067 close TEXT or die "cannot close $tmp: $!";
1070 my $H = tie @h, 'DB_File', $tmp, O_RDWR, 0644, $DB_RECNO
1071 or die "cannot open $tmp: $!";
1073 return "basic DB_File sanity check failed"
1074 if list_diff(\@array, \@h);
1076 # Output from splice():
1077 # Returned value (munged a bit), error msg, warnings
1079 my ($s_r, $s_error, @s_warnings);
1081 my $gather_warning = sub { push @s_warnings, $_[0] };
1082 if ($context eq 'list') {
1085 local $SIG{__WARN__} = $gather_warning;
1086 @r = splice @array, $offset, $length, @list;
1091 elsif ($context eq 'scalar') {
1094 local $SIG{__WARN__} = $gather_warning;
1095 $r = splice @array, $offset, $length, @list;
1100 elsif ($context eq 'void') {
1102 local $SIG{__WARN__} = $gather_warning;
1103 splice @array, $offset, $length, @list;
1109 die "bad context $context";
1112 foreach ($s_error, @s_warnings) {
1114 s/ at \S+ line \d+\.$//;
1117 # Now do the same for DB_File's version of splice
1118 my ($ms_r, $ms_error, @ms_warnings);
1119 $gather_warning = sub { push @ms_warnings, $_[0] };
1120 if ($context eq 'list') {
1123 local $SIG{__WARN__} = $gather_warning;
1124 @r = splice @h, $offset, $length, @list;
1129 elsif ($context eq 'scalar') {
1132 local $SIG{__WARN__} = $gather_warning;
1133 $r = splice @h, $offset, $length, @list;
1138 elsif ($context eq 'void') {
1140 local $SIG{__WARN__} = $gather_warning;
1141 splice @h, $offset, $length, @list;
1147 die "bad context $context";
1150 foreach ($ms_error, @ms_warnings) {
1152 s/ at \S+ line \d+\.?$//;
1155 return "different errors: '$s_error' vs '$ms_error'"
1156 if $s_error ne $ms_error;
1157 return('different return values: ' . Dumper($s_r) . ' vs ' . Dumper($ms_r))
1158 if list_diff($s_r, $ms_r);
1159 return('different changed list: ' . Dumper(\@array) . ' vs ' . Dumper(\@h))
1160 if list_diff(\@array, \@h);
1162 if ((scalar @s_warnings) != (scalar @ms_warnings)) {
1163 return 'different number of warnings';
1166 while (@s_warnings) {
1167 my $sw = shift @s_warnings;
1168 my $msw = shift @ms_warnings;
1170 if (defined $sw and defined $msw) {
1171 $msw =~ s/ \(.+\)$//;
1172 $msw =~ s/ in splice$// if $] < 5.006;
1174 return "different warning: '$sw' vs '$msw'";
1177 elsif (not defined $sw and not defined $msw) {
1181 return "one warning defined, another undef";
1188 open(TEXT, $tmp) or die "cannot open $tmp: $!";
1189 @h = <TEXT>; normalise @h; chomp @h;
1190 close TEXT or die "cannot close $tmp: $!";
1191 return('list is different when re-read from disk: '
1192 . Dumper(\@array) . ' vs ' . Dumper(\@h))
1193 if list_diff(\@array, \@h);
1195 return undef; # success
1201 # Do two lists differ?
1204 # reference to first list
1205 # reference to second list
1207 # Returns true iff they differ. Only works for lists of (string or
1210 # Surely there is a better way to do this?
1213 die 'usage: list_diff(ref to first list, ref to second list)'
1216 my @a = @$a; my @b = @$b;
1217 return 1 if (scalar @a) != (scalar @b);
1218 for (my $i = 0; $i < @a; $i++) {
1219 my ($ae, $be) = ($a[$i], $b[$i]);
1220 if (defined $ae and defined $be) {
1221 return 1 if $ae ne $be;
1223 elsif (not defined $ae and not defined $be) {
1224 # Two undefined values are 'equal'
1236 # Think up a random ARRAY, OFFSET, LENGTH, LIST, and context.
1237 # ARRAY or LIST might be empty, and OFFSET or LENGTH might be
1238 # undefined. Return a 'test' - a listref of these five things.
1241 die 'usage: rand_test()' if @_;
1242 my @contexts = qw<list scalar void>;
1243 my $context = $contexts[int(rand @contexts)];
1244 return [ rand_list(),
1245 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1246 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1253 die 'usage: rand_list()' if @_;
1256 while (rand() > 0.1 * (scalar @r + 1)) {
1257 push @r, rand_word();
1264 die 'usage: rand_word()' if @_;
1266 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>;
1267 while (rand() > 0.1 * (length($r) + 1)) {
1268 $r .= $chars[int(rand(scalar @chars))];