@@ -72,6 +72,10 @@ public void testToSimpleString() throws Exception
72
72
Rational twoThirds = new Rational (10 , 15 );
73
73
assertEquals ("2/3" , twoThirds .toSimpleString (true ));
74
74
75
+ Rational twoSixths = new Rational (2 , 6 );
76
+ assertEquals ("1/3" , twoSixths .toSimpleString (true ));
77
+ assertEquals ("1/3" , twoSixths .toSimpleString (false ));
78
+
75
79
Rational two = new Rational (10 , 5 );
76
80
assertTrue (two .isInteger ());
77
81
assertEquals ("2" , two .toSimpleString (true ));
@@ -93,10 +97,6 @@ public void testToSimpleString() throws Exception
93
97
assertTrue (zero .isInteger ());
94
98
assertEquals ("0" , zero .toSimpleString (true ));
95
99
assertEquals ("0" , zero .toSimpleString (false ));
96
-
97
- // not sure this is a nice presentation of rationals. won't implement it for now.
98
- // Rational twoAndAHalf = new Rational(10,4);
99
- // assertEquals("2 1/2", twoAndAHalf.toSimpleString());
100
100
}
101
101
102
102
@ Test
@@ -215,7 +215,7 @@ public void simplifiedInstances()
215
215
assertEquals (actualSimple .doubleValue (), complex .doubleValue (), 0.0001 );
216
216
}
217
217
218
- simple = new Rational (1 , - 2 );
218
+ simple = new Rational (- 1 , 2 );
219
219
220
220
for (int prime : _primes )
221
221
{
@@ -226,7 +226,7 @@ public void simplifiedInstances()
226
226
assertEquals (actualSimple .doubleValue (), complex .doubleValue (), 0.0001 );
227
227
}
228
228
229
- simple = new Rational (- 1 , - 2 );
229
+ simple = new Rational (1 , 2 );
230
230
231
231
for (int prime : _primes )
232
232
{
@@ -241,4 +241,25 @@ public void simplifiedInstances()
241
241
assertEquals (new Rational (-32768 , 32767 ), new Rational (-32768 , 32767 ).getSimplifiedInstance ());
242
242
}
243
243
244
+ @ Test
245
+ public void getSimplifiedInstance_FlipsSignsIfNeeded ()
246
+ {
247
+ Rational r = new Rational (1 , -2 );
248
+
249
+ Rational s = r .getSimplifiedInstance ();
250
+
251
+ assertEquals (-1 , s .getNumerator ());
252
+ assertEquals (2 , s .getDenominator ());
253
+ }
254
+
255
+ @ Test
256
+ public void getSimplifiedInstance_RemovesSignsIfNeeded ()
257
+ {
258
+ Rational r = new Rational (-1 , -2 );
259
+
260
+ Rational s = r .getSimplifiedInstance ();
261
+
262
+ assertEquals (1 , s .getNumerator ());
263
+ assertEquals (2 , s .getDenominator ());
264
+ }
244
265
}
0 commit comments