Skip to content

Commit a1a66c7

Browse files
committed
Add NumericsTests
1 parent 536f836 commit a1a66c7

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-2
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// NumericsTests.swift
3+
// OctopusKitTests
4+
//
5+
// Created by [email protected] on 2019/12/24.
6+
//
7+
8+
import XCTest
9+
@testable import OctopusKit
10+
11+
class NumericsTests: XCTestCase {
12+
13+
func testAdditiveArithmetic() {
14+
15+
// Test adjustTowards(_:by:)
16+
17+
// #1: Test integers
18+
19+
XCTContext.runActivity(named: "Test integers") { _ in
20+
var value: Int = 10
21+
22+
// #1A: Difference = 1
23+
XCTAssertEqual(value.adjustedTowards(0, by: 1), 9)
24+
XCTAssertEqual(value.adjustedTowards(100, by: 1), 11)
25+
26+
// #1B: Difference > 1
27+
XCTAssertEqual(value.adjustedTowards(0, by: 5), 5)
28+
XCTAssertEqual(value.adjustedTowards(100, by: 5), 15)
29+
30+
// #1C: Exceeding the target should snap to the target.
31+
XCTAssertEqual(value.adjustedTowards(0, by: 1000), 0)
32+
XCTAssertEqual(value.adjustedTowards(100, by: 1000), 100)
33+
34+
// #1D: Negatives
35+
value = -10
36+
XCTAssertEqual(value.adjustedTowards( 100, by: 1), -9)
37+
XCTAssertEqual(value.adjustedTowards(-100, by: 1), -11)
38+
39+
// #1E: Mutation
40+
value = 10
41+
value.adjustTowards(0, by: 1)
42+
XCTAssertEqual(value, 9)
43+
}
44+
45+
// #2: Test floats
46+
47+
XCTContext.runActivity(named: "Test integers") { _ in
48+
var value: Float = 10.0
49+
50+
// #2A: Difference = 0.1
51+
XCTAssertEqual(value.adjustedTowards(0.1, by: 0.1), 9.9)
52+
XCTAssertEqual(value.adjustedTowards(100, by: 0.1), 10.1)
53+
54+
// #2B: Difference = 1.0
55+
XCTAssertEqual(value.adjustedTowards(0.1, by: 1.0), 9.0)
56+
XCTAssertEqual(value.adjustedTowards(100, by: 1.0), 11.0)
57+
58+
// #2C: Exceeding the target should snap to the target.
59+
XCTAssertEqual(value.adjustedTowards(0.1, by: 10.1), 0.1)
60+
XCTAssertEqual(value.adjustedTowards(9.1, by: 10.1), 9.1)
61+
62+
// #2D: Negatives
63+
value = -10
64+
XCTAssertEqual(value.adjustedTowards(0.1, by: 0.1), -9.9)
65+
XCTAssertEqual(value.adjustedTowards(-90.9, by: 0.1), -10.1)
66+
67+
// #2E: Mutation
68+
value = 10.0
69+
value.adjustTowards(0.1, by: 1)
70+
XCTAssertEqual(value, 9.0)
71+
}
72+
73+
}
74+
75+
static var allTests = [
76+
("Test AdditiveArithmetic extensions", testAdditiveArithmetic)
77+
]
78+
}

Tests/OctopusKitTests/XCTestManifests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import XCTest
33
#if !canImport(ObjectiveC)
44
public func allTests() -> [XCTestCaseEntry] {
55
return [
6+
testCase(NumericsTests.allTests),
7+
testCase(CGPointTests.allTests),
8+
testCase(StringTests.allTests),
9+
610
testCase(OctopusKitLaunchTests.allTests),
711
testCase(OctopusLogTests.allTests),
812
testCase(ECSTests.allTests),
9-
testCase(StringTests.allTests),
10-
testCase(CGPointTests.allTests),
13+
1114
testCase(ContiguousArray2DTests.allTests)
1215
]
1316
}

0 commit comments

Comments
 (0)