4dfed682de498c437d769079b50d6d69f37c49c5
[p5sagit/Devel-Size.git] / t / basic.t
1 #!/usr/bin/perl -w
2
3 use Test::More tests => 14;
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.72_52', '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 # GLOBS
72
73 cmp_ok(total_size(*foo), '>', 0, 'total_size(*foo) > 0');
74
75 #############################################################################
76 # CODE ref
77
78 my $code = sub { '1' };
79
80 cmp_ok(total_size($code), '>', 0, 'total_size($code) > 0');
81
82 ##########################################################
83 # RT#14849 (& RT#26781 and possibly RT#29238?)
84 cmp_ok( total_size( sub{ do{ my $t=0 }; } ), '>', 0,
85         'total_size( sub{ my $t=0 } ) > 0' );
86
87 # CPAN RT #58484 and #58485
88 cmp_ok(total_size(\&total_size), '>', 0, 'total_size(\&total_size) > 0');
89
90 use constant LARGE => 'N' x 8192;
91
92 cmp_ok (total_size(\&LARGE), '>', 8192,
93         'total_size for a constant includes the constant');