Simplify the setup code in Size.pm
[p5sagit/Devel-Size.git] / t / basic.t
CommitLineData
0430b7f7 1#!/usr/bin/perl -w
e98cedbf 2
0430b7f7 3use Test::More;
4use strict;
9fc9ab86 5
0430b7f7 6BEGIN
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 16can_ok ('Devel::Size', qw/
17 size
18 total_size
19 /);
e98cedbf 20
0430b7f7 21Devel::Size->import( qw(size total_size) );
22
5a83b7cf 23die ("Uhoh, test uses an outdated version of Devel::Size")
8d1f46c5 24 unless is ($Devel::Size::VERSION, '0.72_52', 'VERSION MATCHES');
0430b7f7 25
26#############################################################################
27# some basic checks:
e98cedbf 28
5073b933 29use vars qw($foo @foo %foo);
30$foo = "12";
31@foo = (1,2,3);
32%foo = (a => 1, b => 2);
b98fcdb9 33
34my $x = "A string";
9fc9ab86 35my $y = "A much much longer string"; # need to be at least 7 bytes longer for 64 bit
1c566e6a 36cmp_ok(size($x), '<', size($y), 'size() of strings');
37cmp_ok(total_size($x), '<', total_size($y), 'total_size() of strings');
b98fcdb9 38
39my @x = (1..4);
0430b7f7 40my @y = (1..200);
41
42my $size_1 = total_size(\@x);
43my $size_2 = total_size(\@y);
44
1c566e6a 45cmp_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 51isnt ( $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 62cmp_ok($size_1, '<', $size_2, ' ."" makes string longer');
0430b7f7 63
64#############################################################################
78dfb4e7 65# check that the tracking_hash is working
66
67my($a,$b) = (1,2);
68my @ary1 = (\$a, \$a);
69my @ary2 = (\$a, \$b);
70
0430b7f7 71isnt ( 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
77my($c1,$c2); $c2 = \$c1; $c1 = \$c2;
78
0430b7f7 79is (total_size($c1), total_size($c2), 'circular references');
80
81#############################################################################
82# GLOBS
83
84isnt (total_size(*foo), 0, 'total_size(*foo) > 0');
85
86#############################################################################
87# CODE ref
88
89my $code = sub { '1' };
90
91isnt (total_size($code), 0, 'total_size($code) > 0');
9fc9ab86 92
93##########################################################
94# RT#14849 (& RT#26781 and possibly RT#29238?)
95isnt( total_size( sub{ do{ my $t=0 }; } ), 0, 'total_size( sub{ my $t=0 } ) > 0' );
87372f42 96
97# CPAN RT #58484 and #58485
98isnt (total_size(\&total_size), 0, 'total_size(\&total_size) > 0');
99
66f50dda 100use constant LARGE => 'N' x 8192;
87372f42 101
66f50dda 102cmp_ok (total_size(\&LARGE), '>', 8192,
103 'total_size for a constant includes the constant');