Reinstate weaken(), inadvertently removed by 8c394e1251fdfe38.
[p5sagit/Devel-Size.git] / t / basic.t
1 #!/usr/bin/perl -w
2
3 use Test::More tests => 19;
4 use strict;
5 use Devel::Size qw(size total_size);
6 use Scalar::Util qw(weaken);
7
8 can_ok ('Devel::Size', qw/
9   size
10   total_size
11   /);
12
13 die ("Uhoh, test uses an outdated version of Devel::Size")
14     unless is ($Devel::Size::VERSION, '0.75_52', 'VERSION MATCHES');
15
16 #############################################################################
17 # some basic checks:
18
19 use vars qw($foo @foo %foo);
20 $foo = "12";
21 @foo = (1,2,3);
22 %foo = (a => 1, b => 2);
23
24 my $x = "A string";
25 my $y = "A much much longer string";        # need to be at least 7 bytes longer for 64 bit
26 cmp_ok(size($x), '<', size($y), 'size() of strings');
27 cmp_ok(total_size($x), '<', total_size($y), 'total_size() of strings');
28
29 my @x = (1..4);
30 my @y = (1..200);
31
32 my $size_1 = total_size(\@x);
33 my $size_2 = total_size(\@y);
34
35 cmp_ok($size_1, '<', $size_2, 'size() of array refs');
36
37 # the arrays alone shouldn't be the same size
38 $size_1 = size(\@x);
39 $size_2 = size(\@y);
40
41 isnt ( $size_1, $size_2, 'size() of array refs');
42
43 #############################################################################
44 # IV vs IV+PV (bug #17586)
45
46 $x = 12;
47 $y = 12; $y .= '';
48
49 $size_1 = size($x);
50 $size_2 = size($y);
51
52 cmp_ok($size_1, '<', $size_2, ' ."" makes string longer');
53
54 #############################################################################
55 # check that the tracking_hash is working
56
57 my($a,$b) = (1,2);
58 my @ary1 = (\$a, \$a);
59 my @ary2 = (\$a, \$b);
60
61 cmp_ok(total_size(\@ary1), '<', total_size(\@ary2),
62        'the tracking hash is working');
63
64 #############################################################################
65 # check that circular references don't mess things up
66
67 my($c1,$c2); $c2 = \$c1; $c1 = \$c2;
68
69 is (total_size($c1), total_size($c2), 'circular references');
70
71 ##########################################################
72 # RT#14849 (& RT#26781 and possibly RT#29238?)
73 cmp_ok( total_size( sub{ do{ my $t=0 }; } ), '>', 0,
74         'total_size( sub{ my $t=0 } ) > 0' );
75
76 # CPAN RT #58484 and #58485
77 cmp_ok(total_size(\&total_size), '>', 0, 'total_size(\&total_size) > 0');
78
79 use constant LARGE => 'N' x 8192;
80
81 cmp_ok (total_size(\&LARGE), '>', 8192,
82         'total_size for a constant includes the constant');
83
84 {
85     my $a = [];
86     my $b = \$a;
87     # making a weakref upgrades the target to PVMG and adds magic
88     weaken $b;
89     is(total_size($a), total_size([]),
90        'Any intial reference is dereferenced and discarded');
91 }
92
93 # Must call direct - avoid all copying:
94 foreach(['undef', total_size(undef)],
95         ['no', total_size(1 == 0)],
96         ['yes', total_size(1 == 1)],
97        ) {
98     my ($name, $size) = @$_;
99     is($size, 0,
100        "PL_sv_$name is interpeter wide, so not counted as part of the structure's size");
101 }
102
103 {
104     # SvOOK stuff
105     my $uurk = "Perl Rules";
106     # This may upgrade the scalar:
107     $uurk =~ s/Perl//;
108     $uurk =~ s/^/Perl/;
109     my $before_size = total_size($uurk);
110     my $before_length = length $uurk;
111     cmp_ok($before_size, '>', $before_length, 'Size before is sane');
112     $uurk =~ s/Perl //;
113     is(total_size($uurk), $before_size,
114        "Size doesn't change because OOK is used");
115     cmp_ok(length $uurk, '<', $before_size, 'but string is shorter');
116 }