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!
11 use Devel::Size ':all';
19 PVIV => do { my $a = 1; $a = "One"; $a },
20 PVNV => do { my $a = 3.14; $a = "Mmm, pi"; $a },
21 PVMG => do { my $a = $!; $a = "Bang!"; $a },
24 plan(tests => 16 + 4 *12 + 2 * scalar keys %types);
26 #############################################################################
27 # verify that pointer sizes in array slots are sensible:
28 # create an array with 4 slots, 2 of them used
29 my $array = [ 1,2,3,4 ]; pop @$array; pop @$array;
31 # the total size minus the array itself minus two scalars is 4 slots
32 my $ptr_size = total_size($array) - total_size( [] ) - total_size(1) * 2;
34 is ($ptr_size % 4, 0, '4 pointers are dividable by 4');
35 isnt ($ptr_size, 0, '4 pointers are not zero');
37 # size of one slot ptr
40 #############################################################################
41 # assert hash and hash key size
45 is (total_size($hash),
46 total_size( { a => undef } ) + total_size(1) - total_size(undef),
47 'assert hash and hash key size');
49 #############################################################################
50 # #24846 (Does not correctly recurse into references in a PVNV-type scalar)
52 # run the following tests with different sizes
54 for my $size (2, 3, 7, 100)
56 my $hash = { a => 1 };
58 # hash + key minus the value
59 my $hash_size = total_size($hash) - total_size(1);
64 my $pvnv_size = total_size(\$hash->{a}) - total_size([]);
66 my $ref_size = total_size(\\1) - total_size(1);
68 # $hash->{a} is now a PVNV, e.g. a scalar NV and a ref to an array:
69 # SV = PVNV(0x81ff9a8) at 0x8170d48
75 # SV = PVAV(0x8175d6c) at 0x81717bc
89 # Compare this to a plain array ref
90 #SV = RV(0x81a2834) at 0x8207a2c
94 # SV = PVAV(0x8175d98) at 0x8170b44
104 # Get the size of the PVNV and the contained array
105 my $element_size = total_size(\$hash->{a});
107 cmp_ok($element_size, '<', total_size($hash), "element < hash with one element");
108 cmp_ok($element_size, '>', total_size(\[]), "PVNV + [] > [] alone");
110 # Dereferencing the PVNV (the argument to total_size) leaves us with
111 # just the array, and this should be equal to a dereferenced array:
112 is (total_size($hash->{a}), total_size([]), '[] vs. []');
114 # the hash with one key
115 # the PVNV in the hash
116 # the RV inside the PVNV
117 # the contents of the array (array size)
119 my $full_hash = total_size($hash);
120 my $array_size = total_size([]);
121 is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV');
122 is ($full_hash, $array_size + $pvnv_size + $hash_size, 'properly recurses into PVNV');
124 $hash->{a} = [0..$size];
126 # the outer references stripped away, so they should be the same
127 is (total_size([0..$size]), total_size( $hash->{a} ), "hash element vs. array");
129 # the outer references included, one is just a normal ref, while the other
130 # is a PVNV, so they shouldn't be the same:
131 isnt (total_size(\[0..$size]), total_size( \$hash->{a} ), "[0..size] vs PVNV");
132 # and the plain ref should be smaller
133 cmp_ok(total_size(\[0..$size]), '<', total_size( \$hash->{a} ), "[0..size] vs. PVNV");
135 $full_hash = total_size($hash);
136 $element_size = total_size(\$hash->{a});
137 $array_size = total_size(\[0..$size]);
139 print "# full_hash = $full_hash\n";
140 print "# hash_size = $hash_size\n";
141 print "# array size: $array_size\n";
142 print "# element size: $element_size\n";
143 print "# ref_size = $ref_size\n";
144 print "# pvnv_size: $pvnv_size\n";
148 # the hash with one key
149 # the PVNV in the hash
150 # the RV inside the PVNV
151 # the contents of the array (array size)
153 is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV');
154 # is ($full_hash, $array_size + $pvnv_size + $hash_size, 'properly recurses into PVNV');
156 #############################################################################
157 # repeat the former test, but mix in some undef elements
159 $array_size = total_size(\[0..$size, undef, undef]);
161 $hash->{a} = [0..$size, undef, undef];
162 $element_size = total_size(\$hash->{a});
163 $full_hash = total_size($hash);
165 print "# full_hash = $full_hash\n";
166 print "# hash_size = $hash_size\n";
167 print "# array size: $array_size\n";
168 print "# element size: $element_size\n";
169 print "# ref_size = $ref_size\n";
170 print "# pvnv_size: $pvnv_size\n";
172 is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV');
174 #############################################################################
175 # repeat the former test, but use a pre-extended array
177 $array = [ 0..$size, undef, undef ]; pop @$array;
179 $array_size = total_size($array);
180 my $scalar_size = total_size(1) * (1+$size) + total_size(undef) * 1 + $ptr_size
181 + $ptr_size * ($size + 2) + total_size([]);
182 is ($scalar_size, $array_size, "computed right size if full array");
184 $hash->{a} = [0..$size, undef, undef]; pop @{$hash->{a}};
185 $full_hash = total_size($hash);
186 $element_size = total_size(\$hash->{a});
187 $array_size = total_size(\$array);
189 print "# full_hash = $full_hash\n";
190 print "# hash_size = $hash_size\n";
191 print "# array size: $array_size\n";
192 print "# element size: $element_size\n";
193 print "# ref_size = $ref_size\n";
194 print "# pvnv_size: $pvnv_size\n";
196 is ($full_hash, $element_size + $hash_size, 'properly handles undef/non-undef inside arrays');
198 } # end for different sizes
201 my($got, $want, $desc) = @_;
202 local $Test::Builder::Level = $Test::Builder::Level + 1;
203 is(@$got, @$want, "$desc (same element count)");
206 is($got->[$i], $want->[$i], "$desc (element $i)");
212 my $undef_size = total_size($undef);
213 cmp_ok($undef_size, '>', 0, 'non-zero size for NULL');
215 my $iv_size = total_size(1);
216 cmp_ok($iv_size, '>', 0, 'non-zero size for IV');
218 # Force the array to allocate storage for elements.
219 # This avoids making the assumption that just because it doesn't happen
220 # initially now, it won't stay that way forever.
222 my $array_1_size = total_size(\@array);
223 cmp_ok($array_1_size, '>', 0, 'non-zero size for array with 1 element');
229 # This might be making too many assumptions about the current implementation
230 my $array_2_size = total_size(\@array);
231 is($array_2_size, $array_1_size + $iv_size,
232 "gaps in arrays don't allocate scalars");
234 # Avoid using is_deeply() as that will read $#array, which is a write
235 # action prior to 5.12. (Different writes on 5.10 and 5.8-and-earlier, but
236 # a write either way, allocating memory.
237 cmp_array_ro(\@array, \@copy, 'two arrays compare the same');
242 is(total_size(\@array), $array_2_size + $undef_size,
243 "assigning undef to a gap in an array allocates a scalar");
245 cmp_array_ro(\@array, \@copy, 'two arrays compare the same');
250 # reverse sort ensures that PVIV, PVNV and RV are processed before
251 # IV, NULL, or NV :-)
252 foreach my $type (reverse sort keys %types) {
253 # Need to make sure this goes in a new scalar every time. Putting it
254 # directly in a lexical means that it's in the pad, and the pad recycles
255 # scalars, a side effect of which is that they get upgraded in ways we
258 $a->[0] = $types{$type};
261 my $expect = $sizes{$type} = size(\$a->[0]);
263 $a->[0] = \('x' x 1024);
265 $expect = $sizes{RV} if $type eq 'NULL';
266 $expect = $sizes{PVNV} if $type eq 'NV';
267 $expect = $sizes{PVIV} if $type eq 'IV' && $] < 5.012;
269 # Remember, size() removes a level of referencing if present. So add
270 # one, so that we get the size of our reference:
271 is(size(\$a->[0]), $expect,
272 "Type $type containing a reference, size() does not recurse to the referent");
273 cmp_ok(total_size(\$a->[0]), '>', 1024,
274 "Type $type, total_size() recurses to the referent");