Skip to content

Commit 37df606

Browse files
authored
Enable tests (#9)
* set swiftSettings * Create swift.yml simple tests * enable tests into package; fix warnings; add one more test; * fix order multiplication; * Feature/linux ci (#1) * enable tests on linux platform * add missing configurations * generate tests for linux * do transpose same time as multiplication
1 parent acacd02 commit 37df606

File tree

8 files changed

+152
-15
lines changed

8 files changed

+152
-15
lines changed

.github/workflows/swift.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Swift
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
strategy:
12+
matrix:
13+
os: [macos-latest, ubuntu-20.04]
14+
15+
runs-on: ${{matrix.os}}
16+
17+
steps:
18+
- name: Install Swift
19+
if: runner.os == 'Linux'
20+
uses: sersoft-gmbh/SwiftyActions@v1
21+
with:
22+
release-version: 5.3.2
23+
platform: ${{matrix.os}}
24+
- uses: actions/checkout@v2
25+
- uses: actions/cache@v2
26+
with:
27+
path: .build
28+
key: ${{runner.os}}-${{github.repository}}-spm-${{hashFiles('**/Package.resolved')}}
29+
restore-keys: |
30+
${{runner.os}}-${{github.repository}}-spm-
31+
- name: Build
32+
run: swift build -v
33+
- name: Run tests
34+
run: swift test -v

Package.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ let package = Package(
1414
name: "SwiftMath",
1515
path: ".",
1616
sources: ["Sources"],
17-
swiftSettings: [ .define("NOSIMD", .when(platforms: [.linux, .android])),])
17+
swiftSettings: [ .define("NOSIMD", .when(platforms: [.linux, .android, .windows, .wasi, ])),]),
18+
19+
.testTarget(
20+
name: "SwiftMathTests",
21+
dependencies: ["SwiftMath"]),
1822
]
1923
)

Sources/Matrix4x4+nosimd.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public struct Matrix4x4f {
172172
}
173173

174174
public extension Matrix4x4f {
175-
public var adjugate: Matrix4x4f {
175+
var adjugate: Matrix4x4f {
176176
var m = Matrix4x4f.identity
177177

178178
m.m11 = m22 * m33 * m44 - m22 * m34 * m43
@@ -246,11 +246,11 @@ public extension Matrix4x4f {
246246
return m11 * m.m11 + m12 * m.m21 + m13 * m.m31 + m14 * m.m41
247247
}
248248

249-
public var determinant: Float {
249+
var determinant: Float {
250250
return determinant(forAdjugate: adjugate)
251251
}
252252

253-
public var transposed: Matrix4x4f {
253+
var transposed: Matrix4x4f {
254254
return Matrix4x4f(
255255
vec4(m11, m21, m31, m41),
256256
vec4(m12, m22, m32, m42),
@@ -259,12 +259,12 @@ public extension Matrix4x4f {
259259
)
260260
}
261261

262-
public var inversed: Matrix4x4f {
262+
var inversed: Matrix4x4f {
263263
let adjugate = self.adjugate // avoid recalculating
264264
return adjugate * (1 / determinant(forAdjugate: adjugate))
265265
}
266266

267-
public static func *(lhs: Matrix4x4f, rhs: Matrix4x4f) -> Matrix4x4f {
267+
static func *(lhs: Matrix4x4f, rhs: Matrix4x4f) -> Matrix4x4f {
268268
var m = Matrix4x4f.identity
269269

270270
m.m11 = lhs.m11 * rhs.m11 + lhs.m21 * rhs.m12
@@ -318,7 +318,7 @@ public extension Matrix4x4f {
318318
return m
319319
}
320320

321-
public static func *(lhs: Matrix4x4f, rhs: Float) -> Matrix4x4f {
321+
static func *(lhs: Matrix4x4f, rhs: Float) -> Matrix4x4f {
322322
return Matrix4x4f(
323323
vec4(lhs.m11 * rhs, lhs.m12 * rhs, lhs.m13 * rhs, lhs.m14 * rhs),
324324
vec4(lhs.m21 * rhs, lhs.m22 * rhs, lhs.m23 * rhs, lhs.m24 * rhs),
@@ -327,7 +327,7 @@ public extension Matrix4x4f {
327327
)
328328
}
329329

330-
public static prefix func -(lhs: Matrix4x4f) -> Matrix4x4f {
330+
static prefix func -(lhs: Matrix4x4f) -> Matrix4x4f {
331331
return Matrix4x4f(
332332
vec4(-lhs.m11, -lhs.m12, -lhs.m13, -lhs.m14),
333333
vec4(-lhs.m21, -lhs.m22, -lhs.m23, -lhs.m24),

Sources/Vector4+nosimd.swift

+10-5
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,20 @@ extension Vector4f: Equatable {
126126

127127
public static func *(lhs: Vector4f, rhs: Matrix4x4f) -> Vector4f {
128128
return Vector4f(
129-
lhs.x * rhs.m11 + lhs.y * rhs.m21 + lhs.z * rhs.m31 + lhs.w * rhs.m41,
130-
lhs.x * rhs.m12 + lhs.y * rhs.m22 + lhs.z * rhs.m32 + lhs.w * rhs.m42,
131-
lhs.x * rhs.m13 + lhs.y * rhs.m23 + lhs.z * rhs.m33 + lhs.w * rhs.m43,
132-
lhs.x * rhs.m14 + lhs.y * rhs.m24 + lhs.z * rhs.m34 + lhs.w * rhs.m44
129+
lhs.x * rhs.m11 + lhs.y * rhs.m12 + lhs.z * rhs.m13 + lhs.w * rhs.m14,
130+
lhs.x * rhs.m21 + lhs.y * rhs.m22 + lhs.z * rhs.m23 + lhs.w * rhs.m24,
131+
lhs.x * rhs.m31 + lhs.y * rhs.m32 + lhs.z * rhs.m33 + lhs.w * rhs.m34,
132+
lhs.x * rhs.m41 + lhs.y * rhs.m42 + lhs.z * rhs.m43 + lhs.w * rhs.m44
133133
)
134134
}
135135

136136
public static func *(lhs: Matrix4x4f, rhs: Vector4f) -> Vector4f {
137-
return rhs * lhs
137+
return Vector4f(
138+
rhs.x * lhs.m11 + rhs.y * lhs.m21 + rhs.z * lhs.m31 + rhs.w * lhs.m41,
139+
rhs.x * lhs.m12 + rhs.y * lhs.m22 + rhs.z * lhs.m32 + rhs.w * lhs.m42,
140+
rhs.x * lhs.m13 + rhs.y * lhs.m23 + rhs.z * lhs.m33 + rhs.w * lhs.m43,
141+
rhs.x * lhs.m14 + rhs.y * lhs.m24 + rhs.z * lhs.m34 + rhs.w * lhs.m44
142+
)
138143
}
139144

140145
public static func /(lhs: Vector4f, rhs: Vector4f) -> Vector4f {

Tests/LinuxMain.swift

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import XCTest
2+
3+
import SwiftMathTests
4+
5+
var tests = [XCTestCaseEntry]()
6+
tests += SwiftMathTests.__allTests()
7+
8+
XCTMain(tests)

Tests/SwiftMathTests/Matrix4x4Tests.swift

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Matrix4x4Tests: XCTestCase {
4848

4949
XCTAssertEqual(10.0, i[0,1])
5050
XCTAssertEqual(10.0, i[0][1])
51+
XCTAssertEqual(12.0, i[2][1])
5152

5253
XCTAssertEqual(32.0, i[2][3])
5354
}

Tests/SwiftMathTests/Vector2Tests.swift

+12-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ class Vector2fTests: XCTestCase {
2323
let b = Matrix4x4f.rotate(z: rad(Float.pi/2.0))
2424
let c = a * b
2525

26-
XCTAssertEqualWithAccuracy(0.0, c.x, accuracy: 0.001)
27-
XCTAssertEqualWithAccuracy(1.0, c.y, accuracy: 0.001)
26+
XCTAssertEqual(0.0, c.x, accuracy: 0.001)
27+
XCTAssertEqual(1.0, c.y, accuracy: 0.001)
28+
}
29+
30+
func testVectorMultiplyWithMatrix4x4_2() {
31+
let a = Vector2f(1.0, 0.0)
32+
var b = Matrix4x4f.rotate(z: rad(Float.pi / -1.5))
33+
b = b * Matrix4x4f.rotate(x: rad(Float.pi / 1.5))
34+
let c = a * b
35+
36+
XCTAssertEqual(-0.499999881, c.x, accuracy: 0.001)
37+
XCTAssertEqual(0.433012635, c.y, accuracy: 0.001)
2838
}
2939
}
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#if !canImport(ObjectiveC)
2+
import XCTest
3+
4+
extension AngleTests {
5+
// DO NOT MODIFY: This is autogenerated, use:
6+
// `swift test --generate-linuxmain`
7+
// to regenerate.
8+
static let __allTests__AngleTests = [
9+
("testAngleAddition", testAngleAddition),
10+
("testAngleCompoundAdditionAndAssignment", testAngleCompoundAdditionAndAssignment),
11+
("testAngleCompoundSubtractionAndAssignment", testAngleCompoundSubtractionAndAssignment),
12+
("testAngleDivide", testAngleDivide),
13+
("testAngleMultiply", testAngleMultiply),
14+
("testAngleSubtraction", testAngleSubtraction),
15+
("testMakeAngleFromIntegerInDegrees", testMakeAngleFromIntegerInDegrees),
16+
("testPostfixConstructsAngleFromFloatInDegrees", testPostfixConstructsAngleFromFloatInDegrees),
17+
("testPostfixConstructsAngleFromIntInDegrees", testPostfixConstructsAngleFromIntInDegrees),
18+
]
19+
}
20+
21+
extension ClampTests {
22+
// DO NOT MODIFY: This is autogenerated, use:
23+
// `swift test --generate-linuxmain`
24+
// to regenerate.
25+
static let __allTests__ClampTests = [
26+
("testClampReturnsMaximumValueWhenGreaterThanMaximum", testClampReturnsMaximumValueWhenGreaterThanMaximum),
27+
("testClampReturnsMinimumValueWhenLessThanMinimum", testClampReturnsMinimumValueWhenLessThanMinimum),
28+
("testClampReturnsSameValueWhenWithinRange", testClampReturnsSameValueWhenWithinRange),
29+
]
30+
}
31+
32+
extension Matrix4x4Tests {
33+
// DO NOT MODIFY: This is autogenerated, use:
34+
// `swift test --generate-linuxmain`
35+
// to regenerate.
36+
static let __allTests__Matrix4x4Tests = [
37+
("testIdentity", testIdentity),
38+
("testInitWithFloats", testInitWithFloats),
39+
("testInitWithFloatsIsRowMajor", testInitWithFloatsIsRowMajor),
40+
("testInitWithVector4IsColumnMajor", testInitWithVector4IsColumnMajor),
41+
("testInitWithVectors", testInitWithVectors),
42+
]
43+
}
44+
45+
extension RectTests {
46+
// DO NOT MODIFY: This is autogenerated, use:
47+
// `swift test --generate-linuxmain`
48+
// to regenerate.
49+
static let __allTests__RectTests = [
50+
("testRectEquatable", testRectEquatable),
51+
]
52+
}
53+
54+
extension Vector2fTests {
55+
// DO NOT MODIFY: This is autogenerated, use:
56+
// `swift test --generate-linuxmain`
57+
// to regenerate.
58+
static let __allTests__Vector2fTests = [
59+
("testVectorMultiplyWithMatrix4x4", testVectorMultiplyWithMatrix4x4),
60+
("testVectorMultiplyWithMatrix4x4_2", testVectorMultiplyWithMatrix4x4_2),
61+
("testVectorsAreEqualUsingEquatable", testVectorsAreEqualUsingEquatable),
62+
("testVectorsAreNotEqualUsingEquatable", testVectorsAreNotEqualUsingEquatable),
63+
]
64+
}
65+
66+
public func __allTests() -> [XCTestCaseEntry] {
67+
return [
68+
testCase(AngleTests.__allTests__AngleTests),
69+
testCase(ClampTests.__allTests__ClampTests),
70+
testCase(Matrix4x4Tests.__allTests__Matrix4x4Tests),
71+
testCase(RectTests.__allTests__RectTests),
72+
testCase(Vector2fTests.__allTests__Vector2fTests),
73+
]
74+
}
75+
#endif

0 commit comments

Comments
 (0)