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'} !~ /\bSDBM_File\b/) && ($^O ne 'VMS')){
15 #If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT
23 print (tie(%h,SDBM_File,'Op_dbmx', O_RDWR|O_CREAT, 0640)
24 ? "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' || $^O eq 'dos') {
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,SDBM_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");
131 print "not " unless $result ;
142 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ;
148 use vars qw( @ISA @EXPORT) ;
153 @EXPORT = @SDBM_File::EXPORT if defined @SDBM_File::EXPORT ;
159 $self->SUPER::STORE($key, $value * 2) ;
165 $self->SUPER::FETCH($key) - 1 ;
172 my $value = $self->FETCH($key) ;
173 return "[[$value]]" ;
181 BEGIN { push @INC, '.'; }
183 eval 'use SubDB ; use Fcntl ;';
184 main::ok(13, $@ eq "") ;
188 $X = tie(%h, "SubDB","dbhash_tmp", O_RDWR|O_CREAT, 0640 );
191 main::ok(14, $@ eq "") ;
193 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ;
194 main::ok(15, $@ eq "") ;
195 main::ok(16, $ret == 5) ;
197 $ret = eval '$X->A_new_method("fred") ' ;
198 main::ok(17, $@ eq "") ;
199 main::ok(18, $ret eq "[[5]]") ;
203 unlink "SubDB.pm", <dbhash_tmp.*> ;
207 ok(19, !exists $h{'goner1'});
208 ok(20, exists $h{'foo'});
211 unlink <Op_dbmx*>, $Dfile;
217 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
221 my($fk, $sk, $fv, $sv) = @_ ;
223 $fetch_key eq $fk && $store_key eq $sk &&
224 $fetch_value eq $fv && $store_value eq $sv &&
229 ok(21, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;
231 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
232 $db->filter_store_key (sub { $store_key = $_ }) ;
233 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
234 $db->filter_store_value (sub { $store_value = $_ }) ;
240 ok(22, checkOutput( "", "fred", "", "joe")) ;
242 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
243 ok(23, $h{"fred"} eq "joe");
245 ok(24, checkOutput( "", "fred", "joe", "")) ;
247 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
248 ok(25, $db->FIRSTKEY() eq "fred") ;
250 ok(26, checkOutput( "fred", "", "", "")) ;
252 # replace the filters, but remember the previous set
253 my ($old_fk) = $db->filter_fetch_key
254 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
255 my ($old_sk) = $db->filter_store_key
256 (sub { $_ = lc $_ ; $store_key = $_ }) ;
257 my ($old_fv) = $db->filter_fetch_value
258 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
259 my ($old_sv) = $db->filter_store_value
260 (sub { s/o/x/g; $store_value = $_ }) ;
262 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
265 ok(27, checkOutput( "", "fred", "", "Jxe")) ;
267 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
268 ok(28, $h{"Fred"} eq "[Jxe]");
270 ok(29, checkOutput( "", "fred", "[Jxe]", "")) ;
272 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
273 ok(30, $db->FIRSTKEY() eq "FRED") ;
275 ok(31, checkOutput( "FRED", "", "", "")) ;
277 # put the original filters back
278 $db->filter_fetch_key ($old_fk);
279 $db->filter_store_key ($old_sk);
280 $db->filter_fetch_value ($old_fv);
281 $db->filter_store_value ($old_sv);
283 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
285 ok(32, checkOutput( "", "fred", "", "joe")) ;
287 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
288 ok(33, $h{"fred"} eq "joe");
289 ok(34, checkOutput( "", "fred", "joe", "")) ;
291 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
292 ok(35, $db->FIRSTKEY() eq "fred") ;
293 ok(36, checkOutput( "fred", "", "", "")) ;
296 $db->filter_fetch_key (undef);
297 $db->filter_store_key (undef);
298 $db->filter_fetch_value (undef);
299 $db->filter_store_value (undef);
301 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
303 ok(37, checkOutput( "", "", "", "")) ;
305 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
306 ok(38, $h{"fred"} eq "joe");
307 ok(39, checkOutput( "", "", "", "")) ;
309 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
310 ok(40, $db->FIRSTKEY() eq "fred") ;
311 ok(41, checkOutput( "", "", "", "")) ;
319 # DBM Filter with a closure
325 ok(42, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;
335 return sub { ++$count ;
337 $result{$name} = "$name - $count: [@kept]" ;
341 $db->filter_store_key(Closure("store key")) ;
342 $db->filter_store_value(Closure("store value")) ;
343 $db->filter_fetch_key(Closure("fetch key")) ;
344 $db->filter_fetch_value(Closure("fetch value")) ;
349 ok(43, $result{"store key"} eq "store key - 1: [fred]");
350 ok(44, $result{"store value"} eq "store value - 1: [joe]");
351 ok(45, !defined $result{"fetch key"} );
352 ok(46, !defined $result{"fetch value"} );
353 ok(47, $_ eq "original") ;
355 ok(48, $db->FIRSTKEY() eq "fred") ;
356 ok(49, $result{"store key"} eq "store key - 1: [fred]");
357 ok(50, $result{"store value"} eq "store value - 1: [joe]");
358 ok(51, $result{"fetch key"} eq "fetch key - 1: [fred]");
359 ok(52, ! defined $result{"fetch value"} );
360 ok(53, $_ eq "original") ;
363 ok(54, $result{"store key"} eq "store key - 2: [fred jim]");
364 ok(55, $result{"store value"} eq "store value - 2: [joe john]");
365 ok(56, $result{"fetch key"} eq "fetch key - 1: [fred]");
366 ok(57, $result{"fetch value"} eq "");
367 ok(58, $_ eq "original") ;
369 ok(59, $h{"fred"} eq "joe");
370 ok(60, $result{"store key"} eq "store key - 3: [fred jim fred]");
371 ok(61, $result{"store value"} eq "store value - 2: [joe john]");
372 ok(62, $result{"fetch key"} eq "fetch key - 1: [fred]");
373 ok(63, $result{"fetch value"} eq "fetch value - 1: [joe]");
374 ok(64, $_ eq "original") ;
382 # DBM Filter recursion detection
387 ok(65, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ;
389 $db->filter_store_key (sub { $_ = $h{$_} }) ;
391 eval '$h{1} = 1234' ;
392 ok(66, $@ =~ /^recursion detected in filter_store_key at/ );