Eliminate needless test boilerplate, and duplicated effort.
[p5sagit/Devel-Size.git] / t / recurse.t
CommitLineData
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
6c3d85e7 9use Test::More tests => 3 + 4 *12;
b1e5ad85 10use strict;
6c3d85e7 11use Devel::Size ':all';
9fc9ab86 12
b1e5ad85 13
14#############################################################################
c8db37d3 15# verify that pointer sizes in array slots are sensible:
16# create an array with 4 slots, 2 of them used
17my $array = [ 1,2,3,4 ]; pop @$array; pop @$array;
18
19# the total size minus the array itself minus two scalars is 4 slots
20my $ptr_size = total_size($array) - total_size( [] ) - total_size(1) * 2;
21
22is ($ptr_size % 4, 0, '4 pointers are dividable by 4');
23isnt ($ptr_size, 0, '4 pointers are not zero');
24
25# size of one slot ptr
26$ptr_size /= 4;
27
28#############################################################################
29# assert hash and hash key size
b1e5ad85 30
b1e5ad85 31my $hash = {};
c8db37d3 32$hash->{a} = 1;
9fc9ab86 33is (total_size($hash),
34 total_size( { a => undef } ) + total_size(1) - total_size(undef),
35 'assert hash and hash key size');
b1e5ad85 36
c8db37d3 37#############################################################################
38# #24846 (Does not correctly recurse into references in a PVNV-type scalar)
b1e5ad85 39
c8db37d3 40# run the following tests with different sizes
41
42for my $size (2, 3, 7, 100)
43 {
44 my $hash = { a => 1 };
45
46 # hash + key minus the value
47 my $hash_size = total_size($hash) - total_size(1);
48
49 $hash->{a} = 0/1;
50 $hash->{a} = [];
51
52 my $pvnv_size = total_size(\$hash->{a}) - total_size([]);
53 # size of one ref
54 my $ref_size = total_size(\\1) - total_size(1);
55
56 # $hash->{a} is now a PVNV, e.g. a scalar NV and a ref to an array:
57# SV = PVNV(0x81ff9a8) at 0x8170d48
58# REFCNT = 1
59# FLAGS = (ROK)
60# IV = 0
61# NV = 0
62# RV = 0x81717bc
63# SV = PVAV(0x8175d6c) at 0x81717bc
64# REFCNT = 1
65# FLAGS = ()
66# IV = 0
67# NV = 0
68# ARRAY = 0x0
69# FILL = -1
70# MAX = -1
71# ARYLEN = 0x0
72# FLAGS = (REAL)
73# PV = 0x81717bc ""
74# CUR = 0
75# LEN = 0
76
77 # Compare this to a plain array ref
78#SV = RV(0x81a2834) at 0x8207a2c
79# REFCNT = 1
80# FLAGS = (TEMP,ROK)
81# RV = 0x8170b44
82# SV = PVAV(0x8175d98) at 0x8170b44
83# REFCNT = 2
84# FLAGS = ()
85# IV = 0
86# NV = 0
87# ARRAY = 0x0
88# FILL = -1
89# MAX = -1
90# ARYLEN = 0x0
91
92 # Get the size of the PVNV and the contained array
93 my $element_size = total_size(\$hash->{a});
94
1c566e6a 95 cmp_ok($element_size, '<', total_size($hash), "element < hash with one element");
96 cmp_ok($element_size, '>', total_size(\[]), "PVNV + [] > [] alone");
c8db37d3 97
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. []');
101
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)
106
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');
111
112 $hash->{a} = [0..$size];
113
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");
116
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
1c566e6a 121 cmp_ok(total_size(\[0..$size]), '<', total_size( \$hash->{a} ), "[0..size] vs. PVNV");
c8db37d3 122
123 $full_hash = total_size($hash);
124 $element_size = total_size(\$hash->{a});
125 $array_size = total_size(\[0..$size]);
126
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";
133
134 # the total size is:
135
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)
140
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');
143
144#############################################################################
145# repeat the former test, but mix in some undef elements
b1e5ad85 146
c8db37d3 147 $array_size = total_size(\[0..$size, undef, undef]);
b1e5ad85 148
c8db37d3 149 $hash->{a} = [0..$size, undef, undef];
150 $element_size = total_size(\$hash->{a});
151 $full_hash = total_size($hash);
b1e5ad85 152
c8db37d3 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";
159
160 is ($full_hash, $element_size + $hash_size, 'properly recurses into PVNV');
161
162#############################################################################
163# repeat the former test, but use a pre-extended array
b1e5ad85 164
c8db37d3 165 $array = [ 0..$size, undef, undef ]; pop @$array;
b1e5ad85 166
c8db37d3 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");
b1e5ad85 171
c8db37d3 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);
b1e5ad85 176
c8db37d3 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";
b1e5ad85 183
c8db37d3 184 is ($full_hash, $element_size + $hash_size, 'properly handles undef/non-undef inside arrays');
b1e5ad85 185
c8db37d3 186 } # end for different sizes