| 1 | # -*- mode: perl; -*- |
| 2 | |
| 3 | use strictscip-perl cpan Math-BigInt 2.005003 strict/; |
| 4 | use warningsscip-perl cpan Math-BigInt 2.005003 warnings/; |
| 5 | |
| 6 | use Test::Morescip-perl cpan Math-BigInt 2.005003 Test/More/ tests => 299; |
| 7 | use Scalar::Utilscip-perl cpan Math-BigInt 2.005003 Scalar/Util/ qw< refaddr >; |
| 8 | |
| 9 | my @classes; |
| 10 | |
| 11 | BEGIN { |
| 12 | @classes = qw< Math::BigInt Math::BigRat Math::BigFloat >; |
| 13 | for my $class (@classes) { |
| 14 | use_okscip-perl cpan Math-BigInt 2.005003 main/use_ok().($class); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | my %cases = |
| 19 | ( |
| 20 | |
| 21 | # Common test cases for Math::BigInt, Math::BigRat, and Math::BigFloat. |
| 22 | |
| 23 | 'common' => |
| 24 | [ |
| 25 | ['NaN', 'NaN', 'NaN'], |
| 26 | ['inf', 'NaN', 'NaN'], |
| 27 | ['NaN', 'inf', 'NaN'], |
| 28 | |
| 29 | ['0', 'NaN', 'NaN'], |
| 30 | ['0', 'inf', 'NaN'], |
| 31 | ['0', '-inf', 'NaN'], |
| 32 | |
| 33 | ['1', 'NaN', 'NaN'], |
| 34 | ['1', 'inf', 'NaN'], |
| 35 | ['1', '-inf', 'NaN'], |
| 36 | |
| 37 | ['-4', '0', '4'], |
| 38 | [ '0', '-4', '4'], |
| 39 | ['-1', '0', '1'], |
| 40 | [ '0', '-1', '1'], |
| 41 | [ '0', '0', '0'], |
| 42 | [ '1', '0', '1'], |
| 43 | [ '0', '1', '1'], |
| 44 | [ '4', '0', '4'], |
| 45 | [ '0', '4', '4'], |
| 46 | |
| 47 | ['-6', '-4', '2'], |
| 48 | ['-6', '4', '2'], |
| 49 | [ '4', '-6', '2'], |
| 50 | [ '4', '6', '2'], |
| 51 | ], |
| 52 | |
| 53 | # Test cases for Math::BigInt only. |
| 54 | |
| 55 | 'Math::BigInt' => |
| 56 | [ |
| 57 | ['0.8', '1.2', 'NaN'], |
| 58 | ['1.2', '0.8', 'NaN'], |
| 59 | [ '0', '0.8', 'NaN'], |
| 60 | ['1.2', '0', 'NaN'], |
| 61 | ], |
| 62 | |
| 63 | # Test cases for Math::BigRat only. |
| 64 | |
| 65 | 'Math::BigRat' => |
| 66 | [ |
| 67 | ['4/5', '6/5', '2/5'], |
| 68 | ['6/5', '4/5', '2/5'], |
| 69 | ], |
| 70 | |
| 71 | # Test cases for Math::BigFloat only. |
| 72 | |
| 73 | 'Math::BigFloat' => |
| 74 | [ |
| 75 | ['0.8', '1.2', '0.4'], |
| 76 | ['1.2', '0.8', '0.4'], |
| 77 | ], |
| 78 | |
| 79 | ); |
| 80 | |
| 81 | for my $class (@classes) { |
| 82 | |
| 83 | my @cases = (@{ $cases{common} }, # common test cases |
| 84 | @{ $cases{$class} }); # class-specific test cases |
| 85 | |
| 86 | for my $case (@cases) { |
| 87 | my ($in0, $in1, $out0) = @$case; |
| 88 | |
| 89 | # As class method. |
| 90 | |
| 91 | { |
| 92 | my ($x, $z); |
| 93 | my $test = qq|\$z = $class -> bgcd("$in0", "$in1");|; |
| 94 | |
| 95 | notescip-perl cpan Math-BigInt 2.005003 main/note().("\n$test\n\n"); |
| 96 | |
| 97 | eval $test; |
| 98 | diescip-perl cpan Math-BigInt 2.005003 main/die(). $@scip-perl cpan Math-BigInt 2.005003 main/`$@`. if $@scip-perl cpan Math-BigInt 2.005003 main/`$@`.; # this should never happen |
| 99 | |
| 100 | subtestscip-perl cpan Math-BigInt 2.005003 main/subtest(). $test, sub { |
| 101 | isscip-perl cpan Math-BigInt 2.005003 main/is().(refscip-perl cpan Math-BigInt 2.005003 main/ref().($z), $class, "output arg is a $class"); |
| 102 | isscip-perl cpan Math-BigInt 2.005003 main/is().($z, $out0, 'output arg has the right value'); |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | # As instance method. |
| 107 | |
| 108 | { |
| 109 | my ($x, $y, $z); |
| 110 | my $test = qq|\$x = $class -> new("$in0");| |
| 111 | . qq| \$y = $class -> new("$in1");| |
| 112 | . qq| \$z = \$x -> bgcd(\$y);|; |
| 113 | |
| 114 | notescip-perl cpan Math-BigInt 2.005003 main/note().("\n$test\n\n"); |
| 115 | |
| 116 | eval $test; |
| 117 | diescip-perl cpan Math-BigInt 2.005003 main/die(). $@scip-perl cpan Math-BigInt 2.005003 main/`$@`. if $@scip-perl cpan Math-BigInt 2.005003 main/`$@`.; # this should never happen |
| 118 | |
| 119 | subtestscip-perl cpan Math-BigInt 2.005003 main/subtest(). $test, sub { |
| 120 | isscip-perl cpan Math-BigInt 2.005003 main/is().(refscip-perl cpan Math-BigInt 2.005003 main/ref().($z), $class, "output arg is a $class"); |
| 121 | isscip-perl cpan Math-BigInt 2.005003 main/is().($z, $out0, 'output arg has the right value'); |
| 122 | isscip-perl cpan Math-BigInt 2.005003 main/is().($x, $in0, 'first operand is unmodified') |
| 123 | unless $class eq "Math::BigInt" && $in0 =~ m|[./]|; |
| 124 | isscip-perl cpan Math-BigInt 2.005003 main/is().($y, $in1, 'second operand is unmodified') |
| 125 | unless $class eq "Math::BigInt" && $in1 =~ m|[./]|; |
| 126 | isntscip-perl cpan Math-BigInt 2.005003 main/isnt().(refaddrscip-perl cpan Math-BigInt 2.005003 main/refaddr().($z), refaddrscip-perl cpan Math-BigInt 2.005003 main/refaddr().($x), |
| 127 | 'output arg is not the first operand'); |
| 128 | isntscip-perl cpan Math-BigInt 2.005003 main/isnt().(refaddrscip-perl cpan Math-BigInt 2.005003 main/refaddr().($z), refaddrscip-perl cpan Math-BigInt 2.005003 main/refaddr().($y), |
| 129 | 'output arg is not the second operand'); |
| 130 | }; |
| 131 | } |
| 132 | |
| 133 | # As function. |
| 134 | # |
| 135 | # For some reason, the original author decided that bgcd() and blcm() |
| 136 | # should support being used functions, not methods. In addition, if |
| 137 | # used as instance methods, they should not modify their invocand. This |
| 138 | # is inconsistent with all other bxxx() methods, but there we are. |
| 139 | |
| 140 | { |
| 141 | my ($x, $y, $z); |
| 142 | my $test = qq|\$x = $class -> new("$in0");| |
| 143 | . qq| \$y = $class -> new("$in1");| |
| 144 | . qq| \$z = ${class}::bgcd(\$x, \$y);|; |
| 145 | |
| 146 | notescip-perl cpan Math-BigInt 2.005003 main/note().("\n$test\n\n"); |
| 147 | |
| 148 | eval $test; |
| 149 | diescip-perl cpan Math-BigInt 2.005003 main/die(). $@scip-perl cpan Math-BigInt 2.005003 main/`$@`. if $@scip-perl cpan Math-BigInt 2.005003 main/`$@`.; # this should never happen |
| 150 | |
| 151 | subtestscip-perl cpan Math-BigInt 2.005003 main/subtest(). $test, sub { |
| 152 | isscip-perl cpan Math-BigInt 2.005003 main/is().(refscip-perl cpan Math-BigInt 2.005003 main/ref().($z), $class, "output arg is a $class"); |
| 153 | isscip-perl cpan Math-BigInt 2.005003 main/is().($z, $out0, 'output arg has the right value'); |
| 154 | isscip-perl cpan Math-BigInt 2.005003 main/is().($x, $in0, 'first operand is unmodified') |
| 155 | unless $class eq "Math::BigInt" && $in0 =~ m|[./]|; |
| 156 | isscip-perl cpan Math-BigInt 2.005003 main/is().($y, $in1, 'second operand is unmodified') |
| 157 | unless $class eq "Math::BigInt" && $in1 =~ m|[./]|; |
| 158 | isntscip-perl cpan Math-BigInt 2.005003 main/isnt().(refaddrscip-perl cpan Math-BigInt 2.005003 main/refaddr().($z), refaddrscip-perl cpan Math-BigInt 2.005003 main/refaddr().($x), |
| 159 | 'output arg is not the first operand'); |
| 160 | isntscip-perl cpan Math-BigInt 2.005003 main/isnt().(refaddrscip-perl cpan Math-BigInt 2.005003 main/refaddr().($z), refaddrscip-perl cpan Math-BigInt 2.005003 main/refaddr().($y), |
| 161 | 'output arg is not the second operand'); |
| 162 | }; |
| 163 | } |
| 164 | |
| 165 | { |
| 166 | my $z; |
| 167 | my $test = qq|\$z = ${class}::bgcd("$in0", "$in1");|; |
| 168 | |
| 169 | eval $test; |
| 170 | diescip-perl cpan Math-BigInt 2.005003 main/die(). $@scip-perl cpan Math-BigInt 2.005003 main/`$@`. if $@scip-perl cpan Math-BigInt 2.005003 main/`$@`.; # this should never happen |
| 171 | |
| 172 | subtestscip-perl cpan Math-BigInt 2.005003 main/subtest(). $test, sub { |
| 173 | isscip-perl cpan Math-BigInt 2.005003 main/is().(refscip-perl cpan Math-BigInt 2.005003 main/ref().($z), $class, "output arg is a $class"); |
| 174 | isscip-perl cpan Math-BigInt 2.005003 main/is().($z, $out0, 'output arg has the right value'); |
| 175 | }; |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | } |