1#!./perl 2 3# $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $ 4 5BEGIN { 6 chdir 't' if -d 't'; 7 @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"; 11 exit 0; 12 } 13} 14 15use strict; 16use warnings; 17 18sub ok 19{ 20 my $no = shift ; 21 my $result = shift ; 22 23 print "not " unless $result ; 24 print "ok $no\n" ; 25} 26 27require ODBM_File; 28#If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT 29use Fcntl; 30 31print "1..78\n"; 32 33unlink <Op.dbmx*>; 34 35umask(0); 36my %h; 37ok(1, tie(%h,'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)); 38 39my $Dfile = "Op.dbmx.pag"; 40if (! -e $Dfile) { 41 ($Dfile) = <Op.dbmx*>; 42} 43if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'NetWare') { 44 print "ok 2 # Skipped: different file permission semantics\n"; 45} 46else { 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"); 50} 51my $i = 0; 52while (my ($key,$value) = each(%h)) { 53 $i++; 54} 55print (!$i ? "ok 3\n" : "not ok 3\n"); 56 57$h{'goner1'} = 'snork'; 58 59$h{'abc'} = 'ABC'; 60$h{'def'} = 'DEF'; 61$h{'jkl','mno'} = "JKL\034MNO"; 62$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); 63$h{'a'} = 'A'; 64$h{'b'} = 'B'; 65$h{'c'} = 'C'; 66$h{'d'} = 'D'; 67$h{'e'} = 'E'; 68$h{'f'} = 'F'; 69$h{'g'} = 'G'; 70$h{'h'} = 'H'; 71$h{'i'} = 'I'; 72 73$h{'goner2'} = 'snork'; 74delete $h{'goner2'}; 75 76untie(%h); 77print (tie(%h,'ODBM_File','Op.dbmx', O_RDWR, 0640) ? "ok 4\n" : "not ok 4\n"); 78 79$h{'j'} = 'J'; 80$h{'k'} = 'K'; 81$h{'l'} = 'L'; 82$h{'m'} = 'M'; 83$h{'n'} = 'N'; 84$h{'o'} = 'O'; 85$h{'p'} = 'P'; 86$h{'q'} = 'Q'; 87$h{'r'} = 'R'; 88$h{'s'} = 'S'; 89$h{'t'} = 'T'; 90$h{'u'} = 'U'; 91$h{'v'} = 'V'; 92$h{'w'} = 'W'; 93$h{'x'} = 'X'; 94$h{'y'} = 'Y'; 95$h{'z'} = 'Z'; 96 97$h{'goner3'} = 'snork'; 98 99delete $h{'goner1'}; 100delete $h{'goner3'}; 101 102my @keys = keys(%h); 103my @values = values(%h); 104 105if ($#keys == 29 && $#values == 29) {print "ok 5\n";} else {print "not ok 5\n";} 106 107while (my ($key,$value) = each(%h)) { 108 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { 109 $key =~ y/a-z/A-Z/; 110 $i++ if $key eq $value; 111 } 112} 113 114if ($i == 30) {print "ok 6\n";} else {print "not ok 6\n";} 115 116@keys = ('blurfl', keys(%h), 'dyick'); 117if ($#keys == 31) {print "ok 7\n";} else {print "not ok 7\n";} 118 119$h{'foo'} = ''; 120$h{''} = 'bar'; 121 122# check cache overflow and numeric keys and contents 123my $ok = 1; 124for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } 125for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } 126print ($ok ? "ok 8\n" : "not ok 8\n"); 127 128my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, 129 $blksize,$blocks) = stat($Dfile); 130print ($size > 0 ? "ok 9\n" : "not ok 9\n"); 131 132@h{0..200} = 200..400; 133my @foo = @h{0..200}; 134print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n"; 135 136print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n"); 137print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n"); 138 139untie %h; 140unlink 'Op.dbmx.dir', $Dfile; 141 142{ 143 # sub-class test 144 145 package Another ; 146 147 use strict ; 148 use warnings ; 149 150 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ; 151 print FILE <<'EOM' ; 152 153 package SubDB ; 154 155 use strict ; 156 use warnings ; 157 use vars qw(@ISA @EXPORT) ; 158 159 require Exporter ; 160 use ODBM_File; 161 @ISA=qw(ODBM_File); 162 @EXPORT = @ODBM_File::EXPORT ; 163 164 sub STORE { 165 my $self = shift ; 166 my $key = shift ; 167 my $value = shift ; 168 $self->SUPER::STORE($key, $value * 2) ; 169 } 170 171 sub FETCH { 172 my $self = shift ; 173 my $key = shift ; 174 $self->SUPER::FETCH($key) - 1 ; 175 } 176 177 sub A_new_method 178 { 179 my $self = shift ; 180 my $key = shift ; 181 my $value = $self->FETCH($key) ; 182 return "[[$value]]" ; 183 } 184 185 1 ; 186EOM 187 188 close FILE ; 189 190 BEGIN { push @INC, '.'; } 191 192 eval 'use SubDB ; use Fcntl ;'; 193 main::ok(13, $@ eq "") ; 194 my %h ; 195 my $X ; 196 eval ' 197 $X = tie(%h, "SubDB","dbhash.tmp", O_RDWR|O_CREAT, 0640 ); 198 ' ; 199 200 main::ok(14, $@ eq "") ; 201 202 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ; 203 main::ok(15, $@ eq "") ; 204 main::ok(16, $ret == 5) ; 205 206 $ret = eval '$X->A_new_method("fred") ' ; 207 main::ok(17, $@ eq "") ; 208 main::ok(18, $ret eq "[[5]]") ; 209 210 undef $X; 211 untie(%h); 212 unlink "SubDB.pm", <dbhash.tmp*> ; 213 214} 215 216{ 217 # DBM Filter tests 218 use strict ; 219 use warnings ; 220 my (%h, $db) ; 221 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 222 223 sub checkOutput 224 { 225 my($fk, $sk, $fv, $sv) = @_ ; 226 print "# ", join('|', $fetch_key, $fk, $store_key, $sk, 227 $fetch_value, $fv, $store_value, $sv, $_), "\n"; 228 return 229 $fetch_key eq $fk && $store_key eq $sk && 230 $fetch_value eq $fv && $store_value eq $sv && 231 $_ eq 'original' ; 232 } 233 234 unlink <Op.dbmx*>; 235 ok(19, $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; 236 237 $db->filter_fetch_key (sub { $fetch_key = $_ }) ; 238 $db->filter_store_key (sub { $store_key = $_ }) ; 239 $db->filter_fetch_value (sub { $fetch_value = $_}) ; 240 $db->filter_store_value (sub { $store_value = $_ }) ; 241 242 $_ = "original" ; 243 244 $h{"fred"} = "joe" ; 245 # fk sk fv sv 246 ok(20, checkOutput( "", "fred", "", "joe")) ; 247 248 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 249 ok(21, $h{"fred"} eq "joe"); 250 # fk sk fv sv 251 ok(22, checkOutput( "", "fred", "joe", "")) ; 252 253 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 254 ok(23, $db->FIRSTKEY() eq "fred") ; 255 # fk sk fv sv 256 ok(24, checkOutput( "fred", "", "", "")) ; 257 258 # replace the filters, but remember the previous set 259 my ($old_fk) = $db->filter_fetch_key 260 (sub { $_ = uc $_ ; $fetch_key = $_ }) ; 261 my ($old_sk) = $db->filter_store_key 262 (sub { $_ = lc $_ ; $store_key = $_ }) ; 263 my ($old_fv) = $db->filter_fetch_value 264 (sub { $_ = "[$_]"; $fetch_value = $_ }) ; 265 my ($old_sv) = $db->filter_store_value 266 (sub { s/o/x/g; $store_value = $_ }) ; 267 268 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 269 $h{"Fred"} = "Joe" ; 270 # fk sk fv sv 271 ok(25, checkOutput( "", "fred", "", "Jxe")) ; 272 273 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 274 ok(26, $h{"Fred"} eq "[Jxe]"); 275 # fk sk fv sv 276 ok(27, checkOutput( "", "fred", "[Jxe]", "")) ; 277 278 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 279 ok(28, $db->FIRSTKEY() eq "FRED") ; 280 # fk sk fv sv 281 ok(29, checkOutput( "FRED", "", "", "")) ; 282 283 # put the original filters back 284 $db->filter_fetch_key ($old_fk); 285 $db->filter_store_key ($old_sk); 286 $db->filter_fetch_value ($old_fv); 287 $db->filter_store_value ($old_sv); 288 289 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 290 $h{"fred"} = "joe" ; 291 ok(30, checkOutput( "", "fred", "", "joe")) ; 292 293 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 294 ok(31, $h{"fred"} eq "joe"); 295 ok(32, checkOutput( "", "fred", "joe", "")) ; 296 297 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 298 ok(33, $db->FIRSTKEY() eq "fred") ; 299 ok(34, checkOutput( "fred", "", "", "")) ; 300 301 # delete the filters 302 $db->filter_fetch_key (undef); 303 $db->filter_store_key (undef); 304 $db->filter_fetch_value (undef); 305 $db->filter_store_value (undef); 306 307 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 308 $h{"fred"} = "joe" ; 309 ok(35, checkOutput( "", "", "", "")) ; 310 311 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 312 ok(36, $h{"fred"} eq "joe"); 313 ok(37, checkOutput( "", "", "", "")) ; 314 315 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 316 ok(38, $db->FIRSTKEY() eq "fred") ; 317 ok(39, checkOutput( "", "", "", "")) ; 318 319 undef $db ; 320 untie %h; 321 unlink <Op.dbmx*>; 322} 323 324{ 325 # DBM Filter with a closure 326 327 use strict ; 328 use warnings ; 329 my (%h, $db) ; 330 331 unlink <Op.dbmx*>; 332 ok(40, $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; 333 334 my %result = () ; 335 336 sub Closure 337 { 338 my ($name) = @_ ; 339 my $count = 0 ; 340 my @kept = () ; 341 342 return sub { ++$count ; 343 push @kept, $_ ; 344 $result{$name} = "$name - $count: [@kept]" ; 345 } 346 } 347 348 $db->filter_store_key(Closure("store key")) ; 349 $db->filter_store_value(Closure("store value")) ; 350 $db->filter_fetch_key(Closure("fetch key")) ; 351 $db->filter_fetch_value(Closure("fetch value")) ; 352 353 $_ = "original" ; 354 355 $h{"fred"} = "joe" ; 356 ok(41, $result{"store key"} eq "store key - 1: [fred]"); 357 ok(42, $result{"store value"} eq "store value - 1: [joe]"); 358 ok(43, !defined $result{"fetch key"} ); 359 ok(44, !defined $result{"fetch value"} ); 360 ok(45, $_ eq "original") ; 361 362 ok(46, $db->FIRSTKEY() eq "fred") ; 363 ok(47, $result{"store key"} eq "store key - 1: [fred]"); 364 ok(48, $result{"store value"} eq "store value - 1: [joe]"); 365 ok(49, $result{"fetch key"} eq "fetch key - 1: [fred]"); 366 ok(50, ! defined $result{"fetch value"} ); 367 ok(51, $_ eq "original") ; 368 369 $h{"jim"} = "john" ; 370 ok(52, $result{"store key"} eq "store key - 2: [fred jim]"); 371 ok(53, $result{"store value"} eq "store value - 2: [joe john]"); 372 ok(54, $result{"fetch key"} eq "fetch key - 1: [fred]"); 373 ok(55, ! defined $result{"fetch value"} ); 374 ok(56, $_ eq "original") ; 375 376 ok(57, $h{"fred"} eq "joe"); 377 ok(58, $result{"store key"} eq "store key - 3: [fred jim fred]"); 378 ok(59, $result{"store value"} eq "store value - 2: [joe john]"); 379 ok(60, $result{"fetch key"} eq "fetch key - 1: [fred]"); 380 ok(61, $result{"fetch value"} eq "fetch value - 1: [joe]"); 381 ok(62, $_ eq "original") ; 382 383 undef $db ; 384 untie %h; 385 unlink <Op.dbmx*>; 386} 387 388{ 389 # DBM Filter recursion detection 390 use strict ; 391 use warnings ; 392 my (%h, $db) ; 393 unlink <Op.dbmx*>; 394 395 ok(63, $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; 396 397 $db->filter_store_key (sub { $_ = $h{$_} }) ; 398 399 eval '$h{1} = 1234' ; 400 ok(64, $@ =~ /^recursion detected in filter_store_key at/ ); 401 402 undef $db ; 403 untie %h; 404 unlink <Op.dbmx*>; 405} 406 407{ 408 # Bug ID 20001013.009 409 # 410 # test that $hash{KEY} = undef doesn't produce the warning 411 # Use of uninitialized value in null operation 412 use warnings ; 413 use strict ; 414 use ODBM_File ; 415 416 unlink <Op.dbmx*>; 417 my %h ; 418 my $a = ""; 419 local $SIG{__WARN__} = sub {$a = $_[0]} ; 420 421 ok(65, tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; 422 $h{ABC} = undef; 423 ok(66, $a eq "") ; 424 untie %h; 425 unlink <Op.dbmx*>; 426} 427 428{ 429 # When iterating over a tied hash using "each", the key passed to FETCH 430 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the 431 # key in FETCH via a filter_fetch_key method we need to check that the 432 # modified key doesn't get passed to NEXTKEY. 433 # Also Test "keys" & "values" while we are at it. 434 435 use warnings ; 436 use strict ; 437 use ODBM_File ; 438 439 unlink <Op.dbmx*>; 440 my $bad_key = 0 ; 441 my %h = () ; 442 ok(67, my $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; 443 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ; 444 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ; 445 446 $h{'Alpha_ABC'} = 2 ; 447 $h{'Alpha_DEF'} = 5 ; 448 449 ok(68, $h{'Alpha_ABC'} == 2); 450 ok(69, $h{'Alpha_DEF'} == 5); 451 452 my ($k, $v) = ("",""); 453 while (($k, $v) = each %h) {} 454 ok(70, $bad_key == 0); 455 456 $bad_key = 0 ; 457 foreach $k (keys %h) {} 458 ok(71, $bad_key == 0); 459 460 $bad_key = 0 ; 461 foreach $v (values %h) {} 462 ok(72, $bad_key == 0); 463 464 undef $db ; 465 untie %h ; 466 unlink <Op.dbmx*>; 467} 468 469 470{ 471 # Check that DBM Filter can cope with read-only $_ 472 473 use warnings ; 474 use strict ; 475 my %h ; 476 unlink <Op.dbmx*>; 477 478 ok(73, my $db = tie(%h, 'ODBM_File','Op.dbmx', O_RDWR|O_CREAT, 0640)) ; 479 480 $db->filter_fetch_key (sub { }) ; 481 $db->filter_store_key (sub { }) ; 482 $db->filter_fetch_value (sub { }) ; 483 $db->filter_store_value (sub { }) ; 484 485 $_ = "original" ; 486 487 $h{"fred"} = "joe" ; 488 ok(74, $h{"fred"} eq "joe"); 489 490 eval { grep { $h{$_} } (1, 2, 3) }; 491 ok (75, ! $@); 492 493 494 # delete the filters 495 $db->filter_fetch_key (undef); 496 $db->filter_store_key (undef); 497 $db->filter_fetch_value (undef); 498 $db->filter_store_value (undef); 499 500 $h{"fred"} = "joe" ; 501 502 ok(76, $h{"fred"} eq "joe"); 503 504 ok(77, $db->FIRSTKEY() eq "fred") ; 505 506 eval { grep { $h{$_} } (1, 2, 3) }; 507 ok (78, ! $@); 508 509 undef $db ; 510 untie %h; 511 unlink <Op.dbmx*>; 512} 513exit ; 514if ($^O eq 'hpux') { 515 print <<EOM; 516# 517# If you experience failures with the odbm test in HP-UX, 518# this is a well-known bug that's unfortunately very hard to fix. 519# The suggested course of action is to avoid using the ODBM_File, 520# but to use instead the NDBM_File extension. 521# 522EOM 523} 524