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