trackler 2.2.1.124 → 2.2.1.125
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trackler/version.rb +1 -1
- data/tracks/delphi/exercises/book-store/uBookStoreExample.pas +7 -0
- data/tracks/swift/config.json +11 -0
- data/tracks/swift/exercises/complex-numbers/.gitignore +4 -0
- data/tracks/swift/exercises/complex-numbers/Package.swift +5 -0
- data/tracks/swift/exercises/complex-numbers/README.md +29 -0
- data/tracks/swift/exercises/complex-numbers/Sources/ComplexNumbers.swift +1 -0
- data/tracks/swift/exercises/complex-numbers/Sources/ComplexNumbersExample.swift +66 -0
- data/tracks/swift/exercises/complex-numbers/Tests/ComplexNumbersTests/ComplexNumbersTests.swift +321 -0
- data/tracks/swift/exercises/complex-numbers/Tests/LinuxMain.swift +6 -0
- data/tracks/typescript/config.json +13 -0
- data/tracks/typescript/exercises/accumulate/README.md +58 -0
- data/tracks/typescript/exercises/accumulate/accumulate.example.ts +12 -0
- data/tracks/typescript/exercises/accumulate/accumulate.test.ts +32 -0
- data/tracks/typescript/exercises/accumulate/accumulate.ts +0 -0
- data/tracks/typescript/exercises/accumulate/package.json +36 -0
- data/tracks/typescript/exercises/accumulate/tsconfig.json +22 -0
- data/tracks/typescript/exercises/accumulate/tslint.json +127 -0
- data/tracks/typescript/exercises/accumulate/yarn.lock +2624 -0
- data/tracks/typescript/exercises/anagram/anagram.ts +5 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fbe6714f67121bde0d0bff60621209109e983f88
|
4
|
+
data.tar.gz: e94a247d8e07206e2b18a5b8adbf65ccba59d38b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 555d14a7c7a07b0c18b25be4ce44d4a4519940c508781fa8e8b7817614b9d4c2aa2d744f935b8c5ef08f87f16e288e403dbcb6a8f4c8711f6ad2ad870093b6f1
|
7
|
+
data.tar.gz: 38ecbbd2bc9a048f757c5c98a1d7a03fe0dba0b422453f5afeab05b9e9262671afd91795f144ca4f7d675565d49a6292bfff2b8cbddb9da8db1ffe11eff05fdf
|
data/lib/trackler/version.rb
CHANGED
@@ -37,6 +37,7 @@ type
|
|
37
37
|
public
|
38
38
|
function Total:integer;
|
39
39
|
constructor Create(aBasket: TArray<Integer>);
|
40
|
+
destructor Destroy; override;
|
40
41
|
end;
|
41
42
|
|
42
43
|
function NewBasket(aBasket: TArray<Integer>): IBasket;
|
@@ -145,6 +146,12 @@ begin
|
|
145
146
|
result := min(subResult[0], subResult[1]);
|
146
147
|
end;
|
147
148
|
|
149
|
+
destructor TBasket.Destroy;
|
150
|
+
begin
|
151
|
+
fIntList.DisposeOf;
|
152
|
+
inherited;
|
153
|
+
end;
|
154
|
+
|
148
155
|
function TBasket.DiscountPercentage(inStr : string):extended;
|
149
156
|
var numDiffBooks: integer;
|
150
157
|
begin
|
data/tracks/swift/config.json
CHANGED
@@ -853,6 +853,17 @@
|
|
853
853
|
"unlocked_by": null,
|
854
854
|
"uuid": "a7074b04-3ac5-4437-817e-2dc31d9d8ae0"
|
855
855
|
},
|
856
|
+
{
|
857
|
+
"core": false,
|
858
|
+
"difficulty": 6,
|
859
|
+
"slug": "complex-numbers",
|
860
|
+
"topics": [
|
861
|
+
"mathematics",
|
862
|
+
"tuples"
|
863
|
+
],
|
864
|
+
"unlocked_by": "leap",
|
865
|
+
"uuid": "5a918ed8-e840-46b8-b824-291d872d2224"
|
866
|
+
},
|
856
867
|
{
|
857
868
|
"core": false,
|
858
869
|
"difficulty": 7,
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Complex Numbers
|
2
|
+
|
3
|
+
A complex number is a number in the form a + b * i where a and b are real and i satisfies i^2 = -1.
|
4
|
+
|
5
|
+
a is called the real part and b is called the imaginary part of z. The conjugate of the number a + b * i is the number a - b * i. The absolute value of a complex number z = a + b * i is a real number |z| = sqrt(a^2 + b^2). The square of the absolute value |z|^2 is the result of multiplication of z by its complex conjugate.
|
6
|
+
|
7
|
+
The sum/difference of two complex numbers involves adding/subtracting their real and imaginary parts separately: (a + i * b) + (c + i * d) = (a + c) + (b + d) * i, (a + i * b) - (c + i * d) = (a - c) + (b - d) * i.
|
8
|
+
|
9
|
+
Multiplication result is by definition (a + i * b) * (c + i * d) = (a * c - b * d) + (b * c + a * d) * i.
|
10
|
+
|
11
|
+
The reciprocal of a non-zero complex number is 1 / (a + i * b) = a/(a^2 + b^2) - b/(a^2 + b^2) * i.
|
12
|
+
|
13
|
+
Dividing a complex number a + i * b by another c + i * d gives: (a + i * b) / (c + i * d) = (a * c + b * d)/(c^2 + d^2) + (b * c - a * d)/(c^2 + d^2) * i.
|
14
|
+
|
15
|
+
Exponent of a complex number can be expressed as exp(a + i * b) = exp(a) * exp(i * b), and the last term is given by Euler's formula exp(i * b) = cos(b) + i * sin(b).
|
16
|
+
|
17
|
+
Implement the following operations:
|
18
|
+
|
19
|
+
addition, subtraction, multiplication and division of two complex numbers,
|
20
|
+
conjugate, absolute value, exponent of a given complex number.
|
21
|
+
Assume the programming language you are using does not have an implementation of complex numbers.
|
22
|
+
|
23
|
+
## Source
|
24
|
+
|
25
|
+
Wikipedia https://en.wikipedia.org/wiki/Complex_number
|
26
|
+
|
27
|
+
## Submitting Incomplete Solutions
|
28
|
+
|
29
|
+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
@@ -0,0 +1 @@
|
|
1
|
+
//Solution goes in Sources
|
@@ -0,0 +1,66 @@
|
|
1
|
+
import Foundation
|
2
|
+
|
3
|
+
struct ComplexNumber {
|
4
|
+
|
5
|
+
var realComponent: Double
|
6
|
+
|
7
|
+
var imaginaryComponent: Double
|
8
|
+
|
9
|
+
func getRealComponent() -> Double {
|
10
|
+
|
11
|
+
return self.realComponent
|
12
|
+
}
|
13
|
+
|
14
|
+
func getImaginaryComponent() -> Double {
|
15
|
+
|
16
|
+
return self.imaginaryComponent
|
17
|
+
}
|
18
|
+
|
19
|
+
func add(complexNumber: ComplexNumber) -> ComplexNumber {
|
20
|
+
|
21
|
+
return ComplexNumber(realComponent: self.realComponent + complexNumber.realComponent, imaginaryComponent: self.imaginaryComponent + complexNumber.imaginaryComponent)
|
22
|
+
|
23
|
+
}
|
24
|
+
|
25
|
+
func subtract(complexNumber: ComplexNumber) -> ComplexNumber {
|
26
|
+
|
27
|
+
return ComplexNumber(realComponent: self.realComponent - complexNumber.realComponent, imaginaryComponent: self.imaginaryComponent - complexNumber.imaginaryComponent)
|
28
|
+
}
|
29
|
+
|
30
|
+
func multiply(complexNumber: ComplexNumber) -> ComplexNumber {
|
31
|
+
|
32
|
+
return ComplexNumber(realComponent: self.realComponent * complexNumber.realComponent - self.imaginaryComponent * complexNumber.imaginaryComponent, imaginaryComponent: self.imaginaryComponent * complexNumber.realComponent + self.realComponent * complexNumber.imaginaryComponent)
|
33
|
+
}
|
34
|
+
|
35
|
+
func divide(complexNumber: ComplexNumber) -> ComplexNumber {
|
36
|
+
|
37
|
+
let amplitudeOfComplexNumber = (complexNumber.realComponent * complexNumber.realComponent) + (complexNumber.imaginaryComponent * complexNumber.imaginaryComponent)
|
38
|
+
|
39
|
+
let realPartOfQuotient = (self.realComponent * complexNumber.realComponent + self.imaginaryComponent * complexNumber.imaginaryComponent) / amplitudeOfComplexNumber
|
40
|
+
|
41
|
+
let imaginaryPartOfQuotient = (self.imaginaryComponent * complexNumber.realComponent - self.realComponent * self.realComponent * complexNumber.imaginaryComponent) / amplitudeOfComplexNumber
|
42
|
+
|
43
|
+
return ComplexNumber(realComponent: realPartOfQuotient, imaginaryComponent: imaginaryPartOfQuotient)
|
44
|
+
}
|
45
|
+
|
46
|
+
func conjugate() -> ComplexNumber {
|
47
|
+
|
48
|
+
return ComplexNumber(realComponent: self.realComponent, imaginaryComponent: (-1 * self.imaginaryComponent))
|
49
|
+
}
|
50
|
+
|
51
|
+
func absolute() -> Double {
|
52
|
+
|
53
|
+
return sqrt(pow(self.realComponent, 2.0) + pow(self.imaginaryComponent, 2.0))
|
54
|
+
}
|
55
|
+
|
56
|
+
func exponent() -> ComplexNumber {
|
57
|
+
|
58
|
+
let realPartOfResult = cos(self.imaginaryComponent)
|
59
|
+
let imaginaryPartOfResult = sin(self.imaginaryComponent)
|
60
|
+
let factor = exp(self.realComponent)
|
61
|
+
|
62
|
+
return ComplexNumber(realComponent: realPartOfResult * factor, imaginaryComponent: imaginaryPartOfResult * factor)
|
63
|
+
|
64
|
+
}
|
65
|
+
|
66
|
+
}
|
data/tracks/swift/exercises/complex-numbers/Tests/ComplexNumbersTests/ComplexNumbersTests.swift
ADDED
@@ -0,0 +1,321 @@
|
|
1
|
+
import XCTest
|
2
|
+
@testable import ComplexNumbers
|
3
|
+
|
4
|
+
class ComplexNumbersTests: XCTestCase {
|
5
|
+
|
6
|
+
override func setUp() {
|
7
|
+
super.setUp()
|
8
|
+
// Put setup code here. This method is called before the invocation of each test method in the class.
|
9
|
+
}
|
10
|
+
|
11
|
+
override func tearDown() {
|
12
|
+
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
13
|
+
super.tearDown()
|
14
|
+
}
|
15
|
+
|
16
|
+
func testRealPartOfPurelyRealNumber() {
|
17
|
+
|
18
|
+
let input = ComplexNumber(realComponent: 1, imaginaryComponent: 0)
|
19
|
+
let expectedOutput = Double(1)
|
20
|
+
XCTAssertEqual(expectedOutput, input.getRealComponent())
|
21
|
+
}
|
22
|
+
|
23
|
+
func testRealPartOfPurelyImaginaryNumber() {
|
24
|
+
|
25
|
+
let input = ComplexNumber(realComponent: 0, imaginaryComponent: 1)
|
26
|
+
let expectedOutput = Double(1)
|
27
|
+
XCTAssertEqual(expectedOutput, input.getImaginaryComponent())
|
28
|
+
}
|
29
|
+
|
30
|
+
func testRealPartOfNumberWithRealAndImaginary() {
|
31
|
+
|
32
|
+
let input = ComplexNumber(realComponent: 1, imaginaryComponent: 2)
|
33
|
+
let expectedOutput = Double(1)
|
34
|
+
XCTAssertEqual(expectedOutput, input.getRealComponent())
|
35
|
+
}
|
36
|
+
|
37
|
+
func testImaginaryPartOfPurelyRealNumber() {
|
38
|
+
|
39
|
+
let input = ComplexNumber(realComponent: 1, imaginaryComponent: 0)
|
40
|
+
let expectedOutput = Double(0)
|
41
|
+
XCTAssertEqual(expectedOutput, input.getImaginaryComponent())
|
42
|
+
}
|
43
|
+
|
44
|
+
func testImaginaryPartOfPurelyImaginaryNumber() {
|
45
|
+
|
46
|
+
let input = ComplexNumber(realComponent: 0, imaginaryComponent: 1)
|
47
|
+
let expectedOutput = Double(1)
|
48
|
+
XCTAssertEqual(expectedOutput, input.getImaginaryComponent())
|
49
|
+
}
|
50
|
+
|
51
|
+
func testImaginaryPartOfNumberWithRealAndImaginary() {
|
52
|
+
|
53
|
+
let input = ComplexNumber(realComponent: 1, imaginaryComponent: 2)
|
54
|
+
let expectedOutput = Double(2)
|
55
|
+
XCTAssertEqual(expectedOutput, input.getImaginaryComponent())
|
56
|
+
}
|
57
|
+
|
58
|
+
func testImaginaryUnit() {
|
59
|
+
|
60
|
+
let multiplicand = ComplexNumber(realComponent: 0, imaginaryComponent: 1)
|
61
|
+
let multiplier = ComplexNumber(realComponent: 0, imaginaryComponent: 1)
|
62
|
+
let expectedProduct = ComplexNumber(realComponent: -1, imaginaryComponent: 0)
|
63
|
+
let actualProduct = multiplicand.multiply(complexNumber: multiplier)
|
64
|
+
XCTAssertEqual([expectedProduct.realComponent, expectedProduct.imaginaryComponent], [actualProduct.realComponent, actualProduct.imaginaryComponent])
|
65
|
+
}
|
66
|
+
|
67
|
+
func testAddPurelyRealNumbers() {
|
68
|
+
|
69
|
+
let addend = ComplexNumber(realComponent: 1, imaginaryComponent: 0)
|
70
|
+
let augend = ComplexNumber(realComponent: 2, imaginaryComponent: 0)
|
71
|
+
let expectedSum = ComplexNumber(realComponent: 3, imaginaryComponent: 0)
|
72
|
+
let actualSum = ComplexNumber(realComponent: addend.add(complexNumber: augend).realComponent, imaginaryComponent: addend.add(complexNumber: augend).imaginaryComponent)
|
73
|
+
XCTAssertEqual([expectedSum.realComponent, expectedSum.imaginaryComponent], [actualSum.realComponent, actualSum.imaginaryComponent])
|
74
|
+
}
|
75
|
+
|
76
|
+
func testAddPurelyImaginaryNumbers() {
|
77
|
+
|
78
|
+
let addend = ComplexNumber(realComponent: 0, imaginaryComponent: 1)
|
79
|
+
let augend = ComplexNumber(realComponent: 0, imaginaryComponent: 2)
|
80
|
+
let expectedSum = ComplexNumber(realComponent: 0, imaginaryComponent: 3)
|
81
|
+
let actualSum = addend.add(complexNumber: augend)
|
82
|
+
|
83
|
+
XCTAssertEqual([expectedSum.realComponent, expectedSum.imaginaryComponent], [actualSum.realComponent, actualSum.imaginaryComponent])
|
84
|
+
}
|
85
|
+
|
86
|
+
func testAddNumbersWithRealAndImaginaryParts() {
|
87
|
+
|
88
|
+
let addend = ComplexNumber(realComponent: 1, imaginaryComponent: 2)
|
89
|
+
let augend = ComplexNumber(realComponent: 3, imaginaryComponent: 4)
|
90
|
+
let expectedSum = ComplexNumber(realComponent: 4, imaginaryComponent: 6)
|
91
|
+
let actualSum = addend.add(complexNumber: augend)
|
92
|
+
|
93
|
+
XCTAssertEqual([expectedSum.realComponent, expectedSum.imaginaryComponent], [actualSum.realComponent, actualSum.imaginaryComponent])
|
94
|
+
}
|
95
|
+
|
96
|
+
func testSubtractPurelyRealNumbers() {
|
97
|
+
|
98
|
+
let subtrahend = ComplexNumber(realComponent: 1, imaginaryComponent: 0)
|
99
|
+
let minuend = ComplexNumber(realComponent: 2, imaginaryComponent: 0)
|
100
|
+
let expectedDifference = ComplexNumber(realComponent: -1, imaginaryComponent: 0)
|
101
|
+
let actualDifference = subtrahend.subtract(complexNumber: minuend)
|
102
|
+
|
103
|
+
XCTAssertEqual([expectedDifference.realComponent, expectedDifference.imaginaryComponent], [actualDifference.realComponent, actualDifference.imaginaryComponent])
|
104
|
+
}
|
105
|
+
|
106
|
+
func testSubtractPurelyImaginaryNumbers() {
|
107
|
+
|
108
|
+
let subtrahend = ComplexNumber(realComponent: 0, imaginaryComponent: 1)
|
109
|
+
let minuend = ComplexNumber(realComponent: 0, imaginaryComponent: 2)
|
110
|
+
let expectedDifference = ComplexNumber(realComponent: 0, imaginaryComponent: -1)
|
111
|
+
let actualDifference = subtrahend.subtract(complexNumber: minuend)
|
112
|
+
|
113
|
+
XCTAssertEqual([expectedDifference.realComponent, expectedDifference.imaginaryComponent], [actualDifference.realComponent, actualDifference.imaginaryComponent])
|
114
|
+
}
|
115
|
+
|
116
|
+
func testSubtractNumbersWithRealAndImaginaryParts() {
|
117
|
+
|
118
|
+
let subtrahend = ComplexNumber(realComponent: 1, imaginaryComponent: 2)
|
119
|
+
let minuend = ComplexNumber(realComponent: 3, imaginaryComponent: 4)
|
120
|
+
let expectedDifference = ComplexNumber(realComponent: -2, imaginaryComponent: -2)
|
121
|
+
let actualDifference = subtrahend.subtract(complexNumber: minuend)
|
122
|
+
|
123
|
+
XCTAssertEqual([expectedDifference.realComponent, expectedDifference.imaginaryComponent], [actualDifference.realComponent, actualDifference.imaginaryComponent])
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
func testMultiplicationOfPurelyRealNumbers() {
|
128
|
+
|
129
|
+
let multiplicand = ComplexNumber(realComponent: 1, imaginaryComponent: 0)
|
130
|
+
let multiplier = ComplexNumber(realComponent: 2, imaginaryComponent: 0)
|
131
|
+
let expectedProduct = ComplexNumber(realComponent: 2, imaginaryComponent: 0)
|
132
|
+
let actualProduct = multiplicand.multiply(complexNumber: multiplier)
|
133
|
+
|
134
|
+
XCTAssertEqual([expectedProduct.realComponent, expectedProduct.imaginaryComponent], [actualProduct.realComponent, actualProduct.imaginaryComponent])
|
135
|
+
|
136
|
+
}
|
137
|
+
|
138
|
+
func testMultiplicationOfPurelyImaginaryNumbers() {
|
139
|
+
|
140
|
+
let multiplicand = ComplexNumber(realComponent: 0, imaginaryComponent: 1)
|
141
|
+
let multiplier = ComplexNumber(realComponent: 0, imaginaryComponent: 2)
|
142
|
+
let expectedProduct = ComplexNumber(realComponent: -2, imaginaryComponent: 0)
|
143
|
+
let actualProduct = multiplicand.multiply(complexNumber: multiplier)
|
144
|
+
|
145
|
+
XCTAssertEqual([expectedProduct.realComponent, expectedProduct.imaginaryComponent], [actualProduct.realComponent, actualProduct.imaginaryComponent])
|
146
|
+
}
|
147
|
+
|
148
|
+
func testMultiplyNumbersWithRealAndImaginaryParts() {
|
149
|
+
|
150
|
+
let multiplicand = ComplexNumber(realComponent: 1, imaginaryComponent: 2)
|
151
|
+
let multiplier = ComplexNumber(realComponent: 3, imaginaryComponent: 4)
|
152
|
+
let expectedProduct = ComplexNumber(realComponent: -5, imaginaryComponent: 10)
|
153
|
+
let actualProduct = multiplicand.multiply(complexNumber: multiplier)
|
154
|
+
|
155
|
+
XCTAssertEqual([expectedProduct.realComponent, expectedProduct.imaginaryComponent], [actualProduct.realComponent, actualProduct.imaginaryComponent])
|
156
|
+
}
|
157
|
+
|
158
|
+
func testDividePurelyRealNumbers() {
|
159
|
+
|
160
|
+
let dividend = ComplexNumber(realComponent: 1, imaginaryComponent: 0)
|
161
|
+
let divisor = ComplexNumber(realComponent: 2, imaginaryComponent: 0)
|
162
|
+
let expectedQuotient = ComplexNumber(realComponent: 0.5, imaginaryComponent: 0)
|
163
|
+
let actualQuotient = dividend.divide(complexNumber: divisor)
|
164
|
+
|
165
|
+
XCTAssertEqual([expectedQuotient.realComponent, expectedQuotient.imaginaryComponent], [actualQuotient.realComponent, actualQuotient.imaginaryComponent])
|
166
|
+
}
|
167
|
+
|
168
|
+
func testDividePurelyImaginaryNumbers() {
|
169
|
+
|
170
|
+
let dividend = ComplexNumber(realComponent: 0, imaginaryComponent: 1)
|
171
|
+
let divisor = ComplexNumber(realComponent: 0, imaginaryComponent: 2)
|
172
|
+
let expectedQuotient = ComplexNumber(realComponent: 0.5, imaginaryComponent: 0)
|
173
|
+
let actualQuotient = dividend.divide(complexNumber: divisor)
|
174
|
+
|
175
|
+
XCTAssertEqual([expectedQuotient.realComponent, expectedQuotient.imaginaryComponent], [actualQuotient.realComponent, actualQuotient.imaginaryComponent])
|
176
|
+
}
|
177
|
+
|
178
|
+
func testDividingNumbersWithRealAndImaginaryParts() {
|
179
|
+
|
180
|
+
let dividend = ComplexNumber(realComponent: 1, imaginaryComponent: 2)
|
181
|
+
let divisor = ComplexNumber(realComponent: 3, imaginaryComponent: 4)
|
182
|
+
let expectedQuotient = ComplexNumber(realComponent: 0.44, imaginaryComponent: 0.08)
|
183
|
+
let actualQuotient = dividend.divide(complexNumber: divisor)
|
184
|
+
|
185
|
+
XCTAssertEqual([expectedQuotient.realComponent, expectedQuotient.imaginaryComponent], [actualQuotient.realComponent, actualQuotient.imaginaryComponent])
|
186
|
+
}
|
187
|
+
|
188
|
+
func testAbsoluteValueOfPositivePurelyRealNumber() {
|
189
|
+
|
190
|
+
let input = ComplexNumber(realComponent: 5, imaginaryComponent: 0)
|
191
|
+
let expectedResult = Double(5)
|
192
|
+
XCTAssertEqual(expectedResult, input.absolute())
|
193
|
+
}
|
194
|
+
|
195
|
+
func testAbsoluteValueOfNegativePurelyRealNumber() {
|
196
|
+
|
197
|
+
let input = ComplexNumber(realComponent: -5, imaginaryComponent: 0)
|
198
|
+
let expectedResult = Double(5)
|
199
|
+
XCTAssertEqual(expectedResult, input.absolute())
|
200
|
+
}
|
201
|
+
|
202
|
+
func testAbsoluteValueOfPositivePurelyImaginaryNumber() {
|
203
|
+
|
204
|
+
let input = ComplexNumber(realComponent: 0, imaginaryComponent: 5)
|
205
|
+
let expectedResult = Double(5)
|
206
|
+
XCTAssertEqual(expectedResult, input.absolute())
|
207
|
+
}
|
208
|
+
|
209
|
+
func testAbsoluteValueOfNegativePurelyImaginaryNumber() {
|
210
|
+
|
211
|
+
let input = ComplexNumber(realComponent: 0, imaginaryComponent: -5)
|
212
|
+
let expectedResult = Double(5)
|
213
|
+
XCTAssertEqual(expectedResult, input.absolute())
|
214
|
+
}
|
215
|
+
|
216
|
+
func testAbsoluteValueOfNumberWithRealAndImaginaryParts() {
|
217
|
+
|
218
|
+
let input = ComplexNumber(realComponent: 3, imaginaryComponent: 4)
|
219
|
+
let expectedResult = Double(5)
|
220
|
+
XCTAssertEqual(expectedResult, input.absolute())
|
221
|
+
}
|
222
|
+
|
223
|
+
func testConjugateOfAPurelyRealNumber() {
|
224
|
+
|
225
|
+
let input = ComplexNumber(realComponent: 5, imaginaryComponent: 0)
|
226
|
+
let expectedResult = ComplexNumber(realComponent: 5, imaginaryComponent: 0)
|
227
|
+
let actualResult = input.conjugate()
|
228
|
+
XCTAssertEqual([expectedResult.realComponent, expectedResult.imaginaryComponent], [actualResult.realComponent, actualResult.imaginaryComponent])
|
229
|
+
}
|
230
|
+
|
231
|
+
func testConjugateOfAPurelyImaginaryNumber() {
|
232
|
+
|
233
|
+
let input = ComplexNumber(realComponent: 0, imaginaryComponent: 5)
|
234
|
+
let expectedResult = ComplexNumber(realComponent: 0, imaginaryComponent: -5)
|
235
|
+
let actualResult = input.conjugate()
|
236
|
+
XCTAssertEqual([expectedResult.realComponent, expectedResult.imaginaryComponent], [actualResult.realComponent, actualResult.imaginaryComponent])
|
237
|
+
}
|
238
|
+
|
239
|
+
func testConjugateOfANumberWithRealAndImaginaryParts() {
|
240
|
+
|
241
|
+
let input = ComplexNumber(realComponent: 1, imaginaryComponent: 1)
|
242
|
+
let expectedResult = ComplexNumber(realComponent: 1, imaginaryComponent: -1)
|
243
|
+
let actualResult = input.conjugate()
|
244
|
+
XCTAssertEqual([expectedResult.realComponent, expectedResult.imaginaryComponent], [actualResult.realComponent, actualResult.imaginaryComponent])
|
245
|
+
}
|
246
|
+
|
247
|
+
func testEulersIdentityForComplexNumbers() {
|
248
|
+
|
249
|
+
let input = ComplexNumber(realComponent: 0, imaginaryComponent: Double(String(format: "%.3f", Double.pi))!)
|
250
|
+
let expectedResult = ComplexNumber(realComponent: -1, imaginaryComponent: 0)
|
251
|
+
let actualResult = input.exponent()
|
252
|
+
XCTAssertEqual([expectedResult.realComponent, expectedResult.imaginaryComponent], [Double(String(format: "%.3f", actualResult.realComponent))!, Double(String(format: "%.3f", actualResult.imaginaryComponent))!])
|
253
|
+
|
254
|
+
}
|
255
|
+
|
256
|
+
func testExponentOfZero() {
|
257
|
+
|
258
|
+
let input = ComplexNumber(realComponent: 0, imaginaryComponent: 0)
|
259
|
+
let expectedResult = ComplexNumber(realComponent: 1, imaginaryComponent: 0)
|
260
|
+
let actualResult = input.exponent()
|
261
|
+
XCTAssertEqual([expectedResult.realComponent, expectedResult.imaginaryComponent], [actualResult.realComponent, actualResult.imaginaryComponent])
|
262
|
+
}
|
263
|
+
|
264
|
+
func testExponentOfPurelyRealNumber() {
|
265
|
+
|
266
|
+
let input = ComplexNumber(realComponent: 1, imaginaryComponent: 0)
|
267
|
+
let expectedResult = ComplexNumber(realComponent: exp(1), imaginaryComponent: 0)
|
268
|
+
let actualResult = input.exponent()
|
269
|
+
XCTAssertEqual([expectedResult.realComponent, expectedResult.imaginaryComponent], [actualResult.realComponent, actualResult.imaginaryComponent])
|
270
|
+
}
|
271
|
+
|
272
|
+
func testExponentOfNumberWithRealAndImaginaryPart() {
|
273
|
+
|
274
|
+
let input = ComplexNumber(realComponent: log(2), imaginaryComponent: Double(String(format: "%.3f", Double.pi))!)
|
275
|
+
let expectedResult = ComplexNumber(realComponent: -2, imaginaryComponent: 0)
|
276
|
+
let actualResult = input.exponent()
|
277
|
+
|
278
|
+
XCTAssertEqual([expectedResult.realComponent, expectedResult.imaginaryComponent], [round(actualResult.realComponent), Double(String(format: "%.1f", actualResult.imaginaryComponent))!])
|
279
|
+
}
|
280
|
+
|
281
|
+
static var allTests: [(String, (ComplexNumbersTests) -> () throws -> Void)] {
|
282
|
+
return [
|
283
|
+
("testRealPartOfPurelyRealNumber", testRealPartOfPurelyRealNumber),
|
284
|
+
("testRealPartOfPurelyImaginaryNumber", testRealPartOfPurelyImaginaryNumber),
|
285
|
+
("testRealPartOfNumberWithRealAndImaginary", testRealPartOfNumberWithRealAndImaginary),
|
286
|
+
("testImaginaryPartOfPurelyRealNumber", testImaginaryPartOfPurelyRealNumber),
|
287
|
+
("testImaginaryPartOfPurelyImaginaryNumber", testImaginaryPartOfPurelyImaginaryNumber),
|
288
|
+
("testImaginaryPartOfNumberWithRealAndImaginary", testImaginaryPartOfNumberWithRealAndImaginary),
|
289
|
+
("testImaginaryUnit", testImaginaryUnit),
|
290
|
+
("testAddPurelyRealNumbers", testAddPurelyRealNumbers),
|
291
|
+
("testAddPurelyImaginaryNumbers", testAddPurelyImaginaryNumbers),
|
292
|
+
("testAddNumbersWithRealAndImaginaryParts", testAddNumbersWithRealAndImaginaryParts),
|
293
|
+
("testSubtractPurelyRealNumbers", testSubtractPurelyRealNumbers),
|
294
|
+
("testSubtractPurelyImaginaryNumbers", testSubtractPurelyImaginaryNumbers),
|
295
|
+
("testSubtractNumbersWithRealAndImaginaryParts", testSubtractNumbersWithRealAndImaginaryParts),
|
296
|
+
("testMultiplicationOfPurelyRealNumbers", testMultiplicationOfPurelyRealNumbers),
|
297
|
+
("testMultiplicationOfPurelyImaginaryNumbers", testMultiplicationOfPurelyImaginaryNumbers),
|
298
|
+
("testMultiplyNumbersWithRealAndImaginaryParts", testMultiplyNumbersWithRealAndImaginaryParts),
|
299
|
+
("testDividePurelyRealNumbers", testDividePurelyRealNumbers),
|
300
|
+
("testDividePurelyImaginaryNumbers", testDividePurelyImaginaryNumbers),
|
301
|
+
("testDividingNumbersWithRealAndImaginaryParts", testDividingNumbersWithRealAndImaginaryParts),
|
302
|
+
("testAbsoluteValueOfPositivePurelyRealNumber", testAbsoluteValueOfPositivePurelyRealNumber),
|
303
|
+
("testAbsoluteValueOfNegativePurelyRealNumber", testAbsoluteValueOfNegativePurelyRealNumber),
|
304
|
+
("testAbsoluteValueOfPositivePurelyImaginaryNumber", testAbsoluteValueOfPositivePurelyImaginaryNumber),
|
305
|
+
("testAbsoluteValueOfNegativePurelyImaginaryNumber", testAbsoluteValueOfNegativePurelyImaginaryNumber),
|
306
|
+
("testAbsoluteValueOfNumberWithRealAndImaginaryParts", testAbsoluteValueOfNumberWithRealAndImaginaryParts),
|
307
|
+
("testConjugateOfAPurelyRealNumber", testConjugateOfAPurelyRealNumber),
|
308
|
+
("testConjugateOfAPurelyImaginaryNumber", testConjugateOfAPurelyImaginaryNumber),
|
309
|
+
|
310
|
+
("testConjugateOfANumberWithRealAndImaginaryParts", testConjugateOfANumberWithRealAndImaginaryParts),
|
311
|
+
|
312
|
+
("testEulersIdentityForComplexNumbers", testEulersIdentityForComplexNumbers),
|
313
|
+
|
314
|
+
("testExponentOfZero", testExponentOfZero),
|
315
|
+
("testExponentOfPurelyRealNumber", testExponentOfPurelyRealNumber),
|
316
|
+
|
317
|
+
("testExponentOfNumberWithRealAndImaginaryPart", testExponentOfNumberWithRealAndImaginaryPart)
|
318
|
+
]
|
319
|
+
}
|
320
|
+
|
321
|
+
}
|