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