| 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', '0'], |
| 38 | [ '0', '-4', '0'], |
| 39 | ['-1', '0', '0'], |
| 40 | [ '0', '-1', '0'], |
| 41 | [ '0', '0', '0'], |
| 42 | [ '1', '0', '0'], |
| 43 | [ '0', '1', '0'], |
| 44 | [ '4', '0', '0'], |
| 45 | [ '0', '4', '0'], |
| 46 | |
| 47 | ['-6', '-4', '12'], |
| 48 | ['-6', '4', '12'], |
| 49 | [ '4', '-6', '12'], |
| 50 | [ '4', '6', '12'], |
| 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 | # For Math::BigRat only: |
| 64 | |
| 65 | 'Math::BigRat' => |
| 66 | [ |
| 67 | ['4/5', '6/5', '12/5'], |
| 68 | ['6/5', '4/5', '12/5'], |
| 69 | ], |
| 70 | |
| 71 | # For Math::BigFloat only: |
| 72 | |
| 73 | 'Math::BigFloat' => |
| 74 | [ |
| 75 | ['0.8', '1.2', '2.4'], |
| 76 | ['1.2', '0.8', '2.4'], |
| 77 | ], |
| 78 | ); |
| 79 | |
| 80 | for my $class (@classes) { |
| 81 | |
| 82 | my @cases = (@{ $cases{common} }, # common test cases |
| 83 | @{ $cases{$class} }); # class-specific test cases |
| 84 | |
| 85 | for my $case (@cases) { |
| 86 | my ($in0, $in1, $out0) = @$case; |
| 87 | |
| 88 | # As class method. |
| 89 | |
| 90 | { |
| 91 | my ($x, $z); |
| 92 | my $test = qq|\$z = $class -> blcm("$in0", "$in1");|; |
| 93 | |
| 94 | notescip-perl cpan Math-BigInt 2.005003 main/note().("\n$test\n\n"); |
| 95 | |
| 96 | eval $test; |
| 97 | 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 |
| 98 | |
| 99 | subtestscip-perl cpan Math-BigInt 2.005003 main/subtest(). $test, sub { |
| 100 | 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"); |
| 101 | isscip-perl cpan Math-BigInt 2.005003 main/is().($z, $out0, 'output arg has the right value'); |
| 102 | }; |
| 103 | } |
| 104 | |
| 105 | # As instance method. |
| 106 | |
| 107 | { |
| 108 | my ($x, $y, $z); |
| 109 | my $test = qq|\$x = $class -> new("$in0");| |
| 110 | . qq| \$y = $class -> new("$in1");| |
| 111 | . qq| \$z = \$x -> blcm(\$y);|; |
| 112 | |
| 113 | notescip-perl cpan Math-BigInt 2.005003 main/note().("\n$test\n\n"); |
| 114 | |
| 115 | eval $test; |
| 116 | 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 |
| 117 | |
| 118 | subtestscip-perl cpan Math-BigInt 2.005003 main/subtest(). $test, sub { |
| 119 | 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"); |
| 120 | isscip-perl cpan Math-BigInt 2.005003 main/is().($z, $out0, 'output arg has the right value'); |
| 121 | isscip-perl cpan Math-BigInt 2.005003 main/is().($x, $in0, 'first operand is unmodified') |
| 122 | unless $class eq "Math::BigInt" && $in0 =~ m|[./]|; |
| 123 | isscip-perl cpan Math-BigInt 2.005003 main/is().($y, $in1, 'second operand is unmodified') |
| 124 | unless $class eq "Math::BigInt" && $in1 =~ m|[./]|; |
| 125 | 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), |
| 126 | 'output arg is not the first operand'); |
| 127 | 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), |
| 128 | 'output arg is not the second operand'); |
| 129 | }; |
| 130 | } |
| 131 | |
| 132 | # As function. |
| 133 | # |
| 134 | # For some reason, the original author decided that bgcd() and blcm() |
| 135 | # should support being used functions, not methods. In addition, if |
| 136 | # used as instance methods, they should not modify their invocand. This |
| 137 | # is inconsistent with all other bxxx() methods, but there we are. |
| 138 | |
| 139 | { |
| 140 | my ($x, $y, $z); |
| 141 | my $test = qq|\$x = $class -> new("$in0");| |
| 142 | . qq| \$y = $class -> new("$in1");| |
| 143 | . qq| \$z = ${class}::blcm(\$x, \$y);|; |
| 144 | |
| 145 | notescip-perl cpan Math-BigInt 2.005003 main/note().("\n$test\n\n"); |
| 146 | |
| 147 | eval $test; |
| 148 | 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 |
| 149 | |
| 150 | subtestscip-perl cpan Math-BigInt 2.005003 main/subtest(). $test, sub { |
| 151 | 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"); |
| 152 | isscip-perl cpan Math-BigInt 2.005003 main/is().($z, $out0, 'output arg has the right value'); |
| 153 | isscip-perl cpan Math-BigInt 2.005003 main/is().($x, $in0, 'first operand is unmodified') |
| 154 | unless $class eq "Math::BigInt" && $in0 =~ m|[./]|; |
| 155 | isscip-perl cpan Math-BigInt 2.005003 main/is().($y, $in1, 'second operand is unmodified') |
| 156 | unless $class eq "Math::BigInt" && $in1 =~ m|[./]|; |
| 157 | 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), |
| 158 | 'output arg is not the first operand'); |
| 159 | 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), |
| 160 | 'output arg is not the second operand'); |
| 161 | }; |
| 162 | } |
| 163 | |
| 164 | { |
| 165 | my $z; |
| 166 | my $test = qq|\$z = ${class}::blcm("$in0", "$in1");|; |
| 167 | |
| 168 | eval $test; |
| 169 | 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 |
| 170 | |
| 171 | subtestscip-perl cpan Math-BigInt 2.005003 main/subtest(). $test, sub { |
| 172 | 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"); |
| 173 | isscip-perl cpan Math-BigInt 2.005003 main/is().($z, $out0, 'output arg has the right value'); |
| 174 | }; |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | } |