3 use Test::More tests => 30;
5 use Devel::Size qw(size total_size);
7 can_ok ('Devel::Size', qw/
12 die ("Uhoh, test uses an outdated version of Devel::Size")
13 unless is ($Devel::Size::VERSION, '0.78_50', 'VERSION MATCHES');
15 #############################################################################
18 use vars qw($foo @foo %foo);
21 %foo = (a => 1, b => 2);
24 my $y = "A much much longer string"; # need to be at least 7 bytes longer for 64 bit
25 cmp_ok(size($x), '<', size($y), 'size() of strings');
26 cmp_ok(total_size($x), '<', total_size($y), 'total_size() of strings');
31 my $size_1 = total_size(\@x);
32 my $size_2 = total_size(\@y);
34 cmp_ok($size_1, '<', $size_2, 'size() of array refs');
36 # the arrays alone shouldn't be the same size
40 isnt ( $size_1, $size_2, 'size() of array refs');
42 #############################################################################
43 # IV vs IV+PV (bug #17586)
51 cmp_ok($size_1, '<', $size_2, ' ."" makes string longer');
53 #############################################################################
54 # check that the tracking_hash is working
57 my @ary1 = (\$a, \$a);
58 my @ary2 = (\$a, \$b);
60 cmp_ok(total_size(\@ary1), '<', total_size(\@ary2),
61 'the tracking hash is working');
63 #############################################################################
64 # check that circular references don't mess things up
66 my($c1,$c2); $c2 = \$c1; $c1 = \$c2;
68 is (total_size($c1), total_size($c2), 'circular references');
70 ##########################################################
71 # RT#14849 (& RT#26781 and possibly RT#29238?)
72 cmp_ok( total_size( sub{ do{ my $t=0 }; } ), '>', 0,
73 'total_size( sub{ my $t=0 } ) > 0' );
75 # CPAN RT #58484 and #58485
76 cmp_ok(total_size(\&total_size), '>', 0, 'total_size(\&total_size) > 0');
78 use constant LARGE => 'N' x 8192;
80 cmp_ok (total_size(\&LARGE), '>', 8192,
81 'total_size for a constant includes the constant');
86 # Scalar::Util isn't in the core before 5.7.something.
87 # The test isn't really testing anything without the weaken(), but it
88 # isn't counter-productive or harmful to run it anyway.
91 # making a weakref upgrades the target to PVMG and adds magic
92 Scalar::Util::weaken($b);
95 die $@ if $] >= 5.008;
98 is(total_size($a), total_size([]),
99 'Any intial reference is dereferenced and discarded');
102 # Must call direct - avoid all copying:
103 foreach(['undef', total_size(undef)],
104 ['no', total_size(1 == 0)],
105 ['yes', total_size(1 == 1)],
107 my ($name, $size) = @$_;
109 "PL_sv_$name is interpeter wide, so not counted as part of the structure's size");
114 my $uurk = "Perl Rules";
115 # This may upgrade the scalar:
118 my $before_size = total_size($uurk);
119 my $before_length = length $uurk;
120 cmp_ok($before_size, '>', $before_length, 'Size before is sane');
122 is(total_size($uurk), $before_size,
123 "Size doesn't change because OOK is used");
124 cmp_ok(length $uurk, '<', $before_size, 'but string is shorter');
127 sub shared_hash_keys {
129 my $one = total_size([keys %h]);
130 cmp_ok($one, '>', 0, 'Size of one entry is sane');
131 my $two = total_size([keys %h, keys %h]);
132 cmp_ok($two, '>', $one, 'Two take more space than one');
133 my $increment = $two - $one;
134 is(total_size([keys %h, keys %h, keys %h]), $one + 2 * $increment,
135 'Linear size increase for three');
140 my $small = shared_hash_keys(Perl => 'Rules');
141 my $big = shared_hash_keys('x' x 1024, '');
143 skip("[keys %h] doesn't copy as shared hash key scalars prior to 5.10.0",
145 is ($small, $big, 'The "shared" part of shared hash keys is spotted');
150 use vars '%DANG_DANG_DANG_DANG_DANG_DANG_DANG_DANG_DANG';
151 my $hash_size = total_size(\%DANG_DANG_DANG_DANG_DANG_DANG_DANG_DANG_DANG);
152 cmp_ok($hash_size, '>', 0, 'Hash size is sane');
154 = total_size(\%DANG_DANG_DANG_DANG_DANG_DANG_DANG_DANG_DANG::);
155 cmp_ok($stash_size, '>',
156 $hash_size + length 'DANG_DANG_DANG_DANG_DANG_DANG_DANG_DANG_DANG',
157 'Stash size is larger than hash size plus length of the name');
161 my %h = (Perl => 'Rules');
162 my $hash_size = total_size(\%h);
163 cmp_ok($hash_size, '>', 0, 'Hash size is sane');
166 is(total_size(\%h), $hash_size,
167 "Creating iteration state doesn't need to allocate storage");
168 # because all hashes carry the overhead of this storage from creation
170 cmp_ok(total_size(\%h), '>', $hash_size,
171 'Creating iteration state allocates storage');