Skip to content

Commit

Permalink
Update Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aidangomez committed Dec 14, 2015
1 parent a860107 commit f579136
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 54 deletions.
20 changes: 10 additions & 10 deletions Tests/ArithmeticTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class ArithmeticTests: XCTestCase {
let array = RealArray(values)

var expected = Real()
for v in array {
expected += v
for i in array.startIndex..<array.endIndex {
expected += array[i]
}

var actual: Real = 0.0
Expand Down Expand Up @@ -79,8 +79,8 @@ class ArithmeticTests: XCTestCase {
let a2: RealArray = [3.0, 2.0, 1.0, 0.0, -1.0]
let r = a1 + a2
XCTAssertEqual(r.count, 3)
for v in r {
XCTAssertEqual(v, 4.0)
for i in r.startIndex..<r.endIndex {
XCTAssertEqual(r[i], 4.0)
}
}

Expand All @@ -89,8 +89,8 @@ class ArithmeticTests: XCTestCase {
let a2: RealArray = [4.0, 3.0, 2.0, 1.0]
let r = a1 - a2
XCTAssertEqual(r.count, 4)
for v in r {
XCTAssertEqual(v, 1.0)
for i in r.startIndex..<r.endIndex {
XCTAssertEqual(r[i], 1.0)
}
}

Expand All @@ -99,8 +99,8 @@ class ArithmeticTests: XCTestCase {
let a2: RealArray = [4.0, 3.0, 2.0, 1.0]
a1 -= a2
XCTAssertEqual(a1.count, 4)
for v in a1 {
XCTAssertEqual(v, 1.0)
for i in a1.startIndex..<a1.endIndex {
XCTAssertEqual(a1[i], 1.0)
}
}

Expand All @@ -112,8 +112,8 @@ class ArithmeticTests: XCTestCase {

let r = s1 + a2
XCTAssertEqual(r.count, 3)
for v in r {
XCTAssertEqual(v, 4.0)
for i in r.startIndex..<r.endIndex {
XCTAssertEqual(r[i], 4.0)
}
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/AuxiliaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ class AuxiliaryTests: XCTestCase {
let n = 10000

func testCopysign() {
let signs = RealArray((0..<n).map {$0 % 2 == 0 ? 1.0 : -1.0})
let signs = RealArray((0..<n).map({ $0 % 2 == 0 ? 1.0 : -1.0 }))

let magnitudes = RealArray(count: n)
for i in 0..<n {
magnitudes[i] = Real(arc4random_uniform(10))
}

let expected = RealArray(count: n)
for (i, (sign, magnitude)) in Zip2Sequence(signs, magnitudes).enumerate() {
expected[i] = sign * abs(magnitude)
for i in 0..<signs.count {
expected[i] = signs[i] * abs(magnitudes[i])
}

var actual: RealArray = []
Expand Down
10 changes: 5 additions & 5 deletions Tests/ComplexTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class ComplexTests: XCTestCase {
let a: ComplexArray = [Complex(real: 1, imag: 1), Complex(real: 2, imag: 0)]
let b: ComplexArray = [Complex(real: 2, imag: 2), Complex(real: 1, imag: 3)]
let r = a + b
for c in r {
XCTAssertEqual(c, Complex(real: 3, imag: 3))
for i in r.startIndex..<r.endIndex {
XCTAssertEqual(r[i], Complex(real: 3, imag: 3))
}
}

Expand All @@ -51,9 +51,9 @@ class ComplexTests: XCTestCase {
})

var expected = Complex()
for v in values {
expected.real += v.real
expected.imag += v.imag
for i in 0..<values.count {
expected.real += values[i].real
expected.imag += values[i].imag
}

var actual = Complex()
Expand Down
4 changes: 2 additions & 2 deletions Tests/RealArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RealArrayTests: XCTestCase {
}

func testAddSlice() {
let a: RealArray = [1, 2, 3, 4]
let a: RealArray = [1.0, 2.0, 3.0, 4.0]
let b = a[0...1] + a[2...3]

XCTAssertEqual(b.count, 2)
Expand All @@ -84,6 +84,6 @@ class RealArrayTests: XCTestCase {
XCTAssertEqual(slice.count, 4)
XCTAssertEqual(slice.startIndex, 5)
XCTAssertEqual(slice.endIndex, 9)
XCTAssertEqual([Double](slice), [5.0, 6.0, 7.0, 8.0])
XCTAssert(slice == RealArray([5.0, 6.0, 7.0, 8.0]))
}
}
32 changes: 16 additions & 16 deletions Tests/RealMatrixTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,35 @@ import XCTest

class RealMatrixTests: XCTestCase {
func testAdd() {
let a = RealMatrix(rows: 2, columns: 2, elements: [1, 2, 3, 4])
let b = RealMatrix(rows: 2, columns: 2, elements: [2, 3, 4, 1])
var a = RealMatrix(rows: 2, columns: 2, elements: [1, 2, 3, 4] as ValueArray<Double>)
let b = RealMatrix(rows: 2, columns: 2, elements: [2, 3, 4, 1] as ValueArray<Double>)
let c = a + b
a += b

let d = RealMatrix(rows: 2, columns: 2, elements: [3, 5, 7, 5])
let d = RealMatrix(rows: 2, columns: 2, elements: [3, 5, 7, 5] as ValueArray<Double>)

XCTAssertEqual(a, c)
XCTAssertEqual(a, d)
}

func testSub() {
let a = RealMatrix(rows: 2, columns: 2, elements: [1, 2, 3, 4])
let b = RealMatrix(rows: 2, columns: 2, elements: [2, 3, 4, 1])
var a = RealMatrix(rows: 2, columns: 2, elements: [1, 2, 3, 4] as ValueArray<Double>)
let b = RealMatrix(rows: 2, columns: 2, elements: [2, 3, 4, 1] as ValueArray<Double>)
let c = a - b
a -= b

let d = RealMatrix(rows: 2, columns: 2, elements: [-1, -1, -1, 3])
let d = RealMatrix(rows: 2, columns: 2, elements: [-1, -1, -1, 3] as ValueArray<Double>)

XCTAssertEqual(a, c)
XCTAssertEqual(a, d)
}

func testMult() {
let a = RealMatrix(rows: 2, columns: 2, elements: [1, 2, 3, 4])
let b = RealMatrix(rows: 2, columns: 2, elements: [2, 3, 4, 5])
let a = RealMatrix(rows: 2, columns: 2, elements: [1, 2, 3, 4] as ValueArray<Double>)
let b = RealMatrix(rows: 2, columns: 2, elements: [2, 3, 4, 5] as ValueArray<Double>)
let c = a * b

let d = RealMatrix(rows: 2, columns: 2, elements: [10, 13, 22, 29])
let d = RealMatrix(rows: 2, columns: 2, elements: [10, 13, 22, 29] as ValueArray<Double>)

XCTAssertEqual(c, d)

Expand All @@ -61,7 +61,7 @@ class RealMatrixTests: XCTestCase {
}

func testMultiplyWithColumn() {
let v = RealArray([2, 3, 4, 5, 6])
let v = RealArray([2, 3, 4, 5, 6] as ValueArray<Double>)
let m = RealMatrix([[1, 2, 3, 4, 5]])
let c = v.toColumnMatrix() * m

Expand All @@ -72,7 +72,7 @@ class RealMatrixTests: XCTestCase {

func testMultiplyWithRow() {
let m = RealMatrix([[1], [2], [3], [4], [5]])
let v = RealArray([2, 3, 4, 5, 6])
let v = RealArray([2, 3, 4, 5, 6] as ValueArray<Double>)
let c = v.toRowMatrix() * m

let d = RealMatrix([[70]])
Expand All @@ -82,7 +82,7 @@ class RealMatrixTests: XCTestCase {

func testPostMultiplyWithColumn() {
let m = RealMatrix([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
let v = RealArray([2, 3, 4, 5, 6])
let v = RealArray([2, 3, 4, 5, 6] as ValueArray<Double>)
let c = m * v.toColumnMatrix()

let d = RealMatrix([[70], [170]])
Expand All @@ -92,7 +92,7 @@ class RealMatrixTests: XCTestCase {

func testPostMultiplyWithRow() {
let m = RealMatrix([[1], [2], [3], [4], [5]])
let v = RealArray([2, 3, 4, 5, 6])
let v = RealArray([2, 3, 4, 5, 6] as ValueArray<Double>)
let c = m * v.toRowMatrix()

let d = RealMatrix([[2, 3, 4, 5, 6], [4, 6, 8, 10, 12], [6, 9, 12, 15, 18], [8, 12, 16, 20, 24], [10, 15, 20, 25, 30]])
Expand All @@ -101,18 +101,18 @@ class RealMatrixTests: XCTestCase {
}

func testTranspose() {
let a = RealMatrix(rows: 2, columns: 2, elements: [1, 2, 3, 4])
let a = RealMatrix(rows: 2, columns: 2, elements: [1, 2, 3, 4] as ValueArray<Double>)
let b = transpose(a)
let c = transpose(b)

XCTAssertEqual(a, c)
}

func testInvert() {
let a = RealMatrix(rows: 2, columns: 2, elements: [2, 6, -2, 4])
let a = RealMatrix(rows: 2, columns: 2, elements: [2, 6, -2, 4] as ValueArray<Double>)
let b = inv(a)

let c = RealMatrix(rows: 2, columns: 2, elements: [0.2, -0.3, 0.1, 0.1])
let c = RealMatrix(rows: 2, columns: 2, elements: [0.2, -0.3, 0.1, 0.1] as ValueArray<Double>)

XCTAssertEqualWithAccuracy(c.elements[0], b.elements[0], accuracy: 0.00001)
XCTAssertEqualWithAccuracy(c.elements[1], b.elements[1], accuracy: 0.00001)
Expand Down
36 changes: 18 additions & 18 deletions Tests/TensorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class TensorTests: XCTestCase {
}

func testSliceAndSubscript() {
let slice1 = diagonalTensor3D[3, 2...3, 2...3]
let slice2 = diagonalTensor4D[1, 1, Interval.All, Interval.All]
let slice3 = diagonalTensor4D.extractMatrix(1, 1, 0...1, 0...1)
let slice1 = diagonalTensor3D[3...3, 2...3, 2...3]
let slice2 = diagonalTensor4D[1...1, 1...1, Interval.All, Interval.All]
let slice3 = diagonalTensor4D.asMatrix(1...1, 1...1, 0...1, 0...1)

XCTAssertEqual(slice1, slice2)
XCTAssert(slice1 == slice3)
XCTAssert(slice3 == slice2)
XCTAssertEqual(slice1[0, 1, 1], 1)
XCTAssertEqual(slice1[3, 3, 3], 1)
}

func testSliceAndValueAssignment() {
Expand All @@ -58,35 +58,35 @@ class TensorTests: XCTestCase {
}

func testSliceValueAssignment() {
let tensor = Tensor(dimensions: [2, 2, 2, 2], elements: [6.4, 2.4, 8.6, 0.2, 6.4, 1.5, 7.3, 1.1, 6.0, 1.4, 7.8, 9.2, 4.2, 6.1, 8.7, 3.6])
diagonalTensor4D[1, Interval.All, 0...1, 0...1] = tensor[0, 0...1, Interval.All, 0...1]
let tensor = Tensor(dimensions: [2, 2, 2, 2], elements: [6.4, 2.4, 8.6, 0.2, 6.4, 1.5, 7.3, 1.1, 6.0, 1.4, 7.8, 9.2, 4.2, 6.1, 8.7, 3.6] as RealArray)
diagonalTensor4D[Interval(integerLiteral: 1), Interval.All, Interval(range: 0...1), Interval(range: 0...1)] = tensor[Interval(integerLiteral: 0), Interval(range: 0...1), Interval.All, Interval(range: 0...1)]

let expected = Tensor(dimensions: [2, 2, 2, 2], elements: [1, 0, 0, 0, 0, 0, 0, 0, 6.4, 2.4, 8.6, 0.2, 6.4, 1.5, 7.3, 1.1])
let expected = Tensor(dimensions: [2, 2, 2, 2], elements: [1, 0, 0, 0, 0, 0, 0, 0, 6.4, 2.4, 8.6, 0.2, 6.4, 1.5, 7.3, 1.1] as RealArray)
XCTAssertEqual(diagonalTensor4D, expected)
}

func testMatrixExtraction() {
var matrix = diagonalTensor4D.extractMatrix(1, 1, 0...1, 0...1)
var matrix = diagonalTensor4D.asMatrix(Interval(integerLiteral: 1), Interval(integerLiteral: 1), Interval(range: 0...1), Interval(range: 0...1))
var expected = RealMatrix([[0, 0], [0, 1]])
XCTAssertEqual(matrix, expected)
XCTAssert(matrix == expected)

matrix = diagonalTensor4D.extractMatrix(0, 0, 0, 0)
matrix = diagonalTensor4D.asMatrix(Interval(integerLiteral: 0), Interval(integerLiteral: 0), Interval(integerLiteral: 0), Interval(integerLiteral: 0))
expected = RealMatrix([[1]])
XCTAssertEqual(matrix, expected)
XCTAssert(matrix == expected)
}

func testContinuity() {
XCTAssert(diagonalTensor3D[1...4, Interval.All, Interval.All].isContiguous)
XCTAssert(diagonalTensor3D[4, 2, 1...2].isContiguous)
XCTAssert(diagonalTensor3D[1, 4, Interval.All].isContiguous)
XCTAssert(diagonalTensor3D[1, Interval.All, Interval.All].isContiguous)
XCTAssert(diagonalTensor3D[1, 2...3, Interval.All].isContiguous)
XCTAssert(diagonalTensor3D[4...4, 2...2, 1...2].isContiguous)
XCTAssert(diagonalTensor3D[1...1, 4...4, Interval.All].isContiguous)
XCTAssert(diagonalTensor3D[1...1, Interval.All, Interval.All].isContiguous)
XCTAssert(diagonalTensor3D[1...1, 2...3, Interval.All].isContiguous)
}

func testNoContinuity() {
XCTAssertFalse(diagonalTensor3D[1...2, 3...4, 1].isContiguous)
XCTAssertFalse(diagonalTensor3D[1...2, 3...4, 1...1].isContiguous)
XCTAssertFalse(diagonalTensor3D[Interval.All, 3...4, Interval.All].isContiguous)
XCTAssertFalse(diagonalTensor3D[1, Interval.All, 1].isContiguous)
XCTAssertFalse(diagonalTensor3D[Interval.All, 1, 1].isContiguous)
XCTAssertFalse(diagonalTensor3D[1...1, Interval.All, 1...1].isContiguous)
XCTAssertFalse(diagonalTensor3D[Interval.All, 1...1, 1...1].isContiguous)
}
}

0 comments on commit f579136

Please sign in to comment.