3 # $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $
8 require Config; import Config;
9 if ($Config{'extensions'} !~ /\bGDBM_File\b/) {
10 print "1..0 # Skip: GDBM_File was not built\n";
27 print (tie(%h,'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640) ? "ok 1\n" : "not ok 1\n");
29 my $Dfile = "Op.dbmx.pag";
31 ($Dfile) = <Op.dbmx*>;
33 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'dos') {
34 print "ok 2 # Skipped: different file permission semantics\n";
37 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
38 $blksize,$blocks) = stat($Dfile);
39 print (($mode & 0777) == 0640 ? "ok 2\n" : "not ok 2\n");
42 while (my ($key,$value) = each(%h)) {
45 print (!$i ? "ok 3\n" : "not ok 3\n");
47 $h{'goner1'} = 'snork';
51 $h{'jkl','mno'} = "JKL\034MNO";
52 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
63 $h{'goner2'} = 'snork';
67 print (tie(%h,'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640) ? "ok 4\n" : "not ok 4\n");
87 $h{'goner3'} = 'snork';
93 my @values = values(%h);
95 if ($#keys == 29 && $#values == 29) {print "ok 5\n";} else {print "not ok 5\n";}
97 while (my ($key,$value) = each(%h)) {
98 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
100 $i++ if $key eq $value;
104 if ($i == 30) {print "ok 6\n";} else {print "not ok 6\n";}
106 @keys = ('blurfl', keys(%h), 'dyick');
107 if ($#keys == 31) {print "ok 7\n";} else {print "not ok 7\n";}
112 # check cache overflow and numeric keys and contents
114 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
115 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
116 print ($ok ? "ok 8\n" : "not ok 8\n");
118 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
119 $blksize,$blocks) = stat($Dfile);
120 print ($size > 0 ? "ok 9\n" : "not ok 9\n");
122 @h{0..200} = 200..400;
123 my @foo = @h{0..200};
124 print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n";
126 print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n");
127 print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n");
130 unlink 'Op.dbmx.dir', $Dfile;
137 print "not " unless $result ;
149 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
155 use vars qw(@ISA @EXPORT) ;
160 @EXPORT = @GDBM_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, '.'; }
189 unlink <dbhash.tmp*> ;
192 main::ok(13, $@ eq "") ;
196 $X = tie(%h, "SubDB","dbhash.tmp", &GDBM_WRCREAT, 0640 );
199 main::ok(14, $@ eq "") ;
201 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
202 main::ok(15, $@ eq "") ;
203 main::ok(16, $ret == 5) ;
205 $ret = eval ' &GDBM_WRCREAT eq &main::GDBM_WRCREAT ' ;
206 main::ok(17, $@ eq "" ) ;
207 main::ok(18, $ret == 1) ;
209 $ret = eval '$X->A_new_method("fred") ' ;
210 main::ok(19, $@ eq "") ;
211 main::ok(20, $ret eq "[[5]]") ;
215 unlink "SubDB.pm", <dbhash.tmp*> ;
224 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
228 my($fk, $sk, $fv, $sv) = @_ ;
230 $fetch_key eq $fk && $store_key eq $sk &&
231 $fetch_value eq $fv && $store_value eq $sv &&
236 ok(21, $db = tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640)) ;
238 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
239 $db->filter_store_key (sub { $store_key = $_ }) ;
240 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
241 $db->filter_store_value (sub { $store_value = $_ }) ;
247 ok(22, checkOutput( "", "fred", "", "joe")) ;
249 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
250 ok(23, $h{"fred"} eq "joe");
252 ok(24, checkOutput( "", "fred", "joe", "")) ;
254 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
255 ok(25, $db->FIRSTKEY() eq "fred") ;
257 ok(26, checkOutput( "fred", "", "", "")) ;
259 # replace the filters, but remember the previous set
260 my ($old_fk) = $db->filter_fetch_key
261 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
262 my ($old_sk) = $db->filter_store_key
263 (sub { $_ = lc $_ ; $store_key = $_ }) ;
264 my ($old_fv) = $db->filter_fetch_value
265 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
266 my ($old_sv) = $db->filter_store_value
267 (sub { s/o/x/g; $store_value = $_ }) ;
269 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
272 ok(27, checkOutput( "", "fred", "", "Jxe")) ;
274 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
275 ok(28, $h{"Fred"} eq "[Jxe]");
277 ok(29, checkOutput( "", "fred", "[Jxe]", "")) ;
279 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
280 ok(30, $db->FIRSTKEY() eq "FRED") ;
282 ok(31, checkOutput( "FRED", "", "", "")) ;
284 # put the original filters back
285 $db->filter_fetch_key ($old_fk);
286 $db->filter_store_key ($old_sk);
287 $db->filter_fetch_value ($old_fv);
288 $db->filter_store_value ($old_sv);
290 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
292 ok(32, checkOutput( "", "fred", "", "joe")) ;
294 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
295 ok(33, $h{"fred"} eq "joe");
296 ok(34, checkOutput( "", "fred", "joe", "")) ;
298 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
299 ok(35, $db->FIRSTKEY() eq "fred") ;
300 ok(36, checkOutput( "fred", "", "", "")) ;
303 $db->filter_fetch_key (undef);
304 $db->filter_store_key (undef);
305 $db->filter_fetch_value (undef);
306 $db->filter_store_value (undef);
308 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
310 ok(37, checkOutput( "", "", "", "")) ;
312 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
313 ok(38, $h{"fred"} eq "joe");
314 ok(39, checkOutput( "", "", "", "")) ;
316 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
317 ok(40, $db->FIRSTKEY() eq "fred") ;
318 ok(41, checkOutput( "", "", "", "")) ;
326 # DBM Filter with a closure
333 ok(42, $db = tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640)) ;
343 return sub { ++$count ;
345 $result{$name} = "$name - $count: [@kept]" ;
349 $db->filter_store_key(Closure("store key")) ;
350 $db->filter_store_value(Closure("store value")) ;
351 $db->filter_fetch_key(Closure("fetch key")) ;
352 $db->filter_fetch_value(Closure("fetch value")) ;
357 ok(43, $result{"store key"} eq "store key - 1: [fred]");
358 ok(44, $result{"store value"} eq "store value - 1: [joe]");
359 ok(45, !defined $result{"fetch key"} );
360 ok(46, !defined $result{"fetch value"} );
361 ok(47, $_ eq "original") ;
363 ok(48, $db->FIRSTKEY() eq "fred") ;
364 ok(49, $result{"store key"} eq "store key - 1: [fred]");
365 ok(50, $result{"store value"} eq "store value - 1: [joe]");
366 ok(51, $result{"fetch key"} eq "fetch key - 1: [fred]");
367 ok(52, ! defined $result{"fetch value"} );
368 ok(53, $_ eq "original") ;
371 ok(54, $result{"store key"} eq "store key - 2: [fred jim]");
372 ok(55, $result{"store value"} eq "store value - 2: [joe john]");
373 ok(56, $result{"fetch key"} eq "fetch key - 1: [fred]");
374 ok(57, ! defined $result{"fetch value"} );
375 ok(58, $_ eq "original") ;
377 ok(59, $h{"fred"} eq "joe");
378 ok(60, $result{"store key"} eq "store key - 3: [fred jim fred]");
379 ok(61, $result{"store value"} eq "store value - 2: [joe john]");
380 ok(62, $result{"fetch key"} eq "fetch key - 1: [fred]");
381 ok(63, $result{"fetch value"} eq "fetch value - 1: [joe]");
382 ok(64, $_ eq "original") ;
390 # DBM Filter recursion detection
396 ok(65, $db = tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640)) ;
398 $db->filter_store_key (sub { $_ = $h{$_} }) ;
400 eval '$h{1} = 1234' ;
401 ok(66, $@ =~ /^recursion detected in filter_store_key at/ );
409 # Bug ID 20001013.009
411 # test that $hash{KEY} = undef doesn't produce the warning
412 # Use of uninitialized value in null operation
420 local $SIG{__WARN__} = sub {$a = $_[0]} ;
422 ok(67, tie(%h, 'GDBM_File','Op.dbmx', &GDBM_WRCREAT, 0640));