[Math] Working out some tests for matrices
This commit is contained in:
parent
90e9b96468
commit
acd879465c
4 changed files with 127 additions and 43 deletions
|
@ -9,7 +9,7 @@
|
|||
/* Begin PBXBuildFile section */
|
||||
C005DFFE1BE1CBA700F1BD3C /* Math.h in Headers */ = {isa = PBXBuildFile; fileRef = C005DFFD1BE1CBA700F1BD3C /* Math.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
C005E0051BE1CBA800F1BD3C /* Math.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C005DFFA1BE1CBA700F1BD3C /* Math.framework */; };
|
||||
C005E00A1BE1CBA800F1BD3C /* MathTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C005E0091BE1CBA800F1BD3C /* MathTests.swift */; };
|
||||
C005E00A1BE1CBA800F1BD3C /* MatrixTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C005E0091BE1CBA800F1BD3C /* MatrixTests.swift */; };
|
||||
C005E0391BE1CC7A00F1BD3C /* Matrix.swift in Sources */ = {isa = PBXBuildFile; fileRef = C005E0381BE1CC7A00F1BD3C /* Matrix.swift */; };
|
||||
C04449C11BE26A0700ABF046 /* Vector.swift in Sources */ = {isa = PBXBuildFile; fileRef = C04449C01BE26A0700ABF046 /* Vector.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
@ -29,7 +29,7 @@
|
|||
C005DFFD1BE1CBA700F1BD3C /* Math.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Math.h; sourceTree = "<group>"; };
|
||||
C005DFFF1BE1CBA700F1BD3C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
C005E0041BE1CBA800F1BD3C /* MathTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MathTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
C005E0091BE1CBA800F1BD3C /* MathTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MathTests.swift; sourceTree = "<group>"; };
|
||||
C005E0091BE1CBA800F1BD3C /* MatrixTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatrixTests.swift; sourceTree = "<group>"; };
|
||||
C005E00B1BE1CBA800F1BD3C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
C005E0381BE1CC7A00F1BD3C /* Matrix.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Matrix.swift; sourceTree = "<group>"; };
|
||||
C04449C01BE26A0700ABF046 /* Vector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Vector.swift; sourceTree = "<group>"; };
|
||||
|
@ -86,7 +86,7 @@
|
|||
C005E0081BE1CBA800F1BD3C /* MathTests */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
C005E0091BE1CBA800F1BD3C /* MathTests.swift */,
|
||||
C005E0091BE1CBA800F1BD3C /* MatrixTests.swift */,
|
||||
C005E00B1BE1CBA800F1BD3C /* Info.plist */,
|
||||
);
|
||||
path = MathTests;
|
||||
|
@ -209,7 +209,7 @@
|
|||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
C005E00A1BE1CBA800F1BD3C /* MathTests.swift in Sources */,
|
||||
C005E00A1BE1CBA800F1BD3C /* MatrixTests.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
/** A square matrix. */
|
||||
public protocol Matrix {
|
||||
/** The identity matrix. */
|
||||
static var identity: Self { get }
|
||||
|
||||
/** Number of elements in the underlying container. */
|
||||
|
@ -19,9 +20,19 @@ public protocol Matrix {
|
|||
*/
|
||||
static var dimension: Int { get }
|
||||
|
||||
/** Initialize a Matrix filled with zeroes. */
|
||||
init()
|
||||
|
||||
/**
|
||||
* Initialize a Matrix with a given array of values. The number of values
|
||||
* must match the count of the array; otherwise, a MatrixError.InvalidSize
|
||||
* error is thrown.
|
||||
*/
|
||||
init(values: [Float]) throws
|
||||
|
||||
/** The underlying data array. */
|
||||
var data: [Float] { get }
|
||||
|
||||
/**
|
||||
* Element access
|
||||
* @{
|
||||
|
@ -52,7 +63,7 @@ public struct Matrix4: Matrix {
|
|||
return matrix
|
||||
}()
|
||||
|
||||
private var data: [Float]
|
||||
public private(set) var data: [Float]
|
||||
|
||||
public init() {
|
||||
data = [Float](count: Matrix4.count, repeatedValue: Float(0))
|
||||
|
@ -65,6 +76,7 @@ public struct Matrix4: Matrix {
|
|||
data = values
|
||||
}
|
||||
|
||||
|
||||
public static var dimension: Int {
|
||||
return 4
|
||||
}
|
||||
|
@ -102,7 +114,7 @@ public struct Matrix3: Matrix {
|
|||
return matrix
|
||||
}()
|
||||
|
||||
private var data: [Float]
|
||||
public private(set) var data: [Float]
|
||||
|
||||
public init() {
|
||||
data = [Float](count: Matrix3.count, repeatedValue: Float(0))
|
||||
|
@ -139,7 +151,7 @@ public struct Matrix3: Matrix {
|
|||
|
||||
/** Convert a (row, col) pair into an index into the data array. */
|
||||
private func indexFromCoordinates(row: Int, _ col: Int) -> Int {
|
||||
return row * Matrix4.dimension + col
|
||||
return row * Matrix3.dimension + col
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
//
|
||||
// MathTests.swift
|
||||
// MathTests
|
||||
//
|
||||
// Created by Eryn Wells on 10/28/15.
|
||||
// Copyright © 2015 Eryn Wells. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
@testable import Math
|
||||
|
||||
class MathTests: XCTestCase {
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
// Put setup code here. This method is called before the invocation of each test method in the class.
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
||||
super.tearDown()
|
||||
}
|
||||
|
||||
func testExample() {
|
||||
// This is an example of a functional test case.
|
||||
// Use XCTAssert and related functions to verify your tests produce the correct results.
|
||||
}
|
||||
|
||||
func testPerformanceExample() {
|
||||
// This is an example of a performance test case.
|
||||
self.measureBlock {
|
||||
// Put the code you want to measure the time of here.
|
||||
}
|
||||
}
|
||||
|
||||
}
|
108
Math/MathTests/MatrixTests.swift
Normal file
108
Math/MathTests/MatrixTests.swift
Normal file
|
@ -0,0 +1,108 @@
|
|||
//
|
||||
// MathTests.swift
|
||||
// MathTests
|
||||
//
|
||||
// Created by Eryn Wells on 10/28/15.
|
||||
// Copyright © 2015 Eryn Wells. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
@testable import Math
|
||||
|
||||
class Matrix4Tests: XCTestCase {
|
||||
func testThatItHasProperDimension() {
|
||||
XCTAssertEqual(Matrix4.dimension, 4)
|
||||
}
|
||||
|
||||
func testThatItHasProperCount() {
|
||||
XCTAssertEqual(Matrix4.count, 16)
|
||||
XCTAssertEqual(Matrix4().data.count, Matrix4.count)
|
||||
}
|
||||
}
|
||||
|
||||
class Matrix4SubscriptTests: XCTestCase {
|
||||
var m: Matrix4! = nil
|
||||
|
||||
override func setUp() {
|
||||
do {
|
||||
m = try Matrix4(values: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
|
||||
} catch {
|
||||
XCTFail()
|
||||
}
|
||||
}
|
||||
|
||||
func testThatSingleSubscriptWorks() {
|
||||
for i in 0..<Matrix4.count {
|
||||
XCTAssertEqual(m[i], m.data[i])
|
||||
}
|
||||
}
|
||||
|
||||
func testThatRowColumnSubscriptWorks() {
|
||||
var value: Int = 0
|
||||
for i in 0..<Matrix4.dimension {
|
||||
for j in 0..<Matrix4.dimension {
|
||||
XCTAssertEqual(m[i,j], Float(value++))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testThatSingleSubscriptAssignmentWorks() {
|
||||
m[6] = 42.0
|
||||
XCTAssertEqual(m[6], 42.0)
|
||||
}
|
||||
|
||||
func testThatRowColumnSubscriptAssignmentWorks() {
|
||||
m[1,2] = 42.0
|
||||
XCTAssertEqual(m[1,2], 42.0)
|
||||
}
|
||||
}
|
||||
|
||||
class Matrix3Tests: XCTestCase {
|
||||
func testThatItHasProperDimension() {
|
||||
XCTAssertEqual(Matrix3.dimension, 3)
|
||||
}
|
||||
|
||||
func testThatItHasProperCount() {
|
||||
XCTAssertEqual(Matrix3.count, 9)
|
||||
XCTAssertEqual(Matrix3().data.count, Matrix3.count)
|
||||
}
|
||||
}
|
||||
|
||||
class Matrix3SubscriptTests: XCTestCase {
|
||||
var m: Matrix3! = nil
|
||||
|
||||
override func setUp() {
|
||||
m = Matrix3()
|
||||
for i in 0..<Matrix3.count {
|
||||
m[i] = Float(i)
|
||||
}
|
||||
}
|
||||
|
||||
func testThatSingleSubscriptWorks() {
|
||||
for i in 0..<Matrix3.count {
|
||||
XCTAssertEqual(m[i], m.data[i])
|
||||
}
|
||||
}
|
||||
|
||||
func testThatRowColumnSubscriptWorks() {
|
||||
XCTAssertEqual(m[0,0], m.data[0])
|
||||
XCTAssertEqual(m[0,1], m.data[1])
|
||||
XCTAssertEqual(m[0,2], m.data[2])
|
||||
XCTAssertEqual(m[1,0], m.data[3])
|
||||
XCTAssertEqual(m[1,1], m.data[4])
|
||||
XCTAssertEqual(m[1,2], m.data[5])
|
||||
XCTAssertEqual(m[2,0], m.data[6])
|
||||
XCTAssertEqual(m[2,1], m.data[7])
|
||||
XCTAssertEqual(m[2,2], m.data[8])
|
||||
}
|
||||
|
||||
func testThatSingleSubscriptAssignmentWorks() {
|
||||
m[6] = 42.0
|
||||
XCTAssertEqual(m[6], 42.0)
|
||||
}
|
||||
|
||||
func testThatRowColumnSubscriptAssignmentWorks() {
|
||||
m[1,2] = 42.0
|
||||
XCTAssertEqual(m[1,2], 42.0)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue