|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+import org.junit.Ignore;
|
|
|
2
|
+import org.junit.Test;
|
|
|
3
|
+
|
|
|
4
|
+import static org.junit.Assert.assertEquals;
|
|
|
5
|
+
|
|
|
6
|
+public class RationalTest {
|
|
|
7
|
+
|
|
|
8
|
+ // Helper methods
|
|
|
9
|
+
|
|
|
10
|
+ private static final double DOUBLE_EQUALITY_TOLERANCE = 1e-15;
|
|
|
11
|
+
|
|
|
12
|
+ private void assertDoublesEqual(double x, double y) {
|
|
|
13
|
+ assertEquals(x, y, DOUBLE_EQUALITY_TOLERANCE);
|
|
|
14
|
+ }
|
|
|
15
|
+
|
|
|
16
|
+ // Tests
|
|
|
17
|
+
|
|
|
18
|
+ @Test
|
|
|
19
|
+ public void testAddTwoPositiveRationalNumbers() {
|
|
|
20
|
+ Rational expected = new Rational(7, 6);
|
|
|
21
|
+ Rational actual = new Rational(1, 2).add(new Rational(2, 3));
|
|
|
22
|
+ assertEquals(expected, actual);
|
|
|
23
|
+ }
|
|
|
24
|
+
|
|
|
25
|
+ @Ignore("Remove to run test")
|
|
|
26
|
+ @Test
|
|
|
27
|
+ public void testAddAPositiveRationalNumberAndANegativeRationalNumber() {
|
|
|
28
|
+ Rational expected = new Rational(-1, 6);
|
|
|
29
|
+ Rational actual = new Rational(1, 2).add(new Rational(-2, 3));
|
|
|
30
|
+ assertEquals(expected, actual);
|
|
|
31
|
+ }
|
|
|
32
|
+
|
|
|
33
|
+ @Ignore("Remove to run test")
|
|
|
34
|
+ @Test
|
|
|
35
|
+ public void testAddTwoNegativeRationalNumbers() {
|
|
|
36
|
+ Rational expected = new Rational(-7, 6);
|
|
|
37
|
+ Rational actual = new Rational(-1, 2).add(new Rational(-2, 3));
|
|
|
38
|
+ assertEquals(expected, actual);
|
|
|
39
|
+ }
|
|
|
40
|
+
|
|
|
41
|
+ @Ignore("Remove to run test")
|
|
|
42
|
+ @Test
|
|
|
43
|
+ public void testAddARationalNumberToItsAdditiveInverse() {
|
|
|
44
|
+ Rational expected = new Rational(0, 1);
|
|
|
45
|
+ Rational actual = new Rational(1, 2).add(new Rational(-1, 2));
|
|
|
46
|
+ assertEquals(expected, actual);
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ @Ignore("Remove to run test")
|
|
|
50
|
+ @Test
|
|
|
51
|
+ public void testSubtractTwoPositiveRationalNumbers() {
|
|
|
52
|
+ Rational expected = new Rational(-1, 6);
|
|
|
53
|
+ Rational actual = new Rational(1, 2).subtract(new Rational(2, 3));
|
|
|
54
|
+ assertEquals(expected, actual);
|
|
|
55
|
+ }
|
|
|
56
|
+
|
|
|
57
|
+ @Ignore("Remove to run test")
|
|
|
58
|
+ @Test
|
|
|
59
|
+ public void testSubtractAPositiveRationalNumberAndANegativeRationalNumber() {
|
|
|
60
|
+ Rational expected = new Rational(7, 6);
|
|
|
61
|
+ Rational actual = new Rational(1, 2).subtract(new Rational(-2, 3));
|
|
|
62
|
+ assertEquals(expected, actual);
|
|
|
63
|
+ }
|
|
|
64
|
+
|
|
|
65
|
+ @Ignore("Remove to run test")
|
|
|
66
|
+ @Test
|
|
|
67
|
+ public void testSubtractTwoNegativeRationalNumbers() {
|
|
|
68
|
+ Rational expected = new Rational(1, 6);
|
|
|
69
|
+ Rational actual = new Rational(-1, 2).subtract(new Rational(-2, 3));
|
|
|
70
|
+ assertEquals(expected, actual);
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
|
73
|
+ @Ignore("Remove to run test")
|
|
|
74
|
+ @Test
|
|
|
75
|
+ public void testSubtractARationalNumberFromItself() {
|
|
|
76
|
+ Rational expected = new Rational(0, 1);
|
|
|
77
|
+ Rational actual = new Rational(1, 2).subtract(new Rational(1, 2));
|
|
|
78
|
+ assertEquals(expected, actual);
|
|
|
79
|
+ }
|
|
|
80
|
+
|
|
|
81
|
+ @Ignore("Remove to run test")
|
|
|
82
|
+ @Test
|
|
|
83
|
+ public void testMultiplyTwoPositiveRationalNumbers() {
|
|
|
84
|
+ Rational expected = new Rational(1, 3);
|
|
|
85
|
+ Rational actual = new Rational(1, 2).multiply(new Rational(2, 3));
|
|
|
86
|
+ assertEquals(expected, actual);
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ @Ignore("Remove to run test")
|
|
|
90
|
+ @Test
|
|
|
91
|
+ public void testMultiplyANegativeRationalNumberByAPositiveRationalNumber() {
|
|
|
92
|
+ Rational expected = new Rational(-1, 3);
|
|
|
93
|
+ Rational actual = new Rational(-1, 2).multiply(new Rational(2, 3));
|
|
|
94
|
+ assertEquals(expected, actual);
|
|
|
95
|
+ }
|
|
|
96
|
+
|
|
|
97
|
+ @Ignore("Remove to run test")
|
|
|
98
|
+ @Test
|
|
|
99
|
+ public void testMultiplyTwoNegativeRationalNumbers() {
|
|
|
100
|
+ Rational expected = new Rational(1, 3);
|
|
|
101
|
+ Rational actual = new Rational(-1, 2).multiply(new Rational(-2, 3));
|
|
|
102
|
+ assertEquals(expected, actual);
|
|
|
103
|
+ }
|
|
|
104
|
+
|
|
|
105
|
+ @Ignore("Remove to run test")
|
|
|
106
|
+ @Test
|
|
|
107
|
+ public void testMultiplyARationalNumberByItsReciprocal() {
|
|
|
108
|
+ Rational expected = new Rational(1, 1);
|
|
|
109
|
+ Rational actual = new Rational(1, 2).multiply(new Rational(2, 1));
|
|
|
110
|
+ assertEquals(expected, actual);
|
|
|
111
|
+ }
|
|
|
112
|
+
|
|
|
113
|
+ @Ignore("Remove to run test")
|
|
|
114
|
+ @Test
|
|
|
115
|
+ public void testMultiplyARationalNumberByOne() {
|
|
|
116
|
+ Rational expected = new Rational(1, 2);
|
|
|
117
|
+ Rational actual = new Rational(1, 2).multiply(new Rational(1, 1));
|
|
|
118
|
+ assertEquals(expected, actual);
|
|
|
119
|
+ }
|
|
|
120
|
+
|
|
|
121
|
+ @Ignore("Remove to run test")
|
|
|
122
|
+ @Test
|
|
|
123
|
+ public void testMultiplyARationalNumberByZero() {
|
|
|
124
|
+ Rational expected = new Rational(0, 1);
|
|
|
125
|
+ Rational actual = new Rational(1, 2).multiply(new Rational(0, 1));
|
|
|
126
|
+ assertEquals(expected, actual);
|
|
|
127
|
+ }
|
|
|
128
|
+
|
|
|
129
|
+ @Ignore("Remove to run test")
|
|
|
130
|
+ @Test
|
|
|
131
|
+ public void testDivideTwoPositiveRationalNumbers() {
|
|
|
132
|
+ Rational expected = new Rational(3, 4);
|
|
|
133
|
+ Rational actual = new Rational(1, 2).divide(new Rational(2, 3));
|
|
|
134
|
+ assertEquals(expected, actual);
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ @Ignore("Remove to run test")
|
|
|
138
|
+ @Test
|
|
|
139
|
+ public void testDivideAPositiveRationalNumberByANegativeRationalNumber() {
|
|
|
140
|
+ Rational expected = new Rational(-3, 4);
|
|
|
141
|
+ Rational actual = new Rational(1, 2).divide(new Rational(-2, 3));
|
|
|
142
|
+ assertEquals(expected, actual);
|
|
|
143
|
+ }
|
|
|
144
|
+
|
|
|
145
|
+ @Ignore("Remove to run test")
|
|
|
146
|
+ @Test
|
|
|
147
|
+ public void testDivideTwoNegativeRationalNumbers() {
|
|
|
148
|
+ Rational expected = new Rational(3, 4);
|
|
|
149
|
+ Rational actual = new Rational(-1, 2).divide(new Rational(-2, 3));
|
|
|
150
|
+ assertEquals(expected, actual);
|
|
|
151
|
+ }
|
|
|
152
|
+
|
|
|
153
|
+ @Ignore("Remove to run test")
|
|
|
154
|
+ @Test
|
|
|
155
|
+ public void testDivideARationalNumberByOne() {
|
|
|
156
|
+ Rational expected = new Rational(1, 2);
|
|
|
157
|
+ Rational actual = new Rational(1, 2).divide(new Rational(1, 1));
|
|
|
158
|
+ assertEquals(expected, actual);
|
|
|
159
|
+ }
|
|
|
160
|
+
|
|
|
161
|
+ @Ignore("Remove to run test")
|
|
|
162
|
+ @Test
|
|
|
163
|
+ public void testAbsoluteValueOfAPositiveRationalNumber() {
|
|
|
164
|
+ Rational expected = new Rational(1, 2);
|
|
|
165
|
+ Rational actual = new Rational(1, 2).abs();
|
|
|
166
|
+ assertEquals(expected, actual);
|
|
|
167
|
+ }
|
|
|
168
|
+
|
|
|
169
|
+ @Ignore("Remove to run test")
|
|
|
170
|
+ @Test
|
|
|
171
|
+ public void testAbsoluteValueOfANegativeRationalNumber() {
|
|
|
172
|
+ Rational expected = new Rational(1, 2);
|
|
|
173
|
+ Rational actual = new Rational(-1, 2).abs();
|
|
|
174
|
+ assertEquals(expected, actual);
|
|
|
175
|
+ }
|
|
|
176
|
+
|
|
|
177
|
+ @Ignore("Remove to run test")
|
|
|
178
|
+ @Test
|
|
|
179
|
+ public void testAbsoluteValueOfZero() {
|
|
|
180
|
+ Rational expected = new Rational(0, 1);
|
|
|
181
|
+ Rational actual = new Rational(0, 1).abs();
|
|
|
182
|
+ assertEquals(expected, actual);
|
|
|
183
|
+ }
|
|
|
184
|
+
|
|
|
185
|
+ @Ignore("Remove to run test")
|
|
|
186
|
+ @Test
|
|
|
187
|
+ public void testRaiseAPositiveRationalNumberToAPositiveIntegerPower() {
|
|
|
188
|
+ Rational expected = new Rational(1, 8);
|
|
|
189
|
+ Rational actual = new Rational(1, 2).pow(3);
|
|
|
190
|
+ assertEquals(expected, actual);
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+ @Ignore("Remove to run test")
|
|
|
194
|
+ @Test
|
|
|
195
|
+ public void testRaiseANegativeRationalNumberToAPositiveIntegerPower() {
|
|
|
196
|
+ Rational expected = new Rational(-1, 8);
|
|
|
197
|
+ Rational actual = new Rational(-1, 2).pow(3);
|
|
|
198
|
+ assertEquals(expected, actual);
|
|
|
199
|
+ }
|
|
|
200
|
+
|
|
|
201
|
+ @Ignore("Remove to run test")
|
|
|
202
|
+ @Test
|
|
|
203
|
+ public void testRaiseZeroToAnIntegerPower() {
|
|
|
204
|
+ Rational expected = new Rational(0, 1);
|
|
|
205
|
+ Rational actual = new Rational(0, 1).pow(5);
|
|
|
206
|
+ assertEquals(expected, actual);
|
|
|
207
|
+ }
|
|
|
208
|
+
|
|
|
209
|
+ @Ignore("Remove to run test")
|
|
|
210
|
+ @Test
|
|
|
211
|
+ public void testRaiseOneToAnIntegerPower() {
|
|
|
212
|
+ Rational expected = new Rational(1, 1);
|
|
|
213
|
+ Rational actual = new Rational(1, 1).pow(4);
|
|
|
214
|
+ assertEquals(expected, actual);
|
|
|
215
|
+ }
|
|
|
216
|
+
|
|
|
217
|
+ @Ignore("Remove to run test")
|
|
|
218
|
+ @Test
|
|
|
219
|
+ public void testRaiseAPositiveRationalNumberToThePowerOfZero() {
|
|
|
220
|
+ Rational expected = new Rational(1, 1);
|
|
|
221
|
+ Rational actual = new Rational(-1, 2).pow(0);
|
|
|
222
|
+ assertEquals(expected, actual);
|
|
|
223
|
+ }
|
|
|
224
|
+
|
|
|
225
|
+ @Ignore("Remove to run test")
|
|
|
226
|
+ @Test
|
|
|
227
|
+ public void testRaiseARealNumberToAPositiveRationalNumber() {
|
|
|
228
|
+ double expected = 16.0;
|
|
|
229
|
+ double actual = new Rational(4, 3).exp(8.0);
|
|
|
230
|
+ assertDoublesEqual(expected, actual);
|
|
|
231
|
+ }
|
|
|
232
|
+
|
|
|
233
|
+ @Ignore("Remove to run test")
|
|
|
234
|
+ @Test
|
|
|
235
|
+ public void testRaiseARealNumberToANegativeRationalNumber() {
|
|
|
236
|
+ double expected = 1.0 / 3;
|
|
|
237
|
+ double actual = new Rational(-1, 2).exp(9);
|
|
|
238
|
+ assertDoublesEqual(expected, actual);
|
|
|
239
|
+ }
|
|
|
240
|
+
|
|
|
241
|
+ @Ignore("Remove to run test")
|
|
|
242
|
+ @Test
|
|
|
243
|
+ public void testReduceAPositiveRationalNumberToLowestTerms() {
|
|
|
244
|
+ Rational expected = new Rational(1, 2);
|
|
|
245
|
+ Rational actual = new Rational(2, 4);
|
|
|
246
|
+ assertEquals(expected, actual);
|
|
|
247
|
+ }
|
|
|
248
|
+
|
|
|
249
|
+ @Ignore("Remove to run test")
|
|
|
250
|
+ @Test
|
|
|
251
|
+ public void testReduceANegativeRationalNumberToLowestTerms() {
|
|
|
252
|
+ Rational expected = new Rational(-2, 3);
|
|
|
253
|
+ Rational actual = new Rational(-4, 6);
|
|
|
254
|
+ assertEquals(expected, actual);
|
|
|
255
|
+ }
|
|
|
256
|
+
|
|
|
257
|
+ @Ignore("Remove to run test")
|
|
|
258
|
+ @Test
|
|
|
259
|
+ public void testReduceARationalNumberWithANegativeDenominatorToLowestTerms() {
|
|
|
260
|
+ Rational expected = new Rational(-1, 3);
|
|
|
261
|
+ Rational actual = new Rational(3, -9);
|
|
|
262
|
+ assertEquals(expected, actual);
|
|
|
263
|
+ }
|
|
|
264
|
+
|
|
|
265
|
+ @Ignore("Remove to run test")
|
|
|
266
|
+ @Test
|
|
|
267
|
+ public void testReduceZeroToLowestTerms() {
|
|
|
268
|
+ Rational expected = new Rational(0, 1);
|
|
|
269
|
+ Rational actual = new Rational(0, 6);
|
|
|
270
|
+ assertEquals(expected, actual);
|
|
|
271
|
+ }
|
|
|
272
|
+
|
|
|
273
|
+ @Ignore("Remove to run test")
|
|
|
274
|
+ @Test
|
|
|
275
|
+ public void testReduceAnIntegerToLowestTerms() {
|
|
|
276
|
+ Rational expected = new Rational(-2, 1);
|
|
|
277
|
+ Rational actual = new Rational(-14, 7);
|
|
|
278
|
+ assertEquals(expected, actual);
|
|
|
279
|
+ }
|
|
|
280
|
+
|
|
|
281
|
+ @Ignore("Remove to run test")
|
|
|
282
|
+ @Test
|
|
|
283
|
+ public void testReduceOneToLowestTerms() {
|
|
|
284
|
+ Rational expected = new Rational(1, 1);
|
|
|
285
|
+ Rational actual = new Rational(13, 13);
|
|
|
286
|
+ assertEquals(expected, actual);
|
|
|
287
|
+ }
|
|
|
288
|
+}
|