source: trunk/essentials/dev-lang/perl/lib/Tie/RefHash.t

Last change on this file was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 8.6 KB
Line 
1#!/usr/bin/perl -w
2#
3# Basic test suite for Tie::RefHash and Tie::RefHash::Nestable.
4#
5# The testing is in two parts: first, run lots of tests on both a tied
6# hash and an ordinary un-tied hash, and check they give the same
7# answer. Then there are tests for those cases where the tied hashes
8# should behave differently to normal hashes, that is, when using
9# references as keys.
10#
11
12BEGIN {
13 chdir 't' if -d 't';
14 @INC = '.';
15 push @INC, '../lib';
16 require Config;
17 if (($Config::Config{'extensions'} !~ m!\bData/Dumper\b!) ){
18 print "1..0 # Skip -- Perl configured without Data::Dumper module\n";
19 exit 0;
20 }
21}
22
23use strict;
24use Tie::RefHash;
25use Data::Dumper;
26my $numtests = 39;
27my $currtest = 1;
28print "1..$numtests\n";
29
30my $ref = []; my $ref1 = [];
31
32package Boustrophedon; # A class with overloaded "".
33sub new { my ($c, $s) = @_; bless \$s, $c }
34use overload '""' => sub { ${$_[0]} . reverse ${$_[0]} };
35package main;
36my $ox = Boustrophedon->new("foobar");
37
38# Test standard hash functionality, by performing the same operations
39# on a tied hash and on a normal hash, and checking that the results
40# are the same. This does of course assume that Perl hashes are not
41# buggy :-)
42#
43my @tests = standard_hash_tests();
44
45my @ordinary_results = runtests(\@tests, undef);
46foreach my $class ('Tie::RefHash', 'Tie::RefHash::Nestable') {
47 my @tied_results = runtests(\@tests, $class);
48 my $all_ok = 1;
49
50 die if @ordinary_results != @tied_results;
51 foreach my $i (0 .. $#ordinary_results) {
52 my ($or, $ow, $oe) = @{$ordinary_results[$i]};
53 my ($tr, $tw, $te) = @{$tied_results[$i]};
54
55 my $ok = 1;
56 local $^W = 0;
57 $ok = 0 if (defined($or) != defined($tr)) or ($or ne $tr);
58 $ok = 0 if (defined($ow) != defined($tw)) or ($ow ne $tw);
59 $ok = 0 if (defined($oe) != defined($te)) or ($oe ne $te);
60
61 if (not $ok) {
62 print STDERR
63 "failed for $class: $tests[$i]\n",
64 "ordinary hash gave:\n",
65 defined $or ? "\tresult: $or\n" : "\tundef result\n",
66 defined $ow ? "\twarning: $ow\n" : "\tno warning\n",
67 defined $oe ? "\texception: $oe\n" : "\tno exception\n",
68 "tied $class hash gave:\n",
69 defined $tr ? "\tresult: $tr\n" : "\tundef result\n",
70 defined $tw ? "\twarning: $tw\n" : "\tno warning\n",
71 defined $te ? "\texception: $te\n" : "\tno exception\n",
72 "\n";
73 $all_ok = 0;
74 }
75 }
76 test($all_ok);
77}
78
79# Now test Tie::RefHash's special powers
80my (%h, $h);
81$h = eval { tie %h, 'Tie::RefHash' };
82warn $@ if $@;
83test(not $@);
84test(ref($h) eq 'Tie::RefHash');
85test(defined(tied(%h)) and tied(%h) =~ /^Tie::RefHash/);
86$h{$ref} = 'cholet';
87test($h{$ref} eq 'cholet');
88test(exists $h{$ref});
89test((keys %h) == 1);
90test(ref((keys %h)[0]) eq 'ARRAY');
91test((keys %h)[0] eq $ref);
92test((values %h) == 1);
93test((values %h)[0] eq 'cholet');
94my $count = 0;
95while (my ($k, $v) = each %h) {
96 if ($count++ == 0) {
97 test(ref($k) eq 'ARRAY');
98 test($k eq $ref);
99 }
100}
101test($count == 1);
102delete $h{$ref};
103test(not defined $h{$ref});
104test(not exists($h{$ref}));
105test((keys %h) == 0);
106test((values %h) == 0);
107$h{$ox} = "bellow"; # overloaded ""
108test(exists $h{$ox});
109test($h{$ox} eq "bellow");
110test(not exists $h{"foobarraboof"});
111undef $h;
112untie %h;
113
114# And now Tie::RefHash::Nestable's differences from Tie::RefHash.
115$h = eval { tie %h, 'Tie::RefHash::Nestable' };
116warn $@ if $@;
117test(not $@);
118test(ref($h) eq 'Tie::RefHash::Nestable');
119test(defined(tied(%h)) and tied(%h) =~ /^Tie::RefHash::Nestable/);
120$h{$ref}->{$ref1} = 'bungo';
121test($h{$ref}->{$ref1} eq 'bungo');
122
123# Test that the nested hash is also tied (for current implementation)
124test(defined(tied(%{$h{$ref}}))
125 and tied(%{$h{$ref}}) =~ /^Tie::RefHash::Nestable=/ );
126
127test((keys %h) == 1);
128test((keys %h)[0] eq $ref);
129test((keys %{$h{$ref}}) == 1);
130test((keys %{$h{$ref}})[0] eq $ref1);
131
132{
133 # Tests that delete returns the deleted element [perl #32193]
134 my $ref = \(my $var = "oink");
135 tie my %oink, 'Tie::RefHash';
136 $oink{$ref} = "ding";
137 test($oink{$ref} eq "ding");
138 test(delete($oink{$ref}) eq "ding");
139}
140
141die "expected to run $numtests tests, but ran ", $currtest - 1
142 if $currtest - 1 != $numtests;
143
144@tests = ();
145undef $ref;
146undef $ref1;
147
148exit();
149
150
151# Print 'ok X' if true, 'not ok X' if false
152# Uses global $currtest.
153#
154sub test {
155 my $t = shift;
156 print 'not ' if not $t;
157 print 'ok ', $currtest++, "\n";
158}
159
160
161# Wrapper for Data::Dumper to 'dump' a scalar as an EXPR string.
162sub dumped {
163 my $s = shift;
164 my $d = Dumper($s);
165 $d =~ s/^\$VAR1 =\s*//;
166 $d =~ s/;$//;
167 chomp $d;
168 return $d;
169}
170
171# Crudely dump a hash into a canonical string representation (because
172# hash keys can appear in any order, Data::Dumper may give different
173# strings for the same hash).
174#
175sub dumph {
176 my $h = shift;
177 my $r = '';
178 foreach (sort keys %$h) {
179 $r = dumped($_) . ' => ' . dumped($h->{$_}) . "\n";
180 }
181 return $r;
182}
183
184# Run the tests and give results.
185#
186# Parameters: reference to list of tests to run
187# name of class to use for tied hash, or undef if not tied
188#
189# Returns: list of [R, W, E] tuples, one for each test.
190# R is the return value from running the test, W any warnings it gave,
191# and E any exception raised with 'die'. E and W will be tidied up a
192# little to remove irrelevant details like line numbers :-)
193#
194# Will also run a few of its own 'ok N' tests.
195#