tagged release 0.7.1
[parrot.git] / t / dynpmc / rational.t
blob7b2ced3235b953aeb5d997b4c2f5d37c63a1e565
1 #! perl
2 # Copyright (C) 2008, The Perl Foundation.
3 # $Id$
5 use strict;
6 use warnings;
7 use lib qw( . lib ../lib ../../lib );
9 use Test::More;
10 use Parrot::Test;
11 use Parrot::Config;
13 =head1 NAME
15 t/dynpmc/rational.t - Rational PMC
17 =head1 SYNOPSIS
19     % prove t/dynpmc/rational.t
21 =head1 DESCRIPTION
23 Tests the Rational PMC.
25 =cut
27 if ( $PConfig{gmp} ) { # If GMP is available, we check all functions.
28     plan tests => 32;
30 else {                 # If GMP is not available, we only test the constructor and the
31     plan tests => 2;   # version-method that is used to detect presence of GMP at runtime.
34 pir_output_is(<<'CODE', <<'OUTPUT', "Initialization");
35   .sub main :main
36     loadlib $P1, 'rational'
37     new $P1, 'Rational'
38     say "ok" 
39   .end
40 CODE
42 OUTPUT
44 if (! $PConfig{gmp}) { # If GMP is not available, this is the last test:
45 pir_output_is(<<'CODE', <<'OUTPUT', "version-method");
46   .sub main :main
47     loadlib $P1, 'rational'
48     new $P1, 'Rational'
49     $S1 = $P1.'version'()
50     say $S1
51   .end
52 CODE
53 0.0.0
54 OUTPUT
55 exit;
58 # These tests are only run, if GMP is available.
59 pir_output_is(<<'CODE', <<'OUTPUT', "version-method");
60   .sub main :main
61     loadlib $P1, 'rational'
62     new $P1, 'Rational'
63     $S1 = $P1.'version'()
64     say "ok"
65   .end
66 CODE
68 OUTPUT
70 pir_output_is(<<'CODE', <<'OUTPUT', "Set and get native integer");
71   .sub main :main
72     loadlib $P1, 'rational'
73     new $P1, 'Rational'
74     
75     $I1 = 42
76     $P1 = $I1
77     $I2 = $P1
78     
79     say $I2
80   .end
81 CODE
83 OUTPUT
85 pir_output_is(<<'CODE', <<'OUTPUT', "Set and get native float");
86   .sub main :main
87     loadlib P0, 'rational'
88     new P0, 'Rational'
89     
90     N0 = 11.1
91     P0 = N0
92     N1 = P0
93     
94     say N1
95   .end
96 CODE
97 11.100000
98 OUTPUT
100 pir_output_is(<<'CODE', <<'OUTPUT', "Set and get native string");
101   .sub main :main
102     loadlib $P1, 'rational'
103     new $P1, 'Rational'
104     
105     $S1 = "7/4"
106     $P1 = $S1
107     $S2 = $P1
108     
109     say $S2
110   .end
111 CODE
113 OUTPUT
115 pir_output_is(<<'CODE', <<'OUTPUT', "Set and get Integer");
116   .sub main :main
117     loadlib $P1, 'rational'
118     new $P1, 'Rational'
119     
120     new $P2, 'Integer'
121     new $P3, 'Integer'
122     
123     $P2 = 7
124     $P1 = $P2
125     $P3 = $P1
126     
127     say $P3
128   .end
129 CODE
131 OUTPUT
133 pir_output_is(<<'CODE', <<'OUTPUT', "Set and get Float");
134   .sub main :main
135     loadlib $P1, 'rational'
136     new $P1, 'Rational'
137     
138     new $P2, 'Float'
139     new $P3, 'Float'
140     
141     $P2 = 7.110000
142     $P1 = $P2
143     $P3 = $P1
144     
145     say $P3
146   .end
147 CODE
148 7.11
149 OUTPUT
151 pir_output_is(<<'CODE', <<'OUTPUT', "Set and get String");
152   .sub main :main
153     loadlib $P1, 'rational'
154     new $P1, 'Rational'
155     
156     new $P2, 'String'
157     new $P3, 'String'
158     
159     $P2 = "7/4"
160     $P1 = $P2
161     $P3 = $P1
162     
163     say $P3
164   .end
165 CODE
167 OUTPUT
169 pir_output_is(<<'CODE', <<'OUTPUT', "Increment and decrement");
170   .sub main :main
171     loadlib $P1, 'rational'
172     new $P1, 'Rational'
173     
174     $P1 = "7/4"
175     inc $P1
176     print $P1
177     print "\n"
178     
179     dec $P1
180     dec $P1
181     say $P1
182   .end
183 CODE
184 11/4
186 OUTPUT
188 pir_output_is(<<'CODE', <<'OUTPUT', "Adding integers (+ inplace operation)");
189   .sub main :main
190     loadlib $P1, 'rational'
191     new $P1, 'Rational'
192     new $P2, 'Rational'
193     $I1 = 7
195     $P1 = "3/2"
196     $P2 = $P1 + $I1
197     $P1 = $P1 + $I1
198     $P1 = $P1 + $I1
199     
200     say $P1
201     say $P2
202   .end
203 CODE
204 31/2
205 17/2
206 OUTPUT
208 pir_output_is(<<'CODE', <<'OUTPUT', "Adding floats (+ inplace operation)");
209   .sub main :main
210     loadlib $P1, 'rational'
211     new $P1, 'Rational'
212     new $P2, 'Rational'
213     $N1 = 7.
215     $P1 = "3/2"
216     $P2 = $P1 + $N1
217     $P1 = $P1 + $N1
218     $P1 = $P1 + $N1
219     
220     say $P1
221     say $P2
222   .end
223 CODE
224 31/2
225 17/2
226 OUTPUT
228 pir_output_is(<<'CODE', <<'OUTPUT', "Adding Integers (+ inplace operation)");
229   .sub main :main
230     loadlib $P1, 'rational'
231     new $P2, 'Rational'
232     new $P3, 'Rational'
233     new $P4, 'Integer'
235     $P4 = 7
237     $P2 = "3/2"
238     $P3 = $P2 + $P4
239     $P2 = $P2 + $P4
241     say $P2
242     say $P3
243   .end
244 CODE
245 17/2
246 17/2
247 OUTPUT
249 pir_output_is(<<'CODE', <<'OUTPUT', "Adding Floats (+ inplace operation)");
250   .sub main :main
251     loadlib $P1, 'rational'
252     new $P2, 'Rational'
253     new $P3, 'Rational'
254     new $P4, 'Float'
256     $P4 = 7.
258     $P2 = "3/2"
259     $P3 = $P2 + $P4
260     $P2 = $P2 + $P4
262     say $P2
263     say $P3
264   .end
265 CODE
266 17/2
267 17/2
268 OUTPUT
270 pir_output_is(<<'CODE', <<'OUTPUT', "Adding Rationals (+inplace operation)");
271   .sub main :main
272     loadlib $P1, 'rational'
273     new $P1, 'Rational'
274     new $P2, 'Rational'
275     new $P3, 'Rational'
276     
277     $P2 = "3/2"
278     $P3 = "5/2"
279     
280     $P1 = $P2 + $P3
281     $P2 = $P2 + $P3
282     
283     say $P1
284     say $P2
285   .end
286 CODE
289 OUTPUT
291 pir_output_is(<<'CODE', <<'OUTPUT', "Subtracting integers (+ inplace operation)");
292   .sub main :main
293     loadlib $P1, 'rational'
294     new $P1, 'Rational'
295     new $P2, 'Rational'
296     $I1 = 7
298     $P1 = "3/2"
299     $P2 = $P1 - $I1
300     $P1 = $P1 - $I1
301     $P1 = $P1 - $I1
302     
303     say $P1
304     say $P2
305   .end
306 CODE
307 -25/2
308 -11/2
309 OUTPUT
311 pir_output_is(<<'CODE', <<'OUTPUT', "Subtracting floats (+ inplace operation)");
312   .sub main :main
313     loadlib $P1, 'rational'
314     new $P1, 'Rational'
315     new $P2, 'Rational'
316     $N1 = 7.
318     $P1 = "3/2"
319     $P2 = $P1 - $N1
320     $P1 = $P1 - $N1
321     $P1 = $P1 - $N1
322     
323     say $P1
324     say $P2
325   .end
326 CODE
327 -25/2
328 -11/2
329 OUTPUT
331 pir_output_is(<<'CODE', <<'OUTPUT', "Subtracting Integers (+ inplace operation)");
332   .sub main :main
333     loadlib $P1, 'rational'
334     new $P2, 'Rational'
335     new $P3, 'Rational'
336     new $P4, 'Integer'
338     $P4 = 7
340     $P2 = "3/2"
341     $P3 = $P2 - $P4
342     $P2 = $P2 - $P4
344     say $P2
345     say $P3
346   .end
347 CODE
348 -11/2
349 -11/2
350 OUTPUT
352 pir_output_is(<<'CODE', <<'OUTPUT', "Subtracting Floats (+ inplace operation)");
353   .sub main :main
354     loadlib $P1, 'rational'
355     new $P2, 'Rational'
356     new $P3, 'Rational'
357     new $P4, 'Float'
359     $P4 = 7.
361     $P2 = "3/2"
362     $P3 = $P2 - $P4
363     $P2 = $P2 - $P4
365     say $P2
366     say $P3
367   .end
368 CODE
369 -11/2
370 -11/2
371 OUTPUT
373 pir_output_is(<<'CODE', <<'OUTPUT', "Subtracting Rationals (+inplace operation)");
374   .sub main :main
375     loadlib $P1, 'rational'
376     new $P1, 'Rational'
377     new $P2, 'Rational'
378     new $P3, 'Rational'
379     
380     $P2 = "3/2"
381     $P3 = "5/2"
382     
383     $P1 = $P2 - $P3
384     $P2 = $P2 - $P3
385     
386     say $P1
387     say $P2
388   .end
389 CODE
392 OUTPUT
394 pir_output_is(<<'CODE', <<'OUTPUT', "Multiplying integers (+ inplace operation)");
395   .sub main :main
396     loadlib $P1, 'rational'
397     new $P1, 'Rational'
398     new $P2, 'Rational'
399     $I1 = 7
401     $P1 = "3/2"
402     $P2 = $P1 * $I1
403     $P1 = $P1 * $I1
404     
405     say $P1
406     say $P2
407   .end
408 CODE
409 21/2
410 21/2
411 OUTPUT
413 pir_output_is(<<'CODE', <<'OUTPUT', "Multiplying floats (+ inplace operation)");
414   .sub main :main
415     loadlib $P1, 'rational'
416     new $P1, 'Rational'
417     new $P2, 'Rational'
418     $N1 = 7.
420     $P1 = "3/2"
421     $P2 = $P1 * $N1
422     $P1 = $P1 * $N1
423     
424     say $P1
425     say $P2
426   .end
427 CODE
428 21/2
429 21/2
430 OUTPUT
432 pir_output_is(<<'CODE', <<'OUTPUT', "Multiplying Integers (+ inplace operation)");
433   .sub main :main
434     loadlib $P1, 'rational'
435     new $P2, 'Rational'
436     new $P3, 'Rational'
437     new $P4, 'Integer'
439     $P4 = 7
441     $P2 = "3/2"
442     $P3 = $P2 * $P4
443     $P2 = $P2 * $P4
445     say $P2
446     say $P3
447   .end
448 CODE
449 21/2
450 21/2
451 OUTPUT
453 pir_output_is(<<'CODE', <<'OUTPUT', "Multiplying Floats (+ inplace operation)");
454   .sub main :main
455     loadlib $P1, 'rational'
456     new $P2, 'Rational'
457     new $P3, 'Rational'
458     new $P4, 'Float'
460     $P4 = 7.
462     $P2 = "3/2"
463     $P3 = $P2 * $P4
464     $P2 = $P2 * $P4
466     say $P2
467     say $P3
468   .end
469 CODE
470 21/2
471 21/2
472 OUTPUT
474 pir_output_is(<<'CODE', <<'OUTPUT', "Multiplying Rationals (+ inplace operation)");
475   .sub main :main
476     loadlib $P1, 'rational'
477     new $P1, 'Rational'
478     new $P2, 'Rational'
479     new $P3, 'Rational'
480     
481     $P2 = "3/2"
482     $P3 = "5/2"
483     
484     $P1 = $P2 * $P3
485     $P2 = $P2 * $P3
486     
487     say $P1
488     say $P2
489   .end
490 CODE
491 15/4
492 15/4
493 OUTPUT
495 pir_output_is(<<'CODE', <<'OUTPUT', "Dividing integers (+ inplace operation)");
496   .sub main :main
497     loadlib $P1, 'rational'
498     new $P1, 'Rational'
499     new $P2, 'Rational'
500     $I1 = 7
502     $P1 = "3/2"
503     $P2 = $P1 / $I1
504     $P1 = $P1 / $I1
505     
506     say $P1
507     say $P2
508   .end
509 CODE
510 3/14
511 3/14
512 OUTPUT
514 pir_output_is(<<'CODE', <<'OUTPUT', "Dividing floats (+ inplace operation)");
515   .sub main :main
516     loadlib $P1, 'rational'
517     new $P1, 'Rational'
518     new $P2, 'Rational'
519     $N1 = 7.
521     $P1 = "3/2"
522     $P2 = $P1 / $N1
523     $P1 = $P1 / $N1
524     
525     say $P1
526     say $P2
527   .end
528 CODE
529 3/14
530 3/14
531 OUTPUT
533 pir_output_is(<<'CODE', <<'OUTPUT', "Dividing Integers (+ inplace operation)");
534   .sub main :main
535     loadlib $P1, 'rational'
536     new $P2, 'Rational'
537     new $P3, 'Rational'
538     new $P4, 'Integer'
540     $P4 = 7
542     $P2 = "3/2"
543     $P3 = $P2 / $P4
544     $P2 = $P2 / $P4
546     say $P2
547     say $P3
548   .end
549 CODE
550 3/14
551 3/14
552 OUTPUT
554 pir_output_is(<<'CODE', <<'OUTPUT', "Dividing Floats (+ inplace operation)");
555   .sub main :main
556     loadlib $P1, 'rational'
557     new $P2, 'Rational'
558     new $P3, 'Rational'
559     new $P4, 'Float'
561     $P4 = 7.
563     $P2 = "3/2"
564     $P3 = $P2 / $P4
565     $P2 = $P2 / $P4
567     say $P2
568     say $P3
569   .end
570 CODE
571 3/14
572 3/14
573 OUTPUT
575 pir_output_is(<<'CODE', <<'OUTPUT', "Dividing Rationals (+ inplace operation)");
576   .sub main :main
577     loadlib $P1, 'rational'
578     new $P1, 'Rational'
579     new $P2, 'Rational'
580     new $P3, 'Rational'
581     
582     $P2 = "3/2"
583     $P3 = "5/2"
584     
585     $P1 = $P2 / $P3
586     $P2 = $P2 / $P3
587     
588     say $P1
589     say $P2
590   .end
591 CODE
594 OUTPUT
595 pir_output_is(<<'CODE', <<'OUTPUT', "Negating (+inplace operation)");
596   .sub main :main
597     loadlib $P1, 'rational'
598     new $P2, 'Rational'
599     new $P3, 'Rational'
600     
601     $P2 = "-3/2"
602     $P3 = -$P2
603     $P2 = -$P2
604     
605     say $P2
606     say $P3
607   .end
608 CODE
611 OUTPUT
613 pir_output_is(<<'CODE', <<'OUTPUT', "Absolute value (+inplace operation)");
614   .sub main :main
615     loadlib $P1, 'rational'
616     new $P2, 'Rational'
617     new $P3, 'Rational'
618     
619     $P2 = "-3/2"
620     $P3 = abs $P2
621     abs $P2
622     
623     say $P2
624     say $P3
625   .end
626 CODE
629 OUTPUT
631 pir_output_is(<<'CODE', <<'OUTPUT', "Comparing rationals to rationals");
632   .sub main :main
633     loadlib $P1, 'rational'
634     new $P2, 'Rational'
635     new $P3, 'Rational'
636     
637     $P2 = "3/2"
638     $P3 = "6/4"
639     
640     if $P2 == $P3 goto EQ
641     goto NE
642   EQ:
643     say "1"
644     goto END_EQ
645   NE:
646     say "0"
647   END_EQ:
648     
649     $P3 = "7/4"
650     cmp $I1, $P2, $P3
651     cmp $I2, $P3, $P2
652     
653     say $I1
654     say $I2
655   .end
656 CODE
660 OUTPUT