Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 418346#!/usr/bin/perl -w12# GMP perl module tests34# Copyright 2001-2003 Free Software Foundation, Inc.5#6# This file is part of the GNU MP Library.7#8# The GNU MP Library is free software; you can redistribute it and/or modify9# it under the terms of either:10#11# * the GNU Lesser General Public License as published by the Free12# Software Foundation; either version 3 of the License, or (at your13# option) any later version.14#15# or16#17# * the GNU General Public License as published by the Free Software18# Foundation; either version 2 of the License, or (at your option) any19# later version.20#21# or both in parallel, as here.22#23# The GNU MP Library is distributed in the hope that it will be useful, but24# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY25# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License26# for more details.27#28# You should have received copies of the GNU General Public License and the29# GNU Lesser General Public License along with the GNU MP Library. If not,30# see https://www.gnu.org/licenses/.313233# These tests aim to exercise the many possible combinations of operands34# etc, and to run all functions at least once, which if nothing else will35# check everything intended is in the :all list.36#37# Use the following in .emacs to match test failure messages.38#39# ;; perl "Test" module error messages40# (eval-after-load "compile"41# '(add-to-list42# 'compilation-error-regexp-alist43# '("^.*Failed test [0-9]+ in \\([^ ]+\\) at line \\([0-9]+\\)" 1 2)))444546use strict;47use Test;4849BEGIN {50plan tests => 123,51onfail => sub { print "there were failures\n" },52}5354use GMP qw(:all);55use GMP::Mpz qw(:all);56use GMP::Mpq qw(:all);57use GMP::Mpf qw(:all);58use GMP::Rand qw(:all);5960use GMP::Mpz qw(:constants);61use GMP::Mpz qw(:noconstants);62use GMP::Mpq qw(:constants);63use GMP::Mpq qw(:noconstants);64use GMP::Mpf qw(:constants);65use GMP::Mpf qw(:noconstants);6667package Mytie;68use Exporter;69use vars qw($val $fetched $stored);70$val = 0;71$fetched = 0;72$stored = 0;73sub TIESCALAR {74my ($class, $newval) = @_;75my $var = 'mytie dummy refed var';76$val = $newval;77$fetched = 0;78$stored = 0;79return bless \$var, $class;80}81sub FETCH {82my ($self) = @_;83$fetched++;84return $val;85}86sub STORE {87my ($self, $newval) = @_;88$val = $newval;89$stored++;90}91package main;9293# check Mytie does what it should94{ tie my $t, 'Mytie', 123;95ok ($Mytie::val == 123);96$Mytie::val = 456;97ok ($t == 456);98$t = 789;99ok ($Mytie::val == 789);100}101102103# Usage: str(x)104# Return x forced to a string, not a PVIV.105#106sub str {107my $s = "$_[0]" . "";108return $s;109}110111my $ivnv_2p128 = 65536.0 * 65536.0 * 65536.0 * 65536.0112* 65536.0 * 65536.0 * 65536.0 * 65536.0;113kill (0, $ivnv_2p128);114my $str_2p128 = '340282366920938463463374607431768211456';115116my $uv_max = ~ 0;117my $uv_max_str = ~ 0;118$uv_max_str = "$uv_max_str";119$uv_max_str = "" . "$uv_max_str";120121122#------------------------------------------------------------------------------123# GMP::version124125use GMP qw(version);126print '$GMP::VERSION ',$GMP::VERSION,' GMP::version() ',version(),"\n";127128129#------------------------------------------------------------------------------130# GMP::Mpz::new131132ok (mpz(0) == 0);133ok (mpz('0') == 0);134ok (mpz(substr('101',1,1)) == 0);135ok (mpz(0.0) == 0);136ok (mpz(mpz(0)) == 0);137ok (mpz(mpq(0)) == 0);138ok (mpz(mpf(0)) == 0);139140{ tie my $t, 'Mytie', 0;141ok (mpz($t) == 0);142ok ($Mytie::fetched > 0);143}144{ tie my $t, 'Mytie', '0';145ok (mpz($t) == 0);146ok ($Mytie::fetched > 0);147}148{ tie my $t, 'Mytie', substr('101',1,1); ok (mpz($t) == 0); }149{ tie my $t, 'Mytie', 0.0; ok (mpz($t) == 0); }150{ tie my $t, 'Mytie', mpz(0); ok (mpz($t) == 0); }151{ tie my $t, 'Mytie', mpq(0); ok (mpz($t) == 0); }152{ tie my $t, 'Mytie', mpf(0); ok (mpz($t) == 0); }153154ok (mpz(-123) == -123);155ok (mpz('-123') == -123);156ok (mpz(substr('1-1231',1,4)) == -123);157ok (mpz(-123.0) == -123);158ok (mpz(mpz(-123)) == -123);159ok (mpz(mpq(-123)) == -123);160ok (mpz(mpf(-123)) == -123);161162{ tie my $t, 'Mytie', -123; ok (mpz($t) == -123); }163{ tie my $t, 'Mytie', '-123'; ok (mpz($t) == -123); }164{ tie my $t, 'Mytie', substr('1-1231',1,4); ok (mpz($t) == -123); }165{ tie my $t, 'Mytie', -123.0; ok (mpz($t) == -123); }166{ tie my $t, 'Mytie', mpz(-123); ok (mpz($t) == -123); }167{ tie my $t, 'Mytie', mpq(-123); ok (mpz($t) == -123); }168{ tie my $t, 'Mytie', mpf(-123); ok (mpz($t) == -123); }169170ok (mpz($ivnv_2p128) == $str_2p128);171{ tie my $t, 'Mytie', $ivnv_2p128; ok (mpz($t) == $str_2p128); }172173ok (mpz($uv_max) > 0);174ok (mpz($uv_max) == mpz($uv_max_str));175{ tie my $t, 'Mytie', $uv_max; ok (mpz($t) > 0); }176{ tie my $t, 'Mytie', $uv_max; ok (mpz($t) == mpz($uv_max_str)); }177178{ my $s = '999999999999999999999999999999';179kill (0, $s);180ok (mpz($s) == '999999999999999999999999999999');181tie my $t, 'Mytie', $s;182ok (mpz($t) == '999999999999999999999999999999');183}184185#------------------------------------------------------------------------------186# GMP::Mpz::overload_abs187188ok (abs(mpz(0)) == 0);189ok (abs(mpz(123)) == 123);190ok (abs(mpz(-123)) == 123);191192{ my $x = mpz(-123); $x = abs($x); ok ($x == 123); }193{ my $x = mpz(0); $x = abs($x); ok ($x == 0); }194{ my $x = mpz(123); $x = abs($x); ok ($x == 123); }195196{ tie my $t, 'Mytie', mpz(0); ok (abs($t) == 0); }197{ tie my $t, 'Mytie', mpz(123); ok (abs($t) == 123); }198{ tie my $t, 'Mytie', mpz(-123); ok (abs($t) == 123); }199200#------------------------------------------------------------------------------201# GMP::Mpz::overload_add202203ok (mpz(0) + 1 == 1);204ok (mpz(-1) + 1 == 0);205ok (1 + mpz(0) == 1);206ok (1 + mpz(-1) == 0);207208#------------------------------------------------------------------------------209# GMP::Mpz::overload_addeq210211{ my $a = mpz(7); $a += 1; ok ($a == 8); }212{ my $a = mpz(7); my $b = $a; $a += 1; ok ($a == 8); ok ($b == 7); }213214#------------------------------------------------------------------------------215# GMP::Mpz::overload_and216217ok ((mpz(3) & 1) == 1);218ok ((mpz(3) & 4) == 0);219220{ my $a = mpz(3); $a &= 1; ok ($a == 1); }221{ my $a = mpz(3); $a &= 4; ok ($a == 0); }222223#------------------------------------------------------------------------------224# GMP::Mpz::overload_bool225226if (mpz(0)) { ok (0); } else { ok (1); }227if (mpz(123)) { ok (1); } else { ok (0); }228229#------------------------------------------------------------------------------230# GMP::Mpz::overload_com231232ok (~ mpz(0) == -1);233ok (~ mpz(1) == -2);234ok (~ mpz(-2) == 1);235ok (~ mpz(0xFF) == -0x100);236ok (~ mpz(-0x100) == 0xFF);237238#------------------------------------------------------------------------------239# GMP::Mpz::overload_dec240241{ my $a = mpz(0); ok ($a-- == 0); ok ($a == -1); }242{ my $a = mpz(0); ok (--$a == -1); }243244{ my $a = mpz(0); my $b = $a; $a--; ok ($a == -1); ok ($b == 0); }245246#------------------------------------------------------------------------------247# GMP::Mpz::overload_div248249ok (mpz(6) / 2 == 3);250ok (mpz(-6) / 2 == -3);251ok (mpz(6) / -2 == -3);252ok (mpz(-6) / -2 == 3);253254#------------------------------------------------------------------------------255# GMP::Mpz::overload_diveq256257{ my $a = mpz(21); $a /= 3; ok ($a == 7); }258{ my $a = mpz(21); my $b = $a; $a /= 3; ok ($a == 7); ok ($b == 21); }259260#------------------------------------------------------------------------------261# GMP::Mpz::overload_eq262263{ my $a = mpz(0);264my $b = $a;265$a = mpz(1);266ok ($a == 1);267ok ($b == 0); }268269#------------------------------------------------------------------------------270# GMP::Mpz::overload_inc271272{ my $a = mpz(0); ok ($a++ == 0); ok ($a == 1); }273{ my $a = mpz(0); ok (++$a == 1); }274275{ my $a = mpz(0); my $b = $a; $a++; ok ($a == 1); ok ($b == 0); }276277#------------------------------------------------------------------------------278# GMP::Mpz::overload_ior279280ok ((mpz(3) | 1) == 3);281ok ((mpz(3) | 4) == 7);282283{ my $a = mpz(3); $a |= 1; ok ($a == 3); }284{ my $a = mpz(3); $a |= 4; ok ($a == 7); }285286ok ((mpz("0xAA") | mpz("0x55")) == mpz("0xFF"));287288#------------------------------------------------------------------------------289# GMP::Mpz::overload_lshift290291{ my $a = mpz(7) << 1; ok ($a == 14); }292293#------------------------------------------------------------------------------294# GMP::Mpz::overload_lshifteq295296{ my $a = mpz(7); $a <<= 1; ok ($a == 14); }297{ my $a = mpz(7); my $b = $a; $a <<= 1; ok ($a == 14); ok ($b == 7); }298299#------------------------------------------------------------------------------300# GMP::Mpz::overload_mul301302ok (mpz(2) * 3 == 6);303304#------------------------------------------------------------------------------305# GMP::Mpz::overload_muleq306307{ my $a = mpz(7); $a *= 3; ok ($a == 21); }308{ my $a = mpz(7); my $b = $a; $a *= 3; ok ($a == 21); ok ($b == 7); }309310#------------------------------------------------------------------------------311# GMP::Mpz::overload_neg312313ok (- mpz(0) == 0);314ok (- mpz(123) == -123);315ok (- mpz(-123) == 123);316317#------------------------------------------------------------------------------318# GMP::Mpz::overload_not319320if (not mpz(0)) { ok (1); } else { ok (0); }321if (not mpz(123)) { ok (0); } else { ok (1); }322323ok ((! mpz(0)) == 1);324ok ((! mpz(123)) == 0);325326#------------------------------------------------------------------------------327# GMP::Mpz::overload_pow328329ok (mpz(0) ** 1 == 0);330ok (mpz(1) ** 1 == 1);331ok (mpz(2) ** 0 == 1);332ok (mpz(2) ** 1 == 2);333ok (mpz(2) ** 2 == 4);334ok (mpz(2) ** 3 == 8);335ok (mpz(2) ** 4 == 16);336337ok (mpz(0) ** mpz(1) == 0);338ok (mpz(1) ** mpz(1) == 1);339ok (mpz(2) ** mpz(0) == 1);340ok (mpz(2) ** mpz(1) == 2);341ok (mpz(2) ** mpz(2) == 4);342ok (mpz(2) ** mpz(3) == 8);343ok (mpz(2) ** mpz(4) == 16);344345#------------------------------------------------------------------------------346# GMP::Mpz::overload_poweq347348{ my $a = mpz(3); $a **= 4; ok ($a == 81); }349{ my $a = mpz(3); my $b = $a; $a **= 4; ok ($a == 81); ok ($b == 3); }350351#------------------------------------------------------------------------------352# GMP::Mpz::overload_rem353354ok (mpz(-8) % 3 == -2);355ok (mpz(-7) % 3 == -1);356ok (mpz(-6) % 3 == 0);357ok (mpz(6) % 3 == 0);358ok (mpz(7) % 3 == 1);359ok (mpz(8) % 3 == 2);360361{ my $a = mpz(24); $a %= 7; ok ($a == 3); }362363#------------------------------------------------------------------------------364# GMP::Mpz::overload_rshift365366{ my $a = mpz(32) >> 1; ok ($a == 16); }367368#------------------------------------------------------------------------------369# GMP::Mpz::overload_rshifteq370371{ my $a = mpz(32); $a >>= 1; ok ($a == 16); }372{ my $a = mpz(32); my $b = $a; $a >>= 1; ok ($a == 16); ok ($b == 32); }373374#------------------------------------------------------------------------------375# GMP::Mpz::overload_spaceship376377ok (mpz(0) < 1);378ok (mpz(0) > -1);379380ok (mpz(0) != 1);381ok (mpz(0) != -1);382ok (mpz(1) != 0);383ok (mpz(1) != -1);384ok (mpz(-1) != 0);385ok (mpz(-1) != 1);386387ok (mpz(0) < 1.0);388ok (mpz(0) < '1');389ok (mpz(0) < substr('-1',1,1));390ok (mpz(0) < mpz(1));391ok (mpz(0) < mpq(1));392ok (mpz(0) < mpf(1));393ok (mpz(0) < $uv_max);394395#------------------------------------------------------------------------------396# GMP::Mpz::overload_sqrt397398ok (sqrt(mpz(0)) == 0);399ok (sqrt(mpz(1)) == 1);400ok (sqrt(mpz(4)) == 2);401ok (sqrt(mpz(81)) == 9);402403#------------------------------------------------------------------------------404# GMP::Mpz::overload_string405406{ my $x = mpz(0); ok("$x" eq "0"); }407{ my $x = mpz(123); ok("$x" eq "123"); }408{ my $x = mpz(-123); ok("$x" eq "-123"); }409410#------------------------------------------------------------------------------411# GMP::Mpz::overload_sub412413ok (mpz(0) - 1 == -1);414ok (mpz(1) - 1 == 0);415ok (1 - mpz(0) == 1);416ok (1 - mpz(1) == 0);417418#------------------------------------------------------------------------------419# GMP::Mpz::overload_subeq420421{ my $a = mpz(7); $a -= 1; ok ($a == 6); }422{ my $a = mpz(7); my $b = $a; $a -= 1; ok ($a == 6); ok ($b == 7); }423424#------------------------------------------------------------------------------425# GMP::Mpz::overload_xor426427ok ((mpz(3) ^ 1) == 2);428ok ((mpz(3) ^ 4) == 7);429430{ my $a = mpz(3); $a ^= 1; ok ($a == 2); }431{ my $a = mpz(3); $a ^= 4; ok ($a == 7); }432433434#------------------------------------------------------------------------------435# GMP::Mpz::bin436437ok (bin(2,0) == 1);438ok (bin(2,1) == 2);439ok (bin(2,2) == 1);440441ok (bin(3,0) == 1);442ok (bin(3,1) == 3);443ok (bin(3,2) == 3);444ok (bin(3,3) == 1);445446447#------------------------------------------------------------------------------448# GMP::Mpz::cdiv449450{ my ($q, $r);451($q, $r) = cdiv (16, 3);452ok ($q == 6);453ok ($r == -2);454($q, $r) = cdiv (16, -3);455ok ($q == -5);456ok ($r == 1);457($q, $r) = cdiv (-16, 3);458ok ($q == -5);459ok ($r == -1);460($q, $r) = cdiv (-16, -3);461ok ($q == 6);462ok ($r == 2);463}464465466#------------------------------------------------------------------------------467# GMP::Mpz::cdiv_2exp468469{ my ($q, $r);470($q, $r) = cdiv_2exp (23, 2);471ok ($q == 6);472ok ($r == -1);473($q, $r) = cdiv_2exp (-23, 2);474ok ($q == -5);475ok ($r == -3);476}477478479#------------------------------------------------------------------------------480# GMP::Mpz::clrbit481482{ my $a = mpz(3); clrbit ($a, 1); ok ($a == 1);483ok (UNIVERSAL::isa($a,"GMP::Mpz")); }484{ my $a = mpz(3); clrbit ($a, 2); ok ($a == 3);485ok (UNIVERSAL::isa($a,"GMP::Mpz")); }486487{ my $a = 3; clrbit ($a, 1); ok ($a == 1);488ok (UNIVERSAL::isa($a,"GMP::Mpz")); }489{ my $a = 3; clrbit ($a, 2); ok ($a == 3);490ok (UNIVERSAL::isa($a,"GMP::Mpz")); }491492# mutate only given variable493{ my $a = mpz(3);494my $b = $a;495clrbit ($a, 0);496ok ($a == 2);497ok ($b == 3);498}499{ my $a = 3;500my $b = $a;501clrbit ($a, 0);502ok ($a == 2);503ok ($b == 3);504}505506{ tie my $a, 'Mytie', mpz(3);507clrbit ($a, 1);508ok ($Mytie::fetched > 0); # used fetch509ok ($Mytie::stored > 0); # used store510ok ($a == 1); # expected result511ok (UNIVERSAL::isa($a,"GMP::Mpz"));512ok (tied($a)); # still tied513}514{ tie my $a, 'Mytie', 3;515clrbit ($a, 1);516ok ($Mytie::fetched > 0); # used fetch517ok ($Mytie::stored > 0); # used store518ok ($a == 1); # expected result519ok (UNIVERSAL::isa($a,"GMP::Mpz"));520ok (tied($a)); # still tied521}522523{ my $b = mpz(3);524tie my $a, 'Mytie', $b;525clrbit ($a, 0);526ok ($a == 2);527ok ($b == 3);528ok (tied($a));529}530{ my $b = 3;531tie my $a, 'Mytie', $b;532clrbit ($a, 0);533ok ($a == 2);534ok ($b == 3);535ok (tied($a));536}537538#------------------------------------------------------------------------------539# GMP::Mpz::combit540541{ my $a = mpz(3); combit ($a, 1); ok ($a == 1);542ok (UNIVERSAL::isa($a,"GMP::Mpz")); }543{ my $a = mpz(3); combit ($a, 2); ok ($a == 7);544ok (UNIVERSAL::isa($a,"GMP::Mpz")); }545546{ my $a = 3; combit ($a, 1); ok ($a == 1);547ok (UNIVERSAL::isa($a,"GMP::Mpz")); }548{ my $a = 3; combit ($a, 2); ok ($a == 7);549ok (UNIVERSAL::isa($a,"GMP::Mpz")); }550551# mutate only given variable552{ my $a = mpz(3);553my $b = $a;554combit ($a, 0);555ok ($a == 2);556ok ($b == 3);557}558{ my $a = 3;559my $b = $a;560combit ($a, 0);561ok ($a == 2);562ok ($b == 3);563}564565{ tie my $a, 'Mytie', mpz(3);566combit ($a, 2);567ok ($Mytie::fetched > 0); # used fetch568ok ($Mytie::stored > 0); # used store569ok ($a == 7); # expected result570ok (UNIVERSAL::isa($a,"GMP::Mpz"));571ok (tied($a)); # still tied572}573{ tie my $a, 'Mytie', 3;574combit ($a, 2);575ok ($Mytie::fetched > 0); # used fetch576ok ($Mytie::stored > 0); # used store577ok ($a == 7); # expected result578ok (UNIVERSAL::isa($a,"GMP::Mpz"));579ok (tied($a)); # still tied580}581582{ my $b = mpz(3);583tie my $a, 'Mytie', $b;584combit ($a, 0);585ok ($a == 2);586ok ($b == 3);587ok (tied($a));588}589{ my $b = 3;590tie my $a, 'Mytie', $b;591combit ($a, 0);592ok ($a == 2);593ok ($b == 3);594ok (tied($a));595}596597#------------------------------------------------------------------------------598# GMP::Mpz::congruent_p599600ok ( congruent_p (21, 0, 7));601ok (! congruent_p (21, 1, 7));602ok ( congruent_p (21, 5, 8));603ok (! congruent_p (21, 6, 8));604605606#------------------------------------------------------------------------------607# GMP::Mpz::congruent_2exp_p608609ok ( congruent_2exp_p (20, 0, 2));610ok (! congruent_2exp_p (21, 0, 2));611ok (! congruent_2exp_p (20, 1, 2));612613#------------------------------------------------------------------------------614# GMP::Mpz::divexact615616ok (divexact(27,3) == 9);617ok (divexact(27,-3) == -9);618ok (divexact(-27,3) == -9);619ok (divexact(-27,-3) == 9);620621#------------------------------------------------------------------------------622# GMP::Mpz::divisible_p623624ok ( divisible_p (21, 7));625ok (! divisible_p (21, 8));626627#------------------------------------------------------------------------------628# GMP::Mpz::divisible_2exp_p629630ok ( divisible_2exp_p (20, 2));631ok (! divisible_2exp_p (21, 2));632633#------------------------------------------------------------------------------634# GMP::Mpz::even_p635636ok (! even_p(mpz(-3)));637ok ( even_p(mpz(-2)));638ok (! even_p(mpz(-1)));639ok ( even_p(mpz(0)));640ok (! even_p(mpz(1)));641ok ( even_p(mpz(2)));642ok (! even_p(mpz(3)));643644#------------------------------------------------------------------------------645# GMP::Mpz::export646647{ my $s = mpz_export (1, 2, 1, 0, "0x61626364");648ok ($s eq 'abcd'); }649{ my $s = mpz_export (-1, 2, 1, 0, "0x61626364");650ok ($s eq 'cdab'); }651{ my $s = mpz_export (1, 2, -1, 0, "0x61626364");652ok ($s eq 'badc'); }653{ my $s = mpz_export (-1, 2, -1, 0, "0x61626364");654ok ($s eq 'dcba'); }655656#------------------------------------------------------------------------------657# GMP::Mpz::fac658659ok (fac(0) == 1);660ok (fac(1) == 1);661ok (fac(2) == 2);662ok (fac(3) == 6);663ok (fac(4) == 24);664ok (fac(5) == 120);665666#------------------------------------------------------------------------------667# GMP::Mpz::fdiv668669{ my ($q, $r);670($q, $r) = fdiv (16, 3);671ok ($q == 5);672ok ($r == 1);673($q, $r) = fdiv (16, -3);674ok ($q == -6);675ok ($r == -2);676($q, $r) = fdiv (-16, 3);677ok ($q == -6);678ok ($r == 2);679($q, $r) = fdiv (-16, -3);680ok ($q == 5);681ok ($r == -1);682}683684#------------------------------------------------------------------------------685# GMP::Mpz::fdiv_2exp686687{ my ($q, $r);688($q, $r) = fdiv_2exp (23, 2);689ok ($q == 5);690ok ($r == 3);691($q, $r) = fdiv_2exp (-23, 2);692ok ($q == -6);693ok ($r == 1);694}695696#------------------------------------------------------------------------------697# GMP::Mpz::fib698699ok (fib(0) == 0);700ok (fib(1) == 1);701ok (fib(2) == 1);702ok (fib(3) == 2);703ok (fib(4) == 3);704ok (fib(5) == 5);705ok (fib(6) == 8);706707#------------------------------------------------------------------------------708# GMP::Mpz::fib2709710{ my ($a, $b) = fib2(0); ok($a==0); ok($b==1); }711{ my ($a, $b) = fib2(1); ok($a==1); ok($b==0); }712{ my ($a, $b) = fib2(2); ok($a==1); ok($b==1); }713{ my ($a, $b) = fib2(3); ok($a==2); ok($b==1); }714{ my ($a, $b) = fib2(4); ok($a==3); ok($b==2); }715{ my ($a, $b) = fib2(5); ok($a==5); ok($b==3); }716{ my ($a, $b) = fib2(6); ok($a==8); ok($b==5); }717718#------------------------------------------------------------------------------719# GMP::Mpz::gcd720721ok (gcd (21) == 21);722ok (gcd (21,15) == 3);723ok (gcd (21,15,30,57) == 3);724ok (gcd (21,-15) == 3);725ok (gcd (-21,15) == 3);726ok (gcd (-21,-15) == 3);727728#------------------------------------------------------------------------------729# GMP::Mpz::gcdext730731{732my ($g, $x, $y) = gcdext (3,5);733ok ($g == 1);734ok ($x == 2);735ok ($y == -1);736}737738#------------------------------------------------------------------------------739# GMP::Mpz::hamdist740741ok (hamdist(5,7) == 1);742743#------------------------------------------------------------------------------744# GMP::Mpz::import745746{ my $z = mpz_import (1, 2, 1, 0, 'abcd');747ok ($z == 0x61626364); }748{ my $z = mpz_import (-1, 2, 1, 0, 'abcd');749ok ($z == 0x63646162); }750{ my $z = mpz_import (1, 2, -1, 0, 'abcd');751ok ($z == 0x62616463); }752{ my $z = mpz_import (-1, 2, -1, 0, 'abcd');753ok ($z == 0x64636261); }754755#------------------------------------------------------------------------------756# GMP::Mpz::invert757758ok (invert(1,123) == 1);759ok (invert(6,7) == 6);760ok (! defined invert(2,8));761762#------------------------------------------------------------------------------763# GMP::Mpz::jacobi, GMP::Mpz::kronecker764765foreach my $i ([ 1, 19, 1 ],766[ 4, 19, 1 ],767[ 5, 19, 1 ],768[ 6, 19, 1 ],769[ 7, 19, 1 ],770[ 9, 19, 1 ],771[ 11, 19, 1 ],772[ 16, 19, 1 ],773[ 17, 19, 1 ],774[ 2, 19, -1 ],775[ 3, 19, -1 ],776[ 8, 19, -1 ],777[ 10, 19, -1 ],778[ 12, 19, -1 ],779[ 13, 19, -1 ],780[ 14, 19, -1 ],781[ 15, 19, -1 ],782[ 18, 19, -1 ]) {783foreach my $fun (\&jacobi, \&kronecker) {784ok (&$fun ($$i[0], $$i[1]) == $$i[2]);785786ok (&$fun ($$i[0], str($$i[1])) == $$i[2]);787ok (&$fun (str($$i[0]), $$i[1]) == $$i[2]);788ok (&$fun (str($$i[0]), str($$i[1])) == $$i[2]);789790ok (&$fun ($$i[0], mpz($$i[1])) == $$i[2]);791ok (&$fun (mpz($$i[0]), $$i[1]) == $$i[2]);792ok (&$fun (mpz($$i[0]), mpz($$i[1])) == $$i[2]);793}794}795796#------------------------------------------------------------------------------797# GMP::Mpz::lcm798799ok (lcm (2) == 2);800ok (lcm (0) == 0);801ok (lcm (0,0) == 0);802ok (lcm (0,0,0) == 0);803ok (lcm (0,0,0,0) == 0);804ok (lcm (2,0) == 0);805ok (lcm (-2,0) == 0);806ok (lcm (2,3) == 6);807ok (lcm (2,3,4) == 12);808ok (lcm (2,-3) == 6);809ok (lcm (-2,3) == 6);810ok (lcm (-2,-3) == 6);811ok (lcm (mpz(2)**512,1) == mpz(2)**512);812ok (lcm (mpz(2)**512,-1) == mpz(2)**512);813ok (lcm (-mpz(2)**512,1) == mpz(2)**512);814ok (lcm (-mpz(2)**512,-1) == mpz(2)**512);815ok (lcm (mpz(2)**512,mpz(2)**512) == mpz(2)**512);816ok (lcm (mpz(2)**512,-mpz(2)**512) == mpz(2)**512);817ok (lcm (-mpz(2)**512,mpz(2)**512) == mpz(2)**512);818ok (lcm (-mpz(2)**512,-mpz(2)**512) == mpz(2)**512);819820#------------------------------------------------------------------------------821# GMP::Mpz::lucnum822823ok (lucnum(0) == 2);824ok (lucnum(1) == 1);825ok (lucnum(2) == 3);826ok (lucnum(3) == 4);827ok (lucnum(4) == 7);828ok (lucnum(5) == 11);829ok (lucnum(6) == 18);830831#------------------------------------------------------------------------------832# GMP::Mpz::lucnum2833834{ my ($a, $b) = lucnum2(0); ok($a==2); ok($b==-1); }835{ my ($a, $b) = lucnum2(1); ok($a==1); ok($b==2); }836{ my ($a, $b) = lucnum2(2); ok($a==3); ok($b==1); }837{ my ($a, $b) = lucnum2(3); ok($a==4); ok($b==3); }838{ my ($a, $b) = lucnum2(4); ok($a==7); ok($b==4); }839{ my ($a, $b) = lucnum2(5); ok($a==11); ok($b==7); }840{ my ($a, $b) = lucnum2(6); ok($a==18); ok($b==11); }841842#------------------------------------------------------------------------------843# GMP::Mpz::nextprime844845ok (nextprime(2) == 3);846ok (nextprime(3) == 5);847ok (nextprime(5) == 7);848ok (nextprime(7) == 11);849ok (nextprime(11) == 13);850851#------------------------------------------------------------------------------852# GMP::Mpz::perfect_power_p853854# ok ( perfect_power_p(mpz(-27)));855# ok (! perfect_power_p(mpz(-9)));856# ok (! perfect_power_p(mpz(-1)));857ok ( perfect_power_p(mpz(0)));858ok ( perfect_power_p(mpz(1)));859ok (! perfect_power_p(mpz(2)));860ok (! perfect_power_p(mpz(3)));861ok ( perfect_power_p(mpz(4)));862ok ( perfect_power_p(mpz(9)));863ok ( perfect_power_p(mpz(27)));864ok ( perfect_power_p(mpz(81)));865866#------------------------------------------------------------------------------867# GMP::Mpz::perfect_square_p868869ok (! perfect_square_p(mpz(-9)));870ok (! perfect_square_p(mpz(-1)));871ok ( perfect_square_p(mpz(0)));872ok ( perfect_square_p(mpz(1)));873ok (! perfect_square_p(mpz(2)));874ok (! perfect_square_p(mpz(3)));875ok ( perfect_square_p(mpz(4)));876ok ( perfect_square_p(mpz(9)));877ok (! perfect_square_p(mpz(27)));878ok ( perfect_square_p(mpz(81)));879880#------------------------------------------------------------------------------881# GMP::Mpz::popcount882883ok (popcount(7) == 3);884885#------------------------------------------------------------------------------886# GMP::Mpz::powm887888ok (powm (3,2,8) == 1);889890#------------------------------------------------------------------------------891# GMP::Mpz::probab_prime_p892893ok ( probab_prime_p(89,1));894ok (! probab_prime_p(81,1));895896#------------------------------------------------------------------------------897# GMP::Mpz::realloc898899{ my $z = mpz(123);900realloc ($z, 512); }901902#------------------------------------------------------------------------------903# GMP::Mpz::remove904905{906my ($rem, $mult);907($rem, $mult) = remove(12,3);908ok ($rem == 4);909ok ($mult == 1);910($rem, $mult) = remove(12,2);911ok ($rem == 3);912ok ($mult == 2);913}914915#------------------------------------------------------------------------------916# GMP::Mpz::root917918ok (root(0,2) == 0);919ok (root(8,3) == 2);920ok (root(-8,3) == -2);921ok (root(81,4) == 3);922ok (root(243,5) == 3);923924#------------------------------------------------------------------------------925# GMP::Mpz::roote926927{ my ($r,$e);928($r, $e) = roote(0,2);929ok ($r == 0);930ok ($e);931($r, $e) = roote(81,4);932ok ($r == 3);933ok ($e);934($r, $e) = roote(85,4);935ok ($r == 3);936ok (! $e);937}938939#------------------------------------------------------------------------------940# GMP::Mpz::rootrem941942{ my ($root, $rem) = rootrem (mpz(0), 1);943ok ($root == 0); ok ($rem == 0); }944{ my ($root, $rem) = rootrem (mpz(0), 2);945ok ($root == 0); ok ($rem == 0); }946{ my ($root, $rem) = rootrem (mpz(64), 2);947ok ($root == 8); ok ($rem == 0); }948{ my ($root, $rem) = rootrem (mpz(64), 3);949ok ($root == 4); ok ($rem == 0); }950{ my ($root, $rem) = rootrem (mpz(65), 3);951ok ($root == 4); ok ($rem == 1); }952953#------------------------------------------------------------------------------954# GMP::Mpz::scan0955956ok (scan0 (0, 0) == 0);957ok (scan0 (1, 0) == 1);958ok (scan0 (3, 0) == 2);959ok (scan0 (-1, 0) == ~0);960ok (scan0 (-2, 1) == ~0);961962#------------------------------------------------------------------------------963# GMP::Mpz::scan1964965ok (scan1 (1, 0) == 0);966ok (scan1 (2, 0) == 1);967ok (scan1 (4, 0) == 2);968ok (scan1 (0, 0) == ~0);969ok (scan1 (3, 2) == ~0);970971#------------------------------------------------------------------------------972# GMP::Mpz::setbit973974{ my $a = mpz(3); setbit ($a, 1); ok ($a == 3); }975{ my $a = mpz(3); setbit ($a, 2); ok ($a == 7); }976977{ my $a = 3; setbit ($a, 1); ok ($a == 3); }978{ my $a = 3; setbit ($a, 2); ok ($a == 7); }979980# mutate only given variable981{ my $a = mpz(0);982my $b = $a;983setbit ($a, 0);984ok ($a == 1);985ok ($b == 0);986}987{ my $a = 0;988my $b = $a;989setbit ($a, 0);990ok ($a == 1);991ok ($b == 0);992}993994{ tie my $a, 'Mytie', mpz(3);995setbit ($a, 2);996ok ($Mytie::fetched > 0); # used fetch997ok ($Mytie::stored > 0); # used store998ok ($a == 7); # expected result999ok (UNIVERSAL::isa($a,"GMP::Mpz"));1000ok (tied($a)); # still tied1001}1002{ tie my $a, 'Mytie', 3;1003setbit ($a, 2);1004ok ($Mytie::fetched > 0); # used fetch1005ok ($Mytie::stored > 0); # used store1006ok ($a == 7); # expected result1007ok (UNIVERSAL::isa($a,"GMP::Mpz"));1008ok (tied($a)); # still tied1009}10101011{ my $b = mpz(2);1012tie my $a, 'Mytie', $b;1013setbit ($a, 0);1014ok ($a == 3);1015ok ($b == 2);1016ok (tied($a));1017}1018{ my $b = 2;1019tie my $a, 'Mytie', $b;1020setbit ($a, 0);1021ok ($a == 3);1022ok ($b == 2);1023ok (tied($a));1024}10251026#------------------------------------------------------------------------------1027# GMP::Mpz::sizeinbase10281029ok (sizeinbase(1,10) == 1);1030ok (sizeinbase(100,10) == 3);1031ok (sizeinbase(9999,10) == 5);10321033#------------------------------------------------------------------------------1034# GMP::Mpz::sqrtrem10351036{1037my ($root, $rem) = sqrtrem(mpz(0));1038ok ($root == 0);1039ok ($rem == 0);1040}1041{1042my ($root, $rem) = sqrtrem(mpz(1));1043ok ($root == 1);1044ok ($rem == 0);1045}1046{1047my ($root, $rem) = sqrtrem(mpz(2));1048ok ($root == 1);1049ok ($rem == 1);1050}1051{1052my ($root, $rem) = sqrtrem(mpz(9));1053ok ($root == 3);1054ok ($rem == 0);1055}1056{1057my ($root, $rem) = sqrtrem(mpz(35));1058ok ($root == 5);1059ok ($rem == 10);1060}1061{1062my ($root, $rem) = sqrtrem(mpz(0));1063ok ($root == 0);1064ok ($rem == 0);1065}10661067#------------------------------------------------------------------------------1068# GMP::Mpz::tdiv10691070{ my ($q, $r);1071($q, $r) = tdiv (16, 3);1072ok ($q == 5);1073ok ($r == 1);1074($q, $r) = tdiv (16, -3);1075ok ($q == -5);1076ok ($r == 1);1077($q, $r) = tdiv (-16, 3);1078ok ($q == -5);1079ok ($r == -1);1080($q, $r) = tdiv (-16, -3);1081ok ($q == 5);1082ok ($r == -1);1083}10841085#------------------------------------------------------------------------------1086# GMP::Mpz::tdiv_2exp10871088{ my ($q, $r);1089($q, $r) = tdiv_2exp (23, 2);1090ok ($q == 5);1091ok ($r == 3);1092($q, $r) = tdiv_2exp (-23, 2);1093ok ($q == -5);1094ok ($r == -3);1095}10961097#------------------------------------------------------------------------------1098# GMP::Mpz::tstbit10991100ok (tstbit (6, 0) == 0);1101ok (tstbit (6, 1) == 1);1102ok (tstbit (6, 2) == 1);1103ok (tstbit (6, 3) == 0);11041105110611071108#------------------------------------------------------------------------------1109# GMP::Mpq11101111#------------------------------------------------------------------------------1112# GMP::Mpq::new11131114ok (mpq(0) == 0);1115ok (mpq('0') == 0);1116ok (mpq(substr('101',1,1)) == 0);1117ok (mpq(0.0) == 0);1118ok (mpq(mpz(0)) == 0);1119ok (mpq(mpq(0)) == 0);1120ok (mpq(mpf(0)) == 0);11211122{ tie my $t, 'Mytie', 0; ok (mpq($t) == 0); }1123{ tie my $t, 'Mytie', '0'; ok (mpq($t) == 0); }1124{ tie my $t, 'Mytie', substr('101',1,1); ok (mpq($t) == 0); }1125{ tie my $t, 'Mytie', 0.0; ok (mpq($t) == 0); }1126{ tie my $t, 'Mytie', mpz(0); ok (mpq($t) == 0); }1127{ tie my $t, 'Mytie', mpq(0); ok (mpq($t) == 0); }1128{ tie my $t, 'Mytie', mpf(0); ok (mpq($t) == 0); }11291130ok (mpq(-123) == -123);1131ok (mpq('-123') == -123);1132ok (mpq(substr('1-1231',1,4)) == -123);1133ok (mpq(-123.0) == -123);1134ok (mpq(mpz(-123)) == -123);1135ok (mpq(mpq(-123)) == -123);1136ok (mpq(mpf(-123)) == -123);11371138{ tie my $t, 'Mytie', -123; ok (mpq($t) == -123); }1139{ tie my $t, 'Mytie', '-123'; ok (mpq($t) == -123); }1140{ tie my $t, 'Mytie', substr('1-1231',1,4); ok (mpq($t) == -123); }1141{ tie my $t, 'Mytie', -123.0; ok (mpq($t) == -123); }1142{ tie my $t, 'Mytie', mpz(-123); ok (mpq($t) == -123); }1143{ tie my $t, 'Mytie', mpq(-123); ok (mpq($t) == -123); }1144{ tie my $t, 'Mytie', mpf(-123); ok (mpq($t) == -123); }11451146ok (mpq($ivnv_2p128) == $str_2p128);1147{ tie my $t, 'Mytie', $ivnv_2p128; ok (mpq($t) == $str_2p128); }11481149ok (mpq('3/2') == mpq(3,2));1150ok (mpq('3/1') == mpq(3,1));1151ok (mpq('-3/2') == mpq(-3,2));1152ok (mpq('-3/1') == mpq(-3,1));1153ok (mpq('0x3') == mpq(3,1));1154ok (mpq('0b111') == mpq(7,1));1155ok (mpq('0b0') == mpq(0,1));11561157ok (mpq($uv_max) > 0);1158ok (mpq($uv_max) == mpq($uv_max_str));1159{ tie my $t, 'Mytie', $uv_max; ok (mpq($t) > 0); }1160{ tie my $t, 'Mytie', $uv_max; ok (mpq($t) == mpq($uv_max_str)); }11611162{ my $x = 123.5;1163kill (0, $x);1164ok (mpq($x) == 123.5);1165tie my $t, 'Mytie', $x;1166ok (mpq($t) == 123.5);1167}11681169#------------------------------------------------------------------------------1170# GMP::Mpq::overload_abs11711172ok (abs(mpq(0)) == 0);1173ok (abs(mpq(123)) == 123);1174ok (abs(mpq(-123)) == 123);11751176{ my $x = mpq(-123); $x = abs($x); ok ($x == 123); }1177{ my $x = mpq(0); $x = abs($x); ok ($x == 0); }1178{ my $x = mpq(123); $x = abs($x); ok ($x == 123); }11791180{ tie my $t, 'Mytie', mpq(0); ok (abs($t) == 0); }1181{ tie my $t, 'Mytie', mpq(123); ok (abs($t) == 123); }1182{ tie my $t, 'Mytie', mpq(-123); ok (abs($t) == 123); }11831184#------------------------------------------------------------------------------1185# GMP::Mpq::overload_add11861187ok (mpq(0) + 1 == 1);1188ok (mpq(-1) + 1 == 0);1189ok (1 + mpq(0) == 1);1190ok (1 + mpq(-1) == 0);11911192ok (mpq(1,2)+mpq(1,3) == mpq(5,6));1193ok (mpq(1,2)+mpq(-1,3) == mpq(1,6));1194ok (mpq(-1,2)+mpq(1,3) == mpq(-1,6));1195ok (mpq(-1,2)+mpq(-1,3) == mpq(-5,6));11961197#------------------------------------------------------------------------------1198# GMP::Mpq::overload_addeq11991200{ my $a = mpq(7); $a += 1; ok ($a == 8); }1201{ my $a = mpq(7); my $b = $a; $a += 1; ok ($a == 8); ok ($b == 7); }12021203#------------------------------------------------------------------------------1204# GMP::Mpq::overload_bool12051206if (mpq(0)) { ok (0); } else { ok (1); }1207if (mpq(123)) { ok (1); } else { ok (0); }12081209#------------------------------------------------------------------------------1210# GMP::Mpq::overload_dec12111212{ my $a = mpq(0); ok ($a-- == 0); ok ($a == -1); }1213{ my $a = mpq(0); ok (--$a == -1); }12141215{ my $a = mpq(0); my $b = $a; $a--; ok ($a == -1); ok ($b == 0); }12161217#------------------------------------------------------------------------------1218# GMP::Mpq::overload_div12191220ok (mpq(6) / 2 == 3);1221ok (mpq(-6) / 2 == -3);1222ok (mpq(6) / -2 == -3);1223ok (mpq(-6) / -2 == 3);12241225#------------------------------------------------------------------------------1226# GMP::Mpq::overload_diveq12271228{ my $a = mpq(21); $a /= 3; ok ($a == 7); }1229{ my $a = mpq(21); my $b = $a; $a /= 3; ok ($a == 7); ok ($b == 21); }12301231#------------------------------------------------------------------------------1232# GMP::Mpq::overload_eq12331234{ my $a = mpq(0);1235my $b = $a;1236$a = mpq(1);1237ok ($a == 1);1238ok ($b == 0); }12391240#------------------------------------------------------------------------------1241# GMP::Mpq::overload_inc12421243{ my $a = mpq(0); ok ($a++ == 0); ok ($a == 1); }1244{ my $a = mpq(0); ok (++$a == 1); }12451246{ my $a = mpq(0); my $b = $a; $a++; ok ($a == 1); ok ($b == 0); }12471248#------------------------------------------------------------------------------1249# GMP::Mpq::overload_lshift12501251{ my $a = mpq(7) << 1; ok ($a == 14); }12521253#------------------------------------------------------------------------------1254# GMP::Mpq::overload_lshifteq12551256{ my $a = mpq(7); $a <<= 1; ok ($a == 14); }1257{ my $a = mpq(7); my $b = $a; $a <<= 1; ok ($a == 14); ok ($b == 7); }12581259#------------------------------------------------------------------------------1260# GMP::Mpq::overload_mul12611262ok (mpq(2) * 3 == 6);12631264#------------------------------------------------------------------------------1265# GMP::Mpq::overload_muleq12661267{ my $a = mpq(7); $a *= 3; ok ($a == 21); }1268{ my $a = mpq(7); my $b = $a; $a *= 3; ok ($a == 21); ok ($b == 7); }12691270#------------------------------------------------------------------------------1271# GMP::Mpq::overload_neg12721273ok (- mpq(0) == 0);1274ok (- mpq(123) == -123);1275ok (- mpq(-123) == 123);12761277#------------------------------------------------------------------------------1278# GMP::Mpq::overload_not12791280if (not mpq(0)) { ok (1); } else { ok (0); }1281if (not mpq(123)) { ok (0); } else { ok (1); }12821283ok ((! mpq(0)) == 1);1284ok ((! mpq(123)) == 0);12851286#------------------------------------------------------------------------------1287# GMP::Mpq::overload_pow12881289ok (mpq(0) ** 1 == 0);1290ok (mpq(1) ** 1 == 1);1291ok (mpq(2) ** 0 == 1);1292ok (mpq(2) ** 1 == 2);1293ok (mpq(2) ** 2 == 4);1294ok (mpq(2) ** 3 == 8);1295ok (mpq(2) ** 4 == 16);12961297ok (mpq(0) ** mpq(1) == 0);1298ok (mpq(1) ** mpq(1) == 1);1299ok (mpq(2) ** mpq(0) == 1);1300ok (mpq(2) ** mpq(1) == 2);1301ok (mpq(2) ** mpq(2) == 4);1302ok (mpq(2) ** mpq(3) == 8);1303ok (mpq(2) ** mpq(4) == 16);13041305#------------------------------------------------------------------------------1306# GMP::Mpq::overload_poweq13071308{ my $a = mpq(3); $a **= 4; ok ($a == 81); }1309{ my $a = mpq(3); my $b = $a; $a **= 4; ok ($a == 81); ok ($b == 3); }13101311#------------------------------------------------------------------------------1312# GMP::Mpq::overload_rshift13131314{ my $a = mpq(32) >> 1; ok ($a == 16); }13151316#------------------------------------------------------------------------------1317# GMP::Mpq::overload_rshifteq13181319{ my $a = mpq(32); $a >>= 1; ok ($a == 16); }1320{ my $a = mpq(32); my $b = $a; $a >>= 1; ok ($a == 16); ok ($b == 32); }13211322#------------------------------------------------------------------------------1323# GMP::Mpq::overload_spaceship13241325ok (mpq(0) < 1);1326ok (mpq(0) > -1);13271328ok (mpq(0) != 1);1329ok (mpq(0) != -1);1330ok (mpq(1) != 0);1331ok (mpq(1) != -1);1332ok (mpq(-1) != 0);1333ok (mpq(-1) != 1);13341335ok (mpq(3,2) > 1);1336ok (mpq(3,2) < 2);13371338ok (mpq(0) < 1.0);1339ok (mpq(0) < '1');1340ok (mpq(0) < substr('-1',1,1));1341ok (mpq(0) < mpz(1));1342ok (mpq(0) < mpq(1));1343ok (mpq(0) < mpf(1));1344ok (mpq(0) < $uv_max);13451346#------------------------------------------------------------------------------1347# GMP::Mpq::overload_string13481349{ my $x = mpq(0); ok("$x" eq "0"); }1350{ my $x = mpq(123); ok("$x" eq "123"); }1351{ my $x = mpq(-123); ok("$x" eq "-123"); }13521353{ my $q = mpq(5,7); ok("$q" eq "5/7"); }1354{ my $q = mpq(-5,7); ok("$q" eq "-5/7"); }13551356#------------------------------------------------------------------------------1357# GMP::Mpq::overload_sub13581359ok (mpq(0) - 1 == -1);1360ok (mpq(1) - 1 == 0);1361ok (1 - mpq(0) == 1);1362ok (1 - mpq(1) == 0);13631364ok (mpq(1,2)-mpq(1,3) == mpq(1,6));1365ok (mpq(1,2)-mpq(-1,3) == mpq(5,6));1366ok (mpq(-1,2)-mpq(1,3) == mpq(-5,6));1367ok (mpq(-1,2)-mpq(-1,3) == mpq(-1,6));13681369#------------------------------------------------------------------------------1370# GMP::Mpq::overload_subeq13711372{ my $a = mpq(7); $a -= 1; ok ($a == 6); }1373{ my $a = mpq(7); my $b = $a; $a -= 1; ok ($a == 6); ok ($b == 7); }13741375#------------------------------------------------------------------------------1376# GMP::Mpq::canonicalize13771378{ my $q = mpq(21,15); canonicalize($q);1379ok (num($q) == 7);1380ok (den($q) == 5);1381}13821383#------------------------------------------------------------------------------1384# GMP::Mpq::den13851386{ my $q = mpq(5,9); ok (den($q) == 9); }13871388#------------------------------------------------------------------------------1389# GMP::Mpq::num13901391{ my $q = mpq(5,9); ok (num($q) == 5); }13921393139413951396#------------------------------------------------------------------------------1397# GMP::Mpf13981399#------------------------------------------------------------------------------1400# GMP::Mpf::new14011402ok (mpf(0) == 0);1403ok (mpf('0') == 0);1404ok (mpf(substr('101',1,1)) == 0);1405ok (mpf(0.0) == 0);1406ok (mpf(mpz(0)) == 0);1407ok (mpf(mpq(0)) == 0);1408ok (mpf(mpf(0)) == 0);14091410{ tie my $t, 'Mytie', 0; ok (mpf($t) == 0); }1411{ tie my $t, 'Mytie', '0'; ok (mpf($t) == 0); }1412{ tie my $t, 'Mytie', substr('101',1,1); ok (mpf($t) == 0); }1413{ tie my $t, 'Mytie', 0.0; ok (mpf($t) == 0); }1414{ tie my $t, 'Mytie', mpz(0); ok (mpf($t) == 0); }1415{ tie my $t, 'Mytie', mpq(0); ok (mpf($t) == 0); }1416{ tie my $t, 'Mytie', mpf(0); ok (mpf($t) == 0); }14171418ok (mpf(-123) == -123);1419ok (mpf('-123') == -123);1420ok (mpf(substr('1-1231',1,4)) == -123);1421ok (mpf(-123.0) == -123);1422ok (mpf(mpz(-123)) == -123);1423ok (mpf(mpq(-123)) == -123);1424ok (mpf(mpf(-123)) == -123);14251426{ tie my $t, 'Mytie', -123; ok (mpf($t) == -123); }1427{ tie my $t, 'Mytie', '-123'; ok (mpf($t) == -123); }1428{ tie my $t, 'Mytie', substr('1-1231',1,4); ok (mpf($t) == -123); }1429{ tie my $t, 'Mytie', -123.0; ok (mpf($t) == -123); }1430{ tie my $t, 'Mytie', mpz(-123); ok (mpf($t) == -123); }1431{ tie my $t, 'Mytie', mpq(-123); ok (mpf($t) == -123); }1432{ tie my $t, 'Mytie', mpf(-123); ok (mpf($t) == -123); }14331434ok (mpf($ivnv_2p128) == $str_2p128);1435{ tie my $t, 'Mytie', $ivnv_2p128; ok (mpf($t) == $str_2p128); }14361437ok (mpf(-1.5) == -1.5);1438ok (mpf(-1.0) == -1.0);1439ok (mpf(-0.5) == -0.5);1440ok (mpf(0) == 0);1441ok (mpf(0.5) == 0.5);1442ok (mpf(1.0) == 1.0);1443ok (mpf(1.5) == 1.5);14441445ok (mpf("-1.5") == -1.5);1446ok (mpf("-1.0") == -1.0);1447ok (mpf("-0.5") == -0.5);1448ok (mpf("0") == 0);1449ok (mpf("0.5") == 0.5);1450ok (mpf("1.0") == 1.0);1451ok (mpf("1.5") == 1.5);14521453ok (mpf($uv_max) > 0);1454ok (mpf($uv_max) == mpf($uv_max_str));1455{ tie my $t, 'Mytie', $uv_max; ok (mpf($t) > 0); }1456{ tie my $t, 'Mytie', $uv_max; ok (mpf($t) == mpf($uv_max_str)); }14571458{ my $x = 123.5;1459kill (0, $x);1460ok (mpf($x) == 123.5);1461tie my $t, 'Mytie', $x;1462ok (mpf($t) == 123.5);1463}14641465#------------------------------------------------------------------------------1466# GMP::Mpf::overload_abs14671468ok (abs(mpf(0)) == 0);1469ok (abs(mpf(123)) == 123);1470ok (abs(mpf(-123)) == 123);14711472{ my $x = mpf(-123); $x = abs($x); ok ($x == 123); }1473{ my $x = mpf(0); $x = abs($x); ok ($x == 0); }1474{ my $x = mpf(123); $x = abs($x); ok ($x == 123); }14751476{ tie my $t, 'Mytie', mpf(0); ok (abs($t) == 0); }1477{ tie my $t, 'Mytie', mpf(123); ok (abs($t) == 123); }1478{ tie my $t, 'Mytie', mpf(-123); ok (abs($t) == 123); }14791480#------------------------------------------------------------------------------1481# GMP::Mpf::overload_add14821483ok (mpf(0) + 1 == 1);1484ok (mpf(-1) + 1 == 0);1485ok (1 + mpf(0) == 1);1486ok (1 + mpf(-1) == 0);14871488#------------------------------------------------------------------------------1489# GMP::Mpf::overload_addeq14901491{ my $a = mpf(7); $a += 1; ok ($a == 8); }1492{ my $a = mpf(7); my $b = $a; $a += 1; ok ($a == 8); ok ($b == 7); }14931494#------------------------------------------------------------------------------1495# GMP::Mpf::overload_bool14961497if (mpf(0)) { ok (0); } else { ok (1); }1498if (mpf(123)) { ok (1); } else { ok (0); }14991500#------------------------------------------------------------------------------1501# GMP::Mpf::overload_dec15021503{ my $a = mpf(0); ok ($a-- == 0); ok ($a == -1); }1504{ my $a = mpf(0); ok (--$a == -1); }15051506{ my $a = mpf(0); my $b = $a; $a--; ok ($a == -1); ok ($b == 0); }15071508#------------------------------------------------------------------------------1509# GMP::Mpf::overload_div15101511ok (mpf(6) / 2 == 3);1512ok (mpf(-6) / 2 == -3);1513ok (mpf(6) / -2 == -3);1514ok (mpf(-6) / -2 == 3);15151516#------------------------------------------------------------------------------1517# GMP::Mpf::overload_diveq15181519{ my $a = mpf(21); $a /= 3; ok ($a == 7); }1520{ my $a = mpf(21); my $b = $a; $a /= 3; ok ($a == 7); ok ($b == 21); }15211522#------------------------------------------------------------------------------1523# GMP::Mpf::overload_eq15241525{ my $a = mpf(0);1526my $b = $a;1527$a = mpf(1);1528ok ($a == 1);1529ok ($b == 0); }15301531#------------------------------------------------------------------------------1532# GMP::Mpf::overload_inc15331534{ my $a = mpf(0); ok ($a++ == 0); ok ($a == 1); }1535{ my $a = mpf(0); ok (++$a == 1); }15361537{ my $a = mpf(0); my $b = $a; $a++; ok ($a == 1); ok ($b == 0); }15381539#------------------------------------------------------------------------------1540# GMP::Mpf::overload_lshift15411542{ my $a = mpf(7) << 1; ok ($a == 14); }15431544#------------------------------------------------------------------------------1545# GMP::Mpf::overload_lshifteq15461547{ my $a = mpf(7); $a <<= 1; ok ($a == 14); }1548{ my $a = mpf(7); my $b = $a; $a <<= 1; ok ($a == 14); ok ($b == 7); }15491550#------------------------------------------------------------------------------1551# GMP::Mpf::overload_mul15521553ok (mpf(2) * 3 == 6);15541555#------------------------------------------------------------------------------1556# GMP::Mpf::overload_muleq15571558{ my $a = mpf(7); $a *= 3; ok ($a == 21); }1559{ my $a = mpf(7); my $b = $a; $a *= 3; ok ($a == 21); ok ($b == 7); }15601561#------------------------------------------------------------------------------1562# GMP::Mpf::overload_neg15631564ok (- mpf(0) == 0);1565ok (- mpf(123) == -123);1566ok (- mpf(-123) == 123);15671568#------------------------------------------------------------------------------1569# GMP::Mpf::overload_not15701571if (not mpf(0)) { ok (1); } else { ok (0); }1572if (not mpf(123)) { ok (0); } else { ok (1); }15731574ok ((! mpf(0)) == 1);1575ok ((! mpf(123)) == 0);15761577#------------------------------------------------------------------------------1578# GMP::Mpf::overload_pow15791580ok (mpf(0) ** 1 == 0);1581ok (mpf(1) ** 1 == 1);1582ok (mpf(2) ** 0 == 1);1583ok (mpf(2) ** 1 == 2);1584ok (mpf(2) ** 2 == 4);1585ok (mpf(2) ** 3 == 8);1586ok (mpf(2) ** 4 == 16);15871588ok (mpf(0) ** mpf(1) == 0);1589ok (mpf(1) ** mpf(1) == 1);1590ok (mpf(2) ** mpf(0) == 1);1591ok (mpf(2) ** mpf(1) == 2);1592ok (mpf(2) ** mpf(2) == 4);1593ok (mpf(2) ** mpf(3) == 8);1594ok (mpf(2) ** mpf(4) == 16);15951596#------------------------------------------------------------------------------1597# GMP::Mpf::overload_poweq15981599{ my $a = mpf(3); $a **= 4; ok ($a == 81); }1600{ my $a = mpf(3); my $b = $a; $a **= 4; ok ($a == 81); ok ($b == 3); }16011602#------------------------------------------------------------------------------1603# GMP::Mpf::overload_rshift16041605{ my $a = mpf(32) >> 1; ok ($a == 16); }16061607#------------------------------------------------------------------------------1608# GMP::Mpf::overload_rshifteq16091610{ my $a = mpf(32); $a >>= 1; ok ($a == 16); }1611{ my $a = mpf(32); my $b = $a; $a >>= 1; ok ($a == 16); ok ($b == 32); }16121613#------------------------------------------------------------------------------1614# GMP::Mpf::overload_sqrt16151616ok (sqrt(mpf(0)) == 0);1617ok (sqrt(mpf(1)) == 1);1618ok (sqrt(mpf(4)) == 2);1619ok (sqrt(mpf(81)) == 9);16201621ok (sqrt(mpf(0.25)) == 0.5);16221623#------------------------------------------------------------------------------1624# GMP::Mpf::overload_spaceship16251626ok (mpf(0) < 1);1627ok (mpf(0) > -1);16281629ok (mpf(0) != 1);1630ok (mpf(0) != -1);1631ok (mpf(1) != 0);1632ok (mpf(1) != -1);1633ok (mpf(-1) != 0);1634ok (mpf(-1) != 1);16351636ok (mpf(0) < 1.0);1637ok (mpf(0) < '1');1638ok (mpf(0) < substr('-1',1,1));1639ok (mpf(0) < mpz(1));1640ok (mpf(0) < mpq(1));1641ok (mpf(0) < mpf(1));1642ok (mpf(0) < $uv_max);16431644#------------------------------------------------------------------------------1645# GMP::Mpf::overload_string16461647{ my $x = mpf(0); ok ("$x" eq "0"); }1648{ my $x = mpf(123); ok ("$x" eq "123"); }1649{ my $x = mpf(-123); ok ("$x" eq "-123"); }16501651{ my $f = mpf(0.25); ok ("$f" eq "0.25"); }1652{ my $f = mpf(-0.25); ok ("$f" eq "-0.25"); }1653{ my $f = mpf(1.25); ok ("$f" eq "1.25"); }1654{ my $f = mpf(-1.25); ok ("$f" eq "-1.25"); }1655{ my $f = mpf(1000000); ok ("$f" eq "1000000"); }1656{ my $f = mpf(-1000000); ok ("$f" eq "-1000000"); }16571658#------------------------------------------------------------------------------1659# GMP::Mpf::overload_sub16601661ok (mpf(0) - 1 == -1);1662ok (mpf(1) - 1 == 0);1663ok (1 - mpf(0) == 1);1664ok (1 - mpf(1) == 0);16651666#------------------------------------------------------------------------------1667# GMP::Mpf::overload_subeq16681669{ my $a = mpf(7); $a -= 1; ok ($a == 6); }1670{ my $a = mpf(7); my $b = $a; $a -= 1; ok ($a == 6); ok ($b == 7); }167116721673#------------------------------------------------------------------------------1674# GMP::Mpf::ceil16751676ok (ceil (mpf(-7.5)) == -7.0);1677ok (ceil (mpf(7.5)) == 8.0);16781679#------------------------------------------------------------------------------1680# GMP::Mpf::floor16811682ok (floor(mpf(-7.5)) == -8.0);1683ok (floor(mpf(7.5)) == 7.0);16841685#------------------------------------------------------------------------------1686# GMP::Mpf::mpf_eq16871688{ my $old_prec = get_default_prec();1689set_default_prec(128);16901691ok ( mpf_eq (mpz("0x10000000000000001"), mpz("0x10000000000000002"), 1));1692ok (! mpf_eq (mpz("0x11"), mpz("0x12"), 128));16931694set_default_prec($old_prec);1695}16961697#------------------------------------------------------------------------------1698# GMP::Mpf::get_default_prec16991700get_default_prec();17011702#------------------------------------------------------------------------------1703# GMP::Mpf::get_prec17041705{ my $x = mpf(1.0, 512);1706ok (get_prec ($x) == 512);1707}17081709#------------------------------------------------------------------------------1710# GMP::Mpf::reldiff17111712ok (reldiff (2,4) == 1);1713ok (reldiff (4,2) == 0.5);17141715#------------------------------------------------------------------------------1716# GMP::Mpf::set_default_prec17171718{ my $old_prec = get_default_prec();17191720set_default_prec(512);1721ok (get_default_prec () == 512);17221723set_default_prec($old_prec);1724}17251726#------------------------------------------------------------------------------1727# GMP::Mpf::set_prec17281729{ my $x = mpf(1.0, 512);1730my $y = $x;1731set_prec ($x, 1024);1732ok (get_prec ($x) == 1024);1733ok (get_prec ($y) == 512);1734}17351736#------------------------------------------------------------------------------1737# GMP::Mpf::trunc17381739ok (trunc(mpf(-7.5)) == -7.0);1740ok (trunc(mpf(7.5)) == 7.0);1741174217431744#------------------------------------------------------------------------------1745# GMP::Rand17461747#------------------------------------------------------------------------------1748# GMP::Rand::new17491750{ my $r = randstate(); ok (defined $r); }1751{ my $r = randstate('lc_2exp', 1, 2, 3); ok (defined $r); }1752{ my $r = randstate('lc_2exp_size', 64); ok (defined $r); }1753{ my $r = randstate('lc_2exp_size', 999999999); ok (! defined $r); }1754{ my $r = randstate('mt'); ok (defined $r); }17551756{ # copying a randstate results in same sequence1757my $r1 = randstate('lc_2exp_size', 64);1758$r1->seed(123);1759my $r2 = randstate($r1);1760for (1 .. 20) {1761my $z1 = mpz_urandomb($r1, 20);1762my $z2 = mpz_urandomb($r2, 20);1763ok ($z1 == $z2);1764}1765}17661767#------------------------------------------------------------------------------1768# GMP::Rand::seed17691770{ my $r = randstate();1771$r->seed(123);1772$r->seed(time());1773}17741775#------------------------------------------------------------------------------1776# GMP::Rand::mpf_urandomb17771778{ my $r = randstate();1779my $f = mpf_urandomb($r,1024);1780ok (UNIVERSAL::isa($f,"GMP::Mpf")); }17811782#------------------------------------------------------------------------------1783# GMP::Rand::mpz_urandomb17841785{ my $r = randstate();1786my $z = mpz_urandomb($r, 1024);1787ok (UNIVERSAL::isa($z,"GMP::Mpz")); }17881789#------------------------------------------------------------------------------1790# GMP::Rand::mpz_rrandomb17911792{ my $r = randstate();1793my $z = mpz_rrandomb($r, 1024);1794ok (UNIVERSAL::isa($z,"GMP::Mpz")); }17951796#------------------------------------------------------------------------------1797# GMP::Rand::mpz_urandomm17981799{ my $r = randstate();1800my $z = mpz_urandomm($r, mpz(3)**100);1801ok (UNIVERSAL::isa($z,"GMP::Mpz")); }18021803#------------------------------------------------------------------------------1804# GMP::Rand::mpz_urandomb_ui18051806{ my $r = randstate();1807foreach (1 .. 20) {1808my $u = gmp_urandomb_ui($r,8);1809ok ($u >= 0);1810ok ($u < 256);1811}1812}18131814#------------------------------------------------------------------------------1815# GMP::Rand::mpz_urandomm_ui18161817{ my $r = randstate();1818foreach (1 .. 20) {1819my $u = gmp_urandomm_ui($r,8);1820ok ($u >= 0);1821ok ($u < 8);1822}1823}18241825182618271828#------------------------------------------------------------------------------1829# GMP module18301831#------------------------------------------------------------------------------1832# GMP::fits_slong_p18331834ok (GMP::fits_slong_p(0));18351836# in perl 5.005 uv_max is only 32-bits on a 64-bit system, so won't exceed a1837# long1838# ok (! GMP::fits_slong_p($uv_max));18391840ok (GMP::fits_slong_p(0.0));18411842ok (GMP::fits_slong_p('0'));18431844ok (GMP::fits_slong_p(substr('999999999999999999999999999999',1,1)));18451846ok (! mpz("-9999999999999999999999999999999999999999999")->fits_slong_p());1847ok ( mpz(-123)->fits_slong_p());1848ok ( mpz(0)->fits_slong_p());1849ok ( mpz(123)->fits_slong_p());1850ok (! mpz("9999999999999999999999999999999999999999999")->fits_slong_p());18511852ok (! mpq("-9999999999999999999999999999999999999999999")->fits_slong_p());1853ok ( mpq(-123)->fits_slong_p());1854ok ( mpq(0)->fits_slong_p());1855ok ( mpq(123)->fits_slong_p());1856ok (! mpq("9999999999999999999999999999999999999999999")->fits_slong_p());18571858ok (! mpf("-9999999999999999999999999999999999999999999")->fits_slong_p());1859ok ( mpf(-123)->fits_slong_p());1860ok ( mpf(0)->fits_slong_p());1861ok ( mpf(123)->fits_slong_p());1862ok (! mpf("9999999999999999999999999999999999999999999")->fits_slong_p());18631864#------------------------------------------------------------------------------1865# GMP::get_d18661867ok (GMP::get_d(123) == 123.0);18681869ok (GMP::get_d($uv_max) > 0);18701871ok (GMP::get_d(123.0) == 123.0);18721873ok (GMP::get_d('123') == 123.0);18741875ok (GMP::get_d(mpz(123)) == 123.0);18761877ok (GMP::get_d(mpq(123)) == 123.0);18781879ok (GMP::get_d(mpf(123)) == 123.0);18801881#------------------------------------------------------------------------------1882# GMP::get_d_2exp18831884{ my ($dbl, $exp) = get_d_2exp (0);1885ok ($dbl == 0); ok ($exp == 0); }1886{ my ($dbl, $exp) = get_d_2exp (1);1887ok ($dbl == 0.5); ok ($exp == 1); }18881889{ my ($dbl, $exp) = get_d_2exp ($uv_max);1890ok ($dbl > 0.0); ok ($exp > 0); }18911892{ my ($dbl, $exp) = get_d_2exp (0.5);1893ok ($dbl == 0.5); ok ($exp == 0); }1894{ my ($dbl, $exp) = get_d_2exp (0.25);1895ok ($dbl == 0.5); ok ($exp == -1); }18961897{ my ($dbl, $exp) = get_d_2exp ("1.0");1898ok ($dbl == 0.5); ok ($exp == 1); }18991900{ my ($dbl, $exp) = get_d_2exp (mpz ("256"));1901ok ($dbl == 0.5); ok ($exp == 9); }19021903{ my ($dbl, $exp) = get_d_2exp (mpq ("1/16"));1904ok ($dbl == 0.5); ok ($exp == -3); }19051906{ my ($dbl, $exp) = get_d_2exp (mpf ("1.5"));1907ok ($dbl == 0.75); ok ($exp == 1); }1908{ my ($dbl, $exp) = get_d_2exp (mpf ("3.0"));1909ok ($dbl == 0.75); ok ($exp == 2); }19101911#------------------------------------------------------------------------------1912# GMP::get_str19131914ok (get_str(-123) eq '-123');1915ok (get_str('-123') eq '-123');1916ok (get_str(substr('x-123x',1,4)) eq '-123');1917ok (get_str(mpz(-123)) eq '-123');1918ok (get_str(mpq(-123)) eq '-123');19191920ok (get_str(-123,10) eq '-123');1921ok (get_str('-123',10) eq '-123');1922ok (get_str(substr('x-123x',1,4),10) eq '-123');1923ok (get_str(mpz(-123),10) eq '-123');1924ok (get_str(mpq(-123),10) eq '-123');19251926ok (get_str(-123,16) eq '-7b');1927ok (get_str('-123',16) eq '-7b');1928ok (get_str(substr('x-123x',1,4),16) eq '-7b');1929ok (get_str(mpz(-123),16) eq '-7b');1930ok (get_str(mpq(-123),16) eq '-7b');19311932ok (get_str(-123,-16) eq '-7B');1933ok (get_str('-123',-16) eq '-7B');1934ok (get_str(substr('x-123x',1,4),-16) eq '-7B');1935ok (get_str(mpz(-123),-16) eq '-7B');1936ok (get_str(mpq(-123),-16) eq '-7B');19371938# is a float in past versions of perl without UV type1939{ my ($str, $exp) = get_str($uv_max);1940ok ($str eq $uv_max_str); }19411942ok (get_str(mpq(5/8)) eq "5/8");1943ok (get_str(mpq(-5/8)) eq "-5/8");1944ok (get_str(mpq(255/256),16) eq "ff/100");1945ok (get_str(mpq(255/256),-16) eq "FF/100");1946ok (get_str(mpq(-255/256),16) eq "-ff/100");1947ok (get_str(mpq(-255/256),-16) eq "-FF/100");19481949{ my ($s,$e) = get_str(1.5, 10); ok ($s eq '15'); ok ($e == 1); }1950{ my ($s,$e) = get_str(mpf(1.5), 10); ok ($s eq '15'); ok ($e == 1); }19511952{ my ($s,$e) = get_str(-1.5, 10); ok ($s eq '-15'); ok ($e == 1); }1953{ my ($s,$e) = get_str(mpf(-1.5), 10); ok ($s eq '-15'); ok ($e == 1); }19541955{ my ($s,$e) = get_str(1.5, 16); ok ($s eq '18'); ok ($e == 1); }1956{ my ($s,$e) = get_str(mpf(1.5), 16); ok ($s eq '18'); ok ($e == 1); }19571958{ my ($s,$e) = get_str(-1.5, 16); ok ($s eq '-18'); ok ($e == 1); }1959{ my ($s,$e) = get_str(mpf(-1.5), 16); ok ($s eq '-18'); ok ($e == 1); }19601961{ my ($s,$e) = get_str(65536.0, 16); ok ($s eq '1'); ok ($e == 5); }1962{ my ($s,$e) = get_str(mpf(65536.0), 16); ok ($s eq '1'); ok ($e == 5); }19631964{ my ($s,$e) = get_str(1.625, 16); ok ($s eq '1a'); ok ($e == 1); }1965{ my ($s,$e) = get_str(mpf(1.625), 16); ok ($s eq '1a'); ok ($e == 1); }19661967{ my ($s,$e) = get_str(1.625, -16); ok ($s eq '1A'); ok ($e == 1); }1968{ my ($s,$e) = get_str(mpf(1.625), -16); ok ($s eq '1A'); ok ($e == 1); }19691970{ my ($s, $e) = get_str(255.0,16,0); ok ($s eq "ff"); ok ($e == 2); }1971{ my ($s, $e) = get_str(mpf(255.0),16,0); ok ($s eq "ff"); ok ($e == 2); }19721973{ my ($s, $e) = get_str(255.0,-16,0); ok ($s eq "FF"); ok ($e == 2); }1974{ my ($s, $e) = get_str(mpf(255.0),-16,0); ok ($s eq "FF"); ok ($e == 2); }19751976#------------------------------------------------------------------------------1977# GMP::get_si19781979ok (GMP::get_si(123) == 123.0);19801981# better not assume anything about the relatives sizes of long and UV1982ok (GMP::get_si($uv_max) != 0);19831984ok (GMP::get_si(123.0) == 123.0);19851986ok (GMP::get_si('123') == 123.0);19871988ok (GMP::get_si(mpz(123)) == 123.0);19891990ok (GMP::get_si(mpq(123)) == 123.0);19911992ok (GMP::get_si(mpf(123)) == 123.0);19931994#------------------------------------------------------------------------------1995# GMP::integer_p19961997ok ( GMP::integer_p (0));1998ok ( GMP::integer_p (123));1999ok ( GMP::integer_p (-123));20002001ok ( GMP::integer_p ($uv_max));20022003ok ( GMP::integer_p (0.0));2004ok ( GMP::integer_p (123.0));2005ok ( GMP::integer_p (-123.0));2006ok (! GMP::integer_p (0.5));2007ok (! GMP::integer_p (123.5));2008ok (! GMP::integer_p (-123.5));20092010ok ( GMP::integer_p ('0'));2011ok ( GMP::integer_p ('123'));2012ok ( GMP::integer_p ('-123'));2013ok (! GMP::integer_p ('0.5'));2014ok (! GMP::integer_p ('123.5'));2015ok (! GMP::integer_p ('-123.5'));2016ok (! GMP::integer_p ('5/8'));20172018ok ( GMP::integer_p (mpz(1)));20192020ok ( GMP::integer_p (mpq(1)));2021ok (! GMP::integer_p (mpq(1,2)));20222023ok ( GMP::integer_p (mpf(1.0)));2024ok (! GMP::integer_p (mpf(1.5)));20252026#------------------------------------------------------------------------------2027# GMP::odd_p20282029ok (! odd_p(0));2030ok ( odd_p(1));2031ok (! odd_p(2));20322033ok ( odd_p($uv_max));20342035ok ( odd_p(mpz(-3)));2036ok (! odd_p(mpz(-2)));2037ok ( odd_p(mpz(-1)));2038ok (! odd_p(mpz(0)));2039ok ( odd_p(mpz(1)));2040ok (! odd_p(mpz(2)));2041ok ( odd_p(mpz(3)));20422043#------------------------------------------------------------------------------2044# GMP::printf20452046GMP::printf ("hello world\n");20472048sub via_printf {2049my $s;2050open TEMP, ">test.tmp" or die;2051GMP::printf TEMP @_;2052close TEMP or die;2053open TEMP, "<test.tmp" or die;2054read (TEMP, $s, 1024);2055close TEMP or die;2056unlink 'test.tmp';2057return $s;2058}20592060ok (sprintf ("%d", mpz(123)) eq '123');2061ok (sprintf ("%d %d %d", 456, mpz(123), 789) eq '456 123 789');2062ok (sprintf ("%d", mpq(15,16)) eq '15/16');2063ok (sprintf ("%f", mpf(1.5)) eq '1.500000');2064ok (sprintf ("%.2f", mpf(1.5)) eq '1.50');20652066ok (sprintf ("%*d", 6, 123) eq ' 123');2067ok (sprintf ("%*d", 6, mpz(123)) eq ' 123');2068ok (sprintf ("%*d", 6, mpq(15,16)) eq ' 15/16');20692070ok (sprintf ("%x", 123) eq '7b');2071ok (sprintf ("%x", mpz(123)) eq '7b');2072ok (sprintf ("%X", 123) eq '7B');2073ok (sprintf ("%X", mpz(123)) eq '7B');2074ok (sprintf ("%#x", 123) eq '0x7b');2075ok (sprintf ("%#x", mpz(123)) eq '0x7b');2076ok (sprintf ("%#X", 123) eq '0X7B');2077ok (sprintf ("%#X", mpz(123)) eq '0X7B');20782079ok (sprintf ("%x", mpq(15,16)) eq 'f/10');2080ok (sprintf ("%X", mpq(15,16)) eq 'F/10');2081ok (sprintf ("%#x", mpq(15,16)) eq '0xf/0x10');2082ok (sprintf ("%#X", mpq(15,16)) eq '0XF/0X10');20832084ok (sprintf ("%*.*f", 10, 3, 1.25) eq ' 1.250');2085ok (sprintf ("%*.*f", 10, 3, mpf(1.5)) eq ' 1.500');20862087ok (via_printf ("%d", mpz(123)) eq '123');2088ok (via_printf ("%d %d %d", 456, mpz(123), 789) eq '456 123 789');2089ok (via_printf ("%d", mpq(15,16)) eq '15/16');2090ok (via_printf ("%f", mpf(1.5)) eq '1.500000');2091ok (via_printf ("%.2f", mpf(1.5)) eq '1.50');20922093ok (via_printf ("%*d", 6, 123) eq ' 123');2094ok (via_printf ("%*d", 6, mpz(123)) eq ' 123');2095ok (via_printf ("%*d", 6, mpq(15,16)) eq ' 15/16');20962097ok (via_printf ("%x", 123) eq '7b');2098ok (via_printf ("%x", mpz(123)) eq '7b');2099ok (via_printf ("%X", 123) eq '7B');2100ok (via_printf ("%X", mpz(123)) eq '7B');2101ok (via_printf ("%#x", 123) eq '0x7b');2102ok (via_printf ("%#x", mpz(123)) eq '0x7b');2103ok (via_printf ("%#X", 123) eq '0X7B');2104ok (via_printf ("%#X", mpz(123)) eq '0X7B');21052106ok (via_printf ("%x", mpq(15,16)) eq 'f/10');2107ok (via_printf ("%X", mpq(15,16)) eq 'F/10');2108ok (via_printf ("%#x", mpq(15,16)) eq '0xf/0x10');2109ok (via_printf ("%#X", mpq(15,16)) eq '0XF/0X10');21102111ok (via_printf ("%*.*f", 10, 3, 1.25) eq ' 1.250');2112ok (via_printf ("%*.*f", 10, 3, mpf(1.5)) eq ' 1.500');21132114#------------------------------------------------------------------------------2115# GMP::sgn21162117ok (sgn(-123) == -1);2118ok (sgn(0) == 0);2119ok (sgn(123) == 1);21202121ok (sgn($uv_max) == 1);21222123ok (sgn(-123.0) == -1);2124ok (sgn(0.0) == 0);2125ok (sgn(123.0) == 1);21262127ok (sgn('-123') == -1);2128ok (sgn('0') == 0);2129ok (sgn('123') == 1);2130ok (sgn('-123.0') == -1);2131ok (sgn('0.0') == 0);2132ok (sgn('123.0') == 1);21332134ok (sgn(substr('x-123x',1,4)) == -1);2135ok (sgn(substr('x0x',1,1)) == 0);2136ok (sgn(substr('x123x',1,3)) == 1);21372138ok (mpz(-123)->sgn() == -1);2139ok (mpz(0) ->sgn() == 0);2140ok (mpz(123) ->sgn() == 1);21412142ok (mpq(-123)->sgn() == -1);2143ok (mpq(0) ->sgn() == 0);2144ok (mpq(123) ->sgn() == 1);21452146ok (mpf(-123)->sgn() == -1);2147ok (mpf(0) ->sgn() == 0);2148ok (mpf(123) ->sgn() == 1);2149215021512152#------------------------------------------------------------------------------2153# overloaded constants21542155if ($] > 5.00503) {2156if (! do 'test2.pl') {2157die "Cannot run test2.pl\n";2158}2159}21602161216221632164#------------------------------------------------------------------------------2165# $# stuff2166#2167# For some reason "local $#" doesn't leave $# back at its default undefined2168# state when exiting the block.21692170{ local $# = 'hi %.0f there';2171my $f = mpf(123);2172ok ("$f" eq 'hi 123 there'); }2173217421752176# Local variables:2177# perl-indent-level: 22178# End:217921802181