3 # $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $
8 require Config; import Config;
9 if (($Config{'extensions'} !~ /\bSDBM_File\b/) && ($^O ne 'VMS')){
23 print "not " unless $result ;
28 #If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT
37 ok(1, tie %h,'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640);
39 my $Dfile = "Op_dbmx.pag";
41 ($Dfile) = <Op_dbmx.*>;
43 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'dos') {
44 print "ok 2 # Skipped: different file permission semantics\n";
47 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
48 $blksize,$blocks) = stat($Dfile);
49 print (($mode & 0777) == 0640 ? "ok 2\n" : "not ok 2\n");
52 while (my ($key,$value) = each(%h)) {
55 print (!$i ? "ok 3\n" : "not ok 3\n");
57 $h{'goner1'} = 'snork';
61 $h{'jkl','mno'} = "JKL\034MNO";
62 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
73 $h{'goner2'} = 'snork';
77 print (tie(%h,'SDBM_File','Op_dbmx', O_RDWR, 0640) ? "ok 4\n" : "not ok 4\n");
97 $h{'goner3'} = 'snork';
103 my @values = values(%h);
105 if ($#keys == 29 && $#values == 29) {print "ok 5\n";} else {print "not ok 5\n";}
107 while (my ($key,$value) = each(%h)) {
108 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
110 $i++ if $key eq $value;
114 if ($i == 30) {print "ok 6\n";} else {print "not ok 6\n";}
116 @keys = ('blurfl', keys(%h), 'dyick');
117 if ($#keys == 31) {print "ok 7\n";} else {print "not ok 7\n";}
122 # check cache overflow and numeric keys and contents
124 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
125 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
126 print ($ok ? "ok 8\n" : "not ok 8\n");
128 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
129 $blksize,$blocks) = stat($Dfile);
130 print ($size > 0 ? "ok 9\n" : "not ok 9\n");
132 @h{0..200} = 200..400;
133 my @foo = @h{0..200};
134 print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n";
136 print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n");
137 print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n");
148 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
155 use vars qw( @ISA @EXPORT) ;
160 @EXPORT = @SDBM_File::EXPORT if defined @SDBM_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.*> ;
214 ok(19, !exists $h{'goner1'});
215 ok(20, exists $h{'foo'});
218 unlink <Op_dbmx*>, $Dfile;
225 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
229 my($fk, $sk, $fv, $sv) = @_ ;
231 $fetch_key eq $fk && $store_key eq $sk &&
232 $fetch_value eq $fv && $store_value eq $sv &&
237 ok(21, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;
239 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
240 $db->filter_store_key (sub { $store_key = $_ }) ;
241 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
242 $db->filter_store_value (sub { $store_value = $_ }) ;
248 ok(22, checkOutput( "", "fred", "", "joe")) ;
250 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
251 ok(23, $h{"fred"} eq "joe");
253 ok(24, checkOutput( "", "fred", "joe", "")) ;
255 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
256 ok(25, $db->FIRSTKEY() eq "fred") ;
258 ok(26, checkOutput( "fred", "", "", "")) ;
260 # replace the filters, but remember the previous set
261 my ($old_fk) = $db->filter_fetch_key
262 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
263 my ($old_sk) = $db->filter_store_key
264 (sub { $_ = lc $_ ; $store_key = $_ }) ;
265 my ($old_fv) = $db->filter_fetch_value
266 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
267 my ($old_sv) = $db->filter_store_value
268 (sub { s/o/x/g; $store_value = $_ }) ;
270 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
273 ok(27, checkOutput( "", "fred", "", "Jxe")) ;
275 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
276 ok(28, $h{"Fred"} eq "[Jxe]");
278 ok(29, checkOutput( "", "fred", "[Jxe]", "")) ;
280 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
281 ok(30, $db->FIRSTKEY() eq "FRED") ;
283 ok(31, checkOutput( "FRED", "", "", "")) ;
285 # put the original filters back
286 $db->filter_fetch_key ($old_fk);
287 $db->filter_store_key ($old_sk);
288 $db->filter_fetch_value ($old_fv);
289 $db->filter_store_value ($old_sv);
291 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
293 ok(32, checkOutput( "", "fred", "", "joe")) ;
295 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
296 ok(33, $h{"fred"} eq "joe");
297 ok(34, checkOutput( "", "fred", "joe", "")) ;
299 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
300 ok(35, $db->FIRSTKEY() eq "fred") ;
301 ok(36, checkOutput( "fred", "", "", "")) ;
304 $db->filter_fetch_key (undef);
305 $db->filter_store_key (undef);
306 $db->filter_fetch_value (undef);
307 $db->filter_store_value (undef);
309 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
311 ok(37, checkOutput( "", "", "", "")) ;
313 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
314 ok(38, $h{"fred"} eq "joe");
315 ok(39, checkOutput( "", "", "", "")) ;
317 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
318 ok(40, $db->FIRSTKEY() eq "fred") ;
319 ok(41, checkOutput( "", "", "", "")) ;
327 # DBM Filter with a closure
334 ok(42, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;
344 return sub { ++$count ;
346 $result{$name} = "$name - $count: [@kept]" ;
350 $db->filter_store_key(Closure("store key")) ;
351 $db->filter_store_value(Closure("store value")) ;
352 $db->filter_fetch_key(Closure("fetch key")) ;
353 $db->filter_fetch_value(Closure("fetch value")) ;
358 ok(43, $result{"store key"} eq "store key - 1: [fred]");
359 ok(44, $result{"store value"} eq "store value - 1: [joe]");
360 ok(45, !defined $result{"fetch key"} );
361 ok(46, !defined $result{"fetch value"} );
362 ok(47, $_ eq "original") ;
364 ok(48, $db->FIRSTKEY() eq "fred") ;
365 ok(49, $result{"store key"} eq "store key - 1: [fred]");
366 ok(50, $result{"store value"} eq "store value - 1: [joe]");
367 ok(51, $result{"fetch key"} eq "fetch key - 1: [fred]");
368 ok(52, ! defined $result{"fetch value"} );
369 ok(53, $_ eq "original") ;
372 ok(54, $result{"store key"} eq "store key - 2: [fred jim]");
373 ok(55, $result{"store value"} eq "store value - 2: [joe john]");
374 ok(56, $result{"fetch key"} eq "fetch key - 1: [fred]");
375 ok(57, ! defined $result{"fetch value"} );
376 ok(58, $_ eq "original") ;
378 ok(59, $h{"fred"} eq "joe");
379 ok(60, $result{"store key"} eq "store key - 3: [fred jim fred]");
380 ok(61, $result{"store value"} eq "store value - 2: [joe john]");
381 ok(62, $result{"fetch key"} eq "fetch key - 1: [fred]");
382 ok(63, $result{"fetch value"} eq "fetch value - 1: [joe]");
383 ok(64, $_ eq "original") ;
391 # DBM Filter recursion detection
397 ok(65, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;
399 $db->filter_store_key (sub { $_ = $h{$_} }) ;
401 eval '$h{1} = 1234' ;
402 ok(66, $@ =~ /^recursion detected in filter_store_key at/ );
410 # Bug ID 20001013.009
412 # test that $hash{KEY} = undef doesn't produce the warning
413 # Use of uninitialized value in null operation
421 local $SIG{__WARN__} = sub {$a = $_[0]} ;
423 ok(67, tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;