3 # $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $
7 unshift @INC, '../lib';
8 require Config; import Config;
9 if ($Config{'extensions'} !~ /\bNDBM_File\b/) {
16 #If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT
24 print (tie(%h,NDBM_File,'Op.dbmx', O_RDWR|O_CREAT, 0640) ? "ok 1\n" : "not ok 1\n");
26 $Dfile = "Op.dbmx.pag";
28 ($Dfile) = <Op.dbmx*>;
30 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32') {
31 print "ok 2 # Skipped: different file permission semantics\n";
34 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
35 $blksize,$blocks) = stat($Dfile);
36 print (($mode & 0777) == 0640 ? "ok 2\n" : "not ok 2\n");
38 while (($key,$value) = each(%h)) {
41 print (!$i ? "ok 3\n" : "not ok 3\n");
43 $h{'goner1'} = 'snork';
47 $h{'jkl','mno'} = "JKL\034MNO";
48 $h{'a',2,3,4,5} = join("\034",'A',2,3,4,5);
59 $h{'goner2'} = 'snork';
63 print (tie(%h,NDBM_File,'Op.dbmx', &O_RDWR, 0640) ? "ok 4\n" : "not ok 4\n");
83 $h{'goner3'} = 'snork';
91 if ($#keys == 29 && $#values == 29) {print "ok 5\n";} else {print "not ok 5\n";}
93 while (($key,$value) = each(%h)) {
94 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) {
96 $i++ if $key eq $value;
100 if ($i == 30) {print "ok 6\n";} else {print "not ok 6\n";}
102 @keys = ('blurfl', keys(%h), 'dyick');
103 if ($#keys == 31) {print "ok 7\n";} else {print "not ok 7\n";}
108 # check cache overflow and numeric keys and contents
110 for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; }
111 for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; }
112 print ($ok ? "ok 8\n" : "not ok 8\n");
114 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
115 $blksize,$blocks) = stat($Dfile);
116 print ($size > 0 ? "ok 9\n" : "not ok 9\n");
118 @h{0..200} = 200..400;
120 print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n";
122 print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n");
123 print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n");
126 unlink 'Op.dbmx.dir', $Dfile;
133 print "not " unless $result ;
144 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
150 use vars qw(@ISA @EXPORT) ;
155 @EXPORT = @NDBM_File::EXPORT if defined @NDBM_File::EXPORT ;
161 $self->SUPER::STORE($key, $value * 2) ;
167 $self->SUPER::FETCH($key) - 1 ;
174 my $value = $self->FETCH($key) ;
175 return "[[$value]]" ;
183 BEGIN { push @INC, '.'; }
185 eval 'use SubDB ; use Fcntl ; ';
186 main::ok(13, $@ eq "") ;
190 $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640 );
193 main::ok(14, $@ eq "") ;
195 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
196 main::ok(15, $@ eq "") ;
197 main::ok(16, $ret == 5) ;
199 $ret = eval '$X->A_new_method("fred") ' ;
200 main::ok(17, $@ eq "") ;
201 main::ok(18, $ret eq "[[5]]") ;
205 unlink "SubDB.pm", <dbhash.tmp*> ;
213 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
217 my($fk, $sk, $fv, $sv) = @_ ;
219 $fetch_key eq $fk && $store_key eq $sk &&
220 $fetch_value eq $fv && $store_value eq $sv &&
225 ok(19, $db = tie(%h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
227 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
228 $db->filter_store_key (sub { $store_key = $_ }) ;
229 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
230 $db->filter_store_value (sub { $store_value = $_ }) ;
236 ok(20, checkOutput( "", "fred", "", "joe")) ;
238 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
239 ok(21, $h{"fred"} eq "joe");
241 ok(22, checkOutput( "", "fred", "joe", "")) ;
243 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
244 ok(23, $db->FIRSTKEY() eq "fred") ;
246 ok(24, checkOutput( "fred", "", "", "")) ;
248 # replace the filters, but remember the previous set
249 my ($old_fk) = $db->filter_fetch_key
250 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
251 my ($old_sk) = $db->filter_store_key
252 (sub { $_ = lc $_ ; $store_key = $_ }) ;
253 my ($old_fv) = $db->filter_fetch_value
254 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
255 my ($old_sv) = $db->filter_store_value
256 (sub { s/o/x/g; $store_value = $_ }) ;
258 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
261 ok(25, checkOutput( "", "fred", "", "Jxe")) ;
263 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
264 ok(26, $h{"Fred"} eq "[Jxe]");
266 ok(27, checkOutput( "", "fred", "[Jxe]", "")) ;
268 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
269 ok(28, $db->FIRSTKEY() eq "FRED") ;
271 ok(29, checkOutput( "FRED", "", "", "")) ;
273 # put the original filters back
274 $db->filter_fetch_key ($old_fk);
275 $db->filter_store_key ($old_sk);
276 $db->filter_fetch_value ($old_fv);
277 $db->filter_store_value ($old_sv);
279 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
281 ok(30, checkOutput( "", "fred", "", "joe")) ;
283 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
284 ok(31, $h{"fred"} eq "joe");
285 ok(32, checkOutput( "", "fred", "joe", "")) ;
287 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
288 ok(33, $db->FIRSTKEY() eq "fred") ;
289 ok(34, checkOutput( "fred", "", "", "")) ;
292 $db->filter_fetch_key (undef);
293 $db->filter_store_key (undef);
294 $db->filter_fetch_value (undef);
295 $db->filter_store_value (undef);
297 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
299 ok(35, checkOutput( "", "", "", "")) ;
301 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
302 ok(36, $h{"fred"} eq "joe");
303 ok(37, checkOutput( "", "", "", "")) ;
305 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
306 ok(38, $db->FIRSTKEY() eq "fred") ;
307 ok(39, checkOutput( "", "", "", "")) ;
315 # DBM Filter with a closure
321 ok(40, $db = tie(%h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
331 return sub { ++$count ;
333 $result{$name} = "$name - $count: [@kept]" ;
337 $db->filter_store_key(Closure("store key")) ;
338 $db->filter_store_value(Closure("store value")) ;
339 $db->filter_fetch_key(Closure("fetch key")) ;
340 $db->filter_fetch_value(Closure("fetch value")) ;
345 ok(41, $result{"store key"} eq "store key - 1: [fred]");
346 ok(42, $result{"store value"} eq "store value - 1: [joe]");
347 ok(43, !defined $result{"fetch key"} );
348 ok(44, !defined $result{"fetch value"} );
349 ok(45, $_ eq "original") ;
351 ok(46, $db->FIRSTKEY() eq "fred") ;
352 ok(47, $result{"store key"} eq "store key - 1: [fred]");
353 ok(48, $result{"store value"} eq "store value - 1: [joe]");
354 ok(49, $result{"fetch key"} eq "fetch key - 1: [fred]");
355 ok(50, ! defined $result{"fetch value"} );
356 ok(51, $_ eq "original") ;
359 ok(52, $result{"store key"} eq "store key - 2: [fred jim]");
360 ok(53, $result{"store value"} eq "store value - 2: [joe john]");
361 ok(54, $result{"fetch key"} eq "fetch key - 1: [fred]");
362 ok(55, $result{"fetch value"} eq "");
363 ok(56, $_ eq "original") ;
365 ok(57, $h{"fred"} eq "joe");
366 ok(58, $result{"store key"} eq "store key - 3: [fred jim fred]");
367 ok(59, $result{"store value"} eq "store value - 2: [joe john]");
368 ok(60, $result{"fetch key"} eq "fetch key - 1: [fred]");
369 ok(61, $result{"fetch value"} eq "fetch value - 1: [joe]");
370 ok(62, $_ eq "original") ;
378 # DBM Filter recursion detection
383 ok(63, $db = tie(%h, 'NDBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
385 $db->filter_store_key (sub { $_ = $h{$_} }) ;
387 eval '$h{1} = 1234' ;
388 ok(64, $@ =~ /^recursion detected in filter_store_key at/ );