Test that unallocated gaps within arrays have zero size.
[p5sagit/Devel-Size.git] / t / recurse.t
1 #!/usr/bin/perl -w
2
3 # IMPORTANT NOTE:
4 #
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!
8
9 use Test::More tests => 16 + 4 *12;
10 use strict;
11 use Devel::Size ':all';
12
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;
17
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;
20
21 is ($ptr_size % 4, 0, '4 pointers are dividable by 4');
22 isnt ($ptr_size, 0, '4 pointers are not zero');
23
24 # size of one slot ptr
25 $ptr_size /= 4;
26
27 #############################################################################
28 # assert hash and hash key size
29
30 my $hash = {};
31 $hash->{a} = 1;
32 is (total_size($hash),
33     total_size( { a => undef } ) + total_size(1) - total_size(undef),
34     'assert hash and hash key size');
35
36 #############################################################################
37 # #24846 (Does not correctly recurse into references in a PVNV-type scalar)
38
39 # run the following tests with different sizes
40
41 for my $size (2, 3, 7, 100)
42   {
43   my $hash = { a => 1 };
44
45   # hash + key minus the value
46   my $hash_size = total_size($hash) - total_size(1);
47
48   $hash->{a} = 0/1;
49   $hash->{a} = [];
50
51   my $pvnv_size = total_size(\$hash->{a}) - total_size([]);
52   # size of one ref
53   my $ref_size = total_size(\\1) - total_size(1);
54
55   # $hash->{a} is now a PVNV, e.g. a scalar NV and a ref to an array:
56 #  SV = PVNV(0x81ff9a8) at 0x8170d48
57 #  REFCNT = 1
58 #  FLAGS = (ROK)
59 #  IV = 0
60 #  NV = 0
61 #  RV = 0x81717bc
62 #  SV = PVAV(0x8175d6c) at 0x81717bc
63 #    REFCNT = 1
64 #    FLAGS = ()
65 #    IV = 0
66 #    NV = 0
67 #    ARRAY = 0x0
68 #    FILL = -1
69 #    MAX = -1
70 #    ARYLEN = 0x0
71 #    FLAGS = (REAL)
72 #  PV = 0x81717bc ""
73 #  CUR = 0
74 #  LEN = 0
75
76   # Compare this to a plain array ref
77 #SV = RV(0x81a2834) at 0x8207a2c
78 #  REFCNT = 1
79 #  FLAGS = (TEMP,ROK)
80 #  RV = 0x8170b44
81 #  SV = PVAV(0x8175d98) at 0x8170b44
82 #    REFCNT = 2
83 #    FLAGS = ()
84 #    IV = 0
85 #    NV = 0
86 #    ARRAY = 0x0
87 #    FILL = -1
88 #    MAX = -1
89 #    ARYLEN = 0x0
90
91   # Get the size of the PVNV and the contained array
92   my $element_size = total_size(\$hash->{a});
93
94   cmp_ok($element_size, '<', total_size($hash), "element < hash with one element");
95   cmp_ok($element_size, '>', total_size(\[]), "PVNV + [] > [] alone");
96
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. []');
100
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)
105
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');
110
111   $hash->{a} = [0..$size];
112
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");
115
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");
121
122   $full_hash = total_size($hash);
123   $element_size = total_size(\$hash->{a});
124   $array_size = total_size(\[0..$size]);
125
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";
132
133   # the total size is:
134
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)
139
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');
142
143 #############################################################################
144 # repeat the former test, but mix in some undef elements
145
146   $array_size = total_size(\[0..$size, undef, undef]);
147
148   $hash->{a} = [0..$size, undef, undef];
149   $element_size = total_size(\$hash->{a});
150   $full_hash = total_size($hash);
151
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";
158
159   is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV');
160
161 #############################################################################
162 # repeat the former test, but use a pre-extended array
163
164   $array = [ 0..$size, undef, undef ]; pop @$array;
165
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");
170
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);
175
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";
182
183   is ($full_hash, $element_size + $hash_size, 'properly handles undef/non-undef inside arrays');
184
185   } # end for different sizes
186
187 sub cmp_array_ro {
188     my($got, $want, $desc) = @_;
189     local $Test::Builder::Level = $Test::Builder::Level + 1;
190     is(@$got, @$want, "$desc (same element count)");
191     my $i = @$want;
192     while ($i--) {
193         is($got->[$i], $want->[$i], "$desc (element $i)");
194     }
195 }
196
197 {
198     my $undef;
199     my $undef_size = total_size($undef);
200     cmp_ok($undef_size, '>', 0, 'non-zero size for NULL');
201
202     my $iv_size = total_size(1);
203     cmp_ok($iv_size, '>', 0, 'non-zero size for IV');
204
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.
208     my @array = 42;
209     my $array_1_size = total_size(\@array);
210     cmp_ok($array_1_size, '>', 0, 'non-zero size for array with 1 element');
211
212     $array[2] = 6 * 9;
213
214     my @copy = @array;
215
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");
220
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');
225
226     # A write action:
227     $array[1] = undef;
228
229     is(total_size(\@array), $array_2_size + $undef_size,
230        "assigning undef to a gap in an array allocates a scalar");
231
232     cmp_array_ro(\@array, \@copy, 'two arrays compare the same');
233 }