5 # When testing total_size(), always remember that it dereferences things, so
6 # total_size([]) will NOT return the size of the ref + the array, it will only
7 # return the size of the array alone!
9 use Test::More tests => 3 + 4 *12;
11 use Devel::Size ':all';
14 #############################################################################
15 # verify that pointer sizes in array slots are sensible:
16 # create an array with 4 slots, 2 of them used
17 my $array = [ 1,2,3,4 ]; pop @$array; pop @$array;
19 # the total size minus the array itself minus two scalars is 4 slots
20 my $ptr_size = total_size($array) - total_size( [] ) - total_size(1) * 2;
22 is ($ptr_size % 4, 0, '4 pointers are dividable by 4');
23 isnt ($ptr_size, 0, '4 pointers are not zero');
25 # size of one slot ptr
28 #############################################################################
29 # assert hash and hash key size
33 is (total_size($hash),
34 total_size( { a => undef } ) + total_size(1) - total_size(undef),
35 'assert hash and hash key size');
37 #############################################################################
38 # #24846 (Does not correctly recurse into references in a PVNV-type scalar)
40 # run the following tests with different sizes
42 for my $size (2, 3, 7, 100)
44 my $hash = { a => 1 };
46 # hash + key minus the value
47 my $hash_size = total_size($hash) - total_size(1);
52 my $pvnv_size = total_size(\$hash->{a}) - total_size([]);
54 my $ref_size = total_size(\\1) - total_size(1);
56 # $hash->{a} is now a PVNV, e.g. a scalar NV and a ref to an array:
57 # SV = PVNV(0x81ff9a8) at 0x8170d48
63 # SV = PVAV(0x8175d6c) at 0x81717bc
77 # Compare this to a plain array ref
78 #SV = RV(0x81a2834) at 0x8207a2c
82 # SV = PVAV(0x8175d98) at 0x8170b44
92 # Get the size of the PVNV and the contained array
93 my $element_size = total_size(\$hash->{a});
95 cmp_ok($element_size, '<', total_size($hash), "element < hash with one element");
96 cmp_ok($element_size, '>', total_size(\[]), "PVNV + [] > [] alone");
98 # Dereferencing the PVNV (the argument to total_size) leaves us with
99 # just the array, and this should be equal to a dereferenced array:
100 is (total_size($hash->{a}), total_size([]), '[] vs. []');
102 # the hash with one key
103 # the PVNV in the hash
104 # the RV inside the PVNV
105 # the contents of the array (array size)
107 my $full_hash = total_size($hash);
108 my $array_size = total_size([]);
109 is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV');
110 is ($full_hash, $array_size + $pvnv_size + $hash_size, 'properly recurses into PVNV');
112 $hash->{a} = [0..$size];
114 # the outer references stripped away, so they should be the same
115 is (total_size([0..$size]), total_size( $hash->{a} ), "hash element vs. array");
117 # the outer references included, one is just a normal ref, while the other
118 # is a PVNV, so they shouldn't be the same:
119 isnt (total_size(\[0..$size]), total_size( \$hash->{a} ), "[0..size] vs PVNV");
120 # and the plain ref should be smaller
121 cmp_ok(total_size(\[0..$size]), '<', total_size( \$hash->{a} ), "[0..size] vs. PVNV");
123 $full_hash = total_size($hash);
124 $element_size = total_size(\$hash->{a});
125 $array_size = total_size(\[0..$size]);
127 print "# full_hash = $full_hash\n";
128 print "# hash_size = $hash_size\n";
129 print "# array size: $array_size\n";
130 print "# element size: $element_size\n";
131 print "# ref_size = $ref_size\n";
132 print "# pvnv_size: $pvnv_size\n";
136 # the hash with one key
137 # the PVNV in the hash
138 # the RV inside the PVNV
139 # the contents of the array (array size)
141 is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV');
142 # is ($full_hash, $array_size + $pvnv_size + $hash_size, 'properly recurses into PVNV');
144 #############################################################################
145 # repeat the former test, but mix in some undef elements
147 $array_size = total_size(\[0..$size, undef, undef]);
149 $hash->{a} = [0..$size, undef, undef];
150 $element_size = total_size(\$hash->{a});
151 $full_hash = total_size($hash);
153 print "# full_hash = $full_hash\n";
154 print "# hash_size = $hash_size\n";
155 print "# array size: $array_size\n";
156 print "# element size: $element_size\n";
157 print "# ref_size = $ref_size\n";
158 print "# pvnv_size: $pvnv_size\n";
160 is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV');
162 #############################################################################
163 # repeat the former test, but use a pre-extended array
165 $array = [ 0..$size, undef, undef ]; pop @$array;
167 $array_size = total_size($array);
168 my $scalar_size = total_size(1) * (1+$size) + total_size(undef) * 1 + $ptr_size
169 + $ptr_size * ($size + 2) + total_size([]);
170 is ($scalar_size, $array_size, "computed right size if full array");
172 $hash->{a} = [0..$size, undef, undef]; pop @{$hash->{a}};
173 $full_hash = total_size($hash);
174 $element_size = total_size(\$hash->{a});
175 $array_size = total_size(\$array);
177 print "# full_hash = $full_hash\n";
178 print "# hash_size = $hash_size\n";
179 print "# array size: $array_size\n";
180 print "# element size: $element_size\n";
181 print "# ref_size = $ref_size\n";
182 print "# pvnv_size: $pvnv_size\n";
184 is ($full_hash, $element_size + $hash_size, 'properly handles undef/non-undef inside arrays');
186 } # end for different sizes