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 'cygwin' || $^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" ||
292 $x eq "abc\r\ndef\r\n\r\nghi\r\n") ;
299 my $dbh = new DB_File::RECNOINFO ;
301 ok(60, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
306 my $x = docat($Dfile) ;
308 my $ok = ($x eq "abc-def--ghi-") ;
309 bad_one() unless $ok ;
314 # Check R_FIXEDLEN with default bval (space)
317 my $dbh = new DB_File::RECNOINFO ;
318 $dbh->{flags} = R_FIXEDLEN ;
320 ok(62, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
325 my $x = docat($Dfile) ;
327 my $ok = ($x eq "abc def ghi ") ;
328 bad_one() unless $ok ;
333 # Check R_FIXEDLEN with user-defined bval
336 my $dbh = new DB_File::RECNOINFO ;
337 $dbh->{flags} = R_FIXEDLEN ;
340 ok(64, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
345 my $x = docat($Dfile) ;
347 my $ok = ($x eq "abc--def-------ghi--") ;
348 bad_one() unless $ok ;
353 # check that attempting to tie an associative array to a DB_RECNO will fail
355 my $filename = "xyz" ;
357 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
358 ok(66, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
370 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
377 use vars qw( @ISA @EXPORT) ;
382 @EXPORT = @DB_File::EXPORT ;
388 $self->SUPER::STORE($key, $value * 2) ;
394 $self->SUPER::FETCH($key) - 1 ;
401 $self->SUPER::put($key, $value * 3) ;
406 $self->SUPER::get($_[0], $_[1]) ;
414 my $value = $self->FETCH($key) ;
415 return "[[$value]]" ;
423 BEGIN { push @INC, '.'; }
425 main::ok(67, $@ eq "") ;
429 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
432 main::ok(68, $@ eq "") ;
434 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
435 main::ok(69, $@ eq "") ;
436 main::ok(70, $ret == 5) ;
439 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
440 main::ok(71, $@ eq "") ;
441 main::ok(72, $ret == 10) ;
443 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
444 main::ok(73, $@ eq "" ) ;
445 main::ok(74, $ret == 1) ;
447 $ret = eval '$X->A_new_method(1) ' ;
448 main::ok(75, $@ eq "") ;
449 main::ok(76, $ret eq "[[11]]") ;
453 unlink "SubDB.pm", "recno.tmp" ;
462 ok(77, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
467 ok(78, $FA ? $#h == 3 : $self->length() == 4) ;
470 my $x = docat($Dfile) ;
471 ok(79, $x eq "abc\ndef\nghi\njkl\n") ;
473 # $# sets array to same length
474 ok(80, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
478 { $self->STORESIZE(4) }
479 ok(81, $FA ? $#h == 3 : $self->length() == 4) ;
483 ok(82, $x eq "abc\ndef\nghi\njkl\n") ;
485 # $# sets array to bigger
486 ok(83, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
490 { $self->STORESIZE(7) }
491 ok(84, $FA ? $#h == 6 : $self->length() == 7) ;
495 ok(85, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
497 # $# sets array smaller
498 ok(86, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
502 { $self->STORESIZE(3) }
503 ok(87, $FA ? $#h == 2 : $self->length() == 3) ;
507 ok(88, $x eq "abc\ndef\nghi\n") ;
519 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
524 my($fk, $sk, $fv, $sv) = @_ ;
526 $fetch_key eq $fk && $store_key eq $sk &&
527 $fetch_value eq $fv && $store_value eq $sv &&
531 ok(89, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
533 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
534 $db->filter_store_key (sub { $store_key = $_ }) ;
535 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
536 $db->filter_store_value (sub { $store_value = $_ }) ;
542 ok(90, checkOutput( "", 0, "", "joe")) ;
544 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
545 ok(91, $h[0] eq "joe");
547 ok(92, checkOutput( "", 0, "joe", "")) ;
549 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
550 ok(93, $db->FIRSTKEY() == 0) ;
552 ok(94, checkOutput( 0, "", "", "")) ;
554 # replace the filters, but remember the previous set
555 my ($old_fk) = $db->filter_fetch_key
556 (sub { ++ $_ ; $fetch_key = $_ }) ;
557 my ($old_sk) = $db->filter_store_key
558 (sub { $_ *= 2 ; $store_key = $_ }) ;
559 my ($old_fv) = $db->filter_fetch_value
560 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
561 my ($old_sv) = $db->filter_store_value
562 (sub { s/o/x/g; $store_value = $_ }) ;
564 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
567 ok(95, checkOutput( "", 2, "", "Jxe")) ;
569 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
570 ok(96, $h[1] eq "[Jxe]");
572 ok(97, checkOutput( "", 2, "[Jxe]", "")) ;
574 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
575 ok(98, $db->FIRSTKEY() == 1) ;
577 ok(99, checkOutput( 1, "", "", "")) ;
579 # put the original filters back
580 $db->filter_fetch_key ($old_fk);
581 $db->filter_store_key ($old_sk);
582 $db->filter_fetch_value ($old_fv);
583 $db->filter_store_value ($old_sv);
585 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
587 ok(100, checkOutput( "", 0, "", "joe")) ;
589 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
590 ok(101, $h[0] eq "joe");
591 ok(102, checkOutput( "", 0, "joe", "")) ;
593 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
594 ok(103, $db->FIRSTKEY() == 0) ;
595 ok(104, checkOutput( 0, "", "", "")) ;
598 $db->filter_fetch_key (undef);
599 $db->filter_store_key (undef);
600 $db->filter_fetch_value (undef);
601 $db->filter_store_value (undef);
603 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
605 ok(105, checkOutput( "", "", "", "")) ;
607 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
608 ok(106, $h[0] eq "joe");
609 ok(107, checkOutput( "", "", "", "")) ;
611 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
612 ok(108, $db->FIRSTKEY() == 0) ;
613 ok(109, checkOutput( "", "", "", "")) ;
621 # DBM Filter with a closure
628 ok(110, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
638 return sub { ++$count ;
640 $result{$name} = "$name - $count: [@kept]" ;
644 $db->filter_store_key(Closure("store key")) ;
645 $db->filter_store_value(Closure("store value")) ;
646 $db->filter_fetch_key(Closure("fetch key")) ;
647 $db->filter_fetch_value(Closure("fetch value")) ;
652 ok(111, $result{"store key"} eq "store key - 1: [0]");
653 ok(112, $result{"store value"} eq "store value - 1: [joe]");
654 ok(113, ! defined $result{"fetch key"} );
655 ok(114, ! defined $result{"fetch value"} );
656 ok(115, $_ eq "original") ;
658 ok(116, $db->FIRSTKEY() == 0 ) ;
659 ok(117, $result{"store key"} eq "store key - 1: [0]");
660 ok(118, $result{"store value"} eq "store value - 1: [joe]");
661 ok(119, $result{"fetch key"} eq "fetch key - 1: [0]");
662 ok(120, ! defined $result{"fetch value"} );
663 ok(121, $_ eq "original") ;
666 ok(122, $result{"store key"} eq "store key - 2: [0 7]");
667 ok(123, $result{"store value"} eq "store value - 2: [joe john]");
668 ok(124, $result{"fetch key"} eq "fetch key - 1: [0]");
669 ok(125, ! defined $result{"fetch value"} );
670 ok(126, $_ eq "original") ;
672 ok(127, $h[0] eq "joe");
673 ok(128, $result{"store key"} eq "store key - 3: [0 7 0]");
674 ok(129, $result{"store value"} eq "store value - 2: [joe john]");
675 ok(130, $result{"fetch key"} eq "fetch key - 1: [0]");
676 ok(131, $result{"fetch value"} eq "fetch value - 1: [joe]");
677 ok(132, $_ eq "original") ;
685 # DBM Filter recursion detection
691 ok(133, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
693 $db->filter_store_key (sub { $_ = $h[0] }) ;
695 eval '$h[1] = 1234' ;
696 ok(134, $@ =~ /^recursion detected in filter_store_key at/ );
705 # Examples from the POD
709 my $redirect = new Redirect $file ;
711 use warnings FATAL => qw(all);
715 my $filename = "text" ;
719 my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
720 or die "Cannot open file 'text': $!\n" ;
722 # Add a few key/value pairs to the file
727 $FA ? push @h, "green", "black"
728 : $x->push("green", "black") ;
730 my $elements = $FA ? scalar @h : $x->length ;
731 print "The array contains $elements entries\n" ;
733 my $last = $FA ? pop @h : $x->pop ;
734 print "popped $last\n" ;
736 $FA ? unshift @h, "white"
737 : $x->unshift("white") ;
738 my $first = $FA ? shift @h : $x->shift ;
739 print "shifted $first\n" ;
741 # Check for existence of a key
742 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
744 # use a negative index
745 print "The last element is $h[-1]\n" ;
746 print "The 2nd last element is $h[-2]\n" ;
754 ok(135, docat_del($file) eq <<'EOM') ;
755 The array contains 5 entries
758 Element 1 Exists with value blue
759 The last element is green
760 The 2nd last element is yellow
763 my $save_output = "xyzt" ;
765 my $redirect = new Redirect $save_output ;
767 use warnings FATAL => qw(all);
769 use vars qw(@h $H $file $i) ;
777 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
778 or die "Cannot open file $file: $!\n" ;
780 # first create a text file to play with
788 # Print the records in order.
790 # The length method is needed here because evaluating a tied
791 # array in a scalar context does not return the number of
792 # elements in the array.
794 print "\nORIGINAL\n" ;
795 foreach $i (0 .. $H->length - 1) {
796 print "$i: $h[$i]\n" ;
799 # use the push & pop methods
802 print "\nThe last record was [$a]\n" ;
804 # and the shift & unshift methods
806 $H->unshift("first") ;
807 print "The first record was [$a]\n" ;
809 # Use the API to add a new record after record 2.
811 $H->put($i, "Newbie", R_IAFTER) ;
813 # and a new record before record 1.
815 $H->put($i, "New One", R_IBEFORE) ;
820 # now print the records in reverse order
821 print "\nREVERSE\n" ;
822 for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
823 { print "$i: $h[$i]\n" }
825 # same again, but use the API functions instead
826 print "\nREVERSE again\n" ;
827 my ($s, $k, $v) = (0, 0, 0) ;
828 for ($s = $H->seq($k, $v, R_LAST) ;
830 $s = $H->seq($k, $v, R_PREV))
839 ok(136, docat_del($save_output) eq <<'EOM') ;
848 The last record was [four]
849 The first record was [zero]
871 # Bug ID 20001013.009
873 # test that $hash{KEY} = undef doesn't produce the warning
874 # Use of uninitialized value in null operation
882 local $SIG{__WARN__} = sub {$a = $_[0]} ;
884 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
885 or die "Can't open file: $!\n" ;
893 # test that %hash = () doesn't produce the warning
894 # Argument "" isn't numeric in entersub
899 local $SIG{__WARN__} = sub {$a = $_[0]} ;
904 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
905 or die "Can't open file: $!\n" ;
912 # Only test splice if this is a newish version of Perl
917 # These are a few regression tests: bundles of five arguments to pass
918 # to test_splice(). The first four arguments correspond to those
919 # given to splice(), and the last says which context to call it in
920 # (scalar, list or void).
922 # The expected result is not needed because we get that by running
923 # Perl's built-in splice().
925 my @tests = ([ [ 'falsely', 'dinosaur', 'remedy', 'commotion',
926 'rarely', 'paleness' ],
928 [ 'redoubled', 'Taylorize', 'Zoe', 'halogen' ],
931 [ [ 'a' ], -2, 1, [ 'B' ], 'void' ],
933 [ [ 'Hartley', 'Islandia', 'assents', 'wishful' ],
938 [ [ 'visibility', 'pocketful', 'rectangles' ],
945 [ 'Margery', 'clearing', 'repercussion', 'clubs',
949 [ [ 'chastises', 'recalculates' ],
951 [ 'momentariness', 'mediates', 'accents', 'toils',
957 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
962 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
965 [ [ 'riheb' ], -8, undef, [], 'void' ],
967 [ [ 'uft', 'qnxs', '' ],
969 [ 'znp', 'mhnkh', 'bn' ],
975 require POSIX; my $tmp = POSIX::tmpnam();
976 foreach my $test (@tests) {
977 my $err = test_splice(@$test);
979 require Data::Dumper;
980 print STDERR "failed: ", Data::Dumper::Dumper($test);
981 print STDERR "error: $err\n";
985 else { ok($testnum++, 1) }
989 # Not worth running the random ones
990 print STDERR 'skipping ', $testnum++, "\n";
993 # A thousand randomly-generated tests
996 foreach (0 .. 1000 - 1) {
997 my $test = rand_test();
998 my $err = test_splice(@$test);
1000 require Data::Dumper;
1001 print STDERR "failed: ", Data::Dumper::Dumper($test);
1002 print STDERR "error: $err\n";
1004 print STDERR "skipping any remaining random tests\n";
1009 ok($testnum++, not $failed);
1012 die if $testnum != $total_tests + 1;
1016 # Subroutines for SPLICE testing
1020 # Test the new splice() against Perl's built-in one. The first four
1021 # parameters are those passed to splice(), except that the lists must
1022 # be (explicitly) passed by reference, and are not actually modified.
1023 # (It's just a test!) The last argument specifies the context in
1024 # which to call the functions: 'list', 'scalar', or 'void'.
1027 # undef, if the two splices give the same results for the given
1028 # arguments and context;
1030 # an error message showing the difference, otherwise.
1032 # Reads global variable $tmp.
1035 die 'usage: test_splice(array, offset, length, list, context)' if @_ != 5;
1036 my ($array, $offset, $length, $list, $context) = @_;
1037 my @array = @$array;
1040 open(TEXT, ">$tmp") or die "cannot write to $tmp: $!";
1041 foreach (@array) { print TEXT "$_\n" }
1042 close TEXT or die "cannot close $tmp: $!";
1045 my $H = tie @h, 'DB_File', $tmp, O_RDWR, 0644, $DB_RECNO
1046 or die "cannot open $tmp: $!";
1048 return "basic DB_File sanity check failed"
1049 if list_diff(\@array, \@h);
1051 # Output from splice():
1052 # Returned value (munged a bit), error msg, warnings
1054 my ($s_r, $s_error, @s_warnings);
1056 my $gather_warning = sub { push @s_warnings, $_[0] };
1057 if ($context eq 'list') {
1060 local $SIG{__WARN__} = $gather_warning;
1061 @r = splice @array, $offset, $length, @list;
1066 elsif ($context eq 'scalar') {
1069 local $SIG{__WARN__} = $gather_warning;
1070 $r = splice @array, $offset, $length, @list;
1075 elsif ($context eq 'void') {
1077 local $SIG{__WARN__} = $gather_warning;
1078 splice @array, $offset, $length, @list;
1084 die "bad context $context";
1087 foreach ($s_error, @s_warnings) {
1089 s/ at \S+ line \d+\.$//;
1092 # Now do the same for DB_File's version of splice
1093 my ($ms_r, $ms_error, @ms_warnings);
1094 $gather_warning = sub { push @ms_warnings, $_[0] };
1095 if ($context eq 'list') {
1098 local $SIG{__WARN__} = $gather_warning;
1099 @r = splice @h, $offset, $length, @list;
1104 elsif ($context eq 'scalar') {
1107 local $SIG{__WARN__} = $gather_warning;
1108 $r = splice @h, $offset, $length, @list;
1113 elsif ($context eq 'void') {
1115 local $SIG{__WARN__} = $gather_warning;
1116 splice @h, $offset, $length, @list;
1122 die "bad context $context";
1125 foreach ($ms_error, @ms_warnings) {
1127 s/ at \S+ line \d+\.?$//;
1130 return "different errors: '$s_error' vs '$ms_error'"
1131 if $s_error ne $ms_error;
1132 return('different return values: ' . Dumper($s_r) . ' vs ' . Dumper($ms_r))
1133 if list_diff($s_r, $ms_r);
1134 return('different changed list: ' . Dumper(\@array) . ' vs ' . Dumper(\@h))
1135 if list_diff(\@array, \@h);
1137 if ((scalar @s_warnings) != (scalar @ms_warnings)) {
1138 return 'different number of warnings';
1141 while (@s_warnings) {
1142 my $sw = shift @s_warnings;
1143 my $msw = shift @ms_warnings;
1145 if (defined $sw and defined $msw) {
1146 $msw =~ s/ \(.+\)$//;
1147 $msw =~ s/ in splice$// if $] < 5.006;
1149 return "different warning: '$sw' vs '$msw'";
1152 elsif (not defined $sw and not defined $msw) {
1156 return "one warning defined, another undef";
1163 open(TEXT, $tmp) or die "cannot open $tmp: $!";
1164 @h = <TEXT>; chomp @h;
1165 close TEXT or die "cannot close $tmp: $!";
1166 return('list is different when re-read from disk: '
1167 . Dumper(\@array) . ' vs ' . Dumper(\@h))
1168 if list_diff(\@array, \@h);
1170 return undef; # success
1176 # Do two lists differ?
1179 # reference to first list
1180 # reference to second list
1182 # Returns true iff they differ. Only works for lists of (string or
1185 # Surely there is a better way to do this?
1188 die 'usage: list_diff(ref to first list, ref to second list)'
1191 my @a = @$a; my @b = @$b;
1192 return 1 if (scalar @a) != (scalar @b);
1193 for (my $i = 0; $i < @a; $i++) {
1194 my ($ae, $be) = ($a[$i], $b[$i]);
1195 if (defined $ae and defined $be) {
1196 return 1 if $ae ne $be;
1198 elsif (not defined $ae and not defined $be) {
1199 # Two undefined values are 'equal'
1211 # Think up a random ARRAY, OFFSET, LENGTH, LIST, and context.
1212 # ARRAY or LIST might be empty, and OFFSET or LENGTH might be
1213 # undefined. Return a 'test' - a listref of these five things.
1216 die 'usage: rand_test()' if @_;
1217 my @contexts = qw<list scalar void>;
1218 my $context = $contexts[int(rand @contexts)];
1219 return [ rand_list(),
1220 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1221 (rand() < 0.5) ? (int(rand(20)) - 10) : undef,
1228 die 'usage: rand_list()' if @_;
1231 while (rand() > 0.1 * (scalar @r + 1)) {
1232 push @r, rand_word();
1239 die 'usage: rand_word()' if @_;
1241 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>;
1242 while (rand() > 0.1 * (length($r) + 1)) {
1243 $r .= $chars[int(rand(scalar @chars))];