Re: [ID 20011025.054] Segmentation fault when using the function read not correctly
[p5sagit/p5-mst-13.2.git] / ext / DB_File / t / db-recno.t
CommitLineData
f6b705ef 1#!./perl -w
a0d0e21e 2
3BEGIN {
88587957 4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
a0d0e21e 6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bDB_File\b/) {
45c0de28 8 print "1..0 # Skip: DB_File was not built\n";
a0d0e21e 9 exit 0;
10 }
11}
12
13use DB_File;
14use Fcntl;
55d68b4a 15use strict ;
3245f058 16use warnings;
045291aa 17use vars qw($dbh $Dfile $bad_ones $FA) ;
18
19# full tied array support started in Perl 5.004_57
a9fd575d 20# Double check to see if it is available.
21
22{
23 sub try::TIEARRAY { bless [], "try" }
24 sub try::FETCHSIZE { $FA = 1 }
25 $FA = 0 ;
26 my @a ;
27 tie @a, 'try' ;
28 my $a = @a ;
29}
30
a0d0e21e 31
55d68b4a 32sub ok
33{
34 my $no = shift ;
35 my $result = shift ;
a0d0e21e 36
55d68b4a 37 print "not " unless $result ;
38 print "ok $no\n" ;
6250ba0a 39
40 return $result ;
41}
42
9b761c68 43{
44 package Redirect ;
45 use Symbol ;
46
47 sub new
48 {
49 my $class = shift ;
50 my $filename = shift ;
51 my $fh = gensym ;
52 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
53 my $real_stdout = select($fh) ;
54 return bless [$fh, $real_stdout ] ;
55
56 }
57 sub DESTROY
58 {
59 my $self = shift ;
60 close $self->[0] ;
61 select($self->[1]) ;
62 }
63}
64
65sub docat
66{
67 my $file = shift;
68 local $/ = undef;
69 open(CAT,$file) || die "Cannot open $file:$!";
70 my $result = <CAT>;
71 close(CAT);
72 return $result;
73}
74
75sub docat_del
76{
77 my $file = shift;
78 local $/ = undef;
79 open(CAT,$file) || die "Cannot open $file: $!";
80 my $result = <CAT>;
81 close(CAT);
82 unlink $file ;
83 return $result;
84}
85
6250ba0a 86sub bad_one
87{
25268f15 88 print STDERR <<EOM unless $bad_ones++ ;
89#
20896112 90# Some older versions of Berkeley DB version 1 will fail tests 51,
91# 53 and 55.
6250ba0a 92#
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.
96#
20896112 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.
6250ba0a 100#
101EOM
55d68b4a 102}
103
3245f058 104print "1..128\n";
55d68b4a 105
106my $Dfile = "recno.tmp";
107unlink $Dfile ;
a0d0e21e 108
109umask(0);
110
111# Check the interface to RECNOINFO
112
55d68b4a 113my $dbh = new DB_File::RECNOINFO ;
3fe9a6f1 114ok(1, ! defined $dbh->{bval}) ;
115ok(2, ! defined $dbh->{cachesize}) ;
116ok(3, ! defined $dbh->{psize}) ;
117ok(4, ! defined $dbh->{flags}) ;
118ok(5, ! defined $dbh->{lorder}) ;
119ok(6, ! defined $dbh->{reclen}) ;
120ok(7, ! defined $dbh->{bfname}) ;
a0d0e21e 121
122$dbh->{bval} = 3000 ;
f6b705ef 123ok(8, $dbh->{bval} == 3000 );
a0d0e21e 124
125$dbh->{cachesize} = 9000 ;
f6b705ef 126ok(9, $dbh->{cachesize} == 9000 );
a0d0e21e 127
128$dbh->{psize} = 400 ;
f6b705ef 129ok(10, $dbh->{psize} == 400 );
a0d0e21e 130
131$dbh->{flags} = 65 ;
f6b705ef 132ok(11, $dbh->{flags} == 65 );
a0d0e21e 133
134$dbh->{lorder} = 123 ;
f6b705ef 135ok(12, $dbh->{lorder} == 123 );
a0d0e21e 136
137$dbh->{reclen} = 1234 ;
f6b705ef 138ok(13, $dbh->{reclen} == 1234 );
a0d0e21e 139
140$dbh->{bfname} = 1234 ;
f6b705ef 141ok(14, $dbh->{bfname} == 1234 );
a0d0e21e 142
143
144# Check that an invalid entry is caught both for store & fetch
145eval '$dbh->{fred} = 1234' ;
f6b705ef 146ok(15, $@ =~ /^DB_File::RECNOINFO::STORE - Unknown element 'fred' at/ );
55d68b4a 147eval 'my $q = $dbh->{fred}' ;
f6b705ef 148ok(16, $@ =~ /^DB_File::RECNOINFO::FETCH - Unknown element 'fred' at/ );
a0d0e21e 149
150# Now check the interface to RECNOINFO
151
55d68b4a 152my $X ;
153my @h ;
154ok(17, $X = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
a0d0e21e 155
d536870a 156ok(18, ((stat($Dfile))[2] & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640)
2986a63f 157 || $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'amigaos') ;
a0d0e21e 158
55d68b4a 159#my $l = @h ;
160my $l = $X->length ;
045291aa 161ok(19, ($FA ? @h == 0 : !$l) );
a0d0e21e 162
55d68b4a 163my @data = qw( a b c d ever f g h i j k longername m n o p) ;
a0d0e21e 164
165$h[0] = shift @data ;
f6b705ef 166ok(20, $h[0] eq 'a' );
a0d0e21e 167
55d68b4a 168my $ i;
a0d0e21e 169foreach (@data)
170 { $h[++$i] = $_ }
171
172unshift (@data, 'a') ;
173
f6b705ef 174ok(21, defined $h[1] );
175ok(22, ! defined $h[16] );
045291aa 176ok(23, $FA ? @h == @data : $X->length == @data );
a0d0e21e 177
178
179# Overwrite an entry & check fetch it
180$h[3] = 'replaced' ;
181$data[3] = 'replaced' ;
f6b705ef 182ok(24, $h[3] eq 'replaced' );
a0d0e21e 183
184#PUSH
55d68b4a 185my @push_data = qw(added to the end) ;
045291aa 186($FA ? push(@h, @push_data) : $X->push(@push_data)) ;
a0d0e21e 187push (@data, @push_data) ;
f6b705ef 188ok(25, $h[++$i] eq 'added' );
189ok(26, $h[++$i] eq 'to' );
190ok(27, $h[++$i] eq 'the' );
191ok(28, $h[++$i] eq 'end' );
a0d0e21e 192
193# POP
f6b705ef 194my $popped = pop (@data) ;
045291aa 195my $value = ($FA ? pop @h : $X->pop) ;
f6b705ef 196ok(29, $value eq $popped) ;
a0d0e21e 197
198# SHIFT
045291aa 199$value = ($FA ? shift @h : $X->shift) ;
f6b705ef 200my $shifted = shift @data ;
201ok(30, $value eq $shifted );
a0d0e21e 202
203# UNSHIFT
204
205# empty list
936edb8b 206($FA ? unshift @h,() : $X->unshift) ;
045291aa 207ok(31, ($FA ? @h == @data : $X->length == @data ));
a0d0e21e 208
55d68b4a 209my @new_data = qw(add this to the start of the array) ;
045291aa 210$FA ? unshift (@h, @new_data) : $X->unshift (@new_data) ;
a0d0e21e 211unshift (@data, @new_data) ;
045291aa 212ok(32, $FA ? @h == @data : $X->length == @data );
f6b705ef 213ok(33, $h[0] eq "add") ;
214ok(34, $h[1] eq "this") ;
215ok(35, $h[2] eq "to") ;
216ok(36, $h[3] eq "the") ;
217ok(37, $h[4] eq "start") ;
218ok(38, $h[5] eq "of") ;
219ok(39, $h[6] eq "the") ;
220ok(40, $h[7] eq "array") ;
221ok(41, $h[8] eq $data[8]) ;
a0d0e21e 222
223# SPLICE
224
225# Now both arrays should be identical
226
55d68b4a 227my $ok = 1 ;
228my $j = 0 ;
a0d0e21e 229foreach (@data)
230{
231 $ok = 0, last if $_ ne $h[$j ++] ;
232}
f6b705ef 233ok(42, $ok );
a0d0e21e 234
55d68b4a 235# Neagtive subscripts
236
237# get the last element of the array
f6b705ef 238ok(43, $h[-1] eq $data[-1] );
045291aa 239ok(44, $h[-1] eq $h[ ($FA ? @h : $X->length) -1] );
55d68b4a 240
241# get the first element using a negative subscript
045291aa 242eval '$h[ - ( $FA ? @h : $X->length)] = "abcd"' ;
f6b705ef 243ok(45, $@ eq "" );
244ok(46, $h[0] eq "abcd" );
55d68b4a 245
246# now try to read before the start of the array
045291aa 247eval '$h[ - (1 + ($FA ? @h : $X->length))] = 1234' ;
f6b705ef 248ok(47, $@ =~ '^Modification of non-creatable array value attempted' );
55d68b4a 249
a0d0e21e 250# IMPORTANT - $X must be undefined before the untie otherwise the
251# underlying DB close routine will not get called.
252undef $X ;
253untie(@h);
254
255unlink $Dfile;
256
a6ed719b 257
36477c24 258{
259 # Check bval defaults to \n
260
261 my @h = () ;
262 my $dbh = new DB_File::RECNOINFO ;
263 ok(48, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
264 $h[0] = "abc" ;
265 $h[1] = "def" ;
266 $h[3] = "ghi" ;
267 untie @h ;
a6ed719b 268 my $x = docat($Dfile) ;
36477c24 269 unlink $Dfile;
6250ba0a 270 ok(49, $x eq "abc\ndef\n\nghi\n") ;
36477c24 271}
272
273{
274 # Change bval
275
276 my @h = () ;
277 my $dbh = new DB_File::RECNOINFO ;
278 $dbh->{bval} = "-" ;
279 ok(50, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
280 $h[0] = "abc" ;
281 $h[1] = "def" ;
282 $h[3] = "ghi" ;
283 untie @h ;
a6ed719b 284 my $x = docat($Dfile) ;
36477c24 285 unlink $Dfile;
6250ba0a 286 my $ok = ($x eq "abc-def--ghi-") ;
287 bad_one() unless $ok ;
288 ok(51, $ok) ;
36477c24 289}
290
291{
292 # Check R_FIXEDLEN with default bval (space)
293
294 my @h = () ;
295 my $dbh = new DB_File::RECNOINFO ;
296 $dbh->{flags} = R_FIXEDLEN ;
297 $dbh->{reclen} = 5 ;
298 ok(52, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
299 $h[0] = "abc" ;
300 $h[1] = "def" ;
301 $h[3] = "ghi" ;
302 untie @h ;
a6ed719b 303 my $x = docat($Dfile) ;
36477c24 304 unlink $Dfile;
6250ba0a 305 my $ok = ($x eq "abc def ghi ") ;
306 bad_one() unless $ok ;
307 ok(53, $ok) ;
36477c24 308}
309
310{
311 # Check R_FIXEDLEN with user-defined bval
312
313 my @h = () ;
314 my $dbh = new DB_File::RECNOINFO ;
315 $dbh->{flags} = R_FIXEDLEN ;
316 $dbh->{bval} = "-" ;
317 $dbh->{reclen} = 5 ;
318 ok(54, tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $dbh ) ;
319 $h[0] = "abc" ;
320 $h[1] = "def" ;
321 $h[3] = "ghi" ;
322 untie @h ;
a6ed719b 323 my $x = docat($Dfile) ;
36477c24 324 unlink $Dfile;
6250ba0a 325 my $ok = ($x eq "abc--def-------ghi--") ;
326 bad_one() unless $ok ;
327 ok(55, $ok) ;
36477c24 328}
329
05475680 330{
331 # check that attempting to tie an associative array to a DB_RECNO will fail
332
333 my $filename = "xyz" ;
334 my %x ;
335 eval { tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO ; } ;
336 ok(56, $@ =~ /^DB_File can only tie an array to a DB_RECNO database/) ;
337 unlink $filename ;
338}
339
a6ed719b 340{
341 # sub-class test
342
343 package Another ;
344
3245f058 345 use warnings ;
a6ed719b 346 use strict ;
347
348 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
349 print FILE <<'EOM' ;
350
351 package SubDB ;
352
3245f058 353 use warnings ;
a6ed719b 354 use strict ;
355 use vars qw( @ISA @EXPORT) ;
356
357 require Exporter ;
358 use DB_File;
359 @ISA=qw(DB_File);
360 @EXPORT = @DB_File::EXPORT ;
361
362 sub STORE {
363 my $self = shift ;
364 my $key = shift ;
365 my $value = shift ;
366 $self->SUPER::STORE($key, $value * 2) ;
367 }
368
369 sub FETCH {
370 my $self = shift ;
371 my $key = shift ;
372 $self->SUPER::FETCH($key) - 1 ;
373 }
374
375 sub put {
376 my $self = shift ;
377 my $key = shift ;
378 my $value = shift ;
379 $self->SUPER::put($key, $value * 3) ;
380 }
381
382 sub get {
383 my $self = shift ;
384 $self->SUPER::get($_[0], $_[1]) ;
385 $_[1] -= 2 ;
386 }
387
388 sub A_new_method
389 {
390 my $self = shift ;
391 my $key = shift ;
392 my $value = $self->FETCH($key) ;
393 return "[[$value]]" ;
394 }
395
396 1 ;
397EOM
398
399 close FILE ;
400
045291aa 401 BEGIN { push @INC, '.'; }
a6ed719b 402 eval 'use SubDB ; ';
403 main::ok(57, $@ eq "") ;
404 my @h ;
405 my $X ;
406 eval '
407 $X = tie(@h, "SubDB","recno.tmp", O_RDWR|O_CREAT, 0640, $DB_RECNO );
408 ' ;
409
410 main::ok(58, $@ eq "") ;
411
412 my $ret = eval '$h[3] = 3 ; return $h[3] ' ;
413 main::ok(59, $@ eq "") ;
414 main::ok(60, $ret == 5) ;
415
416 my $value = 0;
417 $ret = eval '$X->put(1, 4) ; $X->get(1, $value) ; return $value' ;
418 main::ok(61, $@ eq "") ;
419 main::ok(62, $ret == 10) ;
420
421 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
422 main::ok(63, $@ eq "" ) ;
423 main::ok(64, $ret == 1) ;
424
425 $ret = eval '$X->A_new_method(1) ' ;
426 main::ok(65, $@ eq "") ;
427 main::ok(66, $ret eq "[[11]]") ;
428
fac76ed7 429 undef $X;
430 untie(@h);
a6ed719b 431 unlink "SubDB.pm", "recno.tmp" ;
432
433}
434
045291aa 435{
436
437 # test $#
438 my $self ;
439 unlink $Dfile;
440 ok(67, $self = tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) ;
441 $h[0] = "abc" ;
442 $h[1] = "def" ;
443 $h[2] = "ghi" ;
444 $h[3] = "jkl" ;
445 ok(68, $FA ? $#h == 3 : $self->length() == 4) ;
446 undef $self ;
447 untie @h ;
448 my $x = docat($Dfile) ;
449 ok(69, $x eq "abc\ndef\nghi\njkl\n") ;
450
451 # $# sets array to same length
452 ok(70, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
453 if ($FA)
454 { $#h = 3 }
455 else
456 { $self->STORESIZE(4) }
457 ok(71, $FA ? $#h == 3 : $self->length() == 4) ;
458 undef $self ;
459 untie @h ;
460 $x = docat($Dfile) ;
461 ok(72, $x eq "abc\ndef\nghi\njkl\n") ;
462
463 # $# sets array to bigger
464 ok(73, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
465 if ($FA)
466 { $#h = 6 }
467 else
468 { $self->STORESIZE(7) }
469 ok(74, $FA ? $#h == 6 : $self->length() == 7) ;
470 undef $self ;
471 untie @h ;
472 $x = docat($Dfile) ;
473 ok(75, $x eq "abc\ndef\nghi\njkl\n\n\n\n") ;
474
475 # $# sets array smaller
476 ok(76, $self = tie @h, 'DB_File', $Dfile, O_RDWR, 0640, $DB_RECNO ) ;
477 if ($FA)
478 { $#h = 2 }
479 else
480 { $self->STORESIZE(3) }
481 ok(77, $FA ? $#h == 2 : $self->length() == 3) ;
482 undef $self ;
483 untie @h ;
484 $x = docat($Dfile) ;
485 ok(78, $x eq "abc\ndef\nghi\n") ;
486
487 unlink $Dfile;
488
489
490}
491
9fe6733a 492{
493 # DBM Filter tests
3245f058 494 use warnings ;
9fe6733a 495 use strict ;
496 my (@h, $db) ;
497 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
498 unlink $Dfile;
499
500 sub checkOutput
501 {
502 my($fk, $sk, $fv, $sv) = @_ ;
503 return
504 $fetch_key eq $fk && $store_key eq $sk &&
505 $fetch_value eq $fv && $store_value eq $sv &&
506 $_ eq 'original' ;
507 }
508
509 ok(79, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
510
511 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
512 $db->filter_store_key (sub { $store_key = $_ }) ;
513 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
514 $db->filter_store_value (sub { $store_value = $_ }) ;
515
516 $_ = "original" ;
517
518 $h[0] = "joe" ;
519 # fk sk fv sv
520 ok(80, checkOutput( "", 0, "", "joe")) ;
521
522 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
523 ok(81, $h[0] eq "joe");
524 # fk sk fv sv
525 ok(82, checkOutput( "", 0, "joe", "")) ;
526
527 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
528 ok(83, $db->FIRSTKEY() == 0) ;
529 # fk sk fv sv
530 ok(84, checkOutput( 0, "", "", "")) ;
531
532 # replace the filters, but remember the previous set
533 my ($old_fk) = $db->filter_fetch_key
534 (sub { ++ $_ ; $fetch_key = $_ }) ;
535 my ($old_sk) = $db->filter_store_key
536 (sub { $_ *= 2 ; $store_key = $_ }) ;
537 my ($old_fv) = $db->filter_fetch_value
538 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
539 my ($old_sv) = $db->filter_store_value
540 (sub { s/o/x/g; $store_value = $_ }) ;
541
542 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
543 $h[1] = "Joe" ;
544 # fk sk fv sv
545 ok(85, checkOutput( "", 2, "", "Jxe")) ;
546
547 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
548 ok(86, $h[1] eq "[Jxe]");
549 # fk sk fv sv
550 ok(87, checkOutput( "", 2, "[Jxe]", "")) ;
551
552 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
553 ok(88, $db->FIRSTKEY() == 1) ;
554 # fk sk fv sv
555 ok(89, checkOutput( 1, "", "", "")) ;
556
557 # put the original filters back
558 $db->filter_fetch_key ($old_fk);
559 $db->filter_store_key ($old_sk);
560 $db->filter_fetch_value ($old_fv);
561 $db->filter_store_value ($old_sv);
562
563 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
564 $h[0] = "joe" ;
565 ok(90, checkOutput( "", 0, "", "joe")) ;
566
567 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
568 ok(91, $h[0] eq "joe");
569 ok(92, checkOutput( "", 0, "joe", "")) ;
570
571 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
572 ok(93, $db->FIRSTKEY() == 0) ;
573 ok(94, checkOutput( 0, "", "", "")) ;
574
575 # delete the filters
576 $db->filter_fetch_key (undef);
577 $db->filter_store_key (undef);
578 $db->filter_fetch_value (undef);
579 $db->filter_store_value (undef);
580
581 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
582 $h[0] = "joe" ;
583 ok(95, checkOutput( "", "", "", "")) ;
584
585 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
586 ok(96, $h[0] eq "joe");
587 ok(97, checkOutput( "", "", "", "")) ;
588
589 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
590 ok(98, $db->FIRSTKEY() == 0) ;
591 ok(99, checkOutput( "", "", "", "")) ;
592
593 undef $db ;
594 untie @h;
595 unlink $Dfile;
596}
597
598{
599 # DBM Filter with a closure
600
3245f058 601 use warnings ;
9fe6733a 602 use strict ;
603 my (@h, $db) ;
604
605 unlink $Dfile;
606 ok(100, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
607
608 my %result = () ;
609
610 sub Closure
611 {
612 my ($name) = @_ ;
613 my $count = 0 ;
614 my @kept = () ;
615
616 return sub { ++$count ;
617 push @kept, $_ ;
618 $result{$name} = "$name - $count: [@kept]" ;
619 }
620 }
621
622 $db->filter_store_key(Closure("store key")) ;
623 $db->filter_store_value(Closure("store value")) ;
624 $db->filter_fetch_key(Closure("fetch key")) ;
625 $db->filter_fetch_value(Closure("fetch value")) ;
626
627 $_ = "original" ;
628
629 $h[0] = "joe" ;
630 ok(101, $result{"store key"} eq "store key - 1: [0]");
631 ok(102, $result{"store value"} eq "store value - 1: [joe]");
632 ok(103, ! defined $result{"fetch key"} );
633 ok(104, ! defined $result{"fetch value"} );
634 ok(105, $_ eq "original") ;
635
636 ok(106, $db->FIRSTKEY() == 0 ) ;
637 ok(107, $result{"store key"} eq "store key - 1: [0]");
638 ok(108, $result{"store value"} eq "store value - 1: [joe]");
639 ok(109, $result{"fetch key"} eq "fetch key - 1: [0]");
640 ok(110, ! defined $result{"fetch value"} );
641 ok(111, $_ eq "original") ;
642
643 $h[7] = "john" ;
644 ok(112, $result{"store key"} eq "store key - 2: [0 7]");
645 ok(113, $result{"store value"} eq "store value - 2: [joe john]");
646 ok(114, $result{"fetch key"} eq "fetch key - 1: [0]");
647 ok(115, ! defined $result{"fetch value"} );
648 ok(116, $_ eq "original") ;
649
650 ok(117, $h[0] eq "joe");
651 ok(118, $result{"store key"} eq "store key - 3: [0 7 0]");
652 ok(119, $result{"store value"} eq "store value - 2: [joe john]");
653 ok(120, $result{"fetch key"} eq "fetch key - 1: [0]");
654 ok(121, $result{"fetch value"} eq "fetch value - 1: [joe]");
655 ok(122, $_ eq "original") ;
656
657 undef $db ;
658 untie @h;
659 unlink $Dfile;
660}
661
662{
663 # DBM Filter recursion detection
3245f058 664 use warnings ;
9fe6733a 665 use strict ;
666 my (@h, $db) ;
667 unlink $Dfile;
668
669 ok(123, $db = tie(@h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_RECNO ) );
670
671 $db->filter_store_key (sub { $_ = $h[0] }) ;
672
673 eval '$h[1] = 1234' ;
674 ok(124, $@ =~ /^recursion detected in filter_store_key at/ );
675
676 undef $db ;
677 untie @h;
678 unlink $Dfile;
679}
680
9b761c68 681
682{
683 # Examples from the POD
684
685 my $file = "xyzt" ;
686 {
687 my $redirect = new Redirect $file ;
688
3245f058 689 use warnings FATAL => qw(all);
9b761c68 690 use strict ;
691 use DB_File ;
692
693 my $filename = "text" ;
694 unlink $filename ;
695
696 my @h ;
697 my $x = tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
698 or die "Cannot open file 'text': $!\n" ;
699
700 # Add a few key/value pairs to the file
701 $h[0] = "orange" ;
702 $h[1] = "blue" ;
703 $h[2] = "yellow" ;
704
705 $FA ? push @h, "green", "black"
706 : $x->push("green", "black") ;
707
708 my $elements = $FA ? scalar @h : $x->length ;
709 print "The array contains $elements entries\n" ;
710
711 my $last = $FA ? pop @h : $x->pop ;
712 print "popped $last\n" ;
713
714 $FA ? unshift @h, "white"
715 : $x->unshift("white") ;
716 my $first = $FA ? shift @h : $x->shift ;
717 print "shifted $first\n" ;
718
719 # Check for existence of a key
720 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
721
722 # use a negative index
723 print "The last element is $h[-1]\n" ;
724 print "The 2nd last element is $h[-2]\n" ;
725
726 undef $x ;
727 untie @h ;
728
729 unlink $filename ;
730 }
731
732 ok(125, docat_del($file) eq <<'EOM') ;
733The array contains 5 entries
734popped black
735shifted white
736Element 1 Exists with value blue
737The last element is green
738The 2nd last element is yellow
739EOM
740
741 my $save_output = "xyzt" ;
742 {
743 my $redirect = new Redirect $save_output ;
744
3245f058 745 use warnings FATAL => qw(all);
9b761c68 746 use strict ;
747 use vars qw(@h $H $file $i) ;
748 use DB_File ;
749 use Fcntl ;
750
751 $file = "text" ;
752
753 unlink $file ;
754
755 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
756 or die "Cannot open file $file: $!\n" ;
757
758 # first create a text file to play with
759 $h[0] = "zero" ;
760 $h[1] = "one" ;
761 $h[2] = "two" ;
762 $h[3] = "three" ;
763 $h[4] = "four" ;
764
765
766 # Print the records in order.
767 #
768 # The length method is needed here because evaluating a tied
769 # array in a scalar context does not return the number of
770 # elements in the array.
771
772 print "\nORIGINAL\n" ;
773 foreach $i (0 .. $H->length - 1) {
774 print "$i: $h[$i]\n" ;
775 }
776
777 # use the push & pop methods
778 $a = $H->pop ;
779 $H->push("last") ;
780 print "\nThe last record was [$a]\n" ;
781
782 # and the shift & unshift methods
783 $a = $H->shift ;
784 $H->unshift("first") ;
785 print "The first record was [$a]\n" ;
786
787 # Use the API to add a new record after record 2.
788 $i = 2 ;
789 $H->put($i, "Newbie", R_IAFTER) ;
790
791 # and a new record before record 1.
792 $i = 1 ;
793 $H->put($i, "New One", R_IBEFORE) ;
794
795 # delete record 3
796 $H->del(3) ;
797
798 # now print the records in reverse order
799 print "\nREVERSE\n" ;
800 for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
801 { print "$i: $h[$i]\n" }
802
803 # same again, but use the API functions instead
804 print "\nREVERSE again\n" ;
805 my ($s, $k, $v) = (0, 0, 0) ;
806 for ($s = $H->seq($k, $v, R_LAST) ;
807 $s == 0 ;
808 $s = $H->seq($k, $v, R_PREV))
809 { print "$k: $v\n" }
810
811 undef $H ;
812 untie @h ;
813
814 unlink $file ;
815 }
816
817 ok(126, docat_del($save_output) eq <<'EOM') ;
818
819ORIGINAL
8200: zero
8211: one
8222: two
8233: three
8244: four
825
826The last record was [four]
827The first record was [zero]
828
829REVERSE
8305: last
8314: three
8323: Newbie
8332: one
8341: New One
8350: first
836
837REVERSE again
8385: last
8394: three
8403: Newbie
8412: one
8421: New One
8430: first
844EOM
845
846}
847
cbc5248d 848{
849 # Bug ID 20001013.009
850 #
851 # test that $hash{KEY} = undef doesn't produce the warning
852 # Use of uninitialized value in null operation
853 use warnings ;
854 use strict ;
855 use DB_File ;
856
857 unlink $Dfile;
858 my @h ;
859 my $a = "";
860 local $SIG{__WARN__} = sub {$a = $_[0]} ;
861
3245f058 862 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
cbc5248d 863 or die "Can't open file: $!\n" ;
864 $h[0] = undef;
865 ok(127, $a eq "") ;
3245f058 866 untie @h ;
867 unlink $Dfile;
868}
869
870{
871 # test that %hash = () doesn't produce the warning
872 # Argument "" isn't numeric in entersub
873 use warnings ;
874 use strict ;
875 use DB_File ;
876 my $a = "";
877 local $SIG{__WARN__} = sub {$a = $_[0]} ;
878
879 unlink $Dfile;
880 my @h ;
881
882 tie @h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0664, $DB_RECNO
883 or die "Can't open file: $!\n" ;
884 @h = (); ;
885 ok(128, $a eq "") ;
886 untie @h ;
cbc5248d 887 unlink $Dfile;
888}
889
a0d0e21e 890exit ;