3 # Copyright (c) 1995-2000, Raphael Manfredi
5 # You may redistribute only under the same terms as Perl 5, as specified
6 # in the README file that comes with the distribution.
12 @INC = ('.', '../lib');
16 require Config; import Config;
17 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
18 print "1..0 # Skip: Storable was not built\n";
24 use Storable qw(freeze thaw dclone);
25 use vars qw($debugging $verbose);
30 my($testno, $ok) = @_;
31 print "not " unless $ok;
36 # Uncomment the folowing line to get a dump of the constructed data structure
37 # (you may want to reduce the size of the hashes too)
44 # Use MD5 if its available to make random string keys
46 eval { require "MD5.pm" };
49 # Use Data::Dumper if debugging and it is available to create an ASCII dump
52 eval { require "Data/Dumper.pm" };
56 @fixed_strings = ("January", "February", "March", "April", "May", "June",
57 "July", "August", "September", "October", "November", "December" );
59 # Build some arbitrarily complex data structure starting with a top level hash
60 # (deeper levels contain scalars, references to hashes or references to arrays);
62 for (my $i = 0; $i < $hashsize; $i++) {
63 my($k) = int(rand(1_000_000));
64 $k = MD5->hexhash($k) if $gotmd5 and int(rand(2));
65 $a1{$k} = { key => "$k", "value" => $i };
67 # A third of the elements are references to further hashes
71 my($hash2size) = int(rand($maxhash2size));
72 while ($hash2size--) {
73 my($k2) = $k . $i . int(rand(100));
74 $hash2->{$k2} = $fixed_strings[rand(int(@fixed_strings))];
76 $a1{$k}->{value} = $hash2;
79 # A further third are references to arrays
81 elsif (int(rand(2))) {
83 my($arraysize) = int(rand($maxarraysize));
84 while ($arraysize--) {
85 push(@$arr_ref, $fixed_strings[rand(int(@fixed_strings))]);
87 $a1{$k}->{value} = $arr_ref;
92 print STDERR Data::Dumper::Dumper(\%a1) if ($verbose and $gotdd);
95 # Copy the hash, element by element in order of the keys
97 foreach $k (sort keys %a1) {
98 $a2{$k} = { key => "$k", "value" => $a1{$k}->{value} };
101 # Deep clone the hash
105 # In canonical mode the frozen representation of each of the hashes
106 # should be identical
108 $Storable::canonical = 1;
114 ok 1, (length($x1) > $hashsize); # sanity check
115 ok 2, length($x1) == length($x2); # idem
119 # In normal mode it is exceedingly unlikely that the frozen
120 # representaions of all the hashes will be the same (normally the hash
121 # elements are frozen in the order they are stored internally,
122 # i.e. pseudo-randomly).
124 $Storable::canonical = 0;
131 # Two out of three the same may be a coincidence, all three the same
132 # is much, much more unlikely. Still it could happen, so this test
133 # may report a false negative.
135 ok 5, ($x1 ne $x2) || ($x1 ne $x3);
138 # Ensure refs to "undef" values are properly shared
139 # Same test as in t/dclone.t to ensure the "canonical" code is also correct
142 push @{$$hash{''}}, \$$hash{a};
143 ok 6, $$hash{''}[0] == \$$hash{a};
145 my $cloned = dclone(dclone($hash));
146 ok 7, $$cloned{''}[0] == \$$cloned{a};
148 $$cloned{a} = "blah";
149 ok 8, $$cloned{''}[0] == \$$cloned{a};