3
3
//
4
4
// Complex API tests.
5
5
6
- // TODO: remove import
7
- import TensorFlow
8
-
9
- import StdlibUnittest
10
-
11
- var ComplexTests = TestSuite ( " Complex " )
12
-
13
- ComplexTests . test ( " Initializer " ) {
14
- let complex = Complex < Float > ( real: 2 , imaginary: 3 )
15
- expectEqual ( complex. real, 2 )
16
- expectEqual ( complex. imaginary, 3 )
17
- }
18
-
19
- ComplexTests . test ( " Static Imaginary " ) {
20
- let imaginary = Complex < Float > ( real: 0 , imaginary: 1 )
21
- expectEqual ( imaginary, Complex . i)
22
- }
23
-
24
- ComplexTests . test ( " isFinite " ) {
25
- var complex = Complex < Float > ( real: 999 , imaginary: 0 )
26
- expectTrue ( complex. isFinite)
27
-
28
- complex = Complex ( real: 1.0 / 0.0 , imaginary: 1 )
29
- expectFalse ( complex. isFinite)
30
-
31
- complex = Complex ( real: 1.0 / 0.0 , imaginary: 1.0 / 0.0 )
32
- expectFalse ( complex. isFinite)
33
- }
34
-
35
- ComplexTests . test ( " isInfinite " ) {
36
- var complex = Complex < Float > ( real: 999 , imaginary: 0 )
37
- expectFalse ( complex. isInfinite)
38
-
39
- complex = Complex ( real: 1.0 / 0.0 , imaginary: 1 )
40
- expectTrue ( complex. isInfinite)
41
-
42
- complex = Complex ( real: 1.0 / 0.0 , imaginary: 1.0 / 0.0 )
43
- expectTrue ( complex. isInfinite)
44
- }
45
-
46
- ComplexTests . test ( " isNaN " ) {
47
- var complex = Complex < Float > ( real: 999 , imaginary: 0 )
48
- expectFalse ( complex. isNaN)
49
-
50
- complex = Complex ( real: 0.0 * 1.0 / 0.0 , imaginary: 1 )
51
- expectTrue ( complex. isNaN)
52
-
53
- complex = Complex ( real: 0.0 * 1.0 / 0.0 , imaginary: 0.0 * 1.0 / 0.0 )
54
- expectTrue ( complex. isNaN)
55
- }
56
-
57
- ComplexTests . test ( " isZero " ) {
58
- var complex = Complex < Float > ( real: 999 , imaginary: 0 )
59
- expectFalse ( complex. isZero)
60
-
61
- complex = Complex ( real: 0.0 * 1.0 / 0.0 , imaginary: 0 )
62
- expectFalse ( complex. isZero)
63
-
64
- complex = Complex ( real: 0.0 * 1.0 / 0.0 , imaginary: 0.0 * 1.0 / 0.0 )
65
- expectFalse ( complex. isZero)
66
-
67
- complex = Complex ( real: 0 , imaginary: 0 )
68
- expectTrue ( complex. isZero)
6
+ import XCTest
7
+ @testable import DeepLearning
8
+
9
+ final class ComplexTests : XCTestCase {
10
+ func testInitializer( ) {
11
+ let complex = Complex < Float > ( real: 2 , imaginary: 3 )
12
+ XCTAssertEqual ( complex. real, 2 )
13
+ XCTAssertEqual ( complex. imaginary, 3 )
14
+ }
15
+
16
+ func testStaticImaginary( ) {
17
+ let imaginary = Complex < Float > ( real: 0 , imaginary: 1 )
18
+ XCTAssertEqual ( imaginary, Complex . i)
19
+ }
20
+
21
+ func testIsFinite( ) {
22
+ var complex = Complex < Float > ( real: 999 , imaginary: 0 )
23
+ XCTAssertTrue ( complex. isFinite)
24
+
25
+ complex = Complex ( real: 1.0 / 0.0 , imaginary: 1 )
26
+ XCTAssertFalse ( complex. isFinite)
27
+
28
+ complex = Complex ( real: 1.0 / 0.0 , imaginary: 1.0 / 0.0 )
29
+ XCTAssertFalse ( complex. isFinite)
30
+ }
31
+
32
+ func testIsInfinite( ) {
33
+ var complex = Complex < Float > ( real: 999 , imaginary: 0 )
34
+ XCTAssertFalse ( complex. isInfinite)
35
+
36
+ complex = Complex ( real: 1.0 / 0.0 , imaginary: 1 )
37
+ XCTAssertTrue ( complex. isInfinite)
38
+
39
+ complex = Complex ( real: 1.0 / 0.0 , imaginary: 1.0 / 0.0 )
40
+ XCTAssertTrue ( complex. isInfinite)
41
+ }
42
+
43
+ func testIsNaN( ) {
44
+ var complex = Complex < Float > ( real: 999 , imaginary: 0 )
45
+ XCTAssertFalse ( complex. isNaN)
46
+
47
+ complex = Complex ( real: 0.0 * 1.0 / 0.0 , imaginary: 1 )
48
+ XCTAssertTrue ( complex. isNaN)
49
+
50
+ complex = Complex ( real: 0.0 * 1.0 / 0.0 , imaginary: 0.0 * 1.0 / 0.0 )
51
+ XCTAssertTrue ( complex. isNaN)
52
+ }
53
+
54
+ func testIsZero( ) {
55
+ var complex = Complex < Float > ( real: 999 , imaginary: 0 )
56
+ XCTAssertFalse ( complex. isZero)
57
+
58
+ complex = Complex ( real: 0.0 * 1.0 / 0.0 , imaginary: 0 )
59
+ XCTAssertFalse ( complex. isZero)
60
+
61
+ complex = Complex ( real: 0.0 * 1.0 / 0.0 , imaginary: 0.0 * 1.0 / 0.0 )
62
+ XCTAssertFalse ( complex. isZero)
63
+
64
+ complex = Complex ( real: 0 , imaginary: 0 )
65
+ XCTAssertTrue ( complex. isZero)
66
+ }
67
+
68
+ func testEquals( ) {
69
+ var complexA = Complex < Float > ( real: 999 , imaginary: 0 )
70
+ let complexB = Complex < Float > ( real: 999 , imaginary: 0 )
71
+ XCTAssertEqual ( complexA, complexB)
72
+
73
+ complexA = Complex ( real: 5 , imaginary: 0 )
74
+ expectNotEqual ( complexA, complexB)
75
+ }
76
+
77
+ func testPlus( ) {
78
+ let input = Complex < Float > ( real: 5 , imaginary: 1 )
79
+ let expected = Complex < Float > ( real: 10 , imaginary: 2 )
80
+ XCTAssertEqual ( expected, input + input)
81
+ }
82
+
83
+ func testMinus( ) {
84
+ let inputA = Complex < Float > ( real: 6 , imaginary: 2 )
85
+ let inputB = Complex < Float > ( real: 5 , imaginary: 1 )
86
+ let expected = Complex < Float > ( real: 1 , imaginary: 1 )
87
+ XCTAssertEqual ( expected, inputA - inputB)
88
+ }
89
+
90
+ func testTimes( ) {
91
+ let inputA = Complex < Float > ( real: 6 , imaginary: 2 )
92
+ let inputB = Complex < Float > ( real: 5 , imaginary: 1 )
93
+ let expected = Complex < Float > ( real: 28 , imaginary: 16 )
94
+ XCTAssertEqual ( expected, inputA * inputB)
95
+ }
96
+
97
+ func testNegate( ) {
98
+ var input = Complex < Float > ( real: 6 , imaginary: 2 )
99
+ let negated = Complex < Float > ( real: - 6 , imaginary: - 2 )
100
+ XCTAssertEqual ( - input, negated)
101
+ input. negate ( )
102
+ XCTAssertEqual ( input, negated)
103
+ }
104
+
105
+ func testDivide( ) {
106
+ let inputA = Complex < Float > ( real: 20 , imaginary: - 4 )
107
+ let inputB = Complex < Float > ( real: 3 , imaginary: 2 )
108
+ let expected = Complex < Float > ( real: 4 , imaginary: - 4 )
109
+ XCTAssertEqual ( expected, inputA / inputB)
110
+ }
111
+
112
+ func testComplexConjugate( ) {
113
+ var input = Complex < Float > ( real: 2 , imaginary: - 4 )
114
+ var expected = Complex < Float > ( real: 2 , imaginary: 4 )
115
+ XCTAssertEqual ( expected, input. complexConjugate ( ) )
116
+
117
+ input = Complex < Float > ( real: - 2 , imaginary: - 4 )
118
+ expected = Complex < Float > ( real: - 2 , imaginary: 4 )
119
+ XCTAssertEqual ( expected, input. complexConjugate ( ) )
120
+
121
+ input = Complex < Float > ( real: 2 , imaginary: 4 )
122
+ expected = Complex < Float > ( real: 2 , imaginary: - 4 )
123
+ XCTAssertEqual ( expected, input. complexConjugate ( ) )
124
+ }
125
+
126
+ func testAdding( ) {
127
+ var input = Complex < Float > ( real: 2 , imaginary: - 4 )
128
+ var expected = Complex < Float > ( real: 3 , imaginary: - 4 )
129
+ XCTAssertEqual ( expected, input. adding ( real: 1 ) )
130
+
131
+ input = Complex < Float > ( real: 2 , imaginary: - 4 )
132
+ expected = Complex < Float > ( real: 2 , imaginary: - 3 )
133
+ XCTAssertEqual ( expected, input. adding ( imaginary: 1 ) )
134
+ }
135
+
136
+ func testSubtracting( ) {
137
+ var input = Complex < Float > ( real: 2 , imaginary: - 4 )
138
+ var expected = Complex < Float > ( real: 1 , imaginary: - 4 )
139
+ XCTAssertEqual ( expected, input. subtracting ( real: 1 ) )
140
+
141
+ input = Complex < Float > ( real: 2 , imaginary: - 4 )
142
+ expected = Complex < Float > ( real: 2 , imaginary: - 5 )
143
+ XCTAssertEqual ( expected, input. subtracting ( imaginary: 1 ) )
144
+ }
69
145
}
70
146
71
- ComplexTests . test ( " == " ) {
72
- var complexA = Complex < Float > ( real: 999 , imaginary: 0 )
73
- let complexB = Complex < Float > ( real: 999 , imaginary: 0 )
74
- expectEqual ( complexA, complexB)
75
-
76
- complexA = Complex ( real: 5 , imaginary: 0 )
77
- expectNotEqual ( complexA, complexB)
78
- }
79
-
80
- ComplexTests . test ( " + " ) {
81
- let input = Complex < Float > ( real: 5 , imaginary: 1 )
82
- let expected = Complex < Float > ( real: 10 , imaginary: 2 )
83
- expectEqual ( expected, input + input)
84
- }
85
-
86
- ComplexTests . test ( " - " ) {
87
- let inputA = Complex < Float > ( real: 6 , imaginary: 2 )
88
- let inputB = Complex < Float > ( real: 5 , imaginary: 1 )
89
- let expected = Complex < Float > ( real: 1 , imaginary: 1 )
90
- expectEqual ( expected, inputA - inputB)
91
- }
92
-
93
- ComplexTests . test ( " * " ) {
94
- let inputA = Complex < Float > ( real: 6 , imaginary: 2 )
95
- let inputB = Complex < Float > ( real: 5 , imaginary: 1 )
96
- let expected = Complex < Float > ( real: 28 , imaginary: 16 )
97
- expectEqual ( expected, inputA * inputB)
98
- }
99
-
100
- ComplexTests . test ( " negate " ) {
101
- var input = Complex < Float > ( real: 6 , imaginary: 2 )
102
- let negated = Complex < Float > ( real: - 6 , imaginary: - 2 )
103
- expectEqual ( - input, negated)
104
- input. negate ( )
105
- expectEqual ( input, negated)
106
- }
107
-
108
- ComplexTests . test ( " / " ) {
109
- let inputA = Complex < Float > ( real: 20 , imaginary: - 4 )
110
- let inputB = Complex < Float > ( real: 3 , imaginary: 2 )
111
- let expected = Complex < Float > ( real: 4 , imaginary: - 4 )
112
- expectEqual ( expected, inputA / inputB)
113
- }
114
-
115
- ComplexTests . test ( " complexConjugate " ) {
116
- var input = Complex < Float > ( real: 2 , imaginary: - 4 )
117
- var expected = Complex < Float > ( real: 2 , imaginary: 4 )
118
- expectEqual ( expected, input. complexConjugate ( ) )
119
-
120
- input = Complex < Float > ( real: - 2 , imaginary: - 4 )
121
- expected = Complex < Float > ( real: - 2 , imaginary: 4 )
122
- expectEqual ( expected, input. complexConjugate ( ) )
123
-
124
- input = Complex < Float > ( real: 2 , imaginary: 4 )
125
- expected = Complex < Float > ( real: 2 , imaginary: - 4 )
126
- expectEqual ( expected, input. complexConjugate ( ) )
127
- }
128
-
129
- ComplexTests . test ( " adding " ) {
130
- var input = Complex < Float > ( real: 2 , imaginary: - 4 )
131
- var expected = Complex < Float > ( real: 3 , imaginary: - 4 )
132
- expectEqual ( expected, input. adding ( real: 1 ) )
133
-
134
- input = Complex < Float > ( real: 2 , imaginary: - 4 )
135
- expected = Complex < Float > ( real: 2 , imaginary: - 3 )
136
- expectEqual ( expected, input. adding ( imaginary: 1 ) )
137
- }
138
-
139
- ComplexTests . test ( " subtracting " ) {
140
- var input = Complex < Float > ( real: 2 , imaginary: - 4 )
141
- var expected = Complex < Float > ( real: 1 , imaginary: - 4 )
142
- expectEqual ( expected, input. subtracting ( real: 1 ) )
143
-
144
- input = Complex < Float > ( real: 2 , imaginary: - 4 )
145
- expected = Complex < Float > ( real: 2 , imaginary: - 5 )
146
- expectEqual ( expected, input. subtracting ( imaginary: 1 ) )
147
- }
148
147
149
148
runAllTests ( )
0 commit comments