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