Bump $VERSION to 0.76_50
[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
7 can_ok ('Devel::Size', qw/
8   size
9   total_size
10   /);
11
12 die ("Uhoh, test uses an outdated version of Devel::Size")
13     unless is ($Devel::Size::VERSION, '0.76_50', 'VERSION MATCHES');
14
15 #############################################################################
16 # some basic checks:
17
18 use vars qw($foo @foo %foo);
19 $foo = "12";
20 @foo = (1,2,3);
21 %foo = (a => 1, b => 2);
22
23 my $x = "A string";
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');
27
28 my @x = (1..4);
29 my @y = (1..200);
30
31 my $size_1 = total_size(\@x);
32 my $size_2 = total_size(\@y);
33
34 cmp_ok($size_1, '<', $size_2, 'size() of array refs');
35
36 # the arrays alone shouldn't be the same size
37 $size_1 = size(\@x);
38 $size_2 = size(\@y);
39
40 isnt ( $size_1, $size_2, 'size() of array refs');
41
42 #############################################################################
43 # IV vs IV+PV (bug #17586)
44
45 $x = 12;
46 $y = 12; $y .= '';
47
48 $size_1 = size($x);
49 $size_2 = size($y);
50
51 cmp_ok($size_1, '<', $size_2, ' ."" makes string longer');
52
53 #############################################################################
54 # check that the tracking_hash is working
55
56 my($a,$b) = (1,2);
57 my @ary1 = (\$a, \$a);
58 my @ary2 = (\$a, \$b);
59
60 cmp_ok(total_size(\@ary1), '<', total_size(\@ary2),
61        'the tracking hash is working');
62
63 #############################################################################
64 # check that circular references don't mess things up
65
66 my($c1,$c2); $c2 = \$c1; $c1 = \$c2;
67
68 is (total_size($c1), total_size($c2), 'circular references');
69
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' );
74
75 # CPAN RT #58484 and #58485
76 cmp_ok(total_size(\&total_size), '>', 0, 'total_size(\&total_size) > 0');
77
78 use constant LARGE => 'N' x 8192;
79
80 cmp_ok (total_size(\&LARGE), '>', 8192,
81         'total_size for a constant includes the constant');
82
83 {
84     my $a = [];
85     my $b = \$a;
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.
89     unless (eval {
90         require Scalar::Util;
91         # making a weakref upgrades the target to PVMG and adds magic
92         Scalar::Util::weaken($b);
93         1;
94     }) {
95         die $@ if $] >= 5.008;
96     }
97
98     is(total_size($a), total_size([]),
99        'Any intial reference is dereferenced and discarded');
100 }
101
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)],
106        ) {
107     my ($name, $size) = @$_;
108     is($size, 0,
109        "PL_sv_$name is interpeter wide, so not counted as part of the structure's size");
110 }
111
112 {
113     # SvOOK stuff
114     my $uurk = "Perl Rules";
115     # This may upgrade the scalar:
116     $uurk =~ s/Perl//;
117     $uurk =~ s/^/Perl/;
118     my $before_size = total_size($uurk);
119     my $before_length = length $uurk;
120     cmp_ok($before_size, '>', $before_length, 'Size before is sane');
121     $uurk =~ s/Perl //;
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');
125 }