File: overload_math_gmp.t

package info (click to toggle)
libmath-mpfr-perl 4.45-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,716 kB
  • sloc: perl: 1,508; ansic: 517; makefile: 9
file content (72 lines) | stat: -rwxr-xr-x 2,079 bytes parent folder | download | duplicates (2)
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
# Test overloading of gmp objects.
# The only gotcha here is that Math::GMP overloading of Math::MPFR objects does not work.
# Therefore, while the following DWIMs:
# $ perl -MMath::MPFR -MMath::GMP -le 'print "ok" if Math::MPFR->new(0) < Math::GMP->new(10);'
#   ok
# the converse does not, and will crash in some cases:
# $ perl -MMath::MPFR -MMath::GMP -le 'print "ok" if Math::GMP->new(10) > Math::MPFR->new(0);'
#
# That is, while Math::MPFR evaluates Math::GMP objects as intended, Math::MPFR objects are
# meaningless to Math::GMP.
# It is therefore up to the user to ensure that Math::MPFR is doing the overloading.
# (It seems to me that all Math::MPFR objects will be evaluated by Math::GMP as being zero.)


use strict;
use warnings;

use Math::MPFR qw(:mpfr);
use Test::More;

eval { require Math::GMP; };

if($@) {
  warn "\$\@: $@\n";
  warn "Skipping all tests as Math::GMP could not be loaded\n";
  is(1, 1);
  done_testing();
  exit 0;
}

my $z = Math::GMP->new('1234' x 2);
my $f = Math::MPFR->new(0);

cmp_ok( $f, '<', $z, "'<' ok");
cmp_ok( $f, '<', $z, "'!=' ok");
cmp_ok( $f, '<=', $z, "'<=' ok");
cmp_ok( $f + $z, '==', $z, "'==' ok");
cmp_ok( $f + $z, '==', $z, "'>=' ok");
cmp_ok( $f += $z, '==', $z, "'+=' ok");
$f += $z;
cmp_ok( $f, '>', $z, "'>' ok");
cmp_ok( $f - $z, '==', $z, "'-' ok");
$f -= $z;
cmp_ok($f, '==', $z, "'-=' ok");
cmp_ok($f ** Math::GMP->new(2), '==', $z ** 2, "'**' ok");
$f **= Math::GMP->new(2);
cmp_ok($f, '==', $z ** 2, "'**=' ok");

$f **= 0.5;

cmp_ok($f * 5, '==', $z * 5, "'*' ok");
cmp_ok($f *= Math::GMP->new(5), '==', $z * 5, "'*=' ok");
cmp_ok($f / Math::GMP->new(5), '==', $z, "'/' ok");
cmp_ok($f /= Math::GMP->new(5), '==', $z, "'/=' ok");

Rmpfr_sprintf(my $buf, "%Zd", $z, 32);
cmp_ok($buf, 'eq', "$z", "'%Zd' formatting ok");

Rmpfr_sprintf($buf, "%Zu", $z, 32);
cmp_ok($buf, 'eq', "$z", "'%Zu' formatting ok");

Rmpfr_sprintf($buf, "%Zx", $z, 32);
cmp_ok($buf, 'eq', 'bc4ff2', "'%Zx' formatting ok");

Rmpfr_sprintf($buf, "%ZX", $z, 32);
cmp_ok($buf, 'eq', 'BC4FF2', "'%ZX' formatting ok");





done_testing();