1# -*- mode: perl; -*-
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 strict;
9use Test::More tests => 22;
10
11use Math::BigInt::FastCalc;
12
13#############################################################################
14package Math::BigInt::FastCalc::LeakCheck;
15
16use Math::BigInt::FastCalc;
17our @ISA = qw< Math::BigInt::FastCalc >;
18
19my $destroyed = 0;
20sub DESTROY { $destroyed++; }
21
22#############################################################################
23package main;
24
25for my $method (qw(_zero _one _two _ten))
26  {
27  $destroyed = 0;
28    {
29    my $num = Math::BigInt::FastCalc::LeakCheck->$method();
30    bless $num, "Math::BigInt::FastCalc::LeakCheck";
31    }
32  is ($destroyed, 1, "$method does not leak memory");
33  }
34
35my $num = Math::BigInt::FastCalc->_zero();
36for my $method (qw(_is_zero _is_one _is_two _is_ten _num))
37  {
38  $destroyed = 0;
39    {
40    my $rc = Math::BigInt::FastCalc->$method($num);
41    bless \$rc, "Math::BigInt::FastCalc::LeakCheck";
42    }
43  is ($destroyed, 1, "$method does not leak memory");
44  }
45
46my $num_10 = Math::BigInt::FastCalc->_ten();
47my $num_2 = Math::BigInt::FastCalc->_two();
48
49my $num_long   = Math::BigInt::FastCalc->_new("1234567890");
50my $num_long_2 = Math::BigInt::FastCalc->_new("12345678900987654321");
51
52is (Math::BigInt::FastCalc->_str($num_long), "1234567890");
53is (Math::BigInt::FastCalc->_str($num_long_2), "12345678900987654321");
54
55# to hit all possible code branches
56_test_acmp($num, $num);
57_test_acmp($num_10, $num_10);
58_test_acmp($num, $num_10);
59_test_acmp($num_10, $num);
60_test_acmp($num, $num_2);
61_test_acmp($num_2, $num);
62_test_acmp($num_long, $num);
63_test_acmp($num, $num_long);
64_test_acmp($num_long, $num_long);
65_test_acmp($num_long, $num_long_2);
66_test_acmp($num_long_2, $num_long);
67
68sub _test_acmp
69  {
70  my ($n1,$n2) = @_;
71
72  $destroyed = 0;
73    {
74    my $rc = Math::BigInt::FastCalc->_acmp($n1,$n2);
75    bless \$rc, "Math::BigInt::FastCalc::LeakCheck";
76    }
77  my $n_1 = Math::BigInt::FastCalc->_str($n1);
78  my $n_2 = Math::BigInt::FastCalc->_str($n2);
79  is ($destroyed, 1, "_acmp($n_1,$n_2) does not leak memory");
80  }
81