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.
11 require Config; import Config;
12 if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
13 print "1..0 # Skip: Storable was not built\n";
19 use Storable qw(freeze thaw dclone);
20 use vars qw($debugging $verbose);
25 my($testno, $ok) = @_;
26 print "not " unless $ok;
31 # Uncomment the folowing line to get a dump of the constructed data structure
32 # (you may want to reduce the size of the hashes too)
39 # Use MD5 if its available to make random string keys
41 eval { require "MD5.pm" };
44 # Use Data::Dumper if debugging and it is available to create an ASCII dump
47 eval { require "Data/Dumper.pm" };
51 @fixed_strings = ("January", "February", "March", "April", "May", "June",
52 "July", "August", "September", "October", "November", "December" );
54 # Build some arbitrarily complex data structure starting with a top level hash
55 # (deeper levels contain scalars, references to hashes or references to arrays);
57 for (my $i = 0; $i < $hashsize; $i++) {
58 my($k) = int(rand(1_000_000));
59 $k = MD5->hexhash($k) if $gotmd5 and int(rand(2));
60 $a1{$k} = { key => "$k", "value" => $i };
62 # A third of the elements are references to further hashes
66 my($hash2size) = int(rand($maxhash2size));
67 while ($hash2size--) {
68 my($k2) = $k . $i . int(rand(100));
69 $hash2->{$k2} = $fixed_strings[rand(int(@fixed_strings))];
71 $a1{$k}->{value} = $hash2;
74 # A further third are references to arrays
76 elsif (int(rand(2))) {
78 my($arraysize) = int(rand($maxarraysize));
79 while ($arraysize--) {
80 push(@$arr_ref, $fixed_strings[rand(int(@fixed_strings))]);
82 $a1{$k}->{value} = $arr_ref;
87 print STDERR Data::Dumper::Dumper(\%a1) if ($verbose and $gotdd);
90 # Copy the hash, element by element in order of the keys
92 foreach $k (sort keys %a1) {
93 $a2{$k} = { key => "$k", "value" => $a1{$k}->{value} };
100 # In canonical mode the frozen representation of each of the hashes
101 # should be identical
103 $Storable::canonical = 1;
109 ok 1, (length($x1) > $hashsize); # sanity check
110 ok 2, length($x1) == length($x2); # idem
114 # In normal mode it is exceedingly unlikely that the frozen
115 # representaions of all the hashes will be the same (normally the hash
116 # elements are frozen in the order they are stored internally,
117 # i.e. pseudo-randomly).
119 $Storable::canonical = 0;
126 # Two out of three the same may be a coincidence, all three the same
127 # is much, much more unlikely. Still it could happen, so this test
128 # may report a false negative.
130 ok 5, ($x1 ne $x2) || ($x1 ne $x3);
133 # Ensure refs to "undef" values are properly shared
134 # Same test as in t/dclone.t to ensure the "canonical" code is also correct
137 push @{$$hash{''}}, \$$hash{a};
138 ok 6, $$hash{''}[0] == \$$hash{a};
140 my $cloned = dclone(dclone($hash));
141 ok 7, $$cloned{''}[0] == \$$cloned{a};
143 $$cloned{a} = "blah";
144 ok 8, $$cloned{''}[0] == \$$cloned{a};