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
|
#include <cxxtest/TestSuite.h>
#include "common/rational.h"
class RationalTestSuite : public CxxTest::TestSuite {
public:
void test_operators() {
Common::Rational r0(6, 3);
Common::Rational r1(1, 2);
Common::Rational r2(62, 2);
Common::Rational r3(34, 4);
Common::Rational r4 = (r0 + r1) * 3;
Common::Rational r5 = (r2 - r3) / 3;
Common::Rational r6 = r5 - 1;
TS_ASSERT(r4 == r5);
TS_ASSERT(!(r4 != r5));
TS_ASSERT(r4 != r6);
TS_ASSERT(!(r4 == r6));
TS_ASSERT(-r4 == -r5);
TS_ASSERT(r0 == 2);
TS_ASSERT(!(r0 != 2));
TS_ASSERT(!(r3 == 2));
TS_ASSERT(r3 != 2);
TS_ASSERT( r4 > r6);
TS_ASSERT( r4 >= r6);
TS_ASSERT(!(r4 < r6));
TS_ASSERT(!(r4 <= r6));
TS_ASSERT( r4 > 7);
TS_ASSERT( r4 >= 7);
TS_ASSERT(!(r4 < 7));
TS_ASSERT(!(r4 <= 7));
TS_ASSERT( 7 < r4);
TS_ASSERT( 7 <= r4);
TS_ASSERT(!(7 > r4));
TS_ASSERT(!(7 >= r4));
}
void test_assign() {
Common::Rational r0(6, 3);
Common::Rational r1(1, 2);
TS_ASSERT_EQUALS(r0, 2);
TS_ASSERT_EQUALS(r1, Common::Rational(1, 2));
r0 = r1;
TS_ASSERT_EQUALS(r0, r1);
TS_ASSERT_EQUALS(r0, Common::Rational(1, 2));
}
void test_negative() {
Common::Rational r0(6, 3);
Common::Rational r1(1, 2);
r0 = -r0;
r1 = -r1;
TS_ASSERT_EQUALS(r0, -2);
TS_ASSERT_EQUALS(r1, Common::Rational(-1, 2));
TS_ASSERT_EQUALS(r1, Common::Rational(1, -2));
TS_ASSERT_EQUALS(r1, Common::Rational(-25, 50));
TS_ASSERT_EQUALS(r1, Common::Rational(25, -50));
}
void test_add_sub() {
const Common::Rational r0(6, 3);
const Common::Rational r1(1, 2);
TS_ASSERT_EQUALS(r0 + r1, Common::Rational(5, 2));
TS_ASSERT_EQUALS(r1 + r0, Common::Rational(5, 2));
TS_ASSERT_EQUALS(r0 - r1, Common::Rational(3, 2));
TS_ASSERT_EQUALS(r1 - r0, Common::Rational(-3, 2));
TS_ASSERT_EQUALS(1 + r1, Common::Rational(3, 2));
TS_ASSERT_EQUALS(r1 + 1, Common::Rational(3, 2));
TS_ASSERT_EQUALS(1 - r1, Common::Rational(1, 2));
TS_ASSERT_EQUALS(r1 - 1, Common::Rational(-1, 2));
}
void test_mul() {
const Common::Rational r0(6, 3);
const Common::Rational r1(1, 2);
const Common::Rational r2(15, 14);
const Common::Rational r3(7,3);
const Common::Rational r4(5,2);
TS_ASSERT_EQUALS(r0 * r1, 1);
TS_ASSERT_EQUALS(r2 * r3, r4);
TS_ASSERT_EQUALS((-r2) * r3, -r4);
TS_ASSERT_EQUALS(r2 * (-r3), -r4);
TS_ASSERT_EQUALS((-r2) * (-r3), r4);
TS_ASSERT_EQUALS(r1 * 2, 1);
TS_ASSERT_EQUALS(2 * r1, 1);
}
void test_div() {
Common::Rational r0(6, 3);
Common::Rational r1(1, 2);
TS_ASSERT_EQUALS(r0 / r1, 4);
TS_ASSERT_EQUALS(r1 / 2, Common::Rational(1, 4));
TS_ASSERT_EQUALS(2 / r1, Common::Rational(4, 1));
}
};
|