This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
blead is upstream for Math-BigInt
[perl5.git] / cpan / Math-BigInt-FastCalc / t / leak.t
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
8 use Test::More;
9 use strict;
10
11 BEGIN
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
19 use Math::BigInt::FastCalc;
20
21 #############################################################################
22 package Math::BigInt::FastCalc::LeakCheck;
23 use base qw(Math::BigInt::FastCalc);
24
25 my $destroyed = 0;
26 sub DESTROY { $destroyed++; }
27
28 #############################################################################
29 package main;
30
31 for 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
41 my $num = Math::BigInt::FastCalc->_zero();
42 for 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
52 my $num_10 = Math::BigInt::FastCalc->_ten();
53 my $num_2 = Math::BigInt::FastCalc->_two();
54
55 my $num_long   = Math::BigInt::FastCalc->_new("1234567890");
56 my $num_long_2 = Math::BigInt::FastCalc->_new("12345678900987654321");
57
58 is (Math::BigInt::FastCalc->_str($num_long), "1234567890");
59 is (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
74 sub _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