xref: /openbsd-src/gnu/usr.bin/perl/cpan/Math-BigInt-FastCalc/t/leak.t (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1#!/usr/bin/perl -w
2
3# Test for memory leaks.
4
5# XXX TODO: This test file doesn't actually seem to work! If you remove
6# the sv_2mortal() in the XS file, it still happily passes all tests...
7
8use Test::More;
9use strict;
10
11BEGIN
12  {
13  $| = 1;
14  chdir 't' if -d 't' && !$ENV{PERL_CORE};
15  unshift @INC, ('../lib', '../blib/arch');	# for running manually
16  plan tests => 22;
17  }
18
19use Math::BigInt::FastCalc;
20
21#############################################################################
22package Math::BigInt::FastCalc::LeakCheck;
23use base qw(Math::BigInt::FastCalc);
24
25my $destroyed = 0;
26sub DESTROY { $destroyed++; }
27
28#############################################################################
29package main;
30
31for my $method (qw(_zero _one _two _ten))
32  {
33  $destroyed = 0;
34    {
35    my $num = Math::BigInt::FastCalc::LeakCheck->$method();
36    bless $num, "Math::BigInt::FastCalc::LeakCheck";
37    }
38  is ($destroyed, 1, "$method does not leak memory");
39  }
40
41my $num = Math::BigInt::FastCalc->_zero();
42for my $method (qw(_is_zero _is_one _is_two _is_ten _num))
43  {
44  $destroyed = 0;
45    {
46    my $rc = Math::BigInt::FastCalc->$method($num);
47    bless \$rc, "Math::BigInt::FastCalc::LeakCheck";
48    }
49  is ($destroyed, 1, "$method does not leak memory");
50  }
51
52my $num_10 = Math::BigInt::FastCalc->_ten();
53my $num_2 = Math::BigInt::FastCalc->_two();
54
55my $num_long   = Math::BigInt::FastCalc->_new("1234567890");
56my $num_long_2 = Math::BigInt::FastCalc->_new("12345678900987654321");
57
58is (Math::BigInt::FastCalc->_str($num_long), "1234567890");
59is (Math::BigInt::FastCalc->_str($num_long_2), "12345678900987654321");
60
61# to hit all possible code branches
62_test_acmp($num, $num);
63_test_acmp($num_10, $num_10);
64_test_acmp($num, $num_10);
65_test_acmp($num_10, $num);
66_test_acmp($num, $num_2);
67_test_acmp($num_2, $num);
68_test_acmp($num_long, $num);
69_test_acmp($num, $num_long);
70_test_acmp($num_long, $num_long);
71_test_acmp($num_long, $num_long_2);
72_test_acmp($num_long_2, $num_long);
73
74sub _test_acmp
75  {
76  my ($n1,$n2) = @_;
77
78  $destroyed = 0;
79    {
80    my $rc = Math::BigInt::FastCalc->_acmp($n1,$n2);
81    bless \$rc, "Math::BigInt::FastCalc::LeakCheck";
82    }
83  my $n_1 = Math::BigInt::FastCalc->_str($n1);
84  my $n_2 = Math::BigInt::FastCalc->_str($n2);
85  is ($destroyed, 1, "_acmp($n_1,$n_2) does not leak memory");
86  }
87
88