This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
create SV leak test infrastructure
[perl5.git] / t / op / svleak.t
CommitLineData
d97c33b5
DM
1#!./perl
2
3# A place to put some simple leak tests. Uses XS::APItest to make
4# PL_sv_count available, allowing us to run a bit a code multiple times and
5# see if the count increases.
6
7BEGIN {
8 chdir 't';
9 @INC = '../lib';
10 require './test.pl';
11
12 eval { require XS::APItest; XS::APItest->import('sv_count'); 1 }
13 or skip_all("XS::APItest not available");
14}
15
16plan tests => 3;
17
18# run some code N times. If the number of SVs at the end of loop N is
19# greater than (N-1)*delta at the end of loop 1, we've got a leak
20#
21sub leak {
22 my ($n, $delta, $code, @rest) = @_;
23 my $sv0 = 0;
24 my $sv1 = 0;
25 for my $i (1..$n) {
26 &$code();
27 $sv1 = sv_count();
28 $sv0 = $sv1 if $i == 1;
29 }
30 cmp_ok($sv1-$sv0, '<=', ($n-1)*$delta, @rest);
31}
32
33my @a;
34
35leak(5, 0, sub {}, "basic check 1 of leak test infrastructure");
36leak(5, 0, sub {push @a,1;pop @a}, "basic check 2 of leak test infrastructure");
37leak(5, 1, sub {push @a,1;}, "basic check 3 of leak test infrastructure");