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";
16 use Test::More tests => 78;
19 #If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT
26 isa_ok(tie(%h,'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640), 'NDBM_File');
28 my $Dfile = "Op.dbmx.pag";
30 ($Dfile) = <Op.dbmx*>;
33 skip "different file permission semantics on $^O", 1
34 if $^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'NetWare';
35 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
36 $blksize,$blocks) = stat($Dfile);
37 is($mode & 0777, 0640);
40 while (my ($key,$value) = each(%h)) {
45 $h{'goner1'} = 'snork';
49 $h{'jkl','mno'} = "JKL\034MNO";
50 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
61 $h{'goner2'} = 'snork';
65 isa_ok(tie(%h,'NDBM_File','Op.dbmx', O_RDWR, 0640), 'NDBM_File');
85 $h{'goner3'} = 'snork';
91 my @values = values(%h);
96 while (my ($key,$value) = each(%h)) {
97 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
99 $i++ if $key eq $value;
105 @keys = ('blurfl', keys(%h), 'dyick');
112 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
113 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
114 is($ok, 1, 'check cache overflow and numeric keys and contents');
116 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
117 $blksize,$blocks) = stat($Dfile);
118 cmp_ok($size, '>', 0);
120 @h{0..200} = 200..400;
121 my @foo = @h{0..200};
122 is(join(':',200..400), join(':',@foo));
128 unlink <Op.dbmx*>, $Dfile;
135 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
142 use vars qw(@ISA @EXPORT) ;
147 @EXPORT = @NDBM_File::EXPORT if defined @NDBM_File::EXPORT ;
153 $self->SUPER::STORE($key, $value * 2) ;
159 $self->SUPER::FETCH($key) - 1 ;
166 my $value = $self->FETCH($key) ;
167 return "[[$value]]" ;
175 BEGIN { push @INC, '.'; }
177 eval 'use SubDB ; use Fcntl ; ';
182 $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640 );
187 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
191 $ret = eval '$X->A_new_method("fred") ' ;
193 main::is($ret, "[[5]]");
197 unlink "SubDB.pm", <dbhash.tmp*> ;
204 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
208 my($fk, $sk, $fv, $sv) = @_ ;
210 $fetch_key eq $fk && $store_key eq $sk &&
211 $fetch_value eq $fv && $store_value eq $sv &&
216 $db = tie %h, 'NDBM_File', 'Op.dbmx', O_RDWR|O_CREAT, 0640;
217 isa_ok($db, 'NDBM_File');
219 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
220 $db->filter_store_key (sub { $store_key = $_ }) ;
221 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
222 $db->filter_store_value (sub { $store_value = $_ }) ;
228 ok(checkOutput("", "fred", "", "joe"));
230 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
231 is($h{"fred"}, "joe");
233 ok(checkOutput("", "fred", "joe", ""));
235 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
236 is($db->FIRSTKEY(), "fred");
238 ok(checkOutput("fred", "", "", ""));
240 # replace the filters, but remember the previous set
241 my ($old_fk) = $db->filter_fetch_key
242 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
243 my ($old_sk) = $db->filter_store_key
244 (sub { $_ = lc $_ ; $store_key = $_ }) ;
245 my ($old_fv) = $db->filter_fetch_value
246 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
247 my ($old_sv) = $db->filter_store_value
248 (sub { s/o/x/g; $store_value = $_ }) ;
250 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
253 ok(checkOutput("", "fred", "", "Jxe"));
255 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
256 is($h{"Fred"}, "[Jxe]");
258 ok(checkOutput("", "fred", "[Jxe]", ""));
260 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
261 is($db->FIRSTKEY(), "FRED");
263 ok(checkOutput("FRED", "", "", ""));
265 # put the original filters back
266 $db->filter_fetch_key ($old_fk);
267 $db->filter_store_key ($old_sk);
268 $db->filter_fetch_value ($old_fv);
269 $db->filter_store_value ($old_sv);
271 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
273 ok(checkOutput("", "fred", "", "joe"));
275 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
276 is($h{"fred"}, "joe");
277 ok(checkOutput("", "fred", "joe", ""));
279 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
280 is($db->FIRSTKEY(), "fred");
281 ok(checkOutput("fred", "", "", ""));
284 $db->filter_fetch_key (undef);
285 $db->filter_store_key (undef);
286 $db->filter_fetch_value (undef);
287 $db->filter_store_value (undef);
289 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
291 ok(checkOutput("", "", "", ""));
293 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
294 is($h{"fred"}, "joe");
295 ok(checkOutput("", "", "", ""));
297 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
298 is($db->FIRSTKEY(), "fred");
299 ok(checkOutput("", "", "", ""));
307 # DBM Filter with a closure
312 $db = tie %h, 'NDBM_File', 'Op.dbmx', O_RDWR|O_CREAT, 0640;
313 isa_ok($db, 'NDBM_File');
323 return sub { ++$count ;
325 $result{$name} = "$name - $count: [@kept]" ;
329 $db->filter_store_key(Closure("store key")) ;
330 $db->filter_store_value(Closure("store value")) ;
331 $db->filter_fetch_key(Closure("fetch key")) ;
332 $db->filter_fetch_value(Closure("fetch value")) ;
337 is($result{"store key"}, "store key - 1: [fred]");
338 is($result{"store value"}, "store value - 1: [joe]");
339 is($result{"fetch key"}, undef);
340 is($result{"fetch value"}, undef);
343 is($db->FIRSTKEY(), "fred");
344 is($result{"store key"}, "store key - 1: [fred]");
345 is($result{"store value"}, "store value - 1: [joe]");
346 is($result{"fetch key"}, "fetch key - 1: [fred]");
347 is($result{"fetch value"}, undef);
351 is($result{"store key"}, "store key - 2: [fred jim]");
352 is($result{"store value"}, "store value - 2: [joe john]");
353 is($result{"fetch key"}, "fetch key - 1: [fred]");
354 is($result{"fetch value"}, undef);
357 is($h{"fred"}, "joe");
358 is($result{"store key"}, "store key - 3: [fred jim fred]");
359 is($result{"store value"}, "store value - 2: [joe john]");
360 is($result{"fetch key"}, "fetch key - 1: [fred]");
361 is($result{"fetch value"}, "fetch value - 1: [joe]");
370 # DBM Filter recursion detection
374 $db = tie %h, 'NDBM_File', 'Op.dbmx', O_RDWR|O_CREAT, 0640;
375 isa_ok($db, 'NDBM_File');
377 $db->filter_store_key (sub { $_ = $h{$_} }) ;
379 eval '$h{1} = 1234' ;
380 like($@, qr/^recursion detected in filter_store_key at/);
388 # Bug ID 20001013.009
390 # test that $hash{KEY} = undef doesn't produce the warning
391 # Use of uninitialized value in null operation
396 local $SIG{__WARN__} = sub {$a = $_[0]} ;
398 isa_ok(tie(%h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640), 'NDBM_File');
402 # When iterating over a tied hash using "each", the key passed to FETCH
403 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
404 # key in FETCH via a filter_fetch_key method we need to check that the
405 # modified key doesn't get passed to NEXTKEY.
406 # Also Test "keys" & "values" while we are at it.
411 my $db = tie %h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640;
412 isa_ok($db, 'NDBM_File');
413 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
414 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
416 $h{'Alpha_ABC'} = 2 ;
417 $h{'Alpha_DEF'} = 5 ;
419 is($h{'Alpha_ABC'}, 2);
420 is($h{'Alpha_DEF'}, 5);
422 my ($k, $v) = ("","");
423 while (($k, $v) = each %h) {}
427 foreach $k (keys %h) {}
431 foreach $v (values %h) {}
441 # Check that DBM Filter can cope with read-only $_
446 my $db = tie %h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640;
447 isa_ok($db, 'NDBM_File');
449 $db->filter_fetch_key (sub { }) ;
450 $db->filter_store_key (sub { }) ;
451 $db->filter_fetch_value (sub { }) ;
452 $db->filter_store_value (sub { }) ;
457 is($h{"fred"}, "joe");
459 eval { grep { $h{$_} } (1, 2, 3) };
464 $db->filter_fetch_key (undef);
465 $db->filter_store_key (undef);
466 $db->filter_fetch_value (undef);
467 $db->filter_store_value (undef);
471 is($h{"fred"}, "joe");
473 is($db->FIRSTKEY(), "fred");
475 eval { grep { $h{$_} } (1, 2, 3) };