diff --git a/Math/Math.xcodeproj/project.pbxproj b/Math/Math.xcodeproj/project.pbxproj index 925b4fd..d2f4354 100644 --- a/Math/Math.xcodeproj/project.pbxproj +++ b/Math/Math.xcodeproj/project.pbxproj @@ -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 = ""; }; C005DFFF1BE1CBA700F1BD3C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 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 = ""; }; + C005E0091BE1CBA800F1BD3C /* MatrixTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MatrixTests.swift; sourceTree = ""; }; C005E00B1BE1CBA800F1BD3C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; C005E0381BE1CC7A00F1BD3C /* Matrix.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Matrix.swift; sourceTree = ""; }; C04449C01BE26A0700ABF046 /* Vector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Vector.swift; sourceTree = ""; }; @@ -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; }; diff --git a/Math/Math/Matrix.swift b/Math/Math/Matrix.swift index 01b0204..feb2684 100644 --- a/Math/Math/Matrix.swift +++ b/Math/Math/Matrix.swift @@ -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 } } diff --git a/Math/MathTests/MathTests.swift b/Math/MathTests/MathTests.swift deleted file mode 100644 index b345c9f..0000000 --- a/Math/MathTests/MathTests.swift +++ /dev/null @@ -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. - } - } - -} diff --git a/Math/MathTests/MatrixTests.swift b/Math/MathTests/MatrixTests.swift new file mode 100644 index 0000000..749e8b6 --- /dev/null +++ b/Math/MathTests/MatrixTests.swift @@ -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..