This is 0.79_52 - update META.yml, and META.json
[p5sagit/Devel-Size.git] / t / code.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Test::More tests => 14;
5 use Devel::Size ':all';
6
7 sub zwapp;
8 sub swoosh($$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$);
9 sub crunch {
10 }
11
12 my $whack_size = total_size(\&whack);
13 my $zwapp_size = total_size(\&zwapp);
14 my $swoosh_size = total_size(\&swoosh);
15 my $crunch_size = total_size(\&crunch);
16
17 cmp_ok($whack_size, '>', 0, 'CV generated at runtime has a size');
18 if("$]" >= 5.017) {
19     cmp_ok($zwapp_size, '==', $whack_size,
20            'CV stubbed at compiletime is the same size');
21 } else {
22     cmp_ok($zwapp_size, '>', $whack_size,
23            'CV stubbed at compiletime is larger (CvOUTSIDE is set and followed)');
24 }
25 cmp_ok(length prototype \&swoosh, '>', 0, 'prototype has a length');
26 cmp_ok($swoosh_size, '>', $zwapp_size + length prototype \&swoosh,
27        'prototypes add to the size');
28 cmp_ok($crunch_size, '>', $zwapp_size, 'sub bodies add to the size');
29
30 my $anon_proto = sub ($$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$) {};
31 my $anon_size = total_size(sub {});
32 my $anon_proto_size = total_size($anon_proto);
33 cmp_ok($anon_size, '>', 0, 'anonymous subroutines have a size');
34 cmp_ok(length prototype $anon_proto, '>', 0, 'prototype has a length');
35 cmp_ok($anon_proto_size, '>', $anon_size + length prototype $anon_proto,
36        'prototypes add to the size');
37
38 SKIP: {
39     use vars '@b';
40     my $aelemfast_lex = total_size(sub {my @a; $a[0]});
41     my $aelemfast = total_size(sub {my @a; $b[0]});
42
43     # This one is sane even before Dave's lexical aelemfast changes:
44     cmp_ok($aelemfast_lex, '>', $anon_size,
45            'aelemfast for a lexical is handled correctly');
46     skip('alemfast was extended to lexicals after this perl was released', 1)
47       if $] < 5.008004;
48     cmp_ok($aelemfast, '>', $aelemfast_lex,
49            'aelemfast for a package variable is larger');
50 }
51
52 my $short_pvop = total_size(sub {goto GLIT});
53 my $long_pvop = total_size(sub {goto KREEK_KREEK_CLANK_CLANK});
54 cmp_ok($short_pvop, '>', $anon_size, 'OPc_PVOP can be measured');
55 is($long_pvop, $short_pvop + 19, 'the only size difference is the label length');
56
57 sub bloop {
58     my $clunk = shift;
59     if (--$clunk > 0) {
60         bloop($clunk);
61     }
62 }
63
64 my $before_size = total_size(\&bloop);
65 bloop(42);
66 my $after_size = total_size(\&bloop);
67
68 cmp_ok($after_size, '>', $before_size, 'Recursion increases the PADLIST');
69
70 sub closure_with_eval {
71     my $a;
72     return sub { eval ""; $a };
73 }
74
75 sub closure_without_eval {
76     my $a;
77     return sub { require ""; $a };
78 }
79
80 if ($] > 5.017001) {
81     # Again relying too much on the core's implementation, but while that holds,
82     # this does test that CvOUTSIDE() is being followed.
83     cmp_ok(total_size(closure_with_eval()), '>',
84            total_size(closure_without_eval()) + 256,
85            'CvOUTSIDE is now NULL on cloned closures, unless they have eval');
86 } else {
87     # Seems that they differ by a few bytes on 5.8.x
88     cmp_ok(total_size(closure_with_eval()), '<=',
89            total_size(closure_without_eval()) + 256,
90            "CvOUTSIDE is set on all cloned closures, so these won't differ by much");
91 }