Use cmp_ok() in place of ok() with a < comparison, for better diagnostics.
[p5sagit/Devel-Size.git] / t / basic.t
1 #!/usr/bin/perl -w
2
3 use Test::More;
4 use strict;
5
6 BEGIN
7    {
8    chdir 't' if -d 't';
9    plan tests => 13;
10
11    use lib '../lib';
12    use lib '../blib/arch';
13    use_ok('Devel::Size');
14    }
15
16 can_ok ('Devel::Size', qw/
17   size
18   total_size
19   /);
20
21 Devel::Size->import( qw(size total_size) );
22
23 die ("Uhoh, test uses an outdated version of Devel::Size")
24   unless is ($Devel::Size::VERSION, '0.72_50', 'VERSION MATCHES');
25
26 #############################################################################
27 # some basic checks:
28
29 use vars qw($foo @foo %foo);
30 $foo = "12";
31 @foo = (1,2,3);
32 %foo = (a => 1, b => 2);
33
34 my $x = "A string";
35 my $y = "A much much longer string";        # need to be at least 7 bytes longer for 64 bit
36 cmp_ok(size($x), '<', size($y), 'size() of strings');
37 cmp_ok(total_size($x), '<', total_size($y), 'total_size() of strings');
38
39 my @x = (1..4);
40 my @y = (1..200);
41
42 my $size_1 = total_size(\@x);
43 my $size_2 = total_size(\@y);
44
45 cmp_ok($size_1, '<', $size_2, 'size() of array refs');
46
47 # the arrays alone shouldn't be the same size
48 $size_1 = size(\@x);
49 $size_2 = size(\@y);
50
51 isnt ( $size_1, $size_2, 'size() of array refs');
52
53 #############################################################################
54 # IV vs IV+PV (bug #17586)
55
56 $x = 12;
57 $y = 12; $y .= '';
58
59 $size_1 = size($x);
60 $size_2 = size($y);
61
62 cmp_ok($size_1, '<', $size_2, ' ."" makes string longer');
63
64 #############################################################################
65 # check that the tracking_hash is working
66
67 my($a,$b) = (1,2);
68 my @ary1 = (\$a, \$a);
69 my @ary2 = (\$a, \$b);
70
71 isnt ( total_size(\@ary2) - total_size(\@ary1), 0,
72     'total_size(\@ary1) < total_size(\@ary2)');
73
74 #############################################################################
75 # check that circular references don't mess things up
76
77 my($c1,$c2); $c2 = \$c1; $c1 = \$c2;
78
79 is (total_size($c1), total_size($c2), 'circular references');
80
81 #############################################################################
82 # GLOBS
83
84 isnt (total_size(*foo), 0, 'total_size(*foo) > 0');
85
86 #############################################################################
87 # CODE ref
88
89 my $code = sub { '1' };
90
91 isnt (total_size($code), 0, 'total_size($code) > 0');
92
93 ##########################################################
94 # RT#14849 (& RT#26781 and possibly RT#29238?)
95 isnt( total_size( sub{ do{ my $t=0 }; } ), 0, 'total_size( sub{ my $t=0 } ) > 0' );