#
# Basic test suite for Tie::RefHash and Tie::RefHash::Nestable.
#
# The testing is in two parts: first, run lots of tests on both a tied
# hash and an ordinary un-tied hash, and check they give the same
# answer. Then there are tests for those cases where the tied hashes
# should behave differently to normal hashes, that is, when using
# references as keys.
#
BEGIN {
chdir 't' if -d 't';
}
use strict;
my $numtests = 37;
my $currtest = 1;
print "1..$numtests\n";
# Test standard hash functionality, by performing the same operations
# on a tied hash and on a normal hash, and checking that the results
# are the same. This does of course assume that Perl hashes are not
# buggy :-)
#
my $all_ok = 1;
die if @ordinary_results != @tied_results;
my $ok = 1;
local $^W = 0;
print STDERR
"ordinary hash gave:\n",
"\n";
$all_ok = 0;
}
}
}
# Now test Tie::RefHash's special powers
my (%h, $h);
warn $@ if $@;
my $count = 0;
}
}
undef $h;
untie %h;
# And now Tie::RefHash::Nestable's differences from Tie::RefHash.
warn $@ if $@;
# Test that the nested hash is also tied (for current implementation)
exit();
# Print 'ok X' if true, 'not ok X' if false
# Uses global $currtest.
#
sub test {
my $t = shift;
}
# Wrapper for Data::Dumper to 'dump' a scalar as an EXPR string.
sub dumped {
my $s = shift;
$d =~ s/^\$VAR1 =\s*//;
$d =~ s/;$//;
chomp $d;
return $d;
}
# Crudely dump a hash into a canonical string representation (because
# hash keys can appear in any order, Data::Dumper may give different
# strings for the same hash).
#
sub dumph {
my $h = shift;
my $r = '';
}
return $r;
}
# Run the tests and give results.
#
# Parameters: reference to list of tests to run
# name of class to use for tied hash, or undef if not tied
#
# Returns: list of [R, W, E] tuples, one for each test.
# R is the return value from running the test, W any warnings it gave,
# and E any exception raised with 'die'. E and W will be tidied up a
# little to remove irrelevant details like line numbers :-)
#
# Will also run a few of its own 'ok N' tests.
#
sub runtests {
my @r;
my (%h, $h);
warn $@ if $@;
}
if ($@)
{
$exception = $@;
}
}
}
push @r, [ $result, $warning, $exception ];
}
return @r;
}
# Things that should work just the same for an ordinary hash and a
# Tie::RefHash.
#
# Each test is a code string to be eval'd, it should do something with
# %h and give a scalar return value. The global $ref and $ref1 may
# also be used.
#
# One thing we don't test is that the ordering from 'keys', 'values'
# and 'each' is the same. You can't reasonably expect that.
#
sub standard_hash_tests {
my @r;
# Library of standard tests on keys, values and each
my $STD_TESTS = <<'END'
;
# Tests on the existence of the element 'foo'
my $FOO_TESTS = <<'END'
$h{foo};
;
# Test storing and deleting 'foo'
;
# Test storing and removing under ordinary keys
\$h{$key};
\$h{$key};
;
}
}
# Test hash slices
my @slicetests;
@slicetests = split /\n/, <<'END'
@h{'b'} = ();
@h{'c'} = ('d');
@h{'e'} = ('f', 'g');
@h{'h', 'i'} = ();
@h{'j', 'k'} = ('l');
@h{'m', 'n'} = ('o', 'p');
@h{'q', 'r'} = ('s', 't', 'u');
;
my @aaa = @slicetests;
push @r, $_;
}
# Test CLEAR
return @r;
}