|
| 1 | +import org.junit.Ignore; |
| 2 | +import org.junit.Test; |
| 3 | + |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | + |
| 6 | +public class ComplexNumberTest { |
| 7 | + |
| 8 | + // Test helpers |
| 9 | + |
| 10 | + private static final double DOUBLE_EQUALITY_TOLERANCE = 1e-15; |
| 11 | + |
| 12 | + private void assertDoublesEqual(double d1, double d2) { |
| 13 | + assertEquals(d1, d2, DOUBLE_EQUALITY_TOLERANCE); |
| 14 | + } |
| 15 | + |
| 16 | + private void assertComplexNumbersEqual(ComplexNumber c1, ComplexNumber c2) { |
| 17 | + assertDoublesEqual(c1.getReal(), c2.getReal()); |
| 18 | + assertDoublesEqual(c1.getImag(), c2.getImag()); |
| 19 | + } |
| 20 | + |
| 21 | + // Tests |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testImaginaryUnitExhibitsDefiningProperty() { |
| 25 | + ComplexNumber expected = new ComplexNumber(-1.0, 0); |
| 26 | + ComplexNumber actual = new ComplexNumber(0, 1.0).times(new ComplexNumber(0, 1.0)); |
| 27 | + assertComplexNumbersEqual(expected, actual); |
| 28 | + } |
| 29 | + |
| 30 | + @Ignore("Remove to run test") |
| 31 | + @Test |
| 32 | + public void testAdditionWithPurelyRealNumbers() { |
| 33 | + ComplexNumber expected = new ComplexNumber(3.0, 0); |
| 34 | + ComplexNumber actual = new ComplexNumber(1.0, 0).add(new ComplexNumber(2.0, 0)); |
| 35 | + assertComplexNumbersEqual(expected, actual); |
| 36 | + } |
| 37 | + |
| 38 | + @Ignore("Remove to run test") |
| 39 | + @Test |
| 40 | + public void testAdditionWithPurelyImaginaryNumbers() { |
| 41 | + ComplexNumber expected = new ComplexNumber(0, 3.0); |
| 42 | + ComplexNumber actual = new ComplexNumber(0, 1.0).add(new ComplexNumber(0, 2.0)); |
| 43 | + assertComplexNumbersEqual(expected, actual); |
| 44 | + } |
| 45 | + |
| 46 | + @Ignore("Remove to run test") |
| 47 | + @Test |
| 48 | + public void testAdditionWithRealAndImaginaryParts() { |
| 49 | + ComplexNumber expected = new ComplexNumber(4.0, 6.0); |
| 50 | + ComplexNumber actual = new ComplexNumber(1.0, 2.0).add(new ComplexNumber(3.0, 4.0)); |
| 51 | + assertComplexNumbersEqual(expected, actual); |
| 52 | + } |
| 53 | + |
| 54 | + @Ignore("Remove to run test") |
| 55 | + @Test |
| 56 | + public void testSubtractionWithPurelyRealNumbers() { |
| 57 | + ComplexNumber expected = new ComplexNumber(-1.0, 0.0); |
| 58 | + ComplexNumber actual = new ComplexNumber(1.0, 0.0).minus(new ComplexNumber(2.0, 0.0)); |
| 59 | + assertComplexNumbersEqual(expected, actual); |
| 60 | + } |
| 61 | + |
| 62 | + @Ignore("Remove to run test") |
| 63 | + @Test |
| 64 | + public void testSubtractionWithPurelyImaginaryNumbers() { |
| 65 | + ComplexNumber expected = new ComplexNumber(0, -1.0); |
| 66 | + ComplexNumber actual = new ComplexNumber(0, 1.0).minus(new ComplexNumber(0, 2.0)); |
| 67 | + assertComplexNumbersEqual(expected, actual); |
| 68 | + } |
| 69 | + |
| 70 | + @Ignore("Remove to run test") |
| 71 | + @Test |
| 72 | + public void testSubtractionWithRealAndImaginaryParts() { |
| 73 | + ComplexNumber expected = new ComplexNumber(-2.0, -2.0); |
| 74 | + ComplexNumber actual = new ComplexNumber(1.0, 2.0).minus(new ComplexNumber(3.0, 4.0)); |
| 75 | + assertComplexNumbersEqual(expected, actual); |
| 76 | + } |
| 77 | + |
| 78 | + @Ignore("Remove to run test") |
| 79 | + @Test |
| 80 | + public void testMultiplicationWithPurelyRealNumbers() { |
| 81 | + ComplexNumber expected = new ComplexNumber(2.0, 0); |
| 82 | + ComplexNumber actual = new ComplexNumber(1.0, 0).times(new ComplexNumber(2.0, 0)); |
| 83 | + assertComplexNumbersEqual(expected, actual); |
| 84 | + } |
| 85 | + |
| 86 | + @Ignore("Remove to run test") |
| 87 | + @Test |
| 88 | + public void testMultiplicationWithPurelyImaginaryNumbers() { |
| 89 | + ComplexNumber expected = new ComplexNumber(-2.0, 0); |
| 90 | + ComplexNumber actual = new ComplexNumber(0, 1.0).times(new ComplexNumber(0, 2.0)); |
| 91 | + assertComplexNumbersEqual(expected, actual); |
| 92 | + } |
| 93 | + |
| 94 | + @Ignore("Remove to run test") |
| 95 | + @Test |
| 96 | + public void testMultiplicationWithRealAndImaginaryParts() { |
| 97 | + ComplexNumber expected = new ComplexNumber(-5.0, 10.0); |
| 98 | + ComplexNumber actual = new ComplexNumber(1.0, 2.0).times(new ComplexNumber(3.0, 4.0)); |
| 99 | + assertComplexNumbersEqual(expected, actual); |
| 100 | + } |
| 101 | + |
| 102 | + @Ignore("Remove to run test") |
| 103 | + @Test |
| 104 | + public void testDivisionWithPurelyRealNumbers() { |
| 105 | + ComplexNumber expected = new ComplexNumber(0.5, 0); |
| 106 | + ComplexNumber actual = new ComplexNumber(1.0, 0).div(new ComplexNumber(2.0, 0)); |
| 107 | + assertComplexNumbersEqual(expected, actual); |
| 108 | + } |
| 109 | + |
| 110 | + @Ignore("Remove to run test") |
| 111 | + @Test |
| 112 | + public void testDivisionWithPurelyImaginaryNumbers() { |
| 113 | + ComplexNumber expected = new ComplexNumber(0.5, 0); |
| 114 | + ComplexNumber actual = new ComplexNumber(0, 1.0).div(new ComplexNumber(0, 2.0)); |
| 115 | + assertComplexNumbersEqual(expected, actual); |
| 116 | + } |
| 117 | + |
| 118 | + @Ignore("Remove to run test") |
| 119 | + @Test |
| 120 | + public void testDivisionWithRealAndImaginaryParts() { |
| 121 | + ComplexNumber expected = new ComplexNumber(0.44, 0.08); |
| 122 | + ComplexNumber actual = new ComplexNumber(1.0, 2.0).div(new ComplexNumber(3.0, 4.0)); |
| 123 | + assertComplexNumbersEqual(expected, actual); |
| 124 | + } |
| 125 | + |
| 126 | + @Ignore("Remove to run test") |
| 127 | + @Test |
| 128 | + public void testAbsoluteValueOfPositivePurelyRealNumber() { |
| 129 | + double expected = 5.0; |
| 130 | + double actual = new ComplexNumber(5.0, 0).abs(); |
| 131 | + assertDoublesEqual(expected, actual); |
| 132 | + } |
| 133 | + |
| 134 | + @Ignore("Remove to run test") |
| 135 | + @Test |
| 136 | + public void testAbsoluteValueOfNegativePurelyRealNumber() { |
| 137 | + double expected = 5.0; |
| 138 | + double actual = new ComplexNumber(-5.0, 0).abs(); |
| 139 | + assertDoublesEqual(expected, actual); |
| 140 | + } |
| 141 | + |
| 142 | + @Ignore("Remove to run test") |
| 143 | + @Test |
| 144 | + public void testAbsoluteValueOfPurelyImaginaryNumberWithPositiveImaginaryPart() { |
| 145 | + double expected = 5.0; |
| 146 | + double actual = new ComplexNumber(0, 5.0).abs(); |
| 147 | + assertDoublesEqual(expected, actual); |
| 148 | + } |
| 149 | + |
| 150 | + @Ignore("Remove to run test") |
| 151 | + @Test |
| 152 | + public void testAbsoluteValueOfPurelyImaginaryNumberWithNegativeImaginaryPart() { |
| 153 | + double expected = 5.0; |
| 154 | + double actual = new ComplexNumber(0, -5.0).abs(); |
| 155 | + assertDoublesEqual(expected, actual); |
| 156 | + } |
| 157 | + |
| 158 | + @Ignore("Remove to run test") |
| 159 | + @Test |
| 160 | + public void testAbsoluteValueOfNumberWithRealAndImaginaryParts() { |
| 161 | + double expected = 5.0; |
| 162 | + double actual = new ComplexNumber(3.0, 4.0).abs(); |
| 163 | + assertDoublesEqual(expected, actual); |
| 164 | + } |
| 165 | + |
| 166 | + @Ignore("Remove to run test") |
| 167 | + @Test |
| 168 | + public void testConjugationOfPurelyRealNumber() { |
| 169 | + ComplexNumber expected = new ComplexNumber(5.0, 0); |
| 170 | + ComplexNumber actual = new ComplexNumber(5.0, 0).conjugate(); |
| 171 | + assertComplexNumbersEqual(expected, actual); |
| 172 | + } |
| 173 | + |
| 174 | + @Ignore("Remove to run test") |
| 175 | + @Test |
| 176 | + public void testConjugationOfPurelyImaginaryNumber() { |
| 177 | + ComplexNumber expected = new ComplexNumber(0, -5.0); |
| 178 | + ComplexNumber actual = new ComplexNumber(0, 5.0).conjugate(); |
| 179 | + assertComplexNumbersEqual(expected, actual); |
| 180 | + } |
| 181 | + |
| 182 | + @Ignore("Remove to run test") |
| 183 | + @Test |
| 184 | + public void testConjugationOfNumberWithRealAndImaginaryParts() { |
| 185 | + ComplexNumber expected = new ComplexNumber(1.0, -1.0); |
| 186 | + ComplexNumber actual = new ComplexNumber(1.0, 1.0).conjugate(); |
| 187 | + assertComplexNumbersEqual(expected, actual); |
| 188 | + } |
| 189 | + |
| 190 | + @Ignore("Remove to run test") |
| 191 | + @Test |
| 192 | + public void testRealPartOfPurelyRealNumber() { |
| 193 | + double expected = 1.0; |
| 194 | + double actual = new ComplexNumber(1.0, 0).getReal(); |
| 195 | + assertDoublesEqual(expected, actual); |
| 196 | + } |
| 197 | + |
| 198 | + @Ignore("Remove to run test") |
| 199 | + @Test |
| 200 | + public void testRealPartOfPurelyImaginaryNumber() { |
| 201 | + double expected = 0.0; |
| 202 | + double actual = new ComplexNumber(0, 1.0).getReal(); |
| 203 | + assertDoublesEqual(expected, actual); |
| 204 | + } |
| 205 | + |
| 206 | + @Ignore("Remove to run test") |
| 207 | + @Test |
| 208 | + public void testRealPartOfNumberWithRealAndImaginaryParts() { |
| 209 | + double expected = 1.0; |
| 210 | + double actual = new ComplexNumber(1.0, 2.0).getReal(); |
| 211 | + assertDoublesEqual(expected, actual); |
| 212 | + } |
| 213 | + |
| 214 | + @Ignore("Remove to run test") |
| 215 | + @Test |
| 216 | + public void testImaginaryPartOfPurelyRealNumber() { |
| 217 | + double expected = 0.0; |
| 218 | + double actual = new ComplexNumber(1.0, 0).getImag(); |
| 219 | + assertDoublesEqual(expected, actual); |
| 220 | + } |
| 221 | + |
| 222 | + @Ignore("Remove to run test") |
| 223 | + @Test |
| 224 | + public void testImaginaryPartOfPurelyImaginaryNumber() { |
| 225 | + double expected = 1.0; |
| 226 | + double actual = new ComplexNumber(0, 1.0).getImag(); |
| 227 | + assertDoublesEqual(expected, actual); |
| 228 | + } |
| 229 | + |
| 230 | + @Ignore("Remove to run test") |
| 231 | + @Test |
| 232 | + public void testImaginaryPartOfNumberWithRealAndImaginaryParts() { |
| 233 | + double expected = 2.0; |
| 234 | + double actual = new ComplexNumber(1.0, 2.0).getImag(); |
| 235 | + assertDoublesEqual(expected, actual); |
| 236 | + } |
| 237 | + |
| 238 | + @Ignore("Remove to run test") |
| 239 | + @Test |
| 240 | + public void testExponentialOfPurelyImaginaryNumber() { |
| 241 | + ComplexNumber expected = new ComplexNumber(-1.0, 0); |
| 242 | + ComplexNumber actual = new ComplexNumber(0, Math.PI).exponentialOf(); |
| 243 | + assertComplexNumbersEqual(expected, actual); |
| 244 | + } |
| 245 | + |
| 246 | + @Ignore("Remove to run test") |
| 247 | + @Test |
| 248 | + public void testExponentialOfZero() { |
| 249 | + ComplexNumber expected = new ComplexNumber(1.0, 0); |
| 250 | + ComplexNumber actual = new ComplexNumber(0, 0).exponentialOf(); |
| 251 | + assertComplexNumbersEqual(expected, actual); |
| 252 | + } |
| 253 | + |
| 254 | + @Ignore("Remove to run test") |
| 255 | + @Test |
| 256 | + public void testExponentialOfPurelyRealNumber() { |
| 257 | + ComplexNumber expected = new ComplexNumber(Math.E, 0); |
| 258 | + ComplexNumber actual = new ComplexNumber(1.0, 0).exponentialOf(); |
| 259 | + assertComplexNumbersEqual(expected, actual); |
| 260 | + } |
| 261 | + |
| 262 | +} |
0 commit comments