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