1#!./perl 2 3# $RCSfile: dbm.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:43 $ 4 5BEGIN { 6 require Config; import Config; 7 if ($Config{'extensions'} !~ /\bSDBM_File\b/) { 8 print "1..0 # Skip: no SDBM_File\n"; 9 exit 0; 10 } 11} 12 13use strict; 14use warnings; 15 16sub ok 17{ 18 my $no = shift ; 19 my $result = shift ; 20 21 print "not " unless $result ; 22 print "ok $no\n" ; 23} 24 25require SDBM_File; 26#If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT 27use Fcntl; 28 29print "1..80\n"; 30 31unlink <Op_dbmx.*>; 32 33umask(0); 34my %h ; 35ok(1, tie %h,'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640); 36 37my $Dfile = "Op_dbmx.pag"; 38if (! -e $Dfile) { 39 ($Dfile) = <Op_dbmx.*>; 40} 41if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin') { 42 print "ok 2 # Skipped: different file permission semantics\n"; 43} 44else { 45 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, 46 $blksize,$blocks) = stat($Dfile); 47 print (($mode & 0777) == ($^O eq 'vos' ? 0750 : 0640) ? "ok 2\n" : "not ok 2\n"); 48} 49my $i = 0; 50while (my ($key,$value) = each(%h)) { 51 $i++; 52} 53print (!$i ? "ok 3\n" : "not ok 3\n"); 54 55$h{'goner1'} = 'snork'; 56 57$h{'abc'} = 'ABC'; 58$h{'def'} = 'DEF'; 59$h{'jkl','mno'} = "JKL\034MNO"; 60$h{'a',2,3,4,5} = join("\034",'A',2,3,4,5); 61$h{'a'} = 'A'; 62$h{'b'} = 'B'; 63$h{'c'} = 'C'; 64$h{'d'} = 'D'; 65$h{'e'} = 'E'; 66$h{'f'} = 'F'; 67$h{'g'} = 'G'; 68$h{'h'} = 'H'; 69$h{'i'} = 'I'; 70 71$h{'goner2'} = 'snork'; 72delete $h{'goner2'}; 73 74untie(%h); 75print (tie(%h,'SDBM_File','Op_dbmx', O_RDWR, 0640) ? "ok 4\n" : "not ok 4\n"); 76 77$h{'j'} = 'J'; 78$h{'k'} = 'K'; 79$h{'l'} = 'L'; 80$h{'m'} = 'M'; 81$h{'n'} = 'N'; 82$h{'o'} = 'O'; 83$h{'p'} = 'P'; 84$h{'q'} = 'Q'; 85$h{'r'} = 'R'; 86$h{'s'} = 'S'; 87$h{'t'} = 'T'; 88$h{'u'} = 'U'; 89$h{'v'} = 'V'; 90$h{'w'} = 'W'; 91$h{'x'} = 'X'; 92$h{'y'} = 'Y'; 93$h{'z'} = 'Z'; 94 95$h{'goner3'} = 'snork'; 96 97delete $h{'goner1'}; 98delete $h{'goner3'}; 99 100my @keys = keys(%h); 101my @values = values(%h); 102 103if ($#keys == 29 && $#values == 29) {print "ok 5\n";} else {print "not ok 5\n";} 104 105while (my ($key,$value) = each(%h)) { 106 if ($key eq $keys[$i] && $value eq $values[$i] && $key eq lc($value)) { 107 $key =~ y/a-z/A-Z/; 108 $i++ if $key eq $value; 109 } 110} 111 112if ($i == 30) {print "ok 6\n";} else {print "not ok 6\n";} 113 114@keys = ('blurfl', keys(%h), 'dyick'); 115if ($#keys == 31) {print "ok 7\n";} else {print "not ok 7\n";} 116 117$h{'foo'} = ''; 118$h{''} = 'bar'; 119 120# check cache overflow and numeric keys and contents 121my $ok = 1; 122for ($i = 1; $i < 200; $i++) { $h{$i + 0} = $i + 0; } 123for ($i = 1; $i < 200; $i++) { $ok = 0 unless $h{$i} == $i; } 124print ($ok ? "ok 8\n" : "not ok 8\n"); 125 126my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime, 127 $blksize,$blocks) = stat($Dfile); 128print ($size > 0 ? "ok 9\n" : "not ok 9\n"); 129 130@h{0..200} = 200..400; 131my @foo = @h{0..200}; 132print join(':',200..400) eq join(':',@foo) ? "ok 10\n" : "not ok 10\n"; 133 134print ($h{'foo'} eq '' ? "ok 11\n" : "not ok 11\n"); 135print ($h{''} eq 'bar' ? "ok 12\n" : "not ok 12\n"); 136 137 138{ 139 # sub-class test 140 141 package Another ; 142 143 use strict ; 144 use warnings ; 145 146 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ; 147 print FILE <<'EOM' ; 148 149 package SubDB ; 150 151 use strict ; 152 use warnings ; 153 use vars qw( @ISA @EXPORT) ; 154 155 require Exporter ; 156 use SDBM_File; 157 @ISA=qw(SDBM_File); 158 @EXPORT = @SDBM_File::EXPORT if @SDBM_File::EXPORT ; 159 160 sub STORE { 161 my $self = shift ; 162 my $key = shift ; 163 my $value = shift ; 164 $self->SUPER::STORE($key, $value * 2) ; 165 } 166 167 sub FETCH { 168 my $self = shift ; 169 my $key = shift ; 170 $self->SUPER::FETCH($key) - 1 ; 171 } 172 173 sub A_new_method 174 { 175 my $self = shift ; 176 my $key = shift ; 177 my $value = $self->FETCH($key) ; 178 return "[[$value]]" ; 179 } 180 181 1 ; 182EOM 183 184 close FILE or die "Could not close: $!"; 185 186 BEGIN { push @INC, '.'; } 187 188 eval 'use SubDB ; use Fcntl ;'; 189 main::ok(13, $@ eq "") ; 190 my %h ; 191 my $X ; 192 eval ' 193 $X = tie(%h, "SubDB","dbhash_tmp", O_RDWR|O_CREAT, 0640 ); 194 ' ; 195 196 main::ok(14, $@ eq "") ; 197 198 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ; 199 main::ok(15, $@ eq "") ; 200 main::ok(16, $ret == 5) ; 201 202 $ret = eval '$X->A_new_method("fred") ' ; 203 main::ok(17, $@ eq "") ; 204 main::ok(18, $ret eq "[[5]]") ; 205 206 undef $X; 207 untie(%h); 208 unlink "SubDB.pm", <dbhash_tmp.*> ; 209 210} 211 212ok(19, !exists $h{'goner1'}); 213ok(20, exists $h{'foo'}); 214 215untie %h; 216unlink <Op_dbmx*>, $Dfile; 217 218{ 219 # DBM Filter tests 220 use strict ; 221 use warnings ; 222 my (%h, $db) ; 223 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 224 225 sub checkOutput 226 { 227 my($fk, $sk, $fv, $sv) = @_ ; 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(21, $db = tie(%h, 'SDBM_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(22, checkOutput( "", "fred", "", "joe")) ; 247 248 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 249 ok(23, $h{"fred"} eq "joe"); 250 # fk sk fv sv 251 ok(24, checkOutput( "", "fred", "joe", "")) ; 252 253 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 254 ok(25, $db->FIRSTKEY() eq "fred") ; 255 # fk sk fv sv 256 ok(26, 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(27, checkOutput( "", "fred", "", "Jxe")) ; 272 273 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 274 ok(28, $h{"Fred"} eq "[Jxe]"); 275 # fk sk fv sv 276 ok(29, checkOutput( "", "fred", "[Jxe]", "")) ; 277 278 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 279 ok(30, $db->FIRSTKEY() eq "FRED") ; 280 # fk sk fv sv 281 ok(31, 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(32, checkOutput( "", "fred", "", "joe")) ; 292 293 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 294 ok(33, $h{"fred"} eq "joe"); 295 ok(34, checkOutput( "", "fred", "joe", "")) ; 296 297 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 298 ok(35, $db->FIRSTKEY() eq "fred") ; 299 ok(36, 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(37, checkOutput( "", "", "", "")) ; 310 311 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 312 ok(38, $h{"fred"} eq "joe"); 313 ok(39, checkOutput( "", "", "", "")) ; 314 315 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 316 ok(40, $db->FIRSTKEY() eq "fred") ; 317 ok(41, 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(42, $db = tie(%h, 'SDBM_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(43, $result{"store key"} eq "store key - 1: [fred]"); 357 ok(44, $result{"store value"} eq "store value - 1: [joe]"); 358 ok(45, !defined $result{"fetch key"} ); 359 ok(46, !defined $result{"fetch value"} ); 360 ok(47, $_ eq "original") ; 361 362 ok(48, $db->FIRSTKEY() eq "fred") ; 363 ok(49, $result{"store key"} eq "store key - 1: [fred]"); 364 ok(50, $result{"store value"} eq "store value - 1: [joe]"); 365 ok(51, $result{"fetch key"} eq "fetch key - 1: [fred]"); 366 ok(52, ! defined $result{"fetch value"} ); 367 ok(53, $_ eq "original") ; 368 369 $h{"jim"} = "john" ; 370 ok(54, $result{"store key"} eq "store key - 2: [fred jim]"); 371 ok(55, $result{"store value"} eq "store value - 2: [joe john]"); 372 ok(56, $result{"fetch key"} eq "fetch key - 1: [fred]"); 373 ok(57, ! defined $result{"fetch value"} ); 374 ok(58, $_ eq "original") ; 375 376 ok(59, $h{"fred"} eq "joe"); 377 ok(60, $result{"store key"} eq "store key - 3: [fred jim fred]"); 378 ok(61, $result{"store value"} eq "store value - 2: [joe john]"); 379 ok(62, $result{"fetch key"} eq "fetch key - 1: [fred]"); 380 ok(63, $result{"fetch value"} eq "fetch value - 1: [joe]"); 381 ok(64, $_ 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(65, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ; 396 397 $db->filter_store_key (sub { $_ = $h{$_} }) ; 398 399 eval '$h{1} = 1234' ; 400 ok(66, $@ =~ /^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 SDBM_File ; 415 416 unlink <Op_dbmx*>; 417 my %h ; 418 my $a = ""; 419 local $SIG{__WARN__} = sub {$a = $_[0]} ; 420 421 ok(67, tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ; 422 $h{ABC} = undef; 423 ok(68, $a eq "") ; 424 425 untie %h; 426 unlink <Op_dbmx*>; 427} 428 429{ 430 # When iterating over a tied hash using "each", the key passed to FETCH 431 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the 432 # key in FETCH via a filter_fetch_key method we need to check that the 433 # modified key doesn't get passed to NEXTKEY. 434 # Also Test "keys" & "values" while we are at it. 435 436 use warnings ; 437 use strict ; 438 use SDBM_File ; 439 440 unlink <Op_dbmx*>; 441 my $bad_key = 0 ; 442 my %h = () ; 443 ok(69, my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ; 444 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ; 445 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ; 446 447 $h{'Alpha_ABC'} = 2 ; 448 $h{'Alpha_DEF'} = 5 ; 449 450 ok(70, $h{'Alpha_ABC'} == 2); 451 ok(71, $h{'Alpha_DEF'} == 5); 452 453 my ($k, $v) = ("",""); 454 while (($k, $v) = each %h) {} 455 ok(72, $bad_key == 0); 456 457 $bad_key = 0 ; 458 foreach $k (keys %h) {} 459 ok(73, $bad_key == 0); 460 461 $bad_key = 0 ; 462 foreach $v (values %h) {} 463 ok(74, $bad_key == 0); 464 465 undef $db ; 466 untie %h ; 467 unlink <Op_dbmx*>; 468} 469 470 471{ 472 # Check that DBM Filter can cope with read-only $_ 473 474 use warnings ; 475 use strict ; 476 my %h ; 477 unlink <Op1_dbmx*>; 478 479 ok(75, my $db = tie(%h, 'SDBM_File','Op1_dbmx', O_RDWR|O_CREAT, 0640)) ; 480 481 $db->filter_fetch_key (sub { }) ; 482 $db->filter_store_key (sub { }) ; 483 $db->filter_fetch_value (sub { }) ; 484 $db->filter_store_value (sub { }) ; 485 486 $_ = "original" ; 487 488 $h{"fred"} = "joe" ; 489 ok(76, $h{"fred"} eq "joe"); 490 491 eval { map { $h{$_} } (1, 2, 3) }; 492 ok (77, ! $@); 493 494 495 # delete the filters 496 $db->filter_fetch_key (undef); 497 $db->filter_store_key (undef); 498 $db->filter_fetch_value (undef); 499 $db->filter_store_value (undef); 500 501 $h{"fred"} = "joe" ; 502 503 ok(78, $h{"fred"} eq "joe"); 504 505 ok(79, $db->FIRSTKEY() eq "fred") ; 506 507 eval { map { $h{$_} } (1, 2, 3) }; 508 ok (80, ! $@); 509 510 undef $db ; 511 untie %h; 512 unlink <Op1_dbmx*>; 513} 514exit ; 515