DB_File 1.800
[p5sagit/p5-mst-13.2.git] / ext / DB_File / t / db-recno.t
CommitLineData
f6b705ef 1#!./perl -w
a0d0e21e 2
77fd2717 3BEGIN {
4 unless(grep /blib/, @INC) {
5 chdir 't' if -d 't';
6 @INC = '../lib' if -d '../lib';
7 }
8}
9
bb50757b 10use warnings;
11use strict;
77fd2717 12use Config;
13
a0d0e21e 14BEGIN {
77fd2717 15 if(-d "lib" && -f "TEST") {
16 if ($Config{'extensions'} !~ /\bDB_File\b/ ) {
bb50757b 17 print "1..0 # Skip: DB_File was not built\n";
77fd2717 18 exit 0;
19 }
a0d0e21e 20 }
21}
22
23use DB_File;
24use Fcntl;
07200f1b 25our ($dbh, $Dfile, $bad_ones, $FA);
045291aa 26
27# full tied array support started in Perl 5.004_57
a9fd575d 28# Double check to see if it is available.
29
30{
31 sub try::TIEARRAY { bless [], "try" }
32 sub try::FETCHSIZE { $FA = 1 }
33 $FA = 0 ;
34 my @a ;
35 tie @a, 'try' ;
36 my $a = @a ;
37}
38
a0d0e21e 39
55d68b4a 40sub ok
41{
42 my $no = shift ;
43 my $result = shift ;
a0d0e21e 44
55d68b4a 45 print "not " unless $result ;
46 print "ok $no\n" ;
6250ba0a 47
48 return $result ;
49}
50
9b761c68 51{
52 package Redirect ;
53 use Symbol ;
54
55 sub new
56 {
57 my $class = shift ;
58 my $filename = shift ;
59 my $fh = gensym ;
60 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
61 my $real_stdout = select($fh) ;
62 return bless [$fh, $real_stdout ] ;
63
64 }
65 sub DESTROY
66 {
67 my $self = shift ;
68 close $self->[0] ;
69 select($self->[1]) ;
70 }
71}
72
73sub docat
74{
75 my $file = shift;
76 local $/ = undef;
77 open(CAT,$file) || die "Cannot open $file:$!";
78 my $result = <CAT>;
79 close(CAT);
77fd2717 80 normalise($result) ;
9b761c68 81 return $result;
82}
83
84sub docat_del
85{
86 my $file = shift;
77fd2717 87 my $result = docat($file);
9b761c68 88 unlink $file ;
89 return $result;
90}
91
6250ba0a 92sub bad_one
93{
25268f15 94 print STDERR <<EOM unless $bad_ones++ ;
95#
20896112 96# Some older versions of Berkeley DB version 1 will fail tests 51,
97# 53 and 55.
6250ba0a 98#
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.
102#
20896112 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.
6250ba0a 106#
107EOM
55d68b4a 108}
109
77fd2717 110sub normalise
111{
112 return unless $^O eq 'cygwin' ;
113 foreach (@_)
114 { s#\r\n#\n#g }
115}
116
117BEGIN
118{
119 {
120 local $SIG{__DIE__} ;
121 eval { require Data::Dumper ; import Data::Dumper } ;
122 }
123
124 if ($@) {
125 *Dumper = sub { my $a = shift; return "[ @{ $a } ]" } ;
126 }
127}
128
c6c92ad9 129my $splice_tests = 10 + 1; # ten regressions, plus the randoms
130my $total_tests = 138 ;
131$total_tests += $splice_tests if $FA ;
132print "1..$total_tests\n";
55d68b4a 133
07200f1b 134$Dfile = "recno.tmp";
55d68b4a 135unlink $Dfile ;
a0d0e21e 136
137umask(0);
138
139# Check the interface to RECNOINFO
140
07200f1b 141$dbh = new DB_File::RECNOINFO ;
3fe9a6f1 142ok(1, ! defined $dbh->{bval}) ;
143ok(2, ! defined $dbh->{cachesize}) ;
144ok(3, ! defined $dbh->{psize}) ;
145ok(4, ! defined $dbh->{flags}) ;
146ok(5, ! defined $dbh->{lorder}) ;
147ok(6, ! defined $dbh->{reclen}) ;
148ok(7, ! defined $dbh->{bfname}) ;
a0d0e21e 149
150$dbh->{bval} = 3000 ;
f6b705ef 151ok(8, $dbh->{bval} == 3000 );
a0d0e21e 152
153$dbh->{cachesize} = 9000 ;
f6b705ef 154ok(9, $dbh->{cachesize} == 9000 );
a0d0e21e 155
156$dbh->{psize} = 400 ;
f6b705ef 157ok(10, $dbh->{psize} == 400 );
a0d0e21e 158
159$dbh->{flags} = 65 ;
f6b705ef 160ok(11, $dbh->{flags} == 65 );
a0d0e21e 161
162$dbh->{lorder} = 123 ;
f6b705ef 163ok(12, $dbh->{lorder} == 123 );
a0d0e21e 164
165$dbh->{reclen} = 1234 ;
f6b705ef 166ok(13, $dbh->{reclen} == 1234 );
a0d0e21e 167
168$dbh->{bfname} = 1234 ;
f6b705ef 169ok(14, $dbh->{bfname} == 1234 );
a0d0e21e 170
171
172# Check that an invalid entry is caught both for store & fetch
173eval '$dbh->{fred} = 1234' ;
f6b705ef 174ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
55d68b4a 175eval 'my $q = $dbh->{fred}' ;
f6b705ef 176ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
a0d0e21e 177
178# Now check the interface to RECNOINFO
179
55d68b4a 180my $X ;
181my @h ;
182ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
a0d0e21e 183
77fd2717 184my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
185
d536870a 186ok(18, ((stat($Dfile))[2] & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
77fd2717 187 || $noMode{$^O} );
a0d0e21e 188
55d68b4a 189#my $l = @h ;
190my $l = $X->length ;
045291aa 191ok(19, ($FA ? @h == 0 : !$l) );
a0d0e21e 192
55d68b4a 193my @data = qw( a b c d ever f g h i j k longername m n o p) ;
a0d0e21e 194
195$h[0] = shift @data ;
f6b705ef 196ok(20, $h[0] eq 'a' );
a0d0e21e 197
55d68b4a 198my $ i;
a0d0e21e 199foreach (@data)
200 { $h[++$i] = $_ }
201
202unshift (@data, 'a') ;
203
f6b705ef 204ok(21, defined $h[1] );
205ok(22, ! defined $h[16] );
045291aa 206ok(23, $FA ? @h == @data : $X->length == @data );
a0d0e21e 207
208
209# Overwrite an entry & check fetch it
210$h[3] = 'replaced' ;
211$data[3] = 'replaced' ;
f6b705ef 212ok(24, $h[3] eq 'replaced' );
a0d0e21e 213
214#PUSH
55d68b4a 215my @push_data = qw(added to the end) ;
045291aa 216($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
a0d0e21e 217push (@data, @push_data) ;
f6b705ef 218ok(25, $h[++$i] eq 'added' );
219ok(26, $h[++$i] eq 'to' );
220ok(27, $h[++$i] eq 'the' );
221ok(28, $h[++$i] eq 'end' );
a0d0e21e 222
223# POP
f6b705ef 224my $popped = pop (@data) ;
045291aa 225my $value = ($FA ? pop @h : $X->pop) ;
f6b705ef 226ok(29, $value eq $popped) ;
a0d0e21e 227
228# SHIFT
045291aa 229$value = ($FA ? shift @h : $X->shift) ;
f6b705ef 230my $shifted = shift @data ;
231ok(30, $value eq $shifted );
a0d0e21e 232
233# UNSHIFT
234
235# empty list
936edb8b 236($FA ? unshift @h,() : $X->unshift) ;
045291aa 237ok(31, ($FA ? @h == @data : $X->length == @data ));
a0d0e21e 238
55d68b4a 239my @new_data = qw(add this to the start of the array) ;
045291aa 240$FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
a0d0e21e 241unshift (@data, @new_data) ;
045291aa 242ok(32, $FA ? @h == @data : $X->length == @data );
f6b705ef 243ok(33, $h[0] eq "add") ;
244ok(34, $h[1] eq "this") ;
245ok(35, $h[2] eq "to") ;
246ok(36, $h[3] eq "the") ;
247ok(37, $h[4] eq "start") ;
248ok(38, $h[5] eq "of") ;
249ok(39, $h[6] eq "the") ;
250ok(40, $h[7] eq "array") ;
251ok(41, $h[8] eq $data[8]) ;
a0d0e21e 252
c6c92ad9 253# Brief test for SPLICE - more thorough 'soak test' is later.
254my @old;
255if ($FA) {
256 @old = splice(@h, 1, 2, qw(bananas just before));
257}
258else {
259 @old = $X->splice(1, 2, qw(bananas just before));
260}
261ok(42, $h[0] eq "add") ;
262ok(43, $h[1] eq "bananas") ;
263ok(44, $h[2] eq "just") ;
264ok(45, $h[3] eq "before") ;
265ok(46, $h[4] eq "the") ;
266ok(47, $h[5] eq "start") ;
267ok(48, $h[6] eq "of") ;
268ok(49, $h[7] eq "the") ;
269ok(50, $h[8] eq "array") ;
270ok(51, $h[9] eq $data[8]) ;
271$FA ? splice(@h, 1, 3, @old) : $X->splice(1, 3, @old);
a0d0e21e 272
273# Now both arrays should be identical
274
55d68b4a 275my $ok = 1 ;
276my $j = 0 ;
a0d0e21e 277foreach (@data)
278{
279 $ok = 0, last if $_ ne $h[$j ++] ;
280}
c6c92ad9 281ok(52, $ok );
a0d0e21e 282
55d68b4a 283# Neagtive subscripts
284
285# get the last element of the array
c6c92ad9 286ok(53, $h[-1] eq $data[-1] );
287ok(54, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
55d68b4a 288
289# get the first element using a negative subscript
045291aa 290eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
c6c92ad9 291ok(55, $@ eq "" );
292ok(56, $h[0] eq "abcd" );
55d68b4a 293
294# now try to read before the start of the array
045291aa 295eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
c6c92ad9 296ok(57, $@ =~ '^Modification of non-creatable array value attempted' );
55d68b4a 297
a0d0e21e 298# IMPORTANT - $X must be undefined before the untie otherwise the
299# underlying DB close routine will not get called.
300undef $X ;
301untie(@h);
302
303unlink $Dfile;
304
a6ed719b 305
36477c24 306{
307 # Check bval defaults to \n
308
309 my @h = () ;
310 my $dbh = new DB_File::RECNOINFO ;
c6c92ad9 311 ok(58, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
36477c24 312 $h[0] = "abc" ;
313 $h[1] = "def" ;
314 $h[3] = "ghi" ;
315 untie @h ;
a6ed719b 316 my $x = docat($Dfile) ;
36477c24 317 unlink $Dfile;
77fd2717 318 ok(59, $x eq "abc\ndef\n\nghi\n") ;
36477c24 319}
320
321{
322 # Change bval
323
324 my @h = () ;
325 my $dbh = new DB_File::RECNOINFO ;
326 $dbh->{bval} = "-" ;
c6c92ad9 327 ok(60, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
36477c24 328 $h[0] = "abc" ;
329 $h[1] = "def" ;
330 $h[3] = "ghi" ;
331 untie @h ;
a6ed719b 332 my $x = docat($Dfile) ;
36477c24 333 unlink $Dfile;
6250ba0a 334 my $ok = ($x eq "abc-def--ghi-") ;
335 bad_one() unless $ok ;
c6c92ad9 336 ok(61, $ok) ;
36477c24 337}
338
339{
340 # Check R_FIXEDLEN with default bval (space)
341
342 my @h = () ;
343 my $dbh = new DB_File::RECNOINFO ;
344 $dbh->{flags} = R_FIXEDLEN ;
345 $dbh->{reclen} = 5 ;
c6c92ad9 346 ok(62, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
36477c24 347 $h[0] = "abc" ;
348 $h[1] = "def" ;
349 $h[3] = "ghi" ;
350 untie @h ;
a6ed719b 351 my $x = docat($Dfile) ;
36477c24 352 unlink $Dfile;
6250ba0a 353 my $ok = ($x eq "abc def ghi ") ;
354 bad_one() unless $ok ;
c6c92ad9 355 ok(63, $ok) ;
36477c24 356}
357
358{
359 # Check R_FIXEDLEN with user-defined bval
360
361 my @h = () ;
362 my $dbh = new DB_File::RECNOINFO ;
363 $dbh->{flags} = R_FIXEDLEN ;
364 $dbh->{bval} = "-" ;
365 $dbh->{reclen} = 5 ;
c6c92ad9 366 ok(64, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
36477c24 367 $h[0] = "abc" ;
368 $h[1] = "def" ;
369 $h[3] = "ghi" ;
370 untie @h ;
a6ed719b 371 my $x = docat($Dfile) ;
36477c24 372 unlink $Dfile;
6250ba0a 373 my $ok = ($x eq "abc--def-------ghi--") ;
374 bad_one() unless $ok ;
c6c92ad9 375 ok(65, $ok) ;
36477c24 376}
377
05475680 378{
379 # check that attempting to tie an associative array to a DB_RECNO will fail
380
381 my $filename = "xyz" ;
382 my %x ;
383 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
c6c92ad9 384 ok(66, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
05475680 385 unlink $filename ;
386}
387
a6ed719b 388{
389 # sub-class test
390
391 package Another ;
392
3245f058 393 use warnings ;
a6ed719b 394 use strict ;
395
396 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
397 print FILE <<'EOM' ;
398
399 package SubDB ;
400
3245f058 401 use warnings ;
a6ed719b 402 use strict ;
07200f1b 403 our (@ISA, @EXPORT);
a6ed719b 404
405 require Exporter ;
406 use DB_File;
407 @ISA=qw(DB_File);
408 @EXPORT = @DB_File::EXPORT ;
409
410 sub STORE {
411 my $self = shift ;
412 my $key = shift ;
413 my $value = shift ;
414 $self->SUPER::STORE($key, $value * 2) ;
415 }
416
417 sub FETCH {
418 my $self = shift ;
419 my $key = shift ;
420 $self->SUPER::FETCH($key) - 1 ;
421 }
422
423 sub put {
424 my $self = shift ;
425 my $key = shift ;
426 my $value = shift ;
427 $self->SUPER::put($key, $value * 3) ;
428 }
429
430 sub get {
431 my $self = shift ;
432 $self->SUPER::get($_[0], $_[1]) ;
433 $_[1] -= 2 ;
434 }
435
436 sub A_new_method
437 {
438 my $self = shift ;
439 my $key = shift ;
440 my $value = $self->FETCH($key) ;
441 return "[[$value]]" ;
442 }
443
444 1 ;
445EOM
446
447 close FILE ;
448
045291aa 449 BEGIN { push @INC, '.'; }
a6ed719b 450 eval 'use SubDB ; ';
c6c92ad9 451 main::ok(67, $@ eq "") ;
a6ed719b 452 my @h ;
453 my $X ;
454 eval '
455 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
456 ' ;
457
c6c92ad9 458 main::ok(68, $@ eq "") ;
a6ed719b 459
460 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
c6c92ad9 461 main::ok(69, $@ eq "") ;
462 main::ok(70, $ret == 5) ;
a6ed719b 463
464 my $value = 0;
465 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
c6c92ad9 466 main::ok(71, $@ eq "") ;
467 main::ok(72, $ret == 10) ;
a6ed719b 468
469 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
c6c92ad9 470 main::ok(73, $@ eq "" ) ;
471 main::ok(74, $ret == 1) ;
a6ed719b 472
473 $ret = eval '$X->A_new_method(1) ' ;
c6c92ad9 474 main::ok(75, $@ eq "") ;
475 main::ok(76, $ret eq "[[11]]") ;
a6ed719b 476
fac76ed7 477 undef $X;
478 untie(@h);
a6ed719b 479 unlink "SubDB.pm", "recno.tmp" ;
480
481}
482
045291aa 483{
484
485 # test $#
486 my $self ;
487 unlink $Dfile;
c6c92ad9 488 ok(77, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
045291aa 489 $h[0] = "abc" ;
490 $h[1] = "def" ;
491 $h[2] = "ghi" ;
492 $h[3] = "jkl" ;
c6c92ad9 493 ok(78, $FA ? $#h == 3 : $self->length() == 4) ;
045291aa 494 undef $self ;
495 untie @h ;
496 my $x = docat($Dfile) ;
c6c92ad9 497 ok(79, $x eq "abc\ndef\nghi\njkl\n") ;
045291aa 498
499 # $# sets array to same length
c6c92ad9 500 ok(80, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
045291aa 501 if ($FA)
502 { $#h = 3 }
503 else
504 { $self->STORESIZE(4) }
c6c92ad9 505 ok(81, $FA ? $#h == 3 : $self->length() == 4) ;
045291aa 506 undef $self ;
507 untie @h ;
508 $x = docat($Dfile) ;
c6c92ad9 509 ok(82, $x eq "abc\ndef\nghi\njkl\n") ;
045291aa 510
511 # $# sets array to bigger
c6c92ad9 512 ok(83, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
045291aa 513 if ($FA)
514 { $#h = 6 }
515 else
516 { $self->STORESIZE(7) }
c6c92ad9 517 ok(84, $FA ? $#h == 6 : $self->length() == 7) ;
045291aa 518 undef $self ;
519 untie @h ;
520 $x = docat($Dfile) ;
c6c92ad9 521 ok(85, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
045291aa 522
523 # $# sets array smaller
c6c92ad9 524 ok(86, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
045291aa 525 if ($FA)
526 { $#h = 2 }
527 else
528 { $self->STORESIZE(3) }
c6c92ad9 529 ok(87, $FA ? $#h == 2 : $self->length() == 3) ;
045291aa 530 undef $self ;
531 untie @h ;
532 $x = docat($Dfile) ;
c6c92ad9 533 ok(88, $x eq "abc\ndef\nghi\n") ;
045291aa 534
535 unlink $Dfile;
536
537
538}
539
9fe6733a 540{
541 # DBM Filter tests
3245f058 542 use warnings ;
9fe6733a 543 use strict ;
544 my (@h, $db) ;
545 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
546 unlink $Dfile;
547
548 sub checkOutput
549 {
550 my($fk, $sk, $fv, $sv) = @_ ;
551 return
552 $fetch_key eq $fk && $store_key eq $sk &&
553 $fetch_value eq $fv && $store_value eq $sv &&
554 $_ eq 'original' ;
555 }
556
c6c92ad9 557 ok(89, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
9fe6733a 558
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 = $_ }) ;
563
564 $_ = "original" ;
565
566 $h[0] = "joe" ;
567 # fk sk fv sv
c6c92ad9 568 ok(90, checkOutput( "", 0, "", "joe")) ;
9fe6733a 569
570 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
c6c92ad9 571 ok(91, $h[0] eq "joe");
9fe6733a 572 # fk sk fv sv
c6c92ad9 573 ok(92, checkOutput( "", 0, "joe", "")) ;
9fe6733a 574
575 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
c6c92ad9 576 ok(93, $db->FIRSTKEY() == 0) ;
9fe6733a 577 # fk sk fv sv
c6c92ad9 578 ok(94, checkOutput( 0, "", "", "")) ;
9fe6733a 579
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 = $_ }) ;
589
590 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
591 $h[1] = "Joe" ;
592 # fk sk fv sv
c6c92ad9 593 ok(95, checkOutput( "", 2, "", "Jxe")) ;
9fe6733a 594
595 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
c6c92ad9 596 ok(96, $h[1] eq "[Jxe]");
9fe6733a 597 # fk sk fv sv
c6c92ad9 598 ok(97, checkOutput( "", 2, "[Jxe]", "")) ;
9fe6733a 599
600 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
c6c92ad9 601 ok(98, $db->FIRSTKEY() == 1) ;
9fe6733a 602 # fk sk fv sv
c6c92ad9 603 ok(99, checkOutput( 1, "", "", "")) ;
9fe6733a 604
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);
610
611 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
612 $h[0] = "joe" ;
c6c92ad9 613 ok(100, checkOutput( "", 0, "", "joe")) ;
9fe6733a 614
615 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
c6c92ad9 616 ok(101, $h[0] eq "joe");
617 ok(102, checkOutput( "", 0, "joe", "")) ;
9fe6733a 618
619 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
c6c92ad9 620 ok(103, $db->FIRSTKEY() == 0) ;
621 ok(104, checkOutput( 0, "", "", "")) ;
9fe6733a 622
623 # delete the filters
624 $db->filter_fetch_key (undef);
625 $db->filter_store_key (undef);
626 $db->filter_fetch_value (undef);
627 $db->filter_store_value (undef);
628
629 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
630 $h[0] = "joe" ;
c6c92ad9 631 ok(105, checkOutput( "", "", "", "")) ;
9fe6733a 632
633 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
c6c92ad9 634 ok(106, $h[0] eq "joe");
635 ok(107, checkOutput( "", "", "", "")) ;
9fe6733a 636
637 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
c6c92ad9 638 ok(108, $db->FIRSTKEY() == 0) ;
639 ok(109, checkOutput( "", "", "", "")) ;
9fe6733a 640
641 undef $db ;
642 untie @h;
643 unlink $Dfile;
644}
645
646{
647 # DBM Filter with a closure
648
3245f058 649 use warnings ;
9fe6733a 650 use strict ;
651 my (@h, $db) ;
652
653 unlink $Dfile;
c6c92ad9 654 ok(110, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
9fe6733a 655
656 my %result = () ;
657
658 sub Closure
659 {
660 my ($name) = @_ ;
661 my $count = 0 ;
662 my @kept = () ;
663
664 return sub { ++$count ;
665 push @kept, $_ ;
666 $result{$name} = "$name - $count: [@kept]" ;
667 }
668 }
669
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")) ;
674
675 $_ = "original" ;
676
677 $h[0] = "joe" ;
c6c92ad9 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") ;
683
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") ;
9fe6733a 690
691 $h[7] = "john" ;
c6c92ad9 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") ;
697
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") ;
9fe6733a 704
705 undef $db ;
706 untie @h;
707 unlink $Dfile;
708}
709
710{
711 # DBM Filter recursion detection
3245f058 712 use warnings ;
9fe6733a 713 use strict ;
714 my (@h, $db) ;
715 unlink $Dfile;
716
c6c92ad9 717 ok(133, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
9fe6733a 718
719 $db->filter_store_key (sub { $_ = $h[0] }) ;
720
721 eval '$h[1] = 1234' ;
c6c92ad9 722 ok(134, $@ =~ /^recursion detected in filter_store_key at/ );
9fe6733a 723
724 undef $db ;
725 untie @h;
726 unlink $Dfile;
727}
728
9b761c68 729
730{
731 # Examples from the POD
732
733 my $file = "xyzt" ;
734 {
735 my $redirect = new Redirect $file ;
736
3245f058 737 use warnings FATAL => qw(all);
9b761c68 738 use strict ;
739 use DB_File ;
740
741 my $filename = "text" ;
742 unlink $filename ;
743
744 my @h ;
745 my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
746 or die "Cannot open file 'text': $!\n" ;
747
748 # Add a few key/value pairs to the file
749 $h[0] = "orange" ;
750 $h[1] = "blue" ;
751 $h[2] = "yellow" ;
752
753 $FA ? push @h, "green", "black"
754 : $x->push("green", "black") ;
755
756 my $elements = $FA ? scalar @h : $x->length ;
757 print "The array contains $elements entries\n" ;
758
759 my $last = $FA ? pop @h : $x->pop ;
760 print "popped $last\n" ;
761
762 $FA ? unshift @h, "white"
763 : $x->unshift("white") ;
764 my $first = $FA ? shift @h : $x->shift ;
765 print "shifted $first\n" ;
766
767 # Check for existence of a key
768 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
769
770 # use a negative index
771 print "The last element is $h[-1]\n" ;
772 print "The 2nd last element is $h[-2]\n" ;
773
774 undef $x ;
775 untie @h ;
776
777 unlink $filename ;
778 }
779
c6c92ad9 780 ok(135, docat_del($file) eq <<'EOM') ;
9b761c68 781The array contains 5 entries
782popped black
783shifted white
784Element 1 Exists with value blue
785The last element is green
786The 2nd last element is yellow
787EOM
788
789 my $save_output = "xyzt" ;
790 {
791 my $redirect = new Redirect $save_output ;
792
3245f058 793 use warnings FATAL => qw(all);
9b761c68 794 use strict ;
07200f1b 795 our (@h, $H, $file, $i);
9b761c68 796 use DB_File ;
797 use Fcntl ;
798
799 $file = "text" ;
800
801 unlink $file ;
802
803 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
804 or die "Cannot open file $file: $!\n" ;
805
806 # first create a text file to play with
807 $h[0] = "zero" ;
808 $h[1] = "one" ;
809 $h[2] = "two" ;
810 $h[3] = "three" ;
811 $h[4] = "four" ;
812
813
814 # Print the records in order.
815 #
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.
819
820 print "\nORIGINAL\n" ;
821 foreach $i (0 .. $H->length - 1) {
822 print "$i: $h[$i]\n" ;
823 }
824
825 # use the push & pop methods
826 $a = $H->pop ;
827 $H->push("last") ;
828 print "\nThe last record was [$a]\n" ;
829
830 # and the shift & unshift methods
831 $a = $H->shift ;
832 $H->unshift("first") ;
833 print "The first record was [$a]\n" ;
834
835 # Use the API to add a new record after record 2.
836 $i = 2 ;
837 $H->put($i, "Newbie", R_IAFTER) ;
838
839 # and a new record before record 1.
840 $i = 1 ;
841 $H->put($i, "New One", R_IBEFORE) ;
842
843 # delete record 3
844 $H->del(3) ;
845
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" }
850
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) ;
855 $s == 0 ;
856 $s = $H->seq($k, $v, R_PREV))
857 { print "$k: $v\n" }
858
859 undef $H ;
860 untie @h ;
861
862 unlink $file ;
863 }
864
c6c92ad9 865 ok(136, docat_del($save_output) eq <<'EOM') ;
9b761c68 866
867ORIGINAL
8680: zero
8691: one
8702: two
8713: three
8724: four
873
874The last record was [four]
875The first record was [zero]
876
877REVERSE
8785: last
8794: three
8803: Newbie
8812: one
8821: New One
8830: first
884
885REVERSE again
8865: last
8874: three
8883: Newbie
8892: one
8901: New One
8910: first
892EOM
893
894}
895
cbc5248d 896{
897 # Bug ID 20001013.009
898 #
899 # test that $hash{KEY} = undef doesn't produce the warning
900 # Use of uninitialized value in null operation
901 use warnings ;
902 use strict ;
903 use DB_File ;
904
905 unlink $Dfile;
906 my @h ;
907 my $a = "";
908 local $SIG{__WARN__} = sub {$a = $_[0]} ;
909
3245f058 910 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
cbc5248d 911 or die "Can't open file: $!\n" ;
912 $h[0] = undef;
c6c92ad9 913 ok(137, $a eq "") ;
3245f058 914 untie @h ;
915 unlink $Dfile;
916}
917
918{
919 # test that %hash = () doesn't produce the warning
920 # Argument "" isn't numeric in entersub
921 use warnings ;
922 use strict ;
923 use DB_File ;
924 my $a = "";
925 local $SIG{__WARN__} = sub {$a = $_[0]} ;
926
927 unlink $Dfile;
928 my @h ;
929
930 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
931 or die "Can't open file: $!\n" ;
932 @h = (); ;
c6c92ad9 933 ok(138, $a eq "") ;
3245f058 934 untie @h ;
cbc5248d 935 unlink $Dfile;
936}
937
c6c92ad9 938# Only test splice if this is a newish version of Perl
939exit unless $FA ;
940
941# Test SPLICE
942#
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).
947#
948# The expected result is not needed because we get that by running
949# Perl's built-in splice().
950#
951my @tests = ([ [ 'falsely', 'dinosaur', 'remedy', 'commotion',
952 'rarely', 'paleness' ],
953 -4, -2,
954 [ 'redoubled', 'Taylorize', 'Zoe', 'halogen' ],
955 'void' ],
956
957 [ [ 'a' ], -2, 1, [ 'B' ], 'void' ],
958
959 [ [ 'Hartley', 'Islandia', 'assents', 'wishful' ],
960 0, -4,
961 [ 'maids' ],
962 'void' ],
963
964 [ [ 'visibility', 'pocketful', 'rectangles' ],
965 -10, 0,
966 [ 'garbages' ],
967 'void' ],
968
969 [ [ 'sleeplessly' ],
970 8, -4,
971 [ 'Margery', 'clearing', 'repercussion', 'clubs',
972 'arise' ],
973 'void' ],
974
975 [ [ 'chastises', 'recalculates' ],
976 0, 0,
977 [ 'momentariness', 'mediates', 'accents', 'toils',
978 'regaled' ],
979 'void' ],
980
981 [ [ 'b', '' ],
982 9, 8,
983 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
984 'scalar' ],
985
986 [ [ 'b', '' ],
987 undef, undef,
988 [ 'otrb', 'stje', 'ixrpw', 'vxfx', 'lhhf' ],
989 'scalar' ],
990
991 [ [ 'riheb' ], -8, undef, [], 'void' ],
992
993 [ [ 'uft', 'qnxs', '' ],
994 6, -2,
995 [ 'znp', 'mhnkh', 'bn' ],
996 'void' ],
997 );
998
999my $testnum = 139;
1000my $failed = 0;
1001require POSIX; my $tmp = POSIX::tmpnam();
1002foreach my $test (@tests) {
1003 my $err = test_splice(@$test);
1004 if (defined $err) {
77fd2717 1005 print STDERR "# failed: ", Dumper($test);
1006 print STDERR "# error: $err\n";
c6c92ad9 1007 $failed = 1;
1008 ok($testnum++, 0);
1009 }
1010 else { ok($testnum++, 1) }
1011}
1012
1013if ($failed) {
1014 # Not worth running the random ones
77fd2717 1015 print STDERR '# skipping ', $testnum++, "\n";
c6c92ad9 1016}
1017else {
1018 # A thousand randomly-generated tests
1019 $failed = 0;
1020 srand(0);
1021 foreach (0 .. 1000 - 1) {
1022 my $test = rand_test();
1023 my $err = test_splice(@$test);
1024 if (defined $err) {
77fd2717 1025 print STDERR "# failed: ", Dumper($test);
1026 print STDERR "# error: $err\n";
c6c92ad9 1027 $failed = 1;
77fd2717 1028 print STDERR "# skipping any remaining random tests\n";
c6c92ad9 1029 last;
1030 }
1031 }
1032
1033 ok($testnum++, not $failed);
1034}
1035
1036die if $testnum != $total_tests + 1;
1037
a0d0e21e 1038exit ;
c6c92ad9 1039
1040# Subroutines for SPLICE testing
1041
1042# test_splice()
1043#
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'.
1049#
1050# Returns:
1051# undef, if the two splices give the same results for the given
1052# arguments and context;
1053#
1054# an error message showing the difference, otherwise.
1055#
1056# Reads global variable $tmp.
1057#
1058sub test_splice {
1059 die 'usage: test_splice(array, offset, length, list, context)' if @_ != 5;
1060 my ($array, $offset, $length, $list, $context) = @_;
1061 my @array = @$array;
1062 my @list = @$list;
1063
a109f988 1064 unlink $tmp;
c6c92ad9 1065
1066 my @h;
a109f988 1067 my $H = tie @h, 'DB_File', $tmp, O_CREAT|O_RDWR, 0644, $DB_RECNO
c6c92ad9 1068 or die "cannot open $tmp: $!";
a109f988 1069
1070 my $i = 0;
1071 foreach ( @array ) { $h[$i++] = $_ }
c6c92ad9 1072
1073 return "basic DB_File sanity check failed"
1074 if list_diff(\@array, \@h);
1075
1076 # Output from splice():
1077 # Returned value (munged a bit), error msg, warnings
1078 #
1079 my ($s_r, $s_error, @s_warnings);
1080
1081 my $gather_warning = sub { push @s_warnings, $_[0] };
1082 if ($context eq 'list') {
1083 my @r;
1084 eval {
1085 local $SIG{__WARN__} = $gather_warning;
1086 @r = splice @array, $offset, $length, @list;
1087 };
1088 $s_error = $@;
1089 $s_r = \@r;
1090 }
1091 elsif ($context eq 'scalar') {
1092 my $r;
1093 eval {
1094 local $SIG{__WARN__} = $gather_warning;
1095 $r = splice @array, $offset, $length, @list;
1096 };
1097 $s_error = $@;
1098 $s_r = [ $r ];
1099 }
1100 elsif ($context eq 'void') {
1101 eval {
1102 local $SIG{__WARN__} = $gather_warning;
1103 splice @array, $offset, $length, @list;
1104 };
1105 $s_error = $@;
1106 $s_r = [];
1107 }
1108 else {
1109 die "bad context $context";
1110 }
1111
1112 foreach ($s_error, @s_warnings) {
1113 chomp;
1114 s/ at \S+ line \d+\.$//;
1115 }
1116
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') {
1121 my @r;
1122 eval {
1123 local $SIG{__WARN__} = $gather_warning;
1124 @r = splice @h, $offset, $length, @list;
1125 };
1126 $ms_error = $@;
1127 $ms_r = \@r;
1128 }
1129 elsif ($context eq 'scalar') {
1130 my $r;
1131 eval {
1132 local $SIG{__WARN__} = $gather_warning;
1133 $r = splice @h, $offset, $length, @list;
1134 };
1135 $ms_error = $@;
1136 $ms_r = [ $r ];
1137 }
1138 elsif ($context eq 'void') {
1139 eval {
1140 local $SIG{__WARN__} = $gather_warning;
1141 splice @h, $offset, $length, @list;
1142 };
1143 $ms_error = $@;
1144 $ms_r = [];
1145 }
1146 else {
1147 die "bad context $context";
1148 }
1149
1150 foreach ($ms_error, @ms_warnings) {
1151 chomp;
1152 s/ at \S+ line \d+\.?$//;
1153 }
1154
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);
1161
1162 if ((scalar @s_warnings) != (scalar @ms_warnings)) {
1163 return 'different number of warnings';
1164 }
1165
1166 while (@s_warnings) {
1167 my $sw = shift @s_warnings;
1168 my $msw = shift @ms_warnings;
1169
1170 if (defined $sw and defined $msw) {
1171 $msw =~ s/ \(.+\)$//;
1172 $msw =~ s/ in splice$// if $] < 5.006;
1173 if ($sw ne $msw) {
1174 return "different warning: '$sw' vs '$msw'";
1175 }
1176 }
1177 elsif (not defined $sw and not defined $msw) {
1178 # Okay.
1179 }
1180 else {
1181 return "one warning defined, another undef";
1182 }
1183 }
1184
1185 undef $H;
1186 untie @h;
1187
1188 open(TEXT, $tmp) or die "cannot open $tmp: $!";
77fd2717 1189 @h = <TEXT>; normalise @h; chomp @h;
c6c92ad9 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);
1194
1195 return undef; # success
1196}
1197
1198
1199# list_diff()
1200#
1201# Do two lists differ?
1202#
1203# Parameters:
1204# reference to first list
1205# reference to second list
1206#
1207# Returns true iff they differ. Only works for lists of (string or
1208# undef).
1209#
1210# Surely there is a better way to do this?
1211#
1212sub list_diff {
1213 die 'usage: list_diff(ref to first list, ref to second list)'
1214 if @_ != 2;
1215 my ($a, $b) = @_;
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;
1222 }
1223 elsif (not defined $ae and not defined $be) {
1224 # Two undefined values are 'equal'
1225 }
1226 else {
1227 return 1;
1228 }
1229 }
1230 return 0;
1231}
1232
1233
1234# rand_test()
1235#
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.
1239#
1240sub rand_test {
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,
1247 rand_list(),
1248 $context ];
1249}
1250
1251
1252sub rand_list {
1253 die 'usage: rand_list()' if @_;
1254 my @r;
1255
1256 while (rand() > 0.1 * (scalar @r + 1)) {
1257 push @r, rand_word();
1258 }
1259 return \@r;
1260}
1261
1262
1263sub rand_word {
1264 die 'usage: rand_word()' if @_;
1265 my $r = '';
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))];
1269 }
1270 return $r;
1271}