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'} !~ /\bSDBM_File\b/) { 10 print "1..0 # Skip: no SDBM_File\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 SDBM_File; 28#If Fcntl is not available, try 0x202 or 0x102 for O_RDWR|O_CREAT 29use Fcntl; 30 31print "1..80\n"; 32 33unlink <Op_dbmx.*>; 34 35umask(0); 36my %h ; 37ok(1, tie %h,'SDBM_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' || $^O eq 'dos' || $^O eq 'cygwin') { 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) == ($^O eq 'vos' ? 0750 : 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,'SDBM_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 139 140{ 141 # sub-class test 142 143 package Another ; 144 145 use strict ; 146 use warnings ; 147 148 open(FILE, ">SubDB.pm") or die "Cannot open SubDB.pm: $!\n" ; 149 print FILE <<'EOM' ; 150 151 package SubDB ; 152 153 use strict ; 154 use warnings ; 155 use vars qw( @ISA @EXPORT) ; 156 157 require Exporter ; 158 use SDBM_File; 159 @ISA=qw(SDBM_File); 160 @EXPORT = @SDBM_File::EXPORT if defined @SDBM_File::EXPORT ; 161 162 sub STORE { 163 my $self = shift ; 164 my $key = shift ; 165 my $value = shift ; 166 $self->SUPER::STORE($key, $value * 2) ; 167 } 168 169 sub FETCH { 170 my $self = shift ; 171 my $key = shift ; 172 $self->SUPER::FETCH($key) - 1 ; 173 } 174 175 sub A_new_method 176 { 177 my $self = shift ; 178 my $key = shift ; 179 my $value = $self->FETCH($key) ; 180 return "[[$value]]" ; 181 } 182 183 1 ; 184EOM 185 186 close FILE or die "Could not close: $!"; 187 188 BEGIN { push @INC, '.'; } 189 190 eval 'use SubDB ; use Fcntl ;'; 191 main::ok(13, $@ eq "") ; 192 my %h ; 193 my $X ; 194 eval ' 195 $X = tie(%h, "SubDB","dbhash_tmp", O_RDWR|O_CREAT, 0640 ); 196 ' ; 197 198 main::ok(14, $@ eq "") ; 199 200 my $ret = eval '$h{"fred"} = 3 ; return $h{"fred"} ' ; 201 main::ok(15, $@ eq "") ; 202 main::ok(16, $ret == 5) ; 203 204 $ret = eval '$X->A_new_method("fred") ' ; 205 main::ok(17, $@ eq "") ; 206 main::ok(18, $ret eq "[[5]]") ; 207 208 undef $X; 209 untie(%h); 210 unlink "SubDB.pm", <dbhash_tmp.*> ; 211 212} 213 214ok(19, !exists $h{'goner1'}); 215ok(20, exists $h{'foo'}); 216 217untie %h; 218unlink <Op_dbmx*>, $Dfile; 219 220{ 221 # DBM Filter tests 222 use strict ; 223 use warnings ; 224 my (%h, $db) ; 225 my ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 226 227 sub checkOutput 228 { 229 my($fk, $sk, $fv, $sv) = @_ ; 230 return 231 $fetch_key eq $fk && $store_key eq $sk && 232 $fetch_value eq $fv && $store_value eq $sv && 233 $_ eq 'original' ; 234 } 235 236 unlink <Op_dbmx*>; 237 ok(21, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ; 238 239 $db->filter_fetch_key (sub { $fetch_key = $_ }) ; 240 $db->filter_store_key (sub { $store_key = $_ }) ; 241 $db->filter_fetch_value (sub { $fetch_value = $_}) ; 242 $db->filter_store_value (sub { $store_value = $_ }) ; 243 244 $_ = "original" ; 245 246 $h{"fred"} = "joe" ; 247 # fk sk fv sv 248 ok(22, checkOutput( "", "fred", "", "joe")) ; 249 250 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 251 ok(23, $h{"fred"} eq "joe"); 252 # fk sk fv sv 253 ok(24, checkOutput( "", "fred", "joe", "")) ; 254 255 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 256 ok(25, $db->FIRSTKEY() eq "fred") ; 257 # fk sk fv sv 258 ok(26, checkOutput( "fred", "", "", "")) ; 259 260 # replace the filters, but remember the previous set 261 my ($old_fk) = $db->filter_fetch_key 262 (sub { $_ = uc $_ ; $fetch_key = $_ }) ; 263 my ($old_sk) = $db->filter_store_key 264 (sub { $_ = lc $_ ; $store_key = $_ }) ; 265 my ($old_fv) = $db->filter_fetch_value 266 (sub { $_ = "[$_]"; $fetch_value = $_ }) ; 267 my ($old_sv) = $db->filter_store_value 268 (sub { s/o/x/g; $store_value = $_ }) ; 269 270 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 271 $h{"Fred"} = "Joe" ; 272 # fk sk fv sv 273 ok(27, checkOutput( "", "fred", "", "Jxe")) ; 274 275 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 276 ok(28, $h{"Fred"} eq "[Jxe]"); 277 # fk sk fv sv 278 ok(29, checkOutput( "", "fred", "[Jxe]", "")) ; 279 280 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 281 ok(30, $db->FIRSTKEY() eq "FRED") ; 282 # fk sk fv sv 283 ok(31, checkOutput( "FRED", "", "", "")) ; 284 285 # put the original filters back 286 $db->filter_fetch_key ($old_fk); 287 $db->filter_store_key ($old_sk); 288 $db->filter_fetch_value ($old_fv); 289 $db->filter_store_value ($old_sv); 290 291 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 292 $h{"fred"} = "joe" ; 293 ok(32, checkOutput( "", "fred", "", "joe")) ; 294 295 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 296 ok(33, $h{"fred"} eq "joe"); 297 ok(34, checkOutput( "", "fred", "joe", "")) ; 298 299 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 300 ok(35, $db->FIRSTKEY() eq "fred") ; 301 ok(36, checkOutput( "fred", "", "", "")) ; 302 303 # delete the filters 304 $db->filter_fetch_key (undef); 305 $db->filter_store_key (undef); 306 $db->filter_fetch_value (undef); 307 $db->filter_store_value (undef); 308 309 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 310 $h{"fred"} = "joe" ; 311 ok(37, checkOutput( "", "", "", "")) ; 312 313 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 314 ok(38, $h{"fred"} eq "joe"); 315 ok(39, checkOutput( "", "", "", "")) ; 316 317 ($fetch_key, $store_key, $fetch_value, $store_value) = ("") x 4 ; 318 ok(40, $db->FIRSTKEY() eq "fred") ; 319 ok(41, checkOutput( "", "", "", "")) ; 320 321 undef $db ; 322 untie %h; 323 unlink <Op_dbmx*>; 324} 325 326{ 327 # DBM Filter with a closure 328 329 use strict ; 330 use warnings ; 331 my (%h, $db) ; 332 333 unlink <Op_dbmx*>; 334 ok(42, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ; 335 336 my %result = () ; 337 338 sub Closure 339 { 340 my ($name) = @_ ; 341 my $count = 0 ; 342 my @kept = () ; 343 344 return sub { ++$count ; 345 push @kept, $_ ; 346 $result{$name} = "$name - $count: [@kept]" ; 347 } 348 } 349 350 $db->filter_store_key(Closure("store key")) ; 351 $db->filter_store_value(Closure("store value")) ; 352 $db->filter_fetch_key(Closure("fetch key")) ; 353 $db->filter_fetch_value(Closure("fetch value")) ; 354 355 $_ = "original" ; 356 357 $h{"fred"} = "joe" ; 358 ok(43, $result{"store key"} eq "store key - 1: [fred]"); 359 ok(44, $result{"store value"} eq "store value - 1: [joe]"); 360 ok(45, !defined $result{"fetch key"} ); 361 ok(46, !defined $result{"fetch value"} ); 362 ok(47, $_ eq "original") ; 363 364 ok(48, $db->FIRSTKEY() eq "fred") ; 365 ok(49, $result{"store key"} eq "store key - 1: [fred]"); 366 ok(50, $result{"store value"} eq "store value - 1: [joe]"); 367 ok(51, $result{"fetch key"} eq "fetch key - 1: [fred]"); 368 ok(52, ! defined $result{"fetch value"} ); 369 ok(53, $_ eq "original") ; 370 371 $h{"jim"} = "john" ; 372 ok(54, $result{"store key"} eq "store key - 2: [fred jim]"); 373 ok(55, $result{"store value"} eq "store value - 2: [joe john]"); 374 ok(56, $result{"fetch key"} eq "fetch key - 1: [fred]"); 375 ok(57, ! defined $result{"fetch value"} ); 376 ok(58, $_ eq "original") ; 377 378 ok(59, $h{"fred"} eq "joe"); 379 ok(60, $result{"store key"} eq "store key - 3: [fred jim fred]"); 380 ok(61, $result{"store value"} eq "store value - 2: [joe john]"); 381 ok(62, $result{"fetch key"} eq "fetch key - 1: [fred]"); 382 ok(63, $result{"fetch value"} eq "fetch value - 1: [joe]"); 383 ok(64, $_ eq "original") ; 384 385 undef $db ; 386 untie %h; 387 unlink <Op_dbmx*>; 388} 389 390{ 391 # DBM Filter recursion detection 392 use strict ; 393 use warnings ; 394 my (%h, $db) ; 395 unlink <Op_dbmx*>; 396 397 ok(65, $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ; 398 399 $db->filter_store_key (sub { $_ = $h{$_} }) ; 400 401 eval '$h{1} = 1234' ; 402 ok(66, $@ =~ /^recursion detected in filter_store_key at/ ); 403 404 undef $db ; 405 untie %h; 406 unlink <Op_dbmx*>; 407} 408 409{ 410 # Bug ID 20001013.009 411 # 412 # test that $hash{KEY} = undef doesn't produce the warning 413 # Use of uninitialized value in null operation 414 use warnings ; 415 use strict ; 416 use SDBM_File ; 417 418 unlink <Op_dbmx*>; 419 my %h ; 420 my $a = ""; 421 local $SIG{__WARN__} = sub {$a = $_[0]} ; 422 423 ok(67, tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ; 424 $h{ABC} = undef; 425 ok(68, $a eq "") ; 426 427 untie %h; 428 unlink <Op_dbmx*>; 429} 430 431{ 432 # When iterating over a tied hash using "each", the key passed to FETCH 433 # will be recycled and passed to NEXTKEY. If a Source Filter modifies the 434 # key in FETCH via a filter_fetch_key method we need to check that the 435 # modified key doesn't get passed to NEXTKEY. 436 # Also Test "keys" & "values" while we are at it. 437 438 use warnings ; 439 use strict ; 440 use SDBM_File ; 441 442 unlink <Op_dbmx*>; 443 my $bad_key = 0 ; 444 my %h = () ; 445 ok(69, my $db = tie(%h, 'SDBM_File','Op_dbmx', O_RDWR|O_CREAT, 0640)) ; 446 $db->filter_fetch_key (sub { $_ =~ s/^Beta_/Alpha_/ if defined $_}) ; 447 $db->filter_store_key (sub { $bad_key = 1 if /^Beta_/ ; $_ =~ s/^Alpha_/Beta_/}) ; 448 449 $h{'Alpha_ABC'} = 2 ; 450 $h{'Alpha_DEF'} = 5 ; 451 452 ok(70, $h{'Alpha_ABC'} == 2); 453 ok(71, $h{'Alpha_DEF'} == 5); 454 455 my ($k, $v) = ("",""); 456 while (($k, $v) = each %h) {} 457 ok(72, $bad_key == 0); 458 459 $bad_key = 0 ; 460 foreach $k (keys %h) {} 461 ok(73, $bad_key == 0); 462 463 $bad_key = 0 ; 464 foreach $v (values %h) {} 465 ok(74, $bad_key == 0); 466 467 undef $db ; 468 untie %h ; 469 unlink <Op_dbmx*>; 470} 471 472 473{ 474 # Check that DBM Filter can cope with read-only $_ 475 476 use warnings ; 477 use strict ; 478 my %h ; 479 unlink <Op1_dbmx*>; 480 481 ok(75, my $db = tie(%h, 'SDBM_File','Op1_dbmx', O_RDWR|O_CREAT, 0640)) ; 482 483 $db->filter_fetch_key (sub { }) ; 484 $db->filter_store_key (sub { }) ; 485 $db->filter_fetch_value (sub { }) ; 486 $db->filter_store_value (sub { }) ; 487 488 $_ = "original" ; 489 490 $h{"fred"} = "joe" ; 491 ok(76, $h{"fred"} eq "joe"); 492 493 eval { grep { $h{$_} } (1, 2, 3) }; 494 ok (77, ! $@); 495 496 497 # delete the filters 498 $db->filter_fetch_key (undef); 499 $db->filter_store_key (undef); 500 $db->filter_fetch_value (undef); 501 $db->filter_store_value (undef); 502 503 $h{"fred"} = "joe" ; 504 505 ok(78, $h{"fred"} eq "joe"); 506 507 ok(79, $db->FIRSTKEY() eq "fred") ; 508 509 eval { grep { $h{$_} } (1, 2, 3) }; 510 ok (80, ! $@); 511 512 undef $db ; 513 untie %h; 514 unlink <Op1_dbmx*>; 515} 516exit ; 517