| 1 | # -*- mode: perl; -*- |
| 2 | |
| 3 | use strictscip-perl cpan Math-BigInt-GMP 1.7003 strict/; |
| 4 | use warningsscip-perl cpan Math-BigInt-GMP 1.7003 warnings/; |
| 5 | |
| 6 | use Test::Morescip-perl cpan Math-BigInt-GMP 1.7003 Test/More/ tests => 5181; |
| 7 | |
| 8 | ############################################################################### |
| 9 | # Read and load configuration file and backend library. |
| 10 | |
| 11 | use Config::Tinyscip-perl cpan Math-BigInt-GMP 1.7003 Config/Tiny/ (); |
| 12 | |
| 13 | my $config_file = 'xt/author/lib.ini'; |
| 14 | my $config = Config::Tiny -> readscip-perl cpan Math-BigInt-GMP 1.7003 Config/Tiny/read().('xt/author/lib.ini') |
| 15 | or diescip-perl cpan Math-BigInt-GMP 1.7003 main/die(). Config::Tiny -> errstrscip-perl cpan Math-BigInt-GMP 1.7003 Config/Tiny/errstr().(); |
| 16 | |
| 17 | # Read the library to test. |
| 18 | |
| 19 | our $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. = $config->{_}->{lib}; |
| 20 | |
| 21 | diescip-perl cpan Math-BigInt-GMP 1.7003 main/die(). "No library defined in file '$config_file'" |
| 22 | unless definedscip-perl cpan Math-BigInt-GMP 1.7003 main/defined(). $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB.; |
| 23 | diescip-perl cpan Math-BigInt-GMP 1.7003 main/die(). "Invalid library name '$LIB' in file '$config_file'" |
| 24 | unless $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. =~ /^[A-Za-z]\w*(::\w+)*\z/; |
| 25 | |
| 26 | # Read the reference type(s) the library uses. |
| 27 | |
| 28 | our $REFscip-perl cpan Math-BigInt-GMP 1.7003 main/$REF. = $config->{_}->{refscip-perl cpan Math-BigInt-GMP 1.7003 main/ref().}; |
| 29 | |
| 30 | diescip-perl cpan Math-BigInt-GMP 1.7003 main/die(). "No reference type defined in file '$config_file'" |
| 31 | unless definedscip-perl cpan Math-BigInt-GMP 1.7003 main/defined(). $REFscip-perl cpan Math-BigInt-GMP 1.7003 main/$REF.; |
| 32 | diescip-perl cpan Math-BigInt-GMP 1.7003 main/die(). "Invalid reference type '$REF' in file '$config_file'" |
| 33 | unless $REFscip-perl cpan Math-BigInt-GMP 1.7003 main/$REF. =~ /^[A-Za-z]\w*(::\w+)*\z/; |
| 34 | |
| 35 | # Load the library. |
| 36 | |
| 37 | eval "require $LIB"; |
| 38 | diescip-perl cpan Math-BigInt-GMP 1.7003 main/die(). $@scip-perl cpan Math-BigInt-GMP 1.7003 main/`$@`. if $@scip-perl cpan Math-BigInt-GMP 1.7003 main/`$@`.; |
| 39 | |
| 40 | ############################################################################### |
| 41 | |
| 42 | can_okscip-perl cpan Math-BigInt-GMP 1.7003 main/can_ok().($LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB., '_clog2'); |
| 43 | |
| 44 | # ceil(log(x) / log(2) |
| 45 | |
| 46 | sub clog2scip-perl cpan Math-BigInt-GMP 1.7003 main/clog2(). { |
| 47 | my $x = shiftscip-perl cpan Math-BigInt-GMP 1.7003 main/shift().; |
| 48 | my $y = intscip-perl cpan Math-BigInt-GMP 1.7003 main/int().(logscip-perl cpan Math-BigInt-GMP 1.7003 main/log().($x) / logscip-perl cpan Math-BigInt-GMP 1.7003 main/log().(2)); |
| 49 | |
| 50 | my $trial = 2 ** $y; |
| 51 | return $y if $trial == $x; |
| 52 | |
| 53 | while ($trial > $x) { |
| 54 | $y--; |
| 55 | $trial = 2 ** $y; |
| 56 | } |
| 57 | |
| 58 | while ($trial < $x) { |
| 59 | $y++; |
| 60 | $trial = 2 ** $y; |
| 61 | } |
| 62 | |
| 63 | return $y; |
| 64 | } |
| 65 | |
| 66 | my @data; |
| 67 | |
| 68 | # Small numbers. |
| 69 | |
| 70 | for (my $x = 1 ; $x <= 1022 ; ++ $x) { |
| 71 | my $y = clog2scip-perl cpan Math-BigInt-GMP 1.7003 main/clog2().($x); |
| 72 | my $status = 2 ** $y == $x ? 1 : 0; |
| 73 | pushscip-perl cpan Math-BigInt-GMP 1.7003 main/push(). @data, [ $x, $y, $status ]; |
| 74 | } |
| 75 | |
| 76 | # Larger numbers. |
| 77 | |
| 78 | my $b = $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. -> _newscip-perl cpan Math-BigInt-GMP 1.7003 main/_new().(2); |
| 79 | for (my $y = 10 ; $y <= 100 ; $y++) { |
| 80 | my $x = $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. -> _powscip-perl cpan Math-BigInt-GMP 1.7003 main/_pow().($LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. -> _copyscip-perl cpan Math-BigInt-GMP 1.7003 main/_copy().($b), $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. -> _newscip-perl cpan Math-BigInt-GMP 1.7003 main/_new().($y)); |
| 81 | my $x_up = $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. -> _incscip-perl cpan Math-BigInt-GMP 1.7003 main/_inc().($LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. -> _copyscip-perl cpan Math-BigInt-GMP 1.7003 main/_copy().($x)); |
| 82 | my $x_dn = $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. -> _decscip-perl cpan Math-BigInt-GMP 1.7003 main/_dec().($LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. -> _copyscip-perl cpan Math-BigInt-GMP 1.7003 main/_copy().($x)); |
| 83 | pushscip-perl cpan Math-BigInt-GMP 1.7003 main/push(). @data, [ $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. -> _strscip-perl cpan Math-BigInt-GMP 1.7003 main/_str().($x_dn), $y, 0 ]; # clog2(2**$y - 1) = $y |
| 84 | pushscip-perl cpan Math-BigInt-GMP 1.7003 main/push(). @data, [ $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. -> _strscip-perl cpan Math-BigInt-GMP 1.7003 main/_str().($x), $y, 1 ]; # clog2(2**$y) = $y |
| 85 | pushscip-perl cpan Math-BigInt-GMP 1.7003 main/push(). @data, [ $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB. -> _strscip-perl cpan Math-BigInt-GMP 1.7003 main/_str().($x_up), $y + 1, 0 ]; # clog2(2**$y + 1) = $y + 1 |
| 86 | } |
| 87 | |
| 88 | # Scalar context. |
| 89 | |
| 90 | for (my $i = 0 ; $i <= $#datascip-perl cpan Math-BigInt-GMP 1.7003 main/`$#data`. ; ++ $i) { |
| 91 | my ($in0, $out0) = @{ $data[$i] }; |
| 92 | |
| 93 | my ($x, $y, $got); |
| 94 | |
| 95 | my $test = qq|\$x = $LIB->_new("$in0"); | |
| 96 | . qq|\$got = $LIB->_clog2(\$x);|; |
| 97 | |
| 98 | diagscip-perl cpan Math-BigInt-GMP 1.7003 main/diag().("\n$test\n\n") if $ENVscip-perl cpan Math-BigInt-GMP 1.7003 main/`%ENV`.{AUTHOR_DEBUGGING}; |
| 99 | |
| 100 | eval $test; |
| 101 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().($@scip-perl cpan Math-BigInt-GMP 1.7003 main/`$@`., "", "'$test' gives emtpy \$\@"); |
| 102 | |
| 103 | subtestscip-perl cpan Math-BigInt-GMP 1.7003 main/subtest(). "_clog2() in list context: $test", sub { |
| 104 | |
| 105 | unless (definedscip-perl cpan Math-BigInt-GMP 1.7003 main/defined(). $out0) { |
| 106 | planscip-perl cpan Math-BigInt-GMP 1.7003 main/plan(). tests => 1; |
| 107 | |
| 108 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().($got, $out0, |
| 109 | "'$test' output arg has the right value"); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | planscip-perl cpan Math-BigInt-GMP 1.7003 main/plan(). tests => 5; |
| 114 | |
| 115 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().(refscip-perl cpan Math-BigInt-GMP 1.7003 main/ref().($got), $REFscip-perl cpan Math-BigInt-GMP 1.7003 main/$REF., |
| 116 | "'$test' output arg is a $REF"); |
| 117 | |
| 118 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().($LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB.->_checkscip-perl cpan Math-BigInt-GMP 1.7003 main/_check().($got), 0, |
| 119 | "'$test' output is valid"); |
| 120 | |
| 121 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().($LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB.->_strscip-perl cpan Math-BigInt-GMP 1.7003 main/_str().($got), $out0, |
| 122 | "'$test' output arg has the right value"); |
| 123 | |
| 124 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().(refscip-perl cpan Math-BigInt-GMP 1.7003 main/ref().($x), $REFscip-perl cpan Math-BigInt-GMP 1.7003 main/$REF., |
| 125 | "'$test' input arg is still a $REF"); |
| 126 | |
| 127 | okscip-perl cpan Math-BigInt-GMP 1.7003 main/ok().($LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB.->_strscip-perl cpan Math-BigInt-GMP 1.7003 main/_str().($x) eq $out0 || $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB.->_strscip-perl cpan Math-BigInt-GMP 1.7003 main/_str().($x) eq $in0, |
| 128 | "'$test' input arg has the correct value"); |
| 129 | }; |
| 130 | } |
| 131 | |
| 132 | # List context. |
| 133 | |
| 134 | for (my $i = 0 ; $i <= $#datascip-perl cpan Math-BigInt-GMP 1.7003 main/`$#data`. ; ++ $i) { |
| 135 | my ($in0, $out0, $out1) = @{ $data[$i] }; |
| 136 | |
| 137 | my ($x, $y, @got); |
| 138 | |
| 139 | my $test = qq|\$x = $LIB->_new("$in0"); | |
| 140 | . qq|\@got = $LIB->_clog2(\$x);|; |
| 141 | |
| 142 | diagscip-perl cpan Math-BigInt-GMP 1.7003 main/diag().("\n$test\n\n") if $ENVscip-perl cpan Math-BigInt-GMP 1.7003 main/`%ENV`.{AUTHOR_DEBUGGING}; |
| 143 | |
| 144 | eval $test; |
| 145 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().($@scip-perl cpan Math-BigInt-GMP 1.7003 main/`$@`., "", "'$test' gives emtpy \$\@"); |
| 146 | |
| 147 | subtestscip-perl cpan Math-BigInt-GMP 1.7003 main/subtest(). "_clog2() in list context: $test", sub { |
| 148 | |
| 149 | unless (definedscip-perl cpan Math-BigInt-GMP 1.7003 main/defined(). $out0) { |
| 150 | planscip-perl cpan Math-BigInt-GMP 1.7003 main/plan(). tests => 1; |
| 151 | |
| 152 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().($got[0], $out0, |
| 153 | "'$test' output arg has the right value"); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | planscip-perl cpan Math-BigInt-GMP 1.7003 main/plan(). tests => 8; |
| 158 | |
| 159 | # Number of output arguments. |
| 160 | |
| 161 | cmp_okscip-perl cpan Math-BigInt-GMP 1.7003 main/cmp_ok().(scalarscip-perl cpan Math-BigInt-GMP 1.7003 main/scalar().(@got), '==', 2, |
| 162 | "'$test' gives two output args"); |
| 163 | |
| 164 | # First output argument. |
| 165 | |
| 166 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().(refscip-perl cpan Math-BigInt-GMP 1.7003 main/ref().($got[0]), $REFscip-perl cpan Math-BigInt-GMP 1.7003 main/$REF., |
| 167 | "'$test' first output arg is a $REF"); |
| 168 | |
| 169 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().($LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB.->_checkscip-perl cpan Math-BigInt-GMP 1.7003 main/_check().($got[0]), 0, |
| 170 | "'$test' first output is valid"); |
| 171 | |
| 172 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().($LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB.->_strscip-perl cpan Math-BigInt-GMP 1.7003 main/_str().($got[0]), $out0, |
| 173 | "'$test' output arg has the right value"); |
| 174 | |
| 175 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().(refscip-perl cpan Math-BigInt-GMP 1.7003 main/ref().($x), $REFscip-perl cpan Math-BigInt-GMP 1.7003 main/$REF., |
| 176 | "'$test' first input arg is still a $REF"); |
| 177 | |
| 178 | okscip-perl cpan Math-BigInt-GMP 1.7003 main/ok().($LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB.->_strscip-perl cpan Math-BigInt-GMP 1.7003 main/_str().($x) eq $out0 || $LIBscip-perl cpan Math-BigInt-GMP 1.7003 main/$LIB.->_strscip-perl cpan Math-BigInt-GMP 1.7003 main/_str().($x) eq $in0, |
| 179 | "'$test' first input arg has the correct value"); |
| 180 | |
| 181 | # Second output argument. |
| 182 | |
| 183 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().(refscip-perl cpan Math-BigInt-GMP 1.7003 main/ref().($got[1]), "", |
| 184 | "'$test' second output arg is a scalar"); |
| 185 | |
| 186 | isscip-perl cpan Math-BigInt-GMP 1.7003 main/is().($got[1], $out1, |
| 187 | "'$test' second output arg has the right value"); |
| 188 | }; |
| 189 | } |