3 # $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $
6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bGDBM_File\b/) {
8 print "1..0 # Skip: GDBM_File was not built\n";
16 use Test::More tests => 83;
23 isa_ok(tie(%h, 'GDBM_File', 'Op_dbmx', GDBM_WRCREAT, 0640), 'GDBM_File');
25 my $Dfile = "Op_dbmx.pag";
27 ($Dfile) = <Op_dbmx*>;
30 skip "different file permission semantics on $^O", 1
31 if $^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin';
32 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
33 $blksize,$blocks) = stat($Dfile);
34 is($mode & 0777, 0640);
37 while (my ($key,$value) = each(%h)) {
42 $h{'goner1'} = 'snork';
46 $h{'jkl','mno'} = "JKL\034MNO";
47 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
58 $h{'goner2'} = 'snork';
62 isa_ok(tie(%h, 'GDBM_File', 'Op_dbmx', GDBM_WRITER, 0640), 'GDBM_File');
82 $h{'goner3'} = 'snork';
88 my @values = values(%h);
93 while (my ($key,$value) = each(%h)) {
94 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
96 $i++ if $key eq $value;
102 @keys = ('blurfl', keys(%h), 'dyick');
109 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
110 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
111 is($ok, 1, 'check cache overflow and numeric keys and contents');
113 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
114 $blksize,$blocks) = stat($Dfile);
115 cmp_ok($size, '>', 0);
117 @h{0..200} = 200..400;
118 my @foo = @h{0..200};
119 is(join(':',200..400), join(':',@foo));
125 unlink <Op_dbmx*>, $Dfile;
132 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
139 use vars qw(@ISA @EXPORT) ;
144 @EXPORT = @GDBM_File::EXPORT ;
150 $self->SUPER::STORE($key, $value * 2) ;
156 $self->SUPER::FETCH($key) - 1 ;
163 my $value = $self->FETCH($key) ;
164 return "[[$value]]" ;
170 close FILE or die "Could not close: $!";
172 BEGIN { push @INC, '.'; }
173 unlink <dbhash_tmp*> ;
180 $X = tie(%h, "SubDB","dbhash_tmp", &GDBM_WRCREAT, 0640 );
185 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
189 $ret = eval ' &GDBM_WRCREAT eq &main::GDBM_WRCREAT ' ;
193 $ret = eval '$X->A_new_method("fred") ' ;
195 main::is($ret, "[[5]]");
199 unlink "SubDB.pm", <dbhash_tmp.*> ;
204 unlink <Op_dbmx*>, $Dfile;
209 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
213 my($fk, $sk, $fv, $sv) = @_ ;
215 $fetch_key eq $fk && $store_key eq $sk &&
216 $fetch_value eq $fv && $store_value eq $sv &&
221 $db = tie %h, 'GDBM_File', 'Op_dbmx', GDBM_WRCREAT, 0640;
222 isa_ok($db, 'GDBM_File');
224 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
225 $db->filter_store_key (sub { $store_key = $_ }) ;
226 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
227 $db->filter_store_value (sub { $store_value = $_ }) ;
233 ok(checkOutput("", "fred", "", "joe"));
235 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
236 is($h{"fred"}, "joe");
238 ok(checkOutput("", "fred", "joe", ""));
240 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
241 is($db->FIRSTKEY(), "fred");
243 ok(checkOutput("fred", "", "", ""));
245 # replace the filters, but remember the previous set
246 my ($old_fk) = $db->filter_fetch_key
247 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
248 my ($old_sk) = $db->filter_store_key
249 (sub { $_ = lc $_ ; $store_key = $_ }) ;
250 my ($old_fv) = $db->filter_fetch_value
251 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
252 my ($old_sv) = $db->filter_store_value
253 (sub { s/o/x/g; $store_value = $_ }) ;
255 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
258 ok(checkOutput("", "fred", "", "Jxe"));
260 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
261 is($h{"Fred"}, "[Jxe]");
263 ok(checkOutput("", "fred", "[Jxe]", ""));
265 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
266 is($db->FIRSTKEY(), "FRED");
268 ok(checkOutput("FRED", "", "", ""));
270 # put the original filters back
271 $db->filter_fetch_key ($old_fk);
272 $db->filter_store_key ($old_sk);
273 $db->filter_fetch_value ($old_fv);
274 $db->filter_store_value ($old_sv);
276 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
278 ok(checkOutput("", "fred", "", "joe"));
280 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
281 is($h{"fred"}, "joe");
282 ok(checkOutput("", "fred", "joe", ""));
284 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
285 is($db->FIRSTKEY(), "fred");
286 ok(checkOutput("fred", "", "", ""));
289 $db->filter_fetch_key (undef);
290 $db->filter_store_key (undef);
291 $db->filter_fetch_value (undef);
292 $db->filter_store_value (undef);
294 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
296 ok(checkOutput("", "", "", ""));
298 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
299 is($h{"fred"}, "joe");
300 ok(checkOutput("", "", "", ""));
302 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
303 is($db->FIRSTKEY(), "fred");
304 ok(checkOutput("", "", "", ""));
312 # DBM Filter with a closure
317 $db = tie %h, 'GDBM_File','Op_dbmx', GDBM_WRCREAT, 0640;
318 isa_ok($db, 'GDBM_File');
328 return sub { ++$count ;
330 $result{$name} = "$name - $count: [@kept]" ;
334 $db->filter_store_key(Closure("store key")) ;
335 $db->filter_store_value(Closure("store value")) ;
336 $db->filter_fetch_key(Closure("fetch key")) ;
337 $db->filter_fetch_value(Closure("fetch value")) ;
342 is($result{"store key"}, "store key - 1: [fred]");
343 is($result{"store value"}, "store value - 1: [joe]");
344 is($result{"fetch key"}, undef);
345 is($result{"fetch value"}, undef);
348 is($db->FIRSTKEY(), "fred");
349 is($result{"store key"}, "store key - 1: [fred]");
350 is($result{"store value"}, "store value - 1: [joe]");
351 is($result{"fetch key"}, "fetch key - 1: [fred]");
352 is($result{"fetch value"}, undef);
356 is($result{"store key"}, "store key - 2: [fred jim]");
357 is($result{"store value"}, "store value - 2: [joe john]");
358 is($result{"fetch key"}, "fetch key - 1: [fred]");
359 is($result{"fetch value"}, undef);
362 is($h{"fred"}, "joe");
363 is($result{"store key"}, "store key - 3: [fred jim fred]");
364 is($result{"store value"}, "store value - 2: [joe john]");
365 is($result{"fetch key"}, "fetch key - 1: [fred]");
366 is($result{"fetch value"}, "fetch value - 1: [joe]");
375 # DBM Filter recursion detection
379 $db = tie %h, 'GDBM_File','Op_dbmx', GDBM_WRCREAT, 0640;
380 isa_ok($db, 'GDBM_File');
382 $db->filter_store_key (sub { $_ = $h{$_} }) ;
384 eval '$h{1} = 1234' ;
385 like($@, qr/^recursion detected in filter_store_key at/);
393 # Bug ID 20001013.009
395 # test that $hash{KEY} = undef doesn't produce the warning
396 # Use of uninitialized value in null operation
401 local $SIG{__WARN__} = sub {$a = $_[0]} ;
403 isa_ok(tie(%h, 'GDBM_File', 'Op_dbmx', GDBM_WRCREAT, 0640), 'GDBM_File');
411 # When iterating over a tied hash using "each", the key passed to FETCH
412 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the
413 # key in FETCH via a filter_fetch_key method we need to check that the
414 # modified key doesn't get passed to NEXTKEY.
415 # Also Test "keys" & "values" while we are at it.
420 my $db = tie %h, 'GDBM_File', 'Op_dbmx', GDBM_WRCREAT, 0640;
421 isa_ok($db, 'GDBM_File');
422 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ;
423 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ;
425 $h{'Alpha_ABC'} = 2 ;
426 $h{'Alpha_DEF'} = 5 ;
428 is($h{'Alpha_ABC'}, 2);
429 is($h{'Alpha_DEF'}, 5);
431 my ($k, $v) = ("","");
432 while (($k, $v) = each %h) {}
436 foreach $k (keys %h) {}
440 foreach $v (values %h) {}
449 # Check that DBM Filter can cope with read-only $_
454 my $db = tie %h, 'GDBM_File', 'Op1_dbmx', GDBM_WRCREAT, 0640;
455 isa_ok($db, 'GDBM_File');
457 $db->filter_fetch_key (sub { }) ;
458 $db->filter_store_key (sub { }) ;
459 $db->filter_fetch_value (sub { }) ;
460 $db->filter_store_value (sub { }) ;
465 is($h{"fred"}, "joe");
467 is_deeply([eval { map { $h{$_} } (1, 2, 3) }], [undef, undef, undef]);
472 $db->filter_fetch_key (undef);
473 $db->filter_store_key (undef);
474 $db->filter_fetch_value (undef);
475 $db->filter_store_value (undef);
479 is($h{"fred"}, "joe");
481 is($db->FIRSTKEY(), "fred");
483 is_deeply([eval { map { $h{$_} } (1, 2, 3) }], [undef, undef, undef]);