[Math] Some basic Vector tests

This commit is contained in:
Eryn Wells 2015-11-12 23:18:26 -08:00
parent 58dfb3f787
commit 23bbdac76a
4 changed files with 59 additions and 1 deletions

View file

@ -8,7 +8,23 @@
import Foundation
import XCTest
@testable import Math
func assertVectorAlmostEqual<T: Vector>(@autoclosure lhs: () -> T?, @autoclosure _ rhs: () -> T?, message: String = "", file: String = __FILE__, line: UInt = __LINE__) {
let lhsValue = lhs()
let rhsValue = rhs()
let msg = message != "" ? message : "expected \(lhsValue), got \(rhsValue)"
XCTAssert(lhsValue ==~ rhsValue, msg, file: file, line: line)
}
class MatrixVectorTests: XCTestCase {
func
func testThatMultiplyingIdentityByAVectorGivesTheVector() {
let v = Matrix4.identity * Vector4(x: 1, y: 2, z: 3)
assertVectorAlmostEqual(v, Vector4(x: 1, y: 2, z: 3))
}
func testThatMultiplyingTranslationByAVectorGivesTranslatedVector() {
let v = Matrix4(translationWithX: 1, y: 1, z: 1) * Vector4(x: 2, y: 2, z: 2)
assertVectorAlmostEqual(v, Vector4(x: 3, y: 3, z: 3))
}
}

View file

@ -0,0 +1,10 @@
//
// TypeTests.swift
// Math
//
// Created by Eryn Wells on 11/9/15.
// Copyright © 2015 Eryn Wells. All rights reserved.
//
import Foundation
import Math

View file

@ -0,0 +1,24 @@
//
// VectorTests.swift
// Math
//
// Created by Eryn Wells on 11/12/15.
// Copyright © 2015 Eryn Wells. All rights reserved.
//
import XCTest
@testable import Math
class VectorTests: XCTestCase {
func testThatLengthOfAUnitVectorIsSane() {
XCTAssertEqualWithAccuracy(Vector3(x: 1, y: 0, z: 0).length, 1.0, accuracy: Float.Epsilon)
}
func testThatTheLength2OfAVectorIsSane() {
XCTAssertEqualWithAccuracy(Vector3(x: 2, y: 0, z: 0).length2, 4.0, accuracy: Float.Epsilon)
}
func testThatANormalizedVectorHasLength1() {
XCTAssertEqualWithAccuracy(Vector3(x: 1, y: 2, z: 3).normalized.length, 1.0, accuracy: 1e-6)
}
}