1use strict;
2use warnings;
3use Math::MPFR qw(:mpfr);
4
5print "1..8\n";
6
7print  "# Using Math::MPFR version ", $Math::MPFR::VERSION, "\n";
8print  "# Using mpfr library version ", MPFR_VERSION_STRING, "\n";
9print  "# Using gmp library version ", Math::MPFR::gmp_v(), "\n";
10
11Rmpfr_set_default_prec(64);
12
13my($WR1, $WR2, $WR3, $WR4, $WR5, $WR6, $WR7, $WR8);
14my($RD1, $RD2, $RD3, $RD4, $RD5, $RD6, $RD7, $RD8);
15my($ret, $ok, $mpfr, $count, $prefix, $suffix);
16
17$mpfr = Math::MPFR->new(17);
18open($WR1, '>', 'out1.txt') or die "Can't open WR1: $!";
19open($WR2, '>', 'out2.txt') or die "Can't open WR2: $!";
20open($WR3, '>', 'out3.txt') or die "Can't open WR3: $!";
21open($WR4, '>', 'out4.txt') or die "Can't open WR4: $!";
22open($WR5, '>', 'out5.txt') or die "Can't open WR5: $!";
23open($WR6, '>', 'out6.txt') or die "Can't open WR6: $!";
24open($WR7, '>', 'out7.txt') or die "Can't open WR7: $!";
25
26$prefix = "This is the prefix ";
27$suffix = " and this is the suffix\n";
28
29# No prefix, no suffix - the five numbers will all be
30# strung together on the one line.
31for(1..5) {
32   $ret = TRmpfr_out_str(\*$WR1, 10, 0, $mpfr, GMP_RNDN);
33   print $WR7 "From the first loop\n";
34}
35
36# Prefix, but no suffix - again, the output will be
37# strung together on the one line.
38for(1..5) {
39   $ret = TRmpfr_out_str($prefix, \*$WR2, 10, 0, $mpfr, GMP_RNDN);
40   print $WR7 "From the second loop";
41}
42
43# Suffix, but no prefix - this file will contain 5 lines.
44for(1..5) {
45   $ret = TRmpfr_out_str(\*$WR3, 10, 0, $mpfr, GMP_RNDN, $suffix);
46   print $WR7 "\nFrom the third loop";
47}
48
49print $WR7 "\n";
50
51# Both prefix and suffix - this file will contain 5 lines.
52for(1..5) {
53   $ret = TRmpfr_out_str($prefix, \*$WR4, 10, 0, $mpfr, GMP_RNDN, $suffix);
54   print $WR7 "From the fourth loop\n";
55}
56
57$prefix .= "\n";
58
59# Prefix, but no suffix - this file will contain 6 lines.
60for(1..5) {
61   $ret = TRmpfr_out_str($prefix, \*$WR5, 10, 0, $mpfr, GMP_RNDN);
62   print $WR7 "From the fifth loop";
63}
64
65# Both prefix and suffix - this file will contain 10 lines -
66# the prefix appearing on one line, the number and the suffix
67# appearing on the next.
68for(1..5) {
69   $ret = TRmpfr_out_str($prefix, \*$WR6, 10, 0, $mpfr, GMP_RNDN, $suffix);
70   print $WR7 "From the sixth loop";
71}
72
73close $WR1 or die "Can't close WR1: $!";
74close $WR2 or die "Can't close WR2: $!";
75close $WR3 or die "Can't close WR3: $!";
76close $WR4 or die "Can't close WR4: $!";
77close $WR5 or die "Can't close WR5: $!";
78close $WR6 or die "Can't close WR6: $!";
79close $WR7 or die "Can't close WR7: $!";
80
81open($RD1, '<', 'out1.txt') or die "Can't open RD1: $!";
82open($RD2, '<', 'out2.txt') or die "Can't open RD2: $!";
83open($RD3, '<', 'out3.txt') or die "Can't open RD3: $!";
84open($RD4, '<', 'out4.txt') or die "Can't open RD4: $!";
85open($RD5, '<', 'out5.txt') or die "Can't open RD5: $!";
86open($RD6, '<', 'out6.txt') or die "Can't open RD6: $!";
87open($RD7, '<', 'out7.txt') or die "Can't open RD7: $!";
88
89$ok = 1;
90$count = 0;
91
92while(<$RD1>) {
93     $count = $.;
94     chomp;
95     unless($_ eq '1.70000000000000000000e1'x5) {$ok = 0}
96}
97
98if($ok && $count == 1) {print "ok 1\n"}
99else {print "not ok 1 $ok $count\n"}
100
101$ok = 1;
102$count = 0;
103
104while(<$RD2>) {
105     $count = $.;
106     chomp;
107     unless($_ eq 'This is the prefix 1.70000000000000000000e1'x5) {$ok = 0}
108}
109
110if($ok && $count == 1) {print "ok 2\n"}
111else {print "not ok 2 $ok $count\n"}
112
113$ok = 1;
114$count = 0;
115
116while(<$RD3>) {
117     $count = $.;
118     chomp;
119     unless($_ eq '1.70000000000000000000e1 and this is the suffix') {$ok = 0}
120}
121
122if($ok && $count == 5) {print "ok 3\n"}
123else {print "not ok 3 $ok $count\n"}
124
125$ok = 1;
126$count = 0;
127
128while(<$RD4>) {
129     $count = $.;
130     chomp;
131     unless($_ eq 'This is the prefix 1.70000000000000000000e1 and this is the suffix') {$ok = 0}
132}
133
134if($ok && $count == 5) {print "ok 4\n"}
135else {print "not ok 4 $ok $count\n"}
136
137$ok = 1;
138$count = 0;
139
140while(<$RD5>) {
141     $count = $.;
142     chomp;
143     if($. == 1) {
144       unless($_ eq 'This is the prefix ') {$ok = 0}
145     }
146     elsif($. == 6) {
147       unless($_ eq '1.70000000000000000000e1') {$ok = 0}
148     }
149     else {
150       unless($_ eq '1.70000000000000000000e1This is the prefix ') {$ok = 0}
151     }
152}
153
154if($ok && $count == 6) {print "ok 5\n"}
155else {print "not ok 5 $ok $count\n"}
156
157$ok = 1;
158$count = 0;
159
160while(<$RD6>) {
161     $count = $.;
162     chomp;
163     if($. & 1) {
164       unless($_ eq 'This is the prefix ') {$ok = 0}
165     }
166     else {
167       unless($_ eq '1.70000000000000000000e1 and this is the suffix') {$ok = 0}
168     }
169}
170
171if($ok && $count == 10) {print "ok 6\n"}
172else {print "not ok 6 $ok $count\n"}
173
174$ok = 1;
175$count = 0;
176
177while(<$RD7>) {
178     $count = $.;
179     chomp;
180     if($. <= 5 && $. >= 1) {
181       unless($_ eq 'From the first loop') {$ok = 0}
182     }
183     if($. == 6) {
184       unless($_ eq 'From the second loop' x 5) {$ok = 0}
185     }
186     if($. <= 11 && $. >= 7) {
187       unless($_ eq 'From the third loop') {$ok = 0}
188     }
189     if($. <= 16 && $. >= 12) {
190       unless($_ eq 'From the fourth loop') {$ok = 0}
191     }
192     if($. == 17) {
193       unless($_ eq 'From the fifth loop' x 5 . 'From the sixth loop' x 5) {$ok = 0}
194     }
195}
196
197if($ok && $count == 17) {print "ok 7\n"}
198else {print "not ok 7 $ok $count\n"}
199
200close $RD1 or die "Can't close RD1: $!";
201close $RD2 or die "Can't close RD2: $!";
202close $RD3 or die "Can't close RD3: $!";
203close $RD4 or die "Can't close RD4: $!";
204close $RD5 or die "Can't close RD5: $!";
205close $RD6 or die "Can't close RD6: $!";
206close $RD7 or die "Can't close RD7: $!";
207
208open($WR8, '>', 'out1.txt') or die "Can't open WR8: $!";
209print $WR8 "1.5e2\n";
210close $WR8 or die "Can't close WR8: $!";
211
212open($RD8, '<', 'out1.txt') or die "Can't open RD8: $!";
213$ret = TRmpfr_inp_str($mpfr, \*$RD8, 10, GMP_RNDN);
214close $RD8 or die "Can't close RD8: $!";
215
216if($ret == 5 && $mpfr == 150) {print "ok 8\n"}
217else {print "not ok 8 $ret $mpfr\n"}
218
219
220