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