Abolish the "Tied variable freed while still in use" error - I have
[p5sagit/p5-mst-13.2.git] / ext / DB_File / t / db-hash.t
CommitLineData
77fd2717 1#!./perl
2
a0d0e21e 3BEGIN {
77fd2717 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
14BEGIN {
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;
25
5bbd4290 26print "1..151\n";
f6b705ef 27
262eaca6 28unlink glob "__db.*";
29
f6b705ef 30sub ok
31{
32 my $no = shift ;
33 my $result = shift ;
34
35 print "not " unless $result ;
36 print "ok $no\n" ;
37}
a0d0e21e 38
2c2d71f5 39{
40 package Redirect ;
41 use Symbol ;
42
43 sub new
44 {
45 my $class = shift ;
46 my $filename = shift ;
47 my $fh = gensym ;
48 open ($fh, ">$filename") || die "Cannot open $filename: $!" ;
49 my $real_stdout = select($fh) ;
50 return bless [$fh, $real_stdout ] ;
51
52 }
53 sub DESTROY
54 {
55 my $self = shift ;
56 close $self->[0] ;
57 select($self->[1]) ;
58 }
59}
60
61sub docat_del
62{
63 my $file = shift;
64 local $/ = undef;
65 open(CAT,$file) || die "Cannot open $file: $!";
66 my $result = <CAT>;
67 close(CAT);
77fd2717 68 $result = normalise($result) ;
2c2d71f5 69 unlink $file ;
70 return $result;
71}
72
77fd2717 73sub normalise
74{
75 my $data = shift ;
76 $data =~ s#\r\n#\n#g
77 if $^O eq 'cygwin' ;
78 return $data ;
79}
80
efc79c7d 81sub safeUntie
82{
83 my $hashref = shift ;
84 my $no_inner = 1;
85 local $SIG{__WARN__} = sub {-- $no_inner } ;
86 untie %$hashref;
87 return $no_inner;
88}
77fd2717 89
90
9fe6733a 91my $Dfile = "dbhash.tmp";
efc79c7d 92my $Dfile2 = "dbhash2.tmp";
3245f058 93my $null_keys_allowed = ($DB_File::db_ver < 2.004010
94 || $DB_File::db_ver >= 3.1 );
95
a0d0e21e 96unlink $Dfile;
97
98umask(0);
99
100# Check the interface to HASHINFO
101
f6b705ef 102my $dbh = new DB_File::HASHINFO ;
103
3fe9a6f1 104ok(1, ! defined $dbh->{bsize}) ;
105ok(2, ! defined $dbh->{ffactor}) ;
106ok(3, ! defined $dbh->{nelem}) ;
107ok(4, ! defined $dbh->{cachesize}) ;
108ok(5, ! defined $dbh->{hash}) ;
109ok(6, ! defined $dbh->{lorder}) ;
a0d0e21e 110
111$dbh->{bsize} = 3000 ;
f6b705ef 112ok(7, $dbh->{bsize} == 3000 );
a0d0e21e 113
114$dbh->{ffactor} = 9000 ;
f6b705ef 115ok(8, $dbh->{ffactor} == 9000 );
116
a0d0e21e 117$dbh->{nelem} = 400 ;
f6b705ef 118ok(9, $dbh->{nelem} == 400 );
a0d0e21e 119
120$dbh->{cachesize} = 65 ;
f6b705ef 121ok(10, $dbh->{cachesize} == 65 );
a0d0e21e 122
efc79c7d 123my $some_sub = sub {} ;
124$dbh->{hash} = $some_sub;
125ok(11, $dbh->{hash} eq $some_sub );
a0d0e21e 126
127$dbh->{lorder} = 1234 ;
f6b705ef 128ok(12, $dbh->{lorder} == 1234 );
a0d0e21e 129
130# Check that an invalid entry is caught both for store & fetch
131eval '$dbh->{fred} = 1234' ;
f6b705ef 132ok(13, $@ =~ /^DB_File::HASHINFO::STORE - Unknown element 'fred' at/ );
610ab055 133eval 'my $q = $dbh->{fred}' ;
f6b705ef 134ok(14, $@ =~ /^DB_File::HASHINFO::FETCH - Unknown element 'fred' at/ );
a0d0e21e 135
610ab055 136
a0d0e21e 137# Now check the interface to HASH
3245f058 138my ($X, %h);
f6b705ef 139ok(15, $X = tie(%h, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
05a54443 140die "Could not tie: $!" unless $X;
a0d0e21e 141
3245f058 142my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
a0d0e21e 143 $blksize,$blocks) = stat($Dfile);
77fd2717 144
145my %noMode = map { $_, 1} qw( amigaos MSWin32 NetWare cygwin ) ;
146
d536870a 147ok(16, ($mode & 0777) == (($^O eq 'os2' || $^O eq 'MacOS') ? 0666 : 0640) ||
77fd2717 148 $noMode{$^O} );
a0d0e21e 149
3245f058 150my ($key, $value, $i);
a0d0e21e 151while (($key,$value) = each(%h)) {
152 $i++;
153}
f6b705ef 154ok(17, !$i );
a0d0e21e 155
156$h{'goner1'} = 'snork';
157
158$h{'abc'} = 'ABC';
f6b705ef 159ok(18, $h{'abc'} eq 'ABC' );
160ok(19, !defined $h{'jimmy'} );
161ok(20, !exists $h{'jimmy'} );
162ok(21, exists $h{'abc'} );
a0d0e21e 163
164$h{'def'} = 'DEF';
165$h{'jkl','mno'} = "JKL\034MNO";
166$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
167$h{'a'} = 'A';
168
169#$h{'b'} = 'B';
170$X->STORE('b', 'B') ;
171
172$h{'c'} = 'C';
173
174#$h{'d'} = 'D';
175$X->put('d', 'D') ;
176
177$h{'e'} = 'E';
178$h{'f'} = 'F';
179$h{'g'} = 'X';
180$h{'h'} = 'H';
181$h{'i'} = 'I';
182
183$h{'goner2'} = 'snork';
184delete $h{'goner2'};
185
186
187# IMPORTANT - $X must be undefined before the untie otherwise the
188# underlying DB close routine will not get called.
189undef $X ;
190untie(%h);
191
192
193# tie to the same file again, do not supply a type - should default to HASH
f6b705ef 194ok(22, $X = tie(%h,'DB_File',$Dfile, O_RDWR, 0640) );
a0d0e21e 195
196# Modify an entry from the previous tie
197$h{'g'} = 'G';
198
199$h{'j'} = 'J';
200$h{'k'} = 'K';
201$h{'l'} = 'L';
202$h{'m'} = 'M';
203$h{'n'} = 'N';
204$h{'o'} = 'O';
205$h{'p'} = 'P';
206$h{'q'} = 'Q';
207$h{'r'} = 'R';
208$h{'s'} = 'S';
209$h{'t'} = 'T';
210$h{'u'} = 'U';
211$h{'v'} = 'V';
212$h{'w'} = 'W';
213$h{'x'} = 'X';
214$h{'y'} = 'Y';
215$h{'z'} = 'Z';
216
217$h{'goner3'} = 'snork';
218
219delete $h{'goner1'};
220$X->DELETE('goner3');
221
3245f058 222my @keys = keys(%h);
223my @values = values(%h);
a0d0e21e 224
f6b705ef 225ok(23, $#keys == 29 && $#values == 29) ;
a0d0e21e 226
f6b705ef 227$i = 0 ;
55d68b4a 228while (($key,$value) = each(%h)) {
2f52a358 229 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
a0d0e21e 230 $key =~ y/a-z/A-Z/;
231 $i++ if $key eq $value;
232 }
233}
234
f6b705ef 235ok(24, $i == 30) ;
a0d0e21e 236
55d68b4a 237@keys = ('blurfl', keys(%h), 'dyick');
f6b705ef 238ok(25, $#keys == 31) ;
a0d0e21e 239
240$h{'foo'} = '';
f6b705ef 241ok(26, $h{'foo'} eq '' );
a0d0e21e 242
3245f058 243# Berkeley DB from version 2.4.10 to 3.0 does not allow null keys.
244# This feature was reenabled in version 3.1 of Berkeley DB.
245my $result = 0 ;
246if ($null_keys_allowed) {
247 $h{''} = 'bar';
248 $result = ( $h{''} eq 'bar' );
249}
250else
251 { $result = 1 }
252ok(27, $result) ;
a0d0e21e 253
254# check cache overflow and numeric keys and contents
3245f058 255my $ok = 1;
a0d0e21e 256for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
257for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
f6b705ef 258ok(28, $ok );
a0d0e21e 259
260($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
261 $blksize,$blocks) = stat($Dfile);
f6b705ef 262ok(29, $size > 0 );
a0d0e21e 263
264@h{0..200} = 200..400;
3245f058 265my @foo = @h{0..200};
f6b705ef 266ok(30, join(':',200..400) eq join(':',@foo) );
a0d0e21e 267
268
269# Now check all the non-tie specific stuff
270
271# Check NOOVERWRITE will make put fail when attempting to overwrite
272# an existing record.
273
3245f058 274my $status = $X->put( 'x', 'newvalue', R_NOOVERWRITE) ;
f6b705ef 275ok(31, $status == 1 );
a0d0e21e 276
277# check that the value of the key 'x' has not been changed by the
278# previous test
f6b705ef 279ok(32, $h{'x'} eq 'X' );
a0d0e21e 280
281# standard put
282$status = $X->put('key', 'value') ;
f6b705ef 283ok(33, $status == 0 );
a0d0e21e 284
285#check that previous put can be retrieved
f6b705ef 286$value = 0 ;
a0d0e21e 287$status = $X->get('key', $value) ;
f6b705ef 288ok(34, $status == 0 );
289ok(35, $value eq 'value' );
a0d0e21e 290
291# Attempting to delete an existing key should work
292
293$status = $X->del('q') ;
f6b705ef 294ok(36, $status == 0 );
a0d0e21e 295
296# Make sure that the key deleted, cannot be retrieved
3245f058 297{
298 no warnings 'uninitialized' ;
299 ok(37, $h{'q'} eq undef );
300}
a0d0e21e 301
302# Attempting to delete a non-existant key should fail
303
304$status = $X->del('joe') ;
f6b705ef 305ok(38, $status == 1 );
a0d0e21e 306
307# Check the get interface
308
309# First a non-existing key
310$status = $X->get('aaaa', $value) ;
f6b705ef 311ok(39, $status == 1 );
a0d0e21e 312
313# Next an existing key
314$status = $X->get('a', $value) ;
f6b705ef 315ok(40, $status == 0 );
316ok(41, $value eq 'A' );
a0d0e21e 317
318# seq
319# ###
320
321# ditto, but use put to replace the key/value pair.
322
323# use seq to walk backwards through a file - check that this reversed is
324
325# check seq FIRST/LAST
326
327# sync
328# ####
329
330$status = $X->sync ;
f6b705ef 331ok(42, $status == 0 );
a0d0e21e 332
333
334# fd
335# ##
336
337$status = $X->fd ;
f6b705ef 338ok(43, $status != 0 );
a0d0e21e 339
340undef $X ;
341untie %h ;
342
343unlink $Dfile;
344
f6b705ef 345# clear
346# #####
347
348ok(44, tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
349foreach (1 .. 10)
350 { $h{$_} = $_ * 100 }
351
352# check that there are 10 elements in the hash
353$i = 0 ;
354while (($key,$value) = each(%h)) {
355 $i++;
356}
357ok(45, $i == 10);
358
359# now clear the hash
360%h = () ;
361
362# check it is empty
363$i = 0 ;
364while (($key,$value) = each(%h)) {
365 $i++;
366}
367ok(46, $i == 0);
368
369untie %h ;
370unlink $Dfile ;
371
372
a0d0e21e 373# Now try an in memory file
f6b705ef 374ok(47, $X = tie(%h, 'DB_File',undef, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
a0d0e21e 375
376# fd with an in memory file should return fail
377$status = $X->fd ;
f6b705ef 378ok(48, $status == -1 );
a0d0e21e 379
a0d0e21e 380undef $X ;
610ab055 381untie %h ;
382
383{
384 # check ability to override the default hashing
385 my %x ;
386 my $filename = "xyz" ;
387 my $hi = new DB_File::HASHINFO ;
388 $::count = 0 ;
389 $hi->{hash} = sub { ++$::count ; length $_[0] } ;
390 ok(49, tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $hi ) ;
391 $h{"abc"} = 123 ;
392 ok(50, $h{"abc"} == 123) ;
393 untie %x ;
394 unlink $filename ;
395 ok(51, $::count >0) ;
396}
a0d0e21e 397
05475680 398{
399 # check that attempting to tie an array to a DB_HASH will fail
400
401 my $filename = "xyz" ;
402 my @x ;
403 eval { tie @x, 'DB_File', $filename, O_RDWR|O_CREAT, 0640, $DB_HASH ; } ;
404 ok(52, $@ =~ /^DB_File can only tie an associative array to a DB_HASH database/) ;
405 unlink $filename ;
406}
407
a6ed719b 408{
409 # sub-class test
410
411 package Another ;
412
3245f058 413 use warnings ;
a6ed719b 414 use strict ;
415
416 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
417 print FILE <<'EOM' ;
418
419 package SubDB ;
420
3245f058 421 use warnings ;
a6ed719b 422 use strict ;
07200f1b 423 our (@ISA, @EXPORT);
a6ed719b 424
425 require Exporter ;
426 use DB_File;
427 @ISA=qw(DB_File);
428 @EXPORT = @DB_File::EXPORT ;
429
430 sub STORE {
431 my $self = shift ;
432 my $key = shift ;
433 my $value = shift ;
434 $self->SUPER::STORE($key, $value * 2) ;
435 }
436
437 sub FETCH {
438 my $self = shift ;
439 my $key = shift ;
440 $self->SUPER::FETCH($key) - 1 ;
441 }
442
443 sub put {
444 my $self = shift ;
445 my $key = shift ;
446 my $value = shift ;
447 $self->SUPER::put($key, $value * 3) ;
448 }
449
450 sub get {
451 my $self = shift ;
452 $self->SUPER::get($_[0], $_[1]) ;
453 $_[1] -= 2 ;
454 }
455
456 sub A_new_method
457 {
458 my $self = shift ;
459 my $key = shift ;
460 my $value = $self->FETCH($key) ;
461 return "[[$value]]" ;
462 }
463
464 1 ;
465EOM
466
467 close FILE ;
468
a9fd575d 469 BEGIN { push @INC, '.'; }
a6ed719b 470 eval 'use SubDB ; ';
471 main::ok(53, $@ eq "") ;
472 my %h ;
473 my $X ;
474 eval '
475 $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640, $DB_HASH );
476 ' ;
477
478 main::ok(54, $@ eq "") ;
479
480 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
481 main::ok(55, $@ eq "") ;
482 main::ok(56, $ret == 5) ;
483
484 my $value = 0;
485 $ret = eval '$X->put("joe", 4) ; $X->get("joe", $value) ; return $value' ;
486 main::ok(57, $@ eq "") ;
487 main::ok(58, $ret == 10) ;
488
489 $ret = eval ' R_NEXT eq main::R_NEXT ' ;
490 main::ok(59, $@ eq "" ) ;
491 main::ok(60, $ret == 1) ;
492
493 $ret = eval '$X->A_new_method("joe") ' ;
494 main::ok(61, $@ eq "") ;
495 main::ok(62, $ret eq "[[11]]") ;
496
fac76ed7 497 undef $X;
498 untie(%h);
a6ed719b 499 unlink "SubDB.pm", "dbhash.tmp" ;
500
501}
9fe6733a 502
503{
504 # DBM Filter tests
3245f058 505 use warnings ;
9fe6733a 506 use strict ;
507 my (%h, $db) ;
508 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
509 unlink $Dfile;
510
511 sub checkOutput
512 {
efc79c7d 513 no warnings 'uninitialized';
9fe6733a 514 my($fk, $sk, $fv, $sv) = @_ ;
efc79c7d 515
516 print "# Fetch Key : expected '$fk' got '$fetch_key'\n"
517 if $fetch_key ne $fk ;
518 print "# Fetch Value : expected '$fv' got '$fetch_value'\n"
519 if $fetch_value ne $fv ;
520 print "# Store Key : expected '$sk' got '$store_key'\n"
521 if $store_key ne $sk ;
522 print "# Store Value : expected '$sv' got '$store_value'\n"
523 if $store_value ne $sv ;
524 print "# \$_ : expected 'original' got '$_'\n"
525 if $_ ne 'original' ;
526
9fe6733a 527 return
efc79c7d 528 $fetch_key eq $fk && $store_key eq $sk &&
9fe6733a 529 $fetch_value eq $fv && $store_value eq $sv &&
530 $_ eq 'original' ;
531 }
532
533 ok(63, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
534
535 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
536 $db->filter_store_key (sub { $store_key = $_ }) ;
537 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
538 $db->filter_store_value (sub { $store_value = $_ }) ;
539
540 $_ = "original" ;
541
542 $h{"fred"} = "joe" ;
543 # fk sk fv sv
544 ok(64, checkOutput( "", "fred", "", "joe")) ;
545
546 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
547 ok(65, $h{"fred"} eq "joe");
548 # fk sk fv sv
549 ok(66, checkOutput( "", "fred", "joe", "")) ;
550
551 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
efc79c7d 552 my ($k, $v) ;
553 $k = 'fred';
554 ok(67, ! $db->seq($k, $v, R_FIRST) ) ;
555 ok(68, $k eq "fred") ;
556 ok(69, $v eq "joe") ;
9fe6733a 557 # fk sk fv sv
efc79c7d 558 ok(70, checkOutput( "fred", "fred", "joe", "")) ;
9fe6733a 559
560 # replace the filters, but remember the previous set
561 my ($old_fk) = $db->filter_fetch_key
562 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
563 my ($old_sk) = $db->filter_store_key
564 (sub { $_ = lc $_ ; $store_key = $_ }) ;
565 my ($old_fv) = $db->filter_fetch_value
566 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
567 my ($old_sv) = $db->filter_store_value
568 (sub { s/o/x/g; $store_value = $_ }) ;
569
570 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
571 $h{"Fred"} = "Joe" ;
572 # fk sk fv sv
efc79c7d 573 ok(71, checkOutput( "", "fred", "", "Jxe")) ;
9fe6733a 574
575 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
efc79c7d 576 ok(72, $h{"Fred"} eq "[Jxe]");
9fe6733a 577 # fk sk fv sv
efc79c7d 578 ok(73, checkOutput( "", "fred", "[Jxe]", "")) ;
9fe6733a 579
580 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
efc79c7d 581 $k = 'Fred'; $v ='';
582 ok(74, ! $db->seq($k, $v, R_FIRST) ) ;
5bbd4290 583 ok(75, $k eq "Fred") ;
584 #print "k [$k]\n" ;
efc79c7d 585 ok(76, $v eq "[Jxe]") ;
9fe6733a 586 # fk sk fv sv
efc79c7d 587 ok(77, checkOutput( "FRED", "fred", "[Jxe]", "")) ;
9fe6733a 588
589 # put the original filters back
590 $db->filter_fetch_key ($old_fk);
591 $db->filter_store_key ($old_sk);
592 $db->filter_fetch_value ($old_fv);
593 $db->filter_store_value ($old_sv);
594
595 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
596 $h{"fred"} = "joe" ;
efc79c7d 597 ok(78, checkOutput( "", "fred", "", "joe")) ;
9fe6733a 598
599 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
efc79c7d 600 ok(79, $h{"fred"} eq "joe");
601 ok(80, checkOutput( "", "fred", "joe", "")) ;
9fe6733a 602
603 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
efc79c7d 604 #ok(77, $db->FIRSTKEY() eq "fred") ;
605 $k = 'fred';
606 ok(81, ! $db->seq($k, $v, R_FIRST) ) ;
607 ok(82, $k eq "fred") ;
608 ok(83, $v eq "joe") ;
609 # fk sk fv sv
610 ok(84, checkOutput( "fred", "fred", "joe", "")) ;
9fe6733a 611
612 # delete the filters
613 $db->filter_fetch_key (undef);
614 $db->filter_store_key (undef);
615 $db->filter_fetch_value (undef);
616 $db->filter_store_value (undef);
617
618 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
619 $h{"fred"} = "joe" ;
efc79c7d 620 ok(85, checkOutput( "", "", "", "")) ;
9fe6733a 621
622 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
efc79c7d 623 ok(86, $h{"fred"} eq "joe");
624 ok(87, checkOutput( "", "", "", "")) ;
9fe6733a 625
626 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
efc79c7d 627 $k = 'fred';
628 ok(88, ! $db->seq($k, $v, R_FIRST) ) ;
629 ok(89, $k eq "fred") ;
630 ok(90, $v eq "joe") ;
631 ok(91, checkOutput( "", "", "", "")) ;
9fe6733a 632
633 undef $db ;
634 untie %h;
635 unlink $Dfile;
636}
637
638{
639 # DBM Filter with a closure
640
3245f058 641 use warnings ;
9fe6733a 642 use strict ;
643 my (%h, $db) ;
644
645 unlink $Dfile;
efc79c7d 646 ok(92, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
9fe6733a 647
648 my %result = () ;
649
650 sub Closure
651 {
652 my ($name) = @_ ;
653 my $count = 0 ;
654 my @kept = () ;
655
656 return sub { ++$count ;
657 push @kept, $_ ;
658 $result{$name} = "$name - $count: [@kept]" ;
659 }
660 }
661
662 $db->filter_store_key(Closure("store key")) ;
663 $db->filter_store_value(Closure("store value")) ;
664 $db->filter_fetch_key(Closure("fetch key")) ;
665 $db->filter_fetch_value(Closure("fetch value")) ;
666
667 $_ = "original" ;
668
669 $h{"fred"} = "joe" ;
efc79c7d 670 ok(93, $result{"store key"} eq "store key - 1: [fred]");
671 ok(94, $result{"store value"} eq "store value - 1: [joe]");
672 ok(95, ! defined $result{"fetch key"} );
673 ok(96, ! defined $result{"fetch value"} );
674 ok(97, $_ eq "original") ;
675
676 ok(98, $db->FIRSTKEY() eq "fred") ;
677 ok(99, $result{"store key"} eq "store key - 1: [fred]");
678 ok(100, $result{"store value"} eq "store value - 1: [joe]");
679 ok(101, $result{"fetch key"} eq "fetch key - 1: [fred]");
680 ok(102, ! defined $result{"fetch value"} );
681 ok(103, $_ eq "original") ;
9fe6733a 682
683 $h{"jim"} = "john" ;
efc79c7d 684 ok(104, $result{"store key"} eq "store key - 2: [fred jim]");
685 ok(105, $result{"store value"} eq "store value - 2: [joe john]");
686 ok(106, $result{"fetch key"} eq "fetch key - 1: [fred]");
687 ok(107, ! defined $result{"fetch value"} );
688 ok(108, $_ eq "original") ;
689
690 ok(109, $h{"fred"} eq "joe");
691 ok(110, $result{"store key"} eq "store key - 3: [fred jim fred]");
692 ok(111, $result{"store value"} eq "store value - 2: [joe john]");
693 ok(112, $result{"fetch key"} eq "fetch key - 1: [fred]");
694 ok(113, $result{"fetch value"} eq "fetch value - 1: [joe]");
695 ok(114, $_ eq "original") ;
9fe6733a 696
697 undef $db ;
698 untie %h;
699 unlink $Dfile;
700}
701
702{
703 # DBM Filter recursion detection
3245f058 704 use warnings ;
9fe6733a 705 use strict ;
706 my (%h, $db) ;
707 unlink $Dfile;
708
efc79c7d 709 ok(115, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
9fe6733a 710
711 $db->filter_store_key (sub { $_ = $h{$_} }) ;
712
713 eval '$h{1} = 1234' ;
efc79c7d 714 ok(116, $@ =~ /^recursion detected in filter_store_key at/ );
9fe6733a 715
716 undef $db ;
717 untie %h;
718 unlink $Dfile;
719}
720
2c2d71f5 721
722{
723 # Examples from the POD
724
725 my $file = "xyzt" ;
726 {
727 my $redirect = new Redirect $file ;
728
3245f058 729 use warnings FATAL => qw(all);
2c2d71f5 730 use strict ;
731 use DB_File ;
07200f1b 732 our (%h, $k, $v);
2c2d71f5 733
734 unlink "fruit" ;
735 tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH
736 or die "Cannot open file 'fruit': $!\n";
737
738 # Add a few key/value pairs to the file
739 $h{"apple"} = "red" ;
740 $h{"orange"} = "orange" ;
741 $h{"banana"} = "yellow" ;
742 $h{"tomato"} = "red" ;
743
744 # Check for existence of a key
745 print "Banana Exists\n\n" if $h{"banana"} ;
746
747 # Delete a key/value pair.
748 delete $h{"apple"} ;
749
750 # print the contents of the file
751 while (($k, $v) = each %h)
752 { print "$k -> $v\n" }
753
754 untie %h ;
755
756 unlink "fruit" ;
757 }
758
efc79c7d 759 ok(117, docat_del($file) eq <<'EOM') ;
2c2d71f5 760Banana Exists
761
762orange -> orange
763tomato -> red
764banana -> yellow
765EOM
766
767}
768
cbc5248d 769{
770 # Bug ID 20001013.009
771 #
772 # test that $hash{KEY} = undef doesn't produce the warning
773 # Use of uninitialized value in null operation
774 use warnings ;
775 use strict ;
776 use DB_File ;
777
778 unlink $Dfile;
779 my %h ;
780 my $a = "";
781 local $SIG{__WARN__} = sub {$a = $_[0]} ;
782
783 tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;
784 $h{ABC} = undef;
efc79c7d 785 ok(118, $a eq "") ;
3245f058 786 untie %h ;
787 unlink $Dfile;
788}
789
790{
791 # test that %hash = () doesn't produce the warning
792 # Argument "" isn't numeric in entersub
793 use warnings ;
794 use strict ;
795 use DB_File ;
796
797 unlink $Dfile;
798 my %h ;
799 my $a = "";
800 local $SIG{__WARN__} = sub {$a = $_[0]} ;
801
802 tie %h, 'DB_File', $Dfile or die "Can't open file: $!\n" ;
803 %h = (); ;
efc79c7d 804 ok(119, $a eq "") ;
3245f058 805 untie %h ;
cbc5248d 806 unlink $Dfile;
807}
808
0bf2e707 809{
810 # When iterating over a tied hash using "each", the key passed to FETCH
811 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
812 # key in FETCH via a filter_fetch_key method we need to check that the
813 # modified key doesn't get passed to NEXTKEY.
814 # Also Test "keys" & "values" while we are at it.
815
816 use warnings ;
817 use strict ;
818 use DB_File ;
819
820 unlink $Dfile;
821 my $bad_key = 0 ;
822 my %h = () ;
823 my $db ;
efc79c7d 824 ok(120, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
0bf2e707 825 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
826 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
827
828 $h{'Alpha_ABC'} = 2 ;
829 $h{'Alpha_DEF'} = 5 ;
830
efc79c7d 831 ok(121, $h{'Alpha_ABC'} == 2);
832 ok(122, $h{'Alpha_DEF'} == 5);
0bf2e707 833
834 my ($k, $v) = ("","");
835 while (($k, $v) = each %h) {}
efc79c7d 836 ok(123, $bad_key == 0);
0bf2e707 837
838 $bad_key = 0 ;
839 foreach $k (keys %h) {}
efc79c7d 840 ok(124, $bad_key == 0);
0bf2e707 841
842 $bad_key = 0 ;
843 foreach $v (values %h) {}
efc79c7d 844 ok(125, $bad_key == 0);
0bf2e707 845
846 undef $db ;
847 untie %h ;
848 unlink $Dfile;
849}
850
efc79c7d 851{
852 # now an error to pass 'hash' a non-code reference
853 my $dbh = new DB_File::HASHINFO ;
854
855 eval { $dbh->{hash} = 2 };
856 ok(126, $@ =~ /^Key 'hash' not associated with a code reference at/);
857
858}
859
efc79c7d 860
262eaca6 861#{
862# # recursion detection in hash
863# my %hash ;
864# my $Dfile = "xxx.db";
865# unlink $Dfile;
866# my $dbh = new DB_File::HASHINFO ;
867# $dbh->{hash} = sub { $hash{3} = 4 ; length $_[0] } ;
868#
869#
870# ok(127, tie(%hash, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh ) );
871#
872# eval { $hash{1} = 2;
873# $hash{4} = 5;
874# };
875#
876# ok(128, $@ =~ /^DB_File hash callback: recursion detected/);
877# {
5bbd4290 878# local ($^W) = 0; #no warnings;
262eaca6 879# untie %hash;
880# }
881# unlink $Dfile;
882#}
883
5bbd4290 884#ok(127,1);
885#ok(128,1);
efc79c7d 886
887{
888 # Check that two hash's don't interact
889 my %hash1 ;
890 my %hash2 ;
891 my $h1_count = 0;
892 my $h2_count = 0;
893 unlink $Dfile, $Dfile2;
894 my $dbh1 = new DB_File::HASHINFO ;
895 $dbh1->{hash} = sub { ++ $h1_count ; length $_[0] } ;
896
897 my $dbh2 = new DB_File::HASHINFO ;
898 $dbh2->{hash} = sub { ++ $h2_count ; length $_[0] } ;
899
900
901
902 my (%h);
5bbd4290 903 ok(127, tie(%hash1, 'DB_File',$Dfile, O_RDWR|O_CREAT, 0640, $dbh1 ) );
904 ok(128, tie(%hash2, 'DB_File',$Dfile2, O_RDWR|O_CREAT, 0640, $dbh2 ) );
efc79c7d 905
906 $hash1{DEFG} = 5;
907 $hash1{XYZ} = 2;
908 $hash1{ABCDE} = 5;
909
910 $hash2{defg} = 5;
911 $hash2{xyz} = 2;
912 $hash2{abcde} = 5;
913
5bbd4290 914 ok(129, $h1_count > 0);
915 ok(130, $h1_count == $h2_count);
efc79c7d 916
5bbd4290 917 ok(131, safeUntie \%hash1);
918 ok(132, safeUntie \%hash2);
efc79c7d 919 unlink $Dfile, $Dfile2;
920}
921
922{
923 # Passing undef for flags and/or mode when calling tie could cause
924 # Use of uninitialized value in subroutine entry
925
926
927 my $warn_count = 0 ;
928 #local $SIG{__WARN__} = sub { ++ $warn_count };
929 my %hash1;
930 unlink $Dfile;
931
932 tie %hash1, 'DB_File',$Dfile, undef;
5bbd4290 933 ok(133, $warn_count == 0);
efc79c7d 934 $warn_count = 0;
935 tie %hash1, 'DB_File',$Dfile, O_RDWR|O_CREAT, undef;
5bbd4290 936 ok(134, $warn_count == 0);
efc79c7d 937 tie %hash1, 'DB_File',$Dfile, undef, undef;
5bbd4290 938 ok(135, $warn_count == 0);
efc79c7d 939 $warn_count = 0;
940
6d02d21f 941 untie %hash1;
efc79c7d 942 unlink $Dfile;
943}
944
945{
946 # Check that DBM Filter can cope with read-only $_
947
948 use warnings ;
949 use strict ;
950 my (%h, $db) ;
262eaca6 951 my $Dfile = "xxy.db";
efc79c7d 952 unlink $Dfile;
953
5bbd4290 954 ok(136, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
efc79c7d 955
956 $db->filter_fetch_key (sub { }) ;
957 $db->filter_store_key (sub { }) ;
958 $db->filter_fetch_value (sub { }) ;
959 $db->filter_store_value (sub { }) ;
960
961 $_ = "original" ;
962
963 $h{"fred"} = "joe" ;
5bbd4290 964 ok(137, $h{"fred"} eq "joe");
efc79c7d 965
966 eval { grep { $h{$_} } (1, 2, 3) };
5bbd4290 967 ok (138, ! $@);
efc79c7d 968
969
970 # delete the filters
971 $db->filter_fetch_key (undef);
972 $db->filter_store_key (undef);
973 $db->filter_fetch_value (undef);
974 $db->filter_store_value (undef);
975
976 $h{"fred"} = "joe" ;
977
5bbd4290 978 ok(139, $h{"fred"} eq "joe");
efc79c7d 979
5bbd4290 980 ok(140, $db->FIRSTKEY() eq "fred") ;
efc79c7d 981
982 eval { grep { $h{$_} } (1, 2, 3) };
5bbd4290 983 ok (141, ! $@);
efc79c7d 984
985 undef $db ;
986 untie %h;
987 unlink $Dfile;
988}
989
5bbd4290 990{
991 # Check low-level API works with filter
992
993 use warnings ;
994 use strict ;
995 my (%h, $db) ;
996 my $Dfile = "xxy.db";
997 unlink $Dfile;
998
999 ok(142, $db = tie(%h, 'DB_File', $Dfile, O_RDWR|O_CREAT, 0640, $DB_HASH ) );
1000
1001
1002 $db->filter_fetch_key (sub { $_ = unpack("i", $_) } );
1003 $db->filter_store_key (sub { $_ = pack("i", $_) } );
1004 $db->filter_fetch_value (sub { $_ = unpack("i", $_) } );
1005 $db->filter_store_value (sub { $_ = pack("i", $_) } );
1006
1007 $_ = 'fred';
1008
1009 my $key = 22 ;
1010 my $value = 34 ;
1011
1012 $db->put($key, $value) ;
1013 ok 143, $key == 22;
1014 ok 144, $value == 34 ;
1015 ok 145, $_ eq 'fred';
1016 #print "k [$key][$value]\n" ;
1017
1018 my $val ;
1019 $db->get($key, $val) ;
1020 ok 146, $key == 22;
1021 ok 147, $val == 34 ;
1022 ok 148, $_ eq 'fred';
1023
1024 $key = 51 ;
1025 $value = 454;
1026 $h{$key} = $value ;
1027 ok 149, $key == 51;
1028 ok 150, $value == 454 ;
1029 ok 151, $_ eq 'fred';
1030
1031 undef $db ;
1032 untie %h;
1033 unlink $Dfile;
1034}
262eaca6 1035
a0d0e21e 1036exit ;