3 # $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $
6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bNDBM_File\b/) {
8 print "1..0 # Skip: NDBM_File was not built\n";
21 print "not " unless $result ;
26 #If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT
35 ok(1, tie(%h,'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640));
37 my $Dfile = "Op.dbmx.pag";
39 ($Dfile) = <Op.dbmx*>;
41 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'MacOS') {
42 print "ok 2 # Skipped: different file permission semantics\n";
45 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
46 $blksize,$blocks) = stat($Dfile);
47 print (($mode & 0777) == 0640 ? "ok 2\n" : "not ok 2\n");
50 while (my ($key,$value) = each(%h)) {
53 print (!$i ? "ok 3\n" : "not ok 3\n");
55 $h{'goner1'} = 'snork';
59 $h{'jkl','mno'} = "JKL\034MNO";
60 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
71 $h{'goner2'} = 'snork';
75 print (tie(%h,'NDBM_File','Op.dbmx', &O_RDWR, 0640) ? "ok 4\n" : "not ok 4\n");
95 $h{'goner3'} = 'snork';
101 my @values = values(%h);
103 if ($#keys == 29 && $#values == 29) {print "ok 5\n";} else {print "not ok 5\n";}
105 while (my ($key,$value) = each(%h)) {
106 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
108 $i++ if $key eq $value;
112 if ($i == 30) {print "ok 6\n";} else {print "not ok 6\n";}
114 @keys = ('blurfl', keys(%h), 'dyick');
115 if ($#keys == 31) {print "ok 7\n";} else {print "not ok 7\n";}
120 # check cache overflow and numeric keys and contents
122 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
123 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
124 print ($ok ? "ok 8\n" : "not ok 8\n");
126 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
127 $blksize,$blocks) = stat($Dfile);
128 print ($size > 0 ? "ok 9\n" : "not ok 9\n");
130 @h{0..200} = 200..400;
131 my @foo = @h{0..200};
132 print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n";
134 print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n");
135 print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n");
138 unlink <Op.dbmx*>, $Dfile;
148 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
155 use vars qw(@ISA @EXPORT) ;
160 @EXPORT = @NDBM_File::EXPORT if defined @NDBM_File::EXPORT ;
166 $self->SUPER::STORE($key, $value * 2) ;
172 $self->SUPER::FETCH($key) - 1 ;
179 my $value = $self->FETCH($key) ;
180 return "[[$value]]" ;
188 BEGIN { push @INC, '.'; }
190 eval 'use SubDB ; use Fcntl ; ';
191 main::ok(13, $@ eq "") ;
195 $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640 );
198 main::ok(14, $@ eq "") ;
200 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
201 main::ok(15, $@ eq "") ;
202 main::ok(16, $ret == 5) ;
204 $ret = eval '$X->A_new_method("fred") ' ;
205 main::ok(17, $@ eq "") ;
206 main::ok(18, $ret eq "[[5]]") ;
210 unlink "SubDB.pm", <dbhash.tmp*> ;
219 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
223 my($fk, $sk, $fv, $sv) = @_ ;
225 $fetch_key eq $fk && $store_key eq $sk &&
226 $fetch_value eq $fv && $store_value eq $sv &&
231 ok(19, $db = tie(%h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
233 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
234 $db->filter_store_key (sub { $store_key = $_ }) ;
235 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
236 $db->filter_store_value (sub { $store_value = $_ }) ;
242 ok(20, checkOutput( "", "fred", "", "joe")) ;
244 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
245 ok(21, $h{"fred"} eq "joe");
247 ok(22, checkOutput( "", "fred", "joe", "")) ;
249 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
250 ok(23, $db->FIRSTKEY() eq "fred") ;
252 ok(24, checkOutput( "fred", "", "", "")) ;
254 # replace the filters, but remember the previous set
255 my ($old_fk) = $db->filter_fetch_key
256 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
257 my ($old_sk) = $db->filter_store_key
258 (sub { $_ = lc $_ ; $store_key = $_ }) ;
259 my ($old_fv) = $db->filter_fetch_value
260 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
261 my ($old_sv) = $db->filter_store_value
262 (sub { s/o/x/g; $store_value = $_ }) ;
264 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
267 ok(25, checkOutput( "", "fred", "", "Jxe")) ;
269 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
270 ok(26, $h{"Fred"} eq "[Jxe]");
272 ok(27, checkOutput( "", "fred", "[Jxe]", "")) ;
274 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
275 ok(28, $db->FIRSTKEY() eq "FRED") ;
277 ok(29, checkOutput( "FRED", "", "", "")) ;
279 # put the original filters back
280 $db->filter_fetch_key ($old_fk);
281 $db->filter_store_key ($old_sk);
282 $db->filter_fetch_value ($old_fv);
283 $db->filter_store_value ($old_sv);
285 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
287 ok(30, checkOutput( "", "fred", "", "joe")) ;
289 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
290 ok(31, $h{"fred"} eq "joe");
291 ok(32, checkOutput( "", "fred", "joe", "")) ;
293 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
294 ok(33, $db->FIRSTKEY() eq "fred") ;
295 ok(34, checkOutput( "fred", "", "", "")) ;
298 $db->filter_fetch_key (undef);
299 $db->filter_store_key (undef);
300 $db->filter_fetch_value (undef);
301 $db->filter_store_value (undef);
303 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
305 ok(35, checkOutput( "", "", "", "")) ;
307 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
308 ok(36, $h{"fred"} eq "joe");
309 ok(37, checkOutput( "", "", "", "")) ;
311 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
312 ok(38, $db->FIRSTKEY() eq "fred") ;
313 ok(39, checkOutput( "", "", "", "")) ;
321 # DBM Filter with a closure
328 ok(40, $db = tie(%h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
338 return sub { ++$count ;
340 $result{$name} = "$name - $count: [@kept]" ;
344 $db->filter_store_key(Closure("store key")) ;
345 $db->filter_store_value(Closure("store value")) ;
346 $db->filter_fetch_key(Closure("fetch key")) ;
347 $db->filter_fetch_value(Closure("fetch value")) ;
352 ok(41, $result{"store key"} eq "store key - 1: [fred]");
353 ok(42, $result{"store value"} eq "store value - 1: [joe]");
354 ok(43, !defined $result{"fetch key"} );
355 ok(44, !defined $result{"fetch value"} );
356 ok(45, $_ eq "original") ;
358 ok(46, $db->FIRSTKEY() eq "fred") ;
359 ok(47, $result{"store key"} eq "store key - 1: [fred]");
360 ok(48, $result{"store value"} eq "store value - 1: [joe]");
361 ok(49, $result{"fetch key"} eq "fetch key - 1: [fred]");
362 ok(50, ! defined $result{"fetch value"} );
363 ok(51, $_ eq "original") ;
366 ok(52, $result{"store key"} eq "store key - 2: [fred jim]");
367 ok(53, $result{"store value"} eq "store value - 2: [joe john]");
368 ok(54, $result{"fetch key"} eq "fetch key - 1: [fred]");
369 ok(55, ! defined $result{"fetch value"} );
370 ok(56, $_ eq "original") ;
372 ok(57, $h{"fred"} eq "joe");
373 ok(58, $result{"store key"} eq "store key - 3: [fred jim fred]");
374 ok(59, $result{"store value"} eq "store value - 2: [joe john]");
375 ok(60, $result{"fetch key"} eq "fetch key - 1: [fred]");
376 ok(61, $result{"fetch value"} eq "fetch value - 1: [joe]");
377 ok(62, $_ eq "original") ;
385 # DBM Filter recursion detection
391 ok(63, $db = tie(%h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
393 $db->filter_store_key (sub { $_ = $h{$_} }) ;
395 eval '$h{1} = 1234' ;
396 ok(64, $@ =~ /^recursion detected in filter_store_key at/ );
404 # Bug ID 20001013.009
406 # test that $hash{KEY} = undef doesn't produce the warning
407 # Use of uninitialized value in null operation
415 local $SIG{__WARN__} = sub {$a = $_[0]} ;
417 ok(65, tie(%h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
421 # When iterating over a tied hash using "each", the key passed to FETCH
422 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
423 # key in FETCH via a filter_fetch_key method we need to check that the
424 # modified key doesn't get passed to NEXTKEY.
425 # Also Test "keys" & "values" while we are at it.
434 ok(66, my $db = tie(%h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
435 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
436 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
438 $h{'Alpha_ABC'} = 2 ;
439 $h{'Alpha_DEF'} = 5 ;
441 ok(67, $h{'Alpha_ABC'} == 2);
442 ok(68, $h{'Alpha_DEF'} == 5);
444 my ($k, $v) = ("","");
445 while (($k, $v) = each %h) {}
446 ok(69, $bad_key == 0);
449 foreach $k (keys %h) {}
450 ok(70, $bad_key == 0);
453 foreach $v (values %h) {}
454 ok(71, $bad_key == 0);
463 # Check that DBM Filter can cope with read-only $_
470 ok(72, my $db = tie(%h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
472 $db->filter_fetch_key (sub { }) ;
473 $db->filter_store_key (sub { }) ;
474 $db->filter_fetch_value (sub { }) ;
475 $db->filter_store_value (sub { }) ;
480 ok(73, $h{"fred"} eq "joe");
482 eval { grep { $h{$_} } (1, 2, 3) };
487 $db->filter_fetch_key (undef);
488 $db->filter_store_key (undef);
489 $db->filter_fetch_value (undef);
490 $db->filter_store_value (undef);
494 ok(75, $h{"fred"} eq "joe");
496 ok(76, $db->FIRSTKEY() eq "fred") ;
498 eval { grep { $h{$_} } (1, 2, 3) };