Commit | Line | Data |
b1e5ad85 |
1 | #!/usr/bin/perl -w |
2 | |
c8db37d3 |
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 | |
b1e5ad85 |
9 | use Test::More; |
10 | use strict; |
11 | |
12 | my $tests; |
13 | |
14 | BEGIN |
15 | { |
16 | chdir 't' if -d 't'; |
c8db37d3 |
17 | plan tests => 6 + 4 * 12; |
b1e5ad85 |
18 | |
19 | use lib '../lib'; |
20 | use lib '../blib/arch'; |
21 | use_ok('Devel::Size'); |
22 | } |
23 | |
24 | can_ok ('Devel::Size', qw/ |
25 | size |
26 | total_size |
27 | /); |
28 | |
29 | Devel::Size->import( qw(size total_size) ); |
30 | |
5a83b7cf |
31 | die ("Uhoh, test uses an outdated version of Devel::Size") |
b7621729 |
32 | unless is ($Devel::Size::VERSION, '0.71', 'VERSION MATCHES'); |
b1e5ad85 |
33 | |
34 | ############################################################################# |
c8db37d3 |
35 | # verify that pointer sizes in array slots are sensible: |
36 | # create an array with 4 slots, 2 of them used |
37 | my $array = [ 1,2,3,4 ]; pop @$array; pop @$array; |
38 | |
39 | # the total size minus the array itself minus two scalars is 4 slots |
40 | my $ptr_size = total_size($array) - total_size( [] ) - total_size(1) * 2; |
41 | |
42 | is ($ptr_size % 4, 0, '4 pointers are dividable by 4'); |
43 | isnt ($ptr_size, 0, '4 pointers are not zero'); |
44 | |
45 | # size of one slot ptr |
46 | $ptr_size /= 4; |
47 | |
48 | ############################################################################# |
49 | # assert hash and hash key size |
b1e5ad85 |
50 | |
b1e5ad85 |
51 | my $hash = {}; |
c8db37d3 |
52 | $hash->{a} = 1; |
b7621729 |
53 | is (total_size($hash), |
54 | total_size( { a => undef } ) + total_size(1) - total_size(undef), |
55 | 'assert hash and hash key size'); |
b1e5ad85 |
56 | |
c8db37d3 |
57 | ############################################################################# |
58 | # #24846 (Does not correctly recurse into references in a PVNV-type scalar) |
b1e5ad85 |
59 | |
c8db37d3 |
60 | # run the following tests with different sizes |
61 | |
62 | for my $size (2, 3, 7, 100) |
63 | { |
64 | my $hash = { a => 1 }; |
65 | |
66 | # hash + key minus the value |
67 | my $hash_size = total_size($hash) - total_size(1); |
68 | |
69 | $hash->{a} = 0/1; |
70 | $hash->{a} = []; |
71 | |
72 | my $pvnv_size = total_size(\$hash->{a}) - total_size([]); |
73 | # size of one ref |
74 | my $ref_size = total_size(\\1) - total_size(1); |
75 | |
76 | # $hash->{a} is now a PVNV, e.g. a scalar NV and a ref to an array: |
77 | # SV = PVNV(0x81ff9a8) at 0x8170d48 |
78 | # REFCNT = 1 |
79 | # FLAGS = (ROK) |
80 | # IV = 0 |
81 | # NV = 0 |
82 | # RV = 0x81717bc |
83 | # SV = PVAV(0x8175d6c) at 0x81717bc |
84 | # REFCNT = 1 |
85 | # FLAGS = () |
86 | # IV = 0 |
87 | # NV = 0 |
88 | # ARRAY = 0x0 |
89 | # FILL = -1 |
90 | # MAX = -1 |
91 | # ARYLEN = 0x0 |
92 | # FLAGS = (REAL) |
93 | # PV = 0x81717bc "" |
94 | # CUR = 0 |
95 | # LEN = 0 |
96 | |
97 | # Compare this to a plain array ref |
98 | #SV = RV(0x81a2834) at 0x8207a2c |
99 | # REFCNT = 1 |
100 | # FLAGS = (TEMP,ROK) |
101 | # RV = 0x8170b44 |
102 | # SV = PVAV(0x8175d98) at 0x8170b44 |
103 | # REFCNT = 2 |
104 | # FLAGS = () |
105 | # IV = 0 |
106 | # NV = 0 |
107 | # ARRAY = 0x0 |
108 | # FILL = -1 |
109 | # MAX = -1 |
110 | # ARYLEN = 0x0 |
111 | |
112 | # Get the size of the PVNV and the contained array |
113 | my $element_size = total_size(\$hash->{a}); |
114 | |
115 | ok ($element_size < total_size($hash), "element < hash with one element"); |
116 | ok ($element_size > total_size(\[]), "PVNV + [] > [] alone"); |
117 | |
118 | # Dereferencing the PVNV (the argument to total_size) leaves us with |
119 | # just the array, and this should be equal to a dereferenced array: |
120 | is (total_size($hash->{a}), total_size([]), '[] vs. []'); |
121 | |
122 | # the hash with one key |
123 | # the PVNV in the hash |
124 | # the RV inside the PVNV |
125 | # the contents of the array (array size) |
126 | |
127 | my $full_hash = total_size($hash); |
128 | my $array_size = total_size([]); |
129 | is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV'); |
130 | is ($full_hash, $array_size + $pvnv_size + $hash_size, 'properly recurses into PVNV'); |
131 | |
132 | $hash->{a} = [0..$size]; |
133 | |
134 | # the outer references stripped away, so they should be the same |
135 | is (total_size([0..$size]), total_size( $hash->{a} ), "hash element vs. array"); |
136 | |
137 | # the outer references included, one is just a normal ref, while the other |
138 | # is a PVNV, so they shouldn't be the same: |
139 | isnt (total_size(\[0..$size]), total_size( \$hash->{a} ), "[0..size] vs PVNV"); |
140 | # and the plain ref should be smaller |
141 | ok (total_size(\[0..$size]) < total_size( \$hash->{a} ), "[0..size] vs. PVNV"); |
142 | |
143 | $full_hash = total_size($hash); |
144 | $element_size = total_size(\$hash->{a}); |
145 | $array_size = total_size(\[0..$size]); |
146 | |
147 | print "# full_hash = $full_hash\n"; |
148 | print "# hash_size = $hash_size\n"; |
149 | print "# array size: $array_size\n"; |
150 | print "# element size: $element_size\n"; |
151 | print "# ref_size = $ref_size\n"; |
152 | print "# pvnv_size: $pvnv_size\n"; |
153 | |
154 | # the total size is: |
155 | |
156 | # the hash with one key |
157 | # the PVNV in the hash |
158 | # the RV inside the PVNV |
159 | # the contents of the array (array size) |
160 | |
161 | is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV'); |
162 | # is ($full_hash, $array_size + $pvnv_size + $hash_size, 'properly recurses into PVNV'); |
163 | |
164 | ############################################################################# |
165 | # repeat the former test, but mix in some undef elements |
b1e5ad85 |
166 | |
c8db37d3 |
167 | $array_size = total_size(\[0..$size, undef, undef]); |
b1e5ad85 |
168 | |
c8db37d3 |
169 | $hash->{a} = [0..$size, undef, undef]; |
170 | $element_size = total_size(\$hash->{a}); |
171 | $full_hash = total_size($hash); |
b1e5ad85 |
172 | |
c8db37d3 |
173 | print "# full_hash = $full_hash\n"; |
174 | print "# hash_size = $hash_size\n"; |
175 | print "# array size: $array_size\n"; |
176 | print "# element size: $element_size\n"; |
177 | print "# ref_size = $ref_size\n"; |
178 | print "# pvnv_size: $pvnv_size\n"; |
179 | |
180 | is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV'); |
181 | |
182 | ############################################################################# |
183 | # repeat the former test, but use a pre-extended array |
b1e5ad85 |
184 | |
c8db37d3 |
185 | $array = [ 0..$size, undef, undef ]; pop @$array; |
b1e5ad85 |
186 | |
c8db37d3 |
187 | $array_size = total_size($array); |
188 | my $scalar_size = total_size(1) * (1+$size) + total_size(undef) * 1 + $ptr_size |
189 | + $ptr_size * ($size + 2) + total_size([]); |
190 | is ($scalar_size, $array_size, "computed right size if full array"); |
b1e5ad85 |
191 | |
c8db37d3 |
192 | $hash->{a} = [0..$size, undef, undef]; pop @{$hash->{a}}; |
193 | $full_hash = total_size($hash); |
194 | $element_size = total_size(\$hash->{a}); |
195 | $array_size = total_size(\$array); |
b1e5ad85 |
196 | |
c8db37d3 |
197 | print "# full_hash = $full_hash\n"; |
198 | print "# hash_size = $hash_size\n"; |
199 | print "# array size: $array_size\n"; |
200 | print "# element size: $element_size\n"; |
201 | print "# ref_size = $ref_size\n"; |
202 | print "# pvnv_size: $pvnv_size\n"; |
b1e5ad85 |
203 | |
c8db37d3 |
204 | is ($full_hash, $element_size + $hash_size, 'properly handles undef/non-undef inside arrays'); |
b1e5ad85 |
205 | |
c8db37d3 |
206 | } # end for different sizes |