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'} !~ /\bODBM_File\b/) {
10 print "1..0 # Skip: ODBM_File was not built\n";
16 #If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT
24 print (tie(%h,ODBM_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,ODBM_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 = @ODBM_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) = @_ ;
218 print "# ", join('|', $fetch_key, $fk, $store_key, $sk,
219 $fetch_value, $fv, $store_value, $sv, $_), "\n";
221 $fetch_key eq $fk && $store_key eq $sk &&
222 $fetch_value eq $fv && $store_value eq $sv &&
227 ok(19, $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
229 $db->filter_fetch_key (sub { $fetch_key = $_ }) ;
230 $db->filter_store_key (sub { $store_key = $_ }) ;
231 $db->filter_fetch_value (sub { $fetch_value = $_}) ;
232 $db->filter_store_value (sub { $store_value = $_ }) ;
238 ok(20, checkOutput( "", "fred", "", "joe")) ;
240 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
241 ok(21, $h{"fred"} eq "joe");
243 ok(22, checkOutput( "", "fred", "joe", "")) ;
245 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
246 ok(23, $db->FIRSTKEY() eq "fred") ;
248 ok(24, checkOutput( "fred", "", "", "")) ;
250 # replace the filters, but remember the previous set
251 my ($old_fk) = $db->filter_fetch_key
252 (sub { $_ = uc $_ ; $fetch_key = $_ }) ;
253 my ($old_sk) = $db->filter_store_key
254 (sub { $_ = lc $_ ; $store_key = $_ }) ;
255 my ($old_fv) = $db->filter_fetch_value
256 (sub { $_ = "[$_]"; $fetch_value = $_ }) ;
257 my ($old_sv) = $db->filter_store_value
258 (sub { s/o/x/g; $store_value = $_ }) ;
260 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
263 ok(25, checkOutput( "", "fred", "", "Jxe")) ;
265 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
266 ok(26, $h{"Fred"} eq "[Jxe]");
268 ok(27, checkOutput( "", "fred", "[Jxe]", "")) ;
270 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
271 ok(28, $db->FIRSTKEY() eq "FRED") ;
273 ok(29, checkOutput( "FRED", "", "", "")) ;
275 # put the original filters back
276 $db->filter_fetch_key ($old_fk);
277 $db->filter_store_key ($old_sk);
278 $db->filter_fetch_value ($old_fv);
279 $db->filter_store_value ($old_sv);
281 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
283 ok(30, checkOutput( "", "fred", "", "joe")) ;
285 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
286 ok(31, $h{"fred"} eq "joe");
287 ok(32, checkOutput( "", "fred", "joe", "")) ;
289 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
290 ok(33, $db->FIRSTKEY() eq "fred") ;
291 ok(34, checkOutput( "fred", "", "", "")) ;
294 $db->filter_fetch_key (undef);
295 $db->filter_store_key (undef);
296 $db->filter_fetch_value (undef);
297 $db->filter_store_value (undef);
299 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
301 ok(35, checkOutput( "", "", "", "")) ;
303 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
304 ok(36, $h{"fred"} eq "joe");
305 ok(37, checkOutput( "", "", "", "")) ;
307 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ;
308 ok(38, $db->FIRSTKEY() eq "fred") ;
309 ok(39, checkOutput( "", "", "", "")) ;
317 # DBM Filter with a closure
323 ok(40, $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
333 return sub { ++$count ;
335 $result{$name} = "$name - $count: [@kept]" ;
339 $db->filter_store_key(Closure("store key")) ;
340 $db->filter_store_value(Closure("store value")) ;
341 $db->filter_fetch_key(Closure("fetch key")) ;
342 $db->filter_fetch_value(Closure("fetch value")) ;
347 ok(41, $result{"store key"} eq "store key - 1: [fred]");
348 ok(42, $result{"store value"} eq "store value - 1: [joe]");
349 ok(43, !defined $result{"fetch key"} );
350 ok(44, !defined $result{"fetch value"} );
351 ok(45, $_ eq "original") ;
353 ok(46, $db->FIRSTKEY() eq "fred") ;
354 ok(47, $result{"store key"} eq "store key - 1: [fred]");
355 ok(48, $result{"store value"} eq "store value - 1: [joe]");
356 ok(49, $result{"fetch key"} eq "fetch key - 1: [fred]");
357 ok(50, ! defined $result{"fetch value"} );
358 ok(51, $_ eq "original") ;
361 ok(52, $result{"store key"} eq "store key - 2: [fred jim]");
362 ok(53, $result{"store value"} eq "store value - 2: [joe john]");
363 ok(54, $result{"fetch key"} eq "fetch key - 1: [fred]");
364 ok(55, $result{"fetch value"} eq "");
365 ok(56, $_ eq "original") ;
367 ok(57, $h{"fred"} eq "joe");
368 ok(58, $result{"store key"} eq "store key - 3: [fred jim fred]");
369 ok(59, $result{"store value"} eq "store value - 2: [joe john]");
370 ok(60, $result{"fetch key"} eq "fetch key - 1: [fred]");
371 ok(61, $result{"fetch value"} eq "fetch value - 1: [joe]");
372 ok(62, $_ eq "original") ;
380 # DBM Filter recursion detection
385 ok(63, $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ;
387 $db->filter_store_key (sub { $_ = $h{$_} }) ;
389 eval '$h{1} = 1234' ;
390 ok(64, $@ =~ /^recursion detected in filter_store_key at/ );
400 # If you experience failures with the odbm test in HP-UX,
401 # this is a well-known bug that's unfortunately very hard to fix.
402 # The suggested course of action is to avoid using the ODBM_File,
403 # but to use instead the NDBM_File extension.