1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
#!/usr/bin/perl
use strict;
use warnings;
# See:
# https://rt.cpan.org/Ticket/Display.html?id=92593
use Test::More tests => 47;
use Math::GMP;
{
my $x = Math::GMP->new(5);
my $val = $x->bfac(); # 1*2*3*4*5 = 120
# TEST
is ($x.'', "5", "x->bfac did not change x");
# TEST
is ($val.'', '120', 'val=x->bfac is correct.');
}
{
my $x = Math::GMP->new(0b1100);
my $ret = $x->band(0b1010, 0);
# TEST
is ($x.'', 0b1100, "x->band did not change");
# TEST
is ($ret.'', 0b1000, "ret = x->band is correct.");
}
{
my $x = Math::GMP->new(0b1100);
my $ret = $x->bxor(0b1010, 0);
# TEST
is ($x.'', 0b1100, "x did not change after x->bxor");
# TEST
is ($ret.'', 0b110, "ret = x->bxor is correct.");
}
{
my $x = Math::GMP->new(0b1100);
my $ret = $x->bior(0b1010, 0);
# TEST
is ($x.'', 0b1100, "x did not change after x->bior");
# TEST
is ($ret.'', 0b1110, "ret = x->bior is correct.");
}
{
my $x = Math::GMP->new(1000 * 3);
my $ret = $x->bgcd(1000 * 7);
# TEST
is ($x.'', 1000 * 3, "x did not change after x->bgcd");
# TEST
is ($ret.'', 1000, "ret = x->bgcd(y) is correct.");
}
{
my $x = Math::GMP->new(1000 * 3 * 3);
my $ret = $x->blcm(1000 * 3 * 7);
# TEST
is ($x.'', 1000 * 3 * 3, "x did not change after x->blcm");
# TEST
is ($ret.'', 1000 * 3 * 3 * 7, "ret = x->blcm(y) is correct.");
}
{
my $x = Math::GMP->new(5);
my $ret = $x->bmodinv(7);
# TEST
is ($x.'', 5, "x did not change after x->bmodinv");
# TEST
is ($ret.'', 3, "ret = x->bmodinv(y) is correct.");
}
{
my $x = Math::GMP->new(6);
my $ret = $x->bsqrt();
# TEST
is ($x.'', 6, "x did not change after x->bsqrt");
# TEST
is ($ret.'', 2, "ret = x->bsqrt() is correct.");
}
{
my $x = Math::GMP->new(200);
my $ret = $x->legendre(3);
# TEST
is ($x.'', 200, "x did not change after x->legendre");
# TEST
is ($ret, -1, "ret = x->legendre(y) is correct.");
}
{
my $x = Math::GMP->new(200);
my $ret = $x->jacobi(5);
# TEST
is ($x.'', 200, "x did not change after x->jacobi");
# TEST
is ($ret, 0, "ret = x->jacobi(y) is correct.");
}
{
my $x = Math::GMP::fibonacci(200);
# TEST
is ($x.'', '280571172992510140037611932413038677189525', "Math::GMP::fibonacci() works fine");
}
{
my $x = Math::GMP->new(7);
my $is_prime_verdict = $x->probab_prime(10);
# TEST
is ($x.'', '7', "x did not change after x->probab_prime");
# TEST
is ($is_prime_verdict, '2', 'probab_prime works.');
}
{
my $x = Math::GMP->new('1'. ('0' x 100));
$x->add_ui_gmp(500);
# TEST
is ($x.'', '1' . ('0' x (100-3)) . '500', "x was mutated after add_ui_gmp");
}
{
my $x = Math::GMP->new(7);
my ($quo, $rem) = $x->bdiv(3);
# TEST
is ($x.'', 7, "x did not change after x->bdiv");
# TEST
is ($quo.'', 2, "x->bdiv[quo]");
# TEST
is ($rem.'', 1, "x->bdiv[rem]");
}
{
my $x = Math::GMP->new(200);
my $ret = $x->div_2exp_gmp(2);
# TEST
is ($x.'', 200, "x did not change after x->div_2exp_gmp");
# TEST
is ($ret.'', 50, "ret = x->div_2exp_gmp(y) is correct.");
}
{
my $init_n = 3 * 7 + 2 * 7 * 7 + 6 * 7 * 7 * 7;
my $x = Math::GMP->new($init_n);
my $ret = $x->get_str_gmp(7);
# TEST
is ($x.'', $init_n, "x did not change after x->get_str_gmp");
# TEST
is ($ret, "6230", "ret = x->get_str_gmp(base) is correct.");
}
{
my $x = Math::GMP->new('2' . ('123' x 100));
my $y = $x->gmp_copy;
# TEST
is ($x.'', '2'. ('123' x 100), "x did not change after x->gmp_copy");
# TEST
is ($y.'', '2'. ('123' x 100), "->gmp_copy returned a clone.");
$y += 1;
# TEST
is ($x.'', '2'. ('123' x 100), "x did not change after x->gmp_copy+modify");
# TEST
is ($y.'', '2'. ('123' x 99) . '124', "y changed.");
}
{
my $x = Math::GMP->new(0b1000100);
# TEST
is (scalar($x->gmp_tstbit(6)), 1, "gmp_tstbit #1");
# TEST
is (scalar($x->gmp_tstbit(4)), 0, "gmp_tstbit #2");
}
{
my $x = (Math::GMP->new(24) * 5);
my $ret = $x->intify;
# TEST
is ($ret, 120, "test intify");
}
{
my $x = Math::GMP->new(2 . ('0' x 200) . 4);
my $y = Math::GMP->new(5);
my $ret = $x->mmod_gmp($y);
# TEST
is ($ret.'', 4, "mmod_gmp");
# TEST
is ($x.'', '2' . ('0' x 200) . '4', "mmod_gmp did not change first arg");
}
{
my $x = Math::GMP->new(0b10001011);
my $ret = $x->mod_2exp_gmp(4);
# TEST
is ($x.'', 0b10001011, "x did not change after x->mod_2exp_gmp");
# TEST
is ($ret.'', 0b1011, "ret = x->mod_2exp_gmp(y) is correct.");
}
{
my $x = Math::GMP->new(0b10001011);
my $ret = $x->mul_2exp_gmp(4);
# TEST
is ($x.'', 0b10001011, "x did not change after x->mul_2exp_gmp");
# TEST
is ($ret.'', 0b100010110000, "ret = x->mul_2exp_gmp(y) is correct.");
}
{
my $x = Math::GMP->new(157);
my $exp = Math::GMP->new(100);
my $mod = Math::GMP->new(5013);
my $ret = $x->powm_gmp($exp, $mod);
my $brute_force_ret = (($x ** $exp) % $mod);
# TEST
is ($x.'', 157, "x did not change after x->powm_gmp");
# TEST
is ($ret.'', $brute_force_ret.'',
"ret = x->powm_gmp(exp, mod) is correct."
);
}
{
my $x = Math::GMP->new('2' . ('123' x 100));
# TEST
is ($x->sizeinbase_gmp(10), 1 + 3 * 100, "sizeinbase_gmp works");
}
|