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