3 # $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $
6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bSDBM_File\b/) {
8 print "1..0 # Skip: no SDBM_File\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,'SDBM_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 'dos' || $^O eq 'cygwin') {
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) == ($^O eq 'vos' ? 0750 : 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,'SDBM_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");
146 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
153 use vars qw( @ISA @EXPORT) ;
158 @EXPORT = @SDBM_File::EXPORT if @SDBM_File::EXPORT ;
164 $self->SUPER::STORE($key, $value * 2) ;
170 $self->SUPER::FETCH($key) - 1 ;
177 my $value = $self->FETCH($key) ;
178 return "[[$value]]" ;
184 close FILE or die "Could not close: $!";
186 BEGIN { push @INC, '.'; }
188 eval 'use SubDB ; use Fcntl ;';
189 main::ok(13, $@ eq "") ;
193 $X = tie(%h, "SubDB","dbhash_tmp", O_RDWR|O_CREAT, 0640 );
196 main::ok(14, $@ eq "") ;
198 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
199 main::ok(15, $@ eq "") ;
200 main::ok(16, $ret == 5) ;
202 $ret = eval '$X->A_new_method("fred") ' ;
203 main::ok(17, $@ eq "") ;
204 main::ok(18, $ret eq "[[5]]") ;
208 unlink "SubDB.pm", <dbhash_tmp.*> ;
212 ok(19, !exists $h{'goner1'});
213 ok(20, exists $h{'foo'});
216 unlink <Op_dbmx*>, $Dfile;
223 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
227 my($fk, $sk, $fv, $sv) = @_ ;
229 $fetch_key eq $fk && $store_key eq $sk &&
230 $fetch_value eq $fv && $store_value eq $sv &&
235 ok(21, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;
237 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
238 $db->filter_store_key (sub { $store_key = $_ }) ;
239 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
240 $db->filter_store_value (sub { $store_value = $_ }) ;
246 ok(22, checkOutput( "", "fred", "", "joe")) ;
248 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
249 ok(23, $h{"fred"} eq "joe");
251 ok(24, checkOutput( "", "fred", "joe", "")) ;
253 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
254 ok(25, $db->FIRSTKEY() eq "fred") ;
256 ok(26, checkOutput( "fred", "", "", "")) ;
258 # replace the filters, but remember the previous set
259 my ($old_fk) = $db->filter_fetch_key
260 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
261 my ($old_sk) = $db->filter_store_key
262 (sub { $_ = lc $_ ; $store_key = $_ }) ;
263 my ($old_fv) = $db->filter_fetch_value
264 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
265 my ($old_sv) = $db->filter_store_value
266 (sub { s/o/x/g; $store_value = $_ }) ;
268 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
271 ok(27, checkOutput( "", "fred", "", "Jxe")) ;
273 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
274 ok(28, $h{"Fred"} eq "[Jxe]");
276 ok(29, checkOutput( "", "fred", "[Jxe]", "")) ;
278 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
279 ok(30, $db->FIRSTKEY() eq "FRED") ;
281 ok(31, checkOutput( "FRED", "", "", "")) ;
283 # put the original filters back
284 $db->filter_fetch_key ($old_fk);
285 $db->filter_store_key ($old_sk);
286 $db->filter_fetch_value ($old_fv);
287 $db->filter_store_value ($old_sv);
289 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
291 ok(32, checkOutput( "", "fred", "", "joe")) ;
293 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
294 ok(33, $h{"fred"} eq "joe");
295 ok(34, checkOutput( "", "fred", "joe", "")) ;
297 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
298 ok(35, $db->FIRSTKEY() eq "fred") ;
299 ok(36, checkOutput( "fred", "", "", "")) ;
302 $db->filter_fetch_key (undef);
303 $db->filter_store_key (undef);
304 $db->filter_fetch_value (undef);
305 $db->filter_store_value (undef);
307 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
309 ok(37, checkOutput( "", "", "", "")) ;
311 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
312 ok(38, $h{"fred"} eq "joe");
313 ok(39, checkOutput( "", "", "", "")) ;
315 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
316 ok(40, $db->FIRSTKEY() eq "fred") ;
317 ok(41, checkOutput( "", "", "", "")) ;
325 # DBM Filter with a closure
332 ok(42, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;
342 return sub { ++$count ;
344 $result{$name} = "$name - $count: [@kept]" ;
348 $db->filter_store_key(Closure("store key")) ;
349 $db->filter_store_value(Closure("store value")) ;
350 $db->filter_fetch_key(Closure("fetch key")) ;
351 $db->filter_fetch_value(Closure("fetch value")) ;
356 ok(43, $result{"store key"} eq "store key - 1: [fred]");
357 ok(44, $result{"store value"} eq "store value - 1: [joe]");
358 ok(45, !defined $result{"fetch key"} );
359 ok(46, !defined $result{"fetch value"} );
360 ok(47, $_ eq "original") ;
362 ok(48, $db->FIRSTKEY() eq "fred") ;
363 ok(49, $result{"store key"} eq "store key - 1: [fred]");
364 ok(50, $result{"store value"} eq "store value - 1: [joe]");
365 ok(51, $result{"fetch key"} eq "fetch key - 1: [fred]");
366 ok(52, ! defined $result{"fetch value"} );
367 ok(53, $_ eq "original") ;
370 ok(54, $result{"store key"} eq "store key - 2: [fred jim]");
371 ok(55, $result{"store value"} eq "store value - 2: [joe john]");
372 ok(56, $result{"fetch key"} eq "fetch key - 1: [fred]");
373 ok(57, ! defined $result{"fetch value"} );
374 ok(58, $_ eq "original") ;
376 ok(59, $h{"fred"} eq "joe");
377 ok(60, $result{"store key"} eq "store key - 3: [fred jim fred]");
378 ok(61, $result{"store value"} eq "store value - 2: [joe john]");
379 ok(62, $result{"fetch key"} eq "fetch key - 1: [fred]");
380 ok(63, $result{"fetch value"} eq "fetch value - 1: [joe]");
381 ok(64, $_ eq "original") ;
389 # DBM Filter recursion detection
395 ok(65, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;
397 $db->filter_store_key (sub { $_ = $h{$_} }) ;
399 eval '$h{1} = 1234' ;
400 ok(66, $@ =~ /^recursion detected in filter_store_key at/ );
408 # Bug ID 20001013.009
410 # test that $hash{KEY} = undef doesn't produce the warning
411 # Use of uninitialized value in null operation
419 local $SIG{__WARN__} = sub {$a = $_[0]} ;
421 ok(67, tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;
430 # When iterating over a tied hash using "each", the key passed to FETCH
431 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
432 # key in FETCH via a filter_fetch_key method we need to check that the
433 # modified key doesn't get passed to NEXTKEY.
434 # Also Test "keys" & "values" while we are at it.
443 ok(69, my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;
444 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
445 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
447 $h{'Alpha_ABC'} = 2 ;
448 $h{'Alpha_DEF'} = 5 ;
450 ok(70, $h{'Alpha_ABC'} == 2);
451 ok(71, $h{'Alpha_DEF'} == 5);
453 my ($k, $v) = ("","");
454 while (($k, $v) = each %h) {}
455 ok(72, $bad_key == 0);
458 foreach $k (keys %h) {}
459 ok(73, $bad_key == 0);
462 foreach $v (values %h) {}
463 ok(74, $bad_key == 0);
472 # Check that DBM Filter can cope with read-only $_
479 ok(75, my $db = tie(%h, 'SDBM_File','Op1_dbmx', O_RDWR|O_CREAT, 0640)) ;
481 $db->filter_fetch_key (sub { }) ;
482 $db->filter_store_key (sub { }) ;
483 $db->filter_fetch_value (sub { }) ;
484 $db->filter_store_value (sub { }) ;
489 ok(76, $h{"fred"} eq "joe");
491 eval { map { $h{$_} } (1, 2, 3) };
496 $db->filter_fetch_key (undef);
497 $db->filter_store_key (undef);
498 $db->filter_fetch_value (undef);
499 $db->filter_store_value (undef);
503 ok(78, $h{"fred"} eq "joe");
505 ok(79, $db->FIRSTKEY() eq "fred") ;
507 eval { map { $h{$_} } (1, 2, 3) };