File: Rmpfr_get_q.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 (140 lines) | stat: -rwxr-xr-x 2,882 bytes parent folder | download | duplicates (3)
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
use warnings;
use strict;
use Config;
use Math::MPFR qw(:mpfr);

eval {require Math::GMPq; Math::GMPq->import(':mpq');};

if($@) {
  print "1..1\n";
  warn "\nSkipping all tests - Couldn't load Math::GMPq\n";
  print "ok 1\n";
  exit 0;
}

print "1..10\n";

my $nan   = Math::MPFR->new();
my $ninf  = Math::MPFR->new(-1) / Math::MPFR->new(0);
my $pinf  = Math::MPFR->new(1) / Math::MPFR->new(0);
my $pzero = Math::MPFR->new(0);
my $nzero = Math::MPFR->new(0) * Math::MPFR->new(-1);
my $ok;

my $q = Math::GMPq->new();

$ok .= 'a' unless Rmpfr_erangeflag_p();

eval {Rmpfr_get_q($q, $nan);};

if($@ =~ /^In Rmpfr_get_q: Cannot coerce an 'Inf' or 'NaN' to a Math::GMPq object/) {print "ok 1\n"}
else {
  warn "\n\$\@\n:$@\n";
  print "not ok 1\n";
}

$ok .= 'b' if !Rmpfr_erangeflag_p();
Rmpfr_clear_erangeflag();
$ok .= 'c' unless Rmpfr_erangeflag_p();

eval {Rmpfr_get_q($q, $ninf);};

if($@ =~ /^In Rmpfr_get_q: Cannot coerce an 'Inf' or 'NaN' to a Math::GMPq object/) {print "ok 2\n"}
else {
  warn "\n\$\@\n:$@\n";
  print "not ok 2\n";
}

$ok .= 'd' if !Rmpfr_erangeflag_p();
Rmpfr_clear_erangeflag();
$ok .= 'e' unless Rmpfr_erangeflag_p();

eval {Rmpfr_get_q($q, $pinf);};

if($@ =~ /^In Rmpfr_get_q: Cannot coerce an 'Inf' or 'NaN' to a Math::GMPq object/) {print "ok 3\n"}
else {
  warn "\n\$\@\n:$@\n";
  print "not ok 1\n";
}

$ok .= 'f' if !Rmpfr_erangeflag_p();
Rmpfr_clear_erangeflag();
$ok .= 'g' unless Rmpfr_erangeflag_p();

Rmpfr_get_q($q, $pzero);

if($q == 0) {print "ok 4\n"}
else {
  warn "\nExpected 0, got $q\n";
  print "not ok 4\n";
}

Rmpfr_get_q($q, $nzero);

if($q == 0) {print "ok 5\n"}
else {
  warn "\nExpected 0, got $q\n";
  print "not ok 5\n";
}

my $val = Rmpfr_init2(121);
Rmpfr_set_d($val, 2.0, MPFR_RNDN);
$val **= 0.5;

#print "$val\n";

Rmpfr_get_q($q, $val);

if(Rmpfr_cmp_q($val, $q) == 0) {print "ok 6\n"}
else {
  warn "\n\$val ($val) != \$q ($q)\n";
  print "not ok 6\n";
}

$ok .= 'h' unless Rmpfr_erangeflag_p();

if($ok eq 'abcdefgh') {print "ok 7\n"}
else {
  warn "\nExpected 'abcdefgh', got '$ok'\n";
  print "not ok 7\n";
}

my $check = Rmpfr_init2(Rmpfr_get_prec($val) * 2);

Rmpfr_set($check, $val, MPFR_RNDN);
$check **= 2.0;

if(Rmpfr_cmp_q($check, $q * $q) == 0) {print "ok 8\n"}
else {
  warn "\n$check != ", $q * $q, "\n";
  print "not ok 8\n";
}

eval {require Math::GMPz; Math::GMPz->import(':mpz');};

if($@) {
  warn "\nSkipping (canonicalization) tests 9 & 10 - Couldn't load Math::GMPz\n";
  print "ok 9\n";
  print "ok 10\n";
}

else {
  my $num = Math::GMPz->new();
  my $rop = Math::GMPq->new();
  Rmpfr_get_q($rop, Math::MPFR->new(1.5));
  Rmpq_numref($num, $rop);

  if($num == 3) {print "ok 9\n"}
  else {
    warn "\nExpected 3, got $num\n";
    print "not ok 9\n";
  }

  Rmpq_denref($num, $rop);

  if($num == 2) {print "ok 10\n"}
  else {
    warn "\nExpected 2, got $num\n";
    print "not ok 10\n";
  }
}