1*0Sstevel@tonic-gate#!/usr/bin/perl -w 2*0Sstevel@tonic-gate# 3*0Sstevel@tonic-gate# Basic test suite for Tie::RefHash and Tie::RefHash::Nestable. 4*0Sstevel@tonic-gate# 5*0Sstevel@tonic-gate# The testing is in two parts: first, run lots of tests on both a tied 6*0Sstevel@tonic-gate# hash and an ordinary un-tied hash, and check they give the same 7*0Sstevel@tonic-gate# answer. Then there are tests for those cases where the tied hashes 8*0Sstevel@tonic-gate# should behave differently to normal hashes, that is, when using 9*0Sstevel@tonic-gate# references as keys. 10*0Sstevel@tonic-gate# 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gateBEGIN { 13*0Sstevel@tonic-gate chdir 't' if -d 't'; 14*0Sstevel@tonic-gate @INC = '.'; 15*0Sstevel@tonic-gate push @INC, '../lib'; 16*0Sstevel@tonic-gate} 17*0Sstevel@tonic-gate 18*0Sstevel@tonic-gateuse strict; 19*0Sstevel@tonic-gateuse Tie::RefHash; 20*0Sstevel@tonic-gateuse Data::Dumper; 21*0Sstevel@tonic-gatemy $numtests = 37; 22*0Sstevel@tonic-gatemy $currtest = 1; 23*0Sstevel@tonic-gateprint "1..$numtests\n"; 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gatemy $ref = []; my $ref1 = []; 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gatepackage Boustrophedon; # A class with overloaded "". 28*0Sstevel@tonic-gatesub new { my ($c, $s) = @_; bless \$s, $c } 29*0Sstevel@tonic-gateuse overload '""' => sub { ${$_[0]} . reverse ${$_[0]} }; 30*0Sstevel@tonic-gatepackage main; 31*0Sstevel@tonic-gatemy $ox = Boustrophedon->new("foobar"); 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate# Test standard hash functionality, by performing the same operations 34*0Sstevel@tonic-gate# on a tied hash and on a normal hash, and checking that the results 35*0Sstevel@tonic-gate# are the same. This does of course assume that Perl hashes are not 36*0Sstevel@tonic-gate# buggy :-) 37*0Sstevel@tonic-gate# 38*0Sstevel@tonic-gatemy @tests = standard_hash_tests(); 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gatemy @ordinary_results = runtests(\@tests, undef); 41*0Sstevel@tonic-gateforeach my $class ('Tie::RefHash', 'Tie::RefHash::Nestable') { 42*0Sstevel@tonic-gate my @tied_results = runtests(\@tests, $class); 43*0Sstevel@tonic-gate my $all_ok = 1; 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate die if @ordinary_results != @tied_results; 46*0Sstevel@tonic-gate foreach my $i (0 .. $#ordinary_results) { 47*0Sstevel@tonic-gate my ($or, $ow, $oe) = @{$ordinary_results[$i]}; 48*0Sstevel@tonic-gate my ($tr, $tw, $te) = @{$tied_results[$i]}; 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate my $ok = 1; 51*0Sstevel@tonic-gate local $^W = 0; 52*0Sstevel@tonic-gate $ok = 0 if (defined($or) != defined($tr)) or ($or ne $tr); 53*0Sstevel@tonic-gate $ok = 0 if (defined($ow) != defined($tw)) or ($ow ne $tw); 54*0Sstevel@tonic-gate $ok = 0 if (defined($oe) != defined($te)) or ($oe ne $te); 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate if (not $ok) { 57*0Sstevel@tonic-gate print STDERR 58*0Sstevel@tonic-gate "failed for $class: $tests[$i]\n", 59*0Sstevel@tonic-gate "ordinary hash gave:\n", 60*0Sstevel@tonic-gate defined $or ? "\tresult: $or\n" : "\tundef result\n", 61*0Sstevel@tonic-gate defined $ow ? "\twarning: $ow\n" : "\tno warning\n", 62*0Sstevel@tonic-gate defined $oe ? "\texception: $oe\n" : "\tno exception\n", 63*0Sstevel@tonic-gate "tied $class hash gave:\n", 64*0Sstevel@tonic-gate defined $tr ? "\tresult: $tr\n" : "\tundef result\n", 65*0Sstevel@tonic-gate defined $tw ? "\twarning: $tw\n" : "\tno warning\n", 66*0Sstevel@tonic-gate defined $te ? "\texception: $te\n" : "\tno exception\n", 67*0Sstevel@tonic-gate "\n"; 68*0Sstevel@tonic-gate $all_ok = 0; 69*0Sstevel@tonic-gate } 70*0Sstevel@tonic-gate } 71*0Sstevel@tonic-gate test($all_ok); 72*0Sstevel@tonic-gate} 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate# Now test Tie::RefHash's special powers 75*0Sstevel@tonic-gatemy (%h, $h); 76*0Sstevel@tonic-gate$h = eval { tie %h, 'Tie::RefHash' }; 77*0Sstevel@tonic-gatewarn $@ if $@; 78*0Sstevel@tonic-gatetest(not $@); 79*0Sstevel@tonic-gatetest(ref($h) eq 'Tie::RefHash'); 80*0Sstevel@tonic-gatetest(defined(tied(%h)) and tied(%h) =~ /^Tie::RefHash/); 81*0Sstevel@tonic-gate$h{$ref} = 'cholet'; 82*0Sstevel@tonic-gatetest($h{$ref} eq 'cholet'); 83*0Sstevel@tonic-gatetest(exists $h{$ref}); 84*0Sstevel@tonic-gatetest((keys %h) == 1); 85*0Sstevel@tonic-gatetest(ref((keys %h)[0]) eq 'ARRAY'); 86*0Sstevel@tonic-gatetest((keys %h)[0] eq $ref); 87*0Sstevel@tonic-gatetest((values %h) == 1); 88*0Sstevel@tonic-gatetest((values %h)[0] eq 'cholet'); 89*0Sstevel@tonic-gatemy $count = 0; 90*0Sstevel@tonic-gatewhile (my ($k, $v) = each %h) { 91*0Sstevel@tonic-gate if ($count++ == 0) { 92*0Sstevel@tonic-gate test(ref($k) eq 'ARRAY'); 93*0Sstevel@tonic-gate test($k eq $ref); 94*0Sstevel@tonic-gate } 95*0Sstevel@tonic-gate} 96*0Sstevel@tonic-gatetest($count == 1); 97*0Sstevel@tonic-gatedelete $h{$ref}; 98*0Sstevel@tonic-gatetest(not defined $h{$ref}); 99*0Sstevel@tonic-gatetest(not exists($h{$ref})); 100*0Sstevel@tonic-gatetest((keys %h) == 0); 101*0Sstevel@tonic-gatetest((values %h) == 0); 102*0Sstevel@tonic-gate$h{$ox} = "bellow"; # overloaded "" 103*0Sstevel@tonic-gatetest(exists $h{$ox}); 104*0Sstevel@tonic-gatetest($h{$ox} eq "bellow"); 105*0Sstevel@tonic-gatetest(not exists $h{"foobarraboof"}); 106*0Sstevel@tonic-gateundef $h; 107*0Sstevel@tonic-gateuntie %h; 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate# And now Tie::RefHash::Nestable's differences from Tie::RefHash. 110*0Sstevel@tonic-gate$h = eval { tie %h, 'Tie::RefHash::Nestable' }; 111*0Sstevel@tonic-gatewarn $@ if $@; 112*0Sstevel@tonic-gatetest(not $@); 113*0Sstevel@tonic-gatetest(ref($h) eq 'Tie::RefHash::Nestable'); 114*0Sstevel@tonic-gatetest(defined(tied(%h)) and tied(%h) =~ /^Tie::RefHash::Nestable/); 115*0Sstevel@tonic-gate$h{$ref}->{$ref1} = 'bungo'; 116*0Sstevel@tonic-gatetest($h{$ref}->{$ref1} eq 'bungo'); 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate# Test that the nested hash is also tied (for current implementation) 119*0Sstevel@tonic-gatetest(defined(tied(%{$h{$ref}})) 120*0Sstevel@tonic-gate and tied(%{$h{$ref}}) =~ /^Tie::RefHash::Nestable=/ ); 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gatetest((keys %h) == 1); 123*0Sstevel@tonic-gatetest((keys %h)[0] eq $ref); 124*0Sstevel@tonic-gatetest((keys %{$h{$ref}}) == 1); 125*0Sstevel@tonic-gatetest((keys %{$h{$ref}})[0] eq $ref1); 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gatedie "expected to run $numtests tests, but ran ", $currtest - 1 129*0Sstevel@tonic-gate if $currtest - 1 != $numtests; 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate@tests = (); 132*0Sstevel@tonic-gateundef $ref; 133*0Sstevel@tonic-gateundef $ref1; 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gateexit(); 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate# Print 'ok X' if true, 'not ok X' if false 139*0Sstevel@tonic-gate# Uses global $currtest. 140*0Sstevel@tonic-gate# 141*0Sstevel@tonic-gatesub test { 142*0Sstevel@tonic-gate my $t = shift; 143*0Sstevel@tonic-gate print 'not ' if not $t; 144*0Sstevel@tonic-gate print 'ok ', $currtest++, "\n"; 145*0Sstevel@tonic-gate} 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate# Wrapper for Data::Dumper to 'dump' a scalar as an EXPR string. 149*0Sstevel@tonic-gatesub dumped { 150*0Sstevel@tonic-gate my $s = shift; 151*0Sstevel@tonic-gate my $d = Dumper($s); 152*0Sstevel@tonic-gate $d =~ s/^\$VAR1 =\s*//; 153*0Sstevel@tonic-gate $d =~ s/;$//; 154*0Sstevel@tonic-gate chomp $d; 155*0Sstevel@tonic-gate return $d; 156*0Sstevel@tonic-gate} 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate# Crudely dump a hash into a canonical string representation (because 159*0Sstevel@tonic-gate# hash keys can appear in any order, Data::Dumper may give different 160*0Sstevel@tonic-gate# strings for the same hash). 161*0Sstevel@tonic-gate# 162*0Sstevel@tonic-gatesub dumph { 163*0Sstevel@tonic-gate my $h = shift; 164*0Sstevel@tonic-gate my $r = ''; 165*0Sstevel@tonic-gate foreach (sort keys %$h) { 166*0Sstevel@tonic-gate $r = dumped($_) . ' => ' . dumped($h->{$_}) . "\n"; 167*0Sstevel@tonic-gate } 168*0Sstevel@tonic-gate return $r; 169*0Sstevel@tonic-gate} 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate# Run the tests and give results. 172*0Sstevel@tonic-gate# 173*0Sstevel@tonic-gate# Parameters: reference to list of tests to run 174*0Sstevel@tonic-gate# name of class to use for tied hash, or undef if not tied 175*0Sstevel@tonic-gate# 176*0Sstevel@tonic-gate# Returns: list of [R, W, E] tuples, one for each test. 177*0Sstevel@tonic-gate# R is the return value from running the test, W any warnings it gave, 178*0Sstevel@tonic-gate# and E any exception raised with 'die'. E and W will be tidied up a 179*0Sstevel@tonic-gate# little to remove irrelevant details like line numbers :-) 180*0Sstevel@tonic-gate# 181*0Sstevel@tonic-gate# Will also run a few of its own 'ok N' tests. 182*0Sstevel@tonic-gate# 183*0Sstevel@tonic-gatesub runtests { 184*0Sstevel@tonic-gate my ($tests, $class) = @_; 185*0Sstevel@tonic-gate my @r; 186*0Sstevel@tonic-gate 187*0Sstevel@tonic-gate my (%h, $h); 188*0Sstevel@tonic-gate if (defined $class) { 189*0Sstevel@tonic-gate $h = eval { tie %h, $class }; 190*0Sstevel@tonic-gate warn $@ if $@; 191*0Sstevel@tonic-gate test(not $@); 192*0Sstevel@tonic-gate test(ref($h) eq $class); 193*0Sstevel@tonic-gate test(defined(tied(%h)) and tied(%h) =~ /^\Q$class\E/); 194*0Sstevel@tonic-gate } 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate foreach (@$tests) { 197*0Sstevel@tonic-gate my ($result, $warning, $exception); 198*0Sstevel@tonic-gate local $SIG{__WARN__} = sub { $warning .= $_[0] }; 199*0Sstevel@tonic-gate $result = scalar(eval $_); 200*0Sstevel@tonic-gate if ($@) 201*0Sstevel@tonic-gate { 202*0Sstevel@tonic-gate die "$@:$_" unless defined $class; 203*0Sstevel@tonic-gate $exception = $@; 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate foreach ($warning, $exception) { 207*0Sstevel@tonic-gate next if not defined; 208*0Sstevel@tonic-gate s/ at .+ line \d+\.$//mg; 209*0Sstevel@tonic-gate s/ at .+ line \d+, at .*//mg; 210*0Sstevel@tonic-gate s/ at .+ line \d+, near .*//mg; 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate my (@warnings, %seen); 214*0Sstevel@tonic-gate foreach (split /\n/, $warning) { 215*0Sstevel@tonic-gate push @warnings, $_ unless $seen{$_}++; 216*0Sstevel@tonic-gate } 217*0Sstevel@tonic-gate $warning = join("\n", @warnings); 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gate push @r, [ $result, $warning, $exception ]; 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate return @r; 223*0Sstevel@tonic-gate} 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate# Things that should work just the same for an ordinary hash and a 227*0Sstevel@tonic-gate# Tie::RefHash. 228*0Sstevel@tonic-gate# 229*0Sstevel@tonic-gate# Each test is a code string to be eval'd, it should do something with 230*0Sstevel@tonic-gate# %h and give a scalar return value. The global $ref and $ref1 may 231*0Sstevel@tonic-gate# also be used. 232*0Sstevel@tonic-gate# 233*0Sstevel@tonic-gate# One thing we don't test is that the ordering from 'keys', 'values' 234*0Sstevel@tonic-gate# and 'each' is the same. You can't reasonably expect that. 235*0Sstevel@tonic-gate# 236*0Sstevel@tonic-gatesub standard_hash_tests { 237*0Sstevel@tonic-gate my @r; 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate # Library of standard tests on keys, values and each 240*0Sstevel@tonic-gate my $STD_TESTS = <<'END' 241*0Sstevel@tonic-gate join $;, sort keys %h; 242*0Sstevel@tonic-gate join $;, sort values %h; 243*0Sstevel@tonic-gate { my ($v, %tmp); $tmp{$v}++ while (defined($v = each %h)); dumph(\%tmp) } 244*0Sstevel@tonic-gate { my ($k, $v, %tmp); $tmp{"$k$;$v"}++ while (($k, $v) = each %h); dumph(\%tmp) } 245*0Sstevel@tonic-gateEND 246*0Sstevel@tonic-gate ; 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate # Tests on the existence of the element 'foo' 249*0Sstevel@tonic-gate my $FOO_TESTS = <<'END' 250*0Sstevel@tonic-gate defined $h{foo}; 251*0Sstevel@tonic-gate exists $h{foo}; 252*0Sstevel@tonic-gate $h{foo}; 253*0Sstevel@tonic-gateEND 254*0Sstevel@tonic-gate ; 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate # Test storing and deleting 'foo' 257*0Sstevel@tonic-gate push @r, split /\n/, <<"END" 258*0Sstevel@tonic-gate $STD_TESTS; 259*0Sstevel@tonic-gate $FOO_TESTS; 260*0Sstevel@tonic-gate \$h{foo} = undef; 261*0Sstevel@tonic-gate $STD_TESTS; 262*0Sstevel@tonic-gate $FOO_TESTS; 263*0Sstevel@tonic-gate \$h{foo} = 'hello'; 264*0Sstevel@tonic-gate $STD_TESTS; 265*0Sstevel@tonic-gate $FOO_TESTS; 266*0Sstevel@tonic-gate delete \$h{foo}; 267*0Sstevel@tonic-gate $STD_TESTS; 268*0Sstevel@tonic-gate $FOO_TESTS; 269*0Sstevel@tonic-gateEND 270*0Sstevel@tonic-gate ; 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate # Test storing and removing under ordinary keys 273*0Sstevel@tonic-gate my @things = ('boink', 0, 1, '', undef); 274*0Sstevel@tonic-gate foreach my $key (map { dumped($_) } @things) { 275*0Sstevel@tonic-gate foreach my $value ((map { dumped($_) } @things), '$ref') { 276*0Sstevel@tonic-gate push @r, split /\n/, <<"END" 277*0Sstevel@tonic-gate \$h{$key} = $value; 278*0Sstevel@tonic-gate $STD_TESTS; 279*0Sstevel@tonic-gate defined \$h{$key}; 280*0Sstevel@tonic-gate exists \$h{$key}; 281*0Sstevel@tonic-gate \$h{$key}; 282*0Sstevel@tonic-gate delete \$h{$key}; 283*0Sstevel@tonic-gate $STD_TESTS; 284*0Sstevel@tonic-gate defined \$h{$key}; 285*0Sstevel@tonic-gate exists \$h{$key}; 286*0Sstevel@tonic-gate \$h{$key}; 287*0Sstevel@tonic-gateEND 288*0Sstevel@tonic-gate ; 289*0Sstevel@tonic-gate } 290*0Sstevel@tonic-gate } 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate # Test hash slices 293*0Sstevel@tonic-gate my @slicetests; 294*0Sstevel@tonic-gate @slicetests = split /\n/, <<'END' 295*0Sstevel@tonic-gate @h{'b'} = (); 296*0Sstevel@tonic-gate @h{'c'} = ('d'); 297*0Sstevel@tonic-gate @h{'e'} = ('f', 'g'); 298*0Sstevel@tonic-gate @h{'h', 'i'} = (); 299*0Sstevel@tonic-gate @h{'j', 'k'} = ('l'); 300*0Sstevel@tonic-gate @h{'m', 'n'} = ('o', 'p'); 301*0Sstevel@tonic-gate @h{'q', 'r'} = ('s', 't', 'u'); 302*0Sstevel@tonic-gateEND 303*0Sstevel@tonic-gate ; 304*0Sstevel@tonic-gate my @aaa = @slicetests; 305*0Sstevel@tonic-gate foreach (@slicetests) { 306*0Sstevel@tonic-gate push @r, $_; 307*0Sstevel@tonic-gate push @r, split(/\n/, $STD_TESTS); 308*0Sstevel@tonic-gate } 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate # Test CLEAR 311*0Sstevel@tonic-gate push @r, '%h = ();', split(/\n/, $STD_TESTS); 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gate return @r; 314*0Sstevel@tonic-gate} 315*0Sstevel@tonic-gate 316