Commit | Line | Data |
0430b7f7 |
1 | #!/usr/bin/perl -w |
e98cedbf |
2 | |
0430b7f7 |
3 | use Test::More; |
4 | use strict; |
9fc9ab86 |
5 | |
0430b7f7 |
6 | BEGIN |
7 | { |
8 | chdir 't' if -d 't'; |
87372f42 |
9 | plan tests => 15; |
e98cedbf |
10 | |
0430b7f7 |
11 | use lib '../lib'; |
12 | use lib '../blib/arch'; |
13 | use_ok('Devel::Size'); |
14 | } |
e98cedbf |
15 | |
0430b7f7 |
16 | can_ok ('Devel::Size', qw/ |
17 | size |
18 | total_size |
19 | /); |
e98cedbf |
20 | |
0430b7f7 |
21 | Devel::Size->import( qw(size total_size) ); |
22 | |
5a83b7cf |
23 | die ("Uhoh, test uses an outdated version of Devel::Size") |
0d46c0bd |
24 | unless is ($Devel::Size::VERSION, '0.72_50', 'VERSION MATCHES'); |
0430b7f7 |
25 | |
26 | ############################################################################# |
27 | # some basic checks: |
e98cedbf |
28 | |
5073b933 |
29 | use vars qw($foo @foo %foo); |
30 | $foo = "12"; |
31 | @foo = (1,2,3); |
32 | %foo = (a => 1, b => 2); |
b98fcdb9 |
33 | |
34 | my $x = "A string"; |
9fc9ab86 |
35 | my $y = "A much much longer string"; # need to be at least 7 bytes longer for 64 bit |
1c566e6a |
36 | cmp_ok(size($x), '<', size($y), 'size() of strings'); |
37 | cmp_ok(total_size($x), '<', total_size($y), 'total_size() of strings'); |
b98fcdb9 |
38 | |
39 | my @x = (1..4); |
0430b7f7 |
40 | my @y = (1..200); |
41 | |
42 | my $size_1 = total_size(\@x); |
43 | my $size_2 = total_size(\@y); |
44 | |
1c566e6a |
45 | cmp_ok($size_1, '<', $size_2, 'size() of array refs'); |
0430b7f7 |
46 | |
50f7a785 |
47 | # the arrays alone shouldn't be the same size |
0430b7f7 |
48 | $size_1 = size(\@x); |
49 | $size_2 = size(\@y); |
50 | |
50f7a785 |
51 | isnt ( $size_1, $size_2, 'size() of array refs'); |
0430b7f7 |
52 | |
53 | ############################################################################# |
54 | # IV vs IV+PV (bug #17586) |
b98fcdb9 |
55 | |
0430b7f7 |
56 | $x = 12; |
57 | $y = 12; $y .= ''; |
b98fcdb9 |
58 | |
0430b7f7 |
59 | $size_1 = size($x); |
60 | $size_2 = size($y); |
b98fcdb9 |
61 | |
1c566e6a |
62 | cmp_ok($size_1, '<', $size_2, ' ."" makes string longer'); |
0430b7f7 |
63 | |
64 | ############################################################################# |
78dfb4e7 |
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 | |
0430b7f7 |
71 | isnt ( total_size(\@ary2) - total_size(\@ary1), 0, |
9fc9ab86 |
72 | 'total_size(\@ary1) < total_size(\@ary2)'); |
78dfb4e7 |
73 | |
0430b7f7 |
74 | ############################################################################# |
78dfb4e7 |
75 | # check that circular references don't mess things up |
76 | |
77 | my($c1,$c2); $c2 = \$c1; $c1 = \$c2; |
78 | |
0430b7f7 |
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'); |
9fc9ab86 |
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' ); |
87372f42 |
96 | |
97 | # CPAN RT #58484 and #58485 |
98 | isnt (total_size(\&total_size), 0, 'total_size(\&total_size) > 0'); |
99 | |
66f50dda |
100 | use constant LARGE => 'N' x 8192; |
87372f42 |
101 | |
66f50dda |
102 | cmp_ok (total_size(\&LARGE), '>', 8192, |
103 | 'total_size for a constant includes the constant'); |