source: tests/rational.cfa

Last change on this file was d30804a, checked in by Peter A. Buhr <pabuhr@…>, 3 months ago

augment rational test and expected output after rational changes

  • Property mode set to 100644
File size: 2.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// rational.cfa -- test rational number package
8//
9// Author           : Peter A. Buhr
10// Created On       : Mon Mar 28 08:43:12 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Jul 20 18:13:40 2021
13// Update Count     : 107
14//
15
16#include <rational.hfa>
17#include <limits.hfa>
18#include <stdlib.hfa>
19#include <fstream.hfa>
20
21typedef Rational(int) RatInt;
22double convert( int i ) { return (double)i; }                   // used by narrow/widen
23int convert( double d ) { return (int)d; }
24
25int main() {
26        sout | "constructor";
27        RatInt a = { 3 }, b = { 4 }, c, d = 0, e = 1;
28        sout | "a : " | a | "b : " | b | "c : " | c | "d : " | d | "e : " | e;
29
30        a = (RatInt){ 4, 8 };
31        b = (RatInt){ 5, 7 };
32        sout | "a : " | a | "b : " | b;
33        a = (RatInt){ -2, -3 };
34        b = (RatInt){ 3, -2 };
35        sout | "a : " | a | "b : " | b;
36        a = (RatInt){ -2, 3 };
37        b = (RatInt){ 3, 2 };
38        sout | "a : " | a | "b : " | b;
39        sout | nl;
40
41        sout | "comparison";
42        a = (RatInt){ -2 };
43        b = (RatInt){ -3, 2 };
44        sout | "a : " | a | "b : " | b;
45        sout | "a == 0 : " | a == (Rational(int)){0}; // FIX ME
46        sout | "a == 1 : " | a == (Rational(int)){1}; // FIX ME
47        sout | "a != 0 : " | a != 0;
48        sout | "! a : " | ! a;
49        sout | "a != b : " | a != b;
50        sout | "a <  b : " | a <  b;
51        sout | "a <=  b : " | a <= b;
52        sout | "a >  b : " | a >  b;
53        sout | "a >=  b : " | a >= b;
54        sout | nl;
55
56        sout | "arithmetic";
57        sout | "a : " | a | "b : " | b;
58        sout | "a + b : " | a + b;
59        sout | "a += b : " | (a += b);
60        sout | "++a : " | ++a;
61        sout | "a++ : " | a++;
62        sout | "a : " | a;
63        sout | "a - b : " | a - b;
64        sout | "a -= b : " | (a -= b);
65        sout | "--a : " | --a;
66        sout | "a-- : " | a--;
67        sout | "a : " | a;
68        sout | "a * b : " | a * b;
69        sout | "a / b : " | a / b;
70        sout | "a \\ 2 : " | a \ 2u | "b \\ 2 : " | b \ 2u;
71        sout | "a \\ -2 : " | a \ -2 | "b \\ -2 : " | b \ -2;
72        sout | nl;
73
74        sout | "conversion";
75        a = (RatInt){ 3, 4 };
76        sout | widen( a );
77        a = (RatInt){ 1, 7 };
78        sout | widen( a );
79        a = (RatInt){ 355, 113 };
80        sout | widen( a );
81        sout | narrow( 0.75, 4 );
82        sout | narrow( 0.14285714285714, 16 );
83        sout | narrow( 3.14159265358979, 256 );
84        sout | nl;
85
86        // sout | "decompose";
87        // int n, d;
88        // [n, d] = a;
89        // sout | a | n | d;
90
91        sout | "more tests";
92        RatInt x = { 1, 2 }, y = { 2 };
93        sout | x - y;
94        sout | x > y;
95        sout | x | numerator( x, 2 ) | x;
96        sout | y | denominator( y, -2 ) | y;
97
98        RatInt z = { 0, 5 };
99        sout | z;
100
101        sout | x | numerator( x, 0 ) | x;
102
103        x = (RatInt){ 1, MAX } + (RatInt){ 1, MAX };
104        sout | x;
105        x = (RatInt){ 3, MAX } + (RatInt){ 2, MAX };
106        sout | x;
107
108        sin | a | b;
109        sout | a | b;
110} // main
111
112// Local Variables: //
113// tab-width: 4 //
114// compile-command: "cfa rational.cfa" //
115// End: //
Note: See TracBrowser for help on using the repository browser.