This is 0.74_50 - update META.yml
[p5sagit/Devel-Size.git] / t / basic.t
CommitLineData
0430b7f7 1#!/usr/bin/perl -w
e98cedbf 2
2640cff1 3use Test::More tests => 15;
0430b7f7 4use strict;
6c3d85e7 5use Devel::Size qw(size total_size);
2640cff1 6use Scalar::Util qw(weaken);
e98cedbf 7
0430b7f7 8can_ok ('Devel::Size', qw/
9 size
10 total_size
11 /);
e98cedbf 12
5a83b7cf 13die ("Uhoh, test uses an outdated version of Devel::Size")
0649e45e 14 unless is ($Devel::Size::VERSION, '0.74_50', 'VERSION MATCHES');
0430b7f7 15
16#############################################################################
17# some basic checks:
e98cedbf 18
5073b933 19use vars qw($foo @foo %foo);
20$foo = "12";
21@foo = (1,2,3);
22%foo = (a => 1, b => 2);
b98fcdb9 23
24my $x = "A string";
9fc9ab86 25my $y = "A much much longer string"; # need to be at least 7 bytes longer for 64 bit
1c566e6a 26cmp_ok(size($x), '<', size($y), 'size() of strings');
27cmp_ok(total_size($x), '<', total_size($y), 'total_size() of strings');
b98fcdb9 28
29my @x = (1..4);
0430b7f7 30my @y = (1..200);
31
32my $size_1 = total_size(\@x);
33my $size_2 = total_size(\@y);
34
1c566e6a 35cmp_ok($size_1, '<', $size_2, 'size() of array refs');
0430b7f7 36
50f7a785 37# the arrays alone shouldn't be the same size
0430b7f7 38$size_1 = size(\@x);
39$size_2 = size(\@y);
40
50f7a785 41isnt ( $size_1, $size_2, 'size() of array refs');
0430b7f7 42
43#############################################################################
44# IV vs IV+PV (bug #17586)
b98fcdb9 45
0430b7f7 46$x = 12;
47$y = 12; $y .= '';
b98fcdb9 48
0430b7f7 49$size_1 = size($x);
50$size_2 = size($y);
b98fcdb9 51
1c566e6a 52cmp_ok($size_1, '<', $size_2, ' ."" makes string longer');
0430b7f7 53
54#############################################################################
78dfb4e7 55# check that the tracking_hash is working
56
57my($a,$b) = (1,2);
58my @ary1 = (\$a, \$a);
59my @ary2 = (\$a, \$b);
60
cf1d079f 61cmp_ok(total_size(\@ary1), '<', total_size(\@ary2),
62 'the tracking hash is working');
78dfb4e7 63
0430b7f7 64#############################################################################
78dfb4e7 65# check that circular references don't mess things up
66
67my($c1,$c2); $c2 = \$c1; $c1 = \$c2;
68
0430b7f7 69is (total_size($c1), total_size($c2), 'circular references');
70
71#############################################################################
72# GLOBS
73
cf1d079f 74cmp_ok(total_size(*foo), '>', 0, 'total_size(*foo) > 0');
0430b7f7 75
76#############################################################################
77# CODE ref
78
79my $code = sub { '1' };
80
cf1d079f 81cmp_ok(total_size($code), '>', 0, 'total_size($code) > 0');
9fc9ab86 82
83##########################################################
84# RT#14849 (& RT#26781 and possibly RT#29238?)
cf1d079f 85cmp_ok( total_size( sub{ do{ my $t=0 }; } ), '>', 0,
86 'total_size( sub{ my $t=0 } ) > 0' );
87372f42 87
88# CPAN RT #58484 and #58485
cf1d079f 89cmp_ok(total_size(\&total_size), '>', 0, 'total_size(\&total_size) > 0');
87372f42 90
66f50dda 91use constant LARGE => 'N' x 8192;
87372f42 92
66f50dda 93cmp_ok (total_size(\&LARGE), '>', 8192,
94 'total_size for a constant includes the constant');
2640cff1 95
96{
97 my $a = [];
98 my $b = \$a;
8c394e12 99 # making a weakref upgrades the target to PVMG and adds magic
100 is(total_size($a), total_size([]),
101 'Any intial reference is dereferenced and discarded');
2640cff1 102}