Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit 24d52d1

Browse files
committed
Fix tests for running with XCTest.
1 parent 1e5758a commit 24d52d1

File tree

2 files changed

+163
-159
lines changed

2 files changed

+163
-159
lines changed

Tests/DeepLearningTests/ComplexAutoDiffTests.swift

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
1-
// RUN: %target-run-simple-swift
2-
// REQUIRES: executable_test
1+
// Copyright 2019 The TensorFlow Authors. All Rights Reserved.
32
//
4-
// Complex API tests.
5-
6-
// TODO: remove import
7-
import TensorFlow
8-
9-
import StdlibUnittest
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
1014

11-
var AutoDiffComplexTests = TestSuite("AutoDiffComplex")
15+
import XCTest
16+
@testable import DeepLearning
1217

13-
AutoDiffComplexTests.test("_vjpAdd") {
18+
func testVjpAdd() {
1419
let pb: (Complex<Float>) -> Complex<Float> = pullback(at: Complex<Float>(real: 2, imaginary: 3)) { x in
1520
return x + Complex<Float>(real: 5, imaginary: 6)
1621
}
1722
expectEqual(pb(Complex(real: 1, imaginary: 1)), Complex<Float>(real: 1, imaginary: 1))
1823
}
1924

20-
AutoDiffComplexTests.test("_vjpSubtract") {
25+
func testVjpSubtract() {
2126
let pb: (Complex<Float>) -> Complex<Float> = pullback(at: Complex<Float>(real: 2, imaginary: 3)) { x in
2227
return Complex<Float>(real: 5, imaginary: 6) - x
2328
}
2429
expectEqual(pb(Complex(real: 1, imaginary: 1)), Complex<Float>(real: -1, imaginary: -1))
2530
}
2631

27-
AutoDiffComplexTests.test("_vjpMultiply") {
32+
func testVjpMultiply() {
2833
let pb: (Complex<Float>) -> Complex<Float> = pullback(at: Complex<Float>(real: 2, imaginary: 3)) { x in
2934
return x * x
3035
}
@@ -33,15 +38,15 @@ AutoDiffComplexTests.test("_vjpMultiply") {
3338
expectEqual(pb(Complex(real: 1, imaginary: 1)), Complex<Float>(real: -2, imaginary: 10))
3439
}
3540

36-
AutoDiffComplexTests.test("_vjpDivide") {
41+
func testVjpDivide() {
3742
let pb: (Complex<Float>) -> Complex<Float> = pullback(at: Complex<Float>(real: 20, imaginary: -4)) { x in
3843
return x / Complex<Float>(real: 2, imaginary: 2)
3944
}
4045
expectEqual(pb(Complex(real: 1, imaginary: 0)), Complex<Float>(real: 0.25, imaginary: -0.25))
4146
expectEqual(pb(Complex(real: 0, imaginary: 1)), Complex<Float>(real: 0.25, imaginary: 0.25))
4247
}
4348

44-
AutoDiffComplexTests.test("_vjpNegate") {
49+
func testVjpNegate() {
4550
let pb: (Complex<Float>) -> Complex<Float> = pullback(at: Complex<Float>(real: 20, imaginary: -4)) { x in
4651
return -x
4752
}
@@ -50,7 +55,7 @@ AutoDiffComplexTests.test("_vjpNegate") {
5055
expectEqual(pb(Complex(real: 1, imaginary: 1)), Complex<Float>(real: -1, imaginary: -1))
5156
}
5257

53-
AutoDiffComplexTests.test("_vjpComplexConjugate") {
58+
func testVjpComplexConjugate() {
5459
let pb: (Complex<Float>) -> Complex<Float> = pullback(at: Complex<Float>(real: 20, imaginary: -4)) { x in
5560
return x.complexConjugate()
5661
}
@@ -59,7 +64,7 @@ AutoDiffComplexTests.test("_vjpComplexConjugate") {
5964
expectEqual(pb(Complex(real: 1, imaginary: 1)), Complex<Float>(real: -1, imaginary: -1))
6065
}
6166

62-
AutoDiffComplexTests.test("_vjpAdding(real:)") {
67+
func testVjpAdding() {
6368
let pb: (Complex<Float>) -> Complex<Float> = pullback(at: Complex<Float>(real: 20, imaginary: -4)) { x in
6469
return x.adding(real: 5)
6570
}
@@ -68,7 +73,7 @@ AutoDiffComplexTests.test("_vjpAdding(real:)") {
6873
expectEqual(pb(Complex(real: 1, imaginary: 1)), Complex<Float>(real: 1, imaginary: 1))
6974
}
7075

71-
AutoDiffComplexTests.test("_vjpAdding(imaginary:)") {
76+
func testVjpAdding() {
7277
let pb: (Complex<Float>) -> Complex<Float> = pullback(at: Complex<Float>(real: 20, imaginary: -4)) { x in
7378
return x.adding(imaginary: 5)
7479
}
@@ -77,7 +82,7 @@ AutoDiffComplexTests.test("_vjpAdding(imaginary:)") {
7782
expectEqual(pb(Complex(real: 1, imaginary: 1)), Complex<Float>(real: 1, imaginary: 1))
7883
}
7984

80-
AutoDiffComplexTests.test("_vjpSubtracting(real:)") {
85+
func testVjpSubtracting() {
8186
let pb: (Complex<Float>) -> Complex<Float> = pullback(at: Complex<Float>(real: 20, imaginary: -4)) { x in
8287
return x.subtracting(real: 5)
8388
}
@@ -86,7 +91,7 @@ AutoDiffComplexTests.test("_vjpSubtracting(real:)") {
8691
expectEqual(pb(Complex(real: 1, imaginary: 1)), Complex<Float>(real: 1, imaginary: 1))
8792
}
8893

89-
AutoDiffComplexTests.test("_vjpSubtracting(imaginary:)") {
94+
func testVjpSubtracting() {
9095
let pb: (Complex<Float>) -> Complex<Float> = pullback(at: Complex<Float>(real: 20, imaginary: -4)) { x in
9196
return x.subtracting(imaginary: 5)
9297
}

Tests/DeepLearningTests/ComplexTests.swift

Lines changed: 139 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -3,147 +3,146 @@
33
//
44
// Complex API tests.
55

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+
}
69145
}
70146

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-
}
148147

149148
runAllTests()

0 commit comments

Comments
 (0)