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 => 16 + 4 *12;
11 use Devel::Size ':all';
13 #############################################################################
14 # verify that pointer sizes in array slots are sensible:
15 # create an array with 4 slots, 2 of them used
16 my $array = [ 1,2,3,4 ]; pop @$array; pop @$array;
18 # the total size minus the array itself minus two scalars is 4 slots
19 my $ptr_size = total_size($array) - total_size( [] ) - total_size(1) * 2;
21 is ($ptr_size % 4, 0, '4 pointers are dividable by 4');
22 isnt ($ptr_size, 0, '4 pointers are not zero');
24 # size of one slot ptr
27 #############################################################################
28 # assert hash and hash key size
32 is (total_size($hash),
33 total_size( { a => undef } ) + total_size(1) - total_size(undef),
34 'assert hash and hash key size');
36 #############################################################################
37 # #24846 (Does not correctly recurse into references in a PVNV-type scalar)
39 # run the following tests with different sizes
41 for my $size (2, 3, 7, 100)
43 my $hash = { a => 1 };
45 # hash + key minus the value
46 my $hash_size = total_size($hash) - total_size(1);
51 my $pvnv_size = total_size(\$hash->{a}) - total_size([]);
53 my $ref_size = total_size(\\1) - total_size(1);
55 # $hash->{a} is now a PVNV, e.g. a scalar NV and a ref to an array:
56 # SV = PVNV(0x81ff9a8) at 0x8170d48
62 # SV = PVAV(0x8175d6c) at 0x81717bc
76 # Compare this to a plain array ref
77 #SV = RV(0x81a2834) at 0x8207a2c
81 # SV = PVAV(0x8175d98) at 0x8170b44
91 # Get the size of the PVNV and the contained array
92 my $element_size = total_size(\$hash->{a});
94 cmp_ok($element_size, '<', total_size($hash), "element < hash with one element");
95 cmp_ok($element_size, '>', total_size(\[]), "PVNV + [] > [] alone");
97 # Dereferencing the PVNV (the argument to total_size) leaves us with
98 # just the array, and this should be equal to a dereferenced array:
99 is (total_size($hash->{a}), total_size([]), '[] vs. []');
101 # the hash with one key
102 # the PVNV in the hash
103 # the RV inside the PVNV
104 # the contents of the array (array size)
106 my $full_hash = total_size($hash);
107 my $array_size = total_size([]);
108 is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV');
109 is ($full_hash, $array_size + $pvnv_size + $hash_size, 'properly recurses into PVNV');
111 $hash->{a} = [0..$size];
113 # the outer references stripped away, so they should be the same
114 is (total_size([0..$size]), total_size( $hash->{a} ), "hash element vs. array");
116 # the outer references included, one is just a normal ref, while the other
117 # is a PVNV, so they shouldn't be the same:
118 isnt (total_size(\[0..$size]), total_size( \$hash->{a} ), "[0..size] vs PVNV");
119 # and the plain ref should be smaller
120 cmp_ok(total_size(\[0..$size]), '<', total_size( \$hash->{a} ), "[0..size] vs. PVNV");
122 $full_hash = total_size($hash);
123 $element_size = total_size(\$hash->{a});
124 $array_size = total_size(\[0..$size]);
126 print "# full_hash = $full_hash\n";
127 print "# hash_size = $hash_size\n";
128 print "# array size: $array_size\n";
129 print "# element size: $element_size\n";
130 print "# ref_size = $ref_size\n";
131 print "# pvnv_size: $pvnv_size\n";
135 # the hash with one key
136 # the PVNV in the hash
137 # the RV inside the PVNV
138 # the contents of the array (array size)
140 is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV');
141 # is ($full_hash, $array_size + $pvnv_size + $hash_size, 'properly recurses into PVNV');
143 #############################################################################
144 # repeat the former test, but mix in some undef elements
146 $array_size = total_size(\[0..$size, undef, undef]);
148 $hash->{a} = [0..$size, undef, undef];
149 $element_size = total_size(\$hash->{a});
150 $full_hash = total_size($hash);
152 print "# full_hash = $full_hash\n";
153 print "# hash_size = $hash_size\n";
154 print "# array size: $array_size\n";
155 print "# element size: $element_size\n";
156 print "# ref_size = $ref_size\n";
157 print "# pvnv_size: $pvnv_size\n";
159 is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV');
161 #############################################################################
162 # repeat the former test, but use a pre-extended array
164 $array = [ 0..$size, undef, undef ]; pop @$array;
166 $array_size = total_size($array);
167 my $scalar_size = total_size(1) * (1+$size) + total_size(undef) * 1 + $ptr_size
168 + $ptr_size * ($size + 2) + total_size([]);
169 is ($scalar_size, $array_size, "computed right size if full array");
171 $hash->{a} = [0..$size, undef, undef]; pop @{$hash->{a}};
172 $full_hash = total_size($hash);
173 $element_size = total_size(\$hash->{a});
174 $array_size = total_size(\$array);
176 print "# full_hash = $full_hash\n";
177 print "# hash_size = $hash_size\n";
178 print "# array size: $array_size\n";
179 print "# element size: $element_size\n";
180 print "# ref_size = $ref_size\n";
181 print "# pvnv_size: $pvnv_size\n";
183 is ($full_hash, $element_size + $hash_size, 'properly handles undef/non-undef inside arrays');
185 } # end for different sizes
188 my($got, $want, $desc) = @_;
189 local $Test::Builder::Level = $Test::Builder::Level + 1;
190 is(@$got, @$want, "$desc (same element count)");
193 is($got->[$i], $want->[$i], "$desc (element $i)");
199 my $undef_size = total_size($undef);
200 cmp_ok($undef_size, '>', 0, 'non-zero size for NULL');
202 my $iv_size = total_size(1);
203 cmp_ok($iv_size, '>', 0, 'non-zero size for IV');
205 # Force the array to allocate storage for elements.
206 # This avoids making the assumption that just because it doesn't happen
207 # initially now, it won't stay that way forever.
209 my $array_1_size = total_size(\@array);
210 cmp_ok($array_1_size, '>', 0, 'non-zero size for array with 1 element');
216 # This might be making too many assumptions about the current implementation
217 my $array_2_size = total_size(\@array);
218 is($array_2_size, $array_1_size + $iv_size,
219 "gaps in arrays don't allocate scalars");
221 # Avoid using is_deeply() as that will read $#array, which is a write
222 # action prior to 5.12. (Different writes on 5.10 and 5.8-and-earlier, but
223 # a write either way, allocating memory.
224 cmp_array_ro(\@array, \@copy, 'two arrays compare the same');
229 is(total_size(\@array), $array_2_size + $undef_size,
230 "assigning undef to a gap in an array allocates a scalar");
232 cmp_array_ro(\@array, \@copy, 'two arrays compare the same');