Add linter, fix linter warnigns, uncomment tests

This commit is contained in:
Alejandro Isaza 2017-09-24 23:07:45 -07:00
parent 6665edfcee
commit 7c1b6ea3fc
19 changed files with 219 additions and 122 deletions

9
.swiftlint.yml Normal file
View File

@ -0,0 +1,9 @@
disabled_rules:
- file_length
- identifier_name
- line_length
- trailing_comma
opt_in_rules:
- closure_spacing

View File

@ -1,4 +1,5 @@
# Surge [![Build Status](https://travis-ci.org/mattt/Surge.svg?branch=master)](https://travis-ci.org/mattt/Surge) [![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://github.com/mattt/Surge/blob/master/LICENSE) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
# Surge
[![Build Status](https://travis-ci.org/mattt/Surge.svg?branch=master)](https://travis-ci.org/mattt/Surge) [![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg)](https://github.com/mattt/Surge/blob/master/LICENSE) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
Surge is a Swift library that uses the Accelerate framework to provide high-performance functions for matrix math, digital signal processing, and image manipulation.

View File

@ -390,7 +390,6 @@ public func dot<X: ContinuousCollection, Y: ContinuousCollection>(_ x: X, _ y: Y
return result
}
public func dot<X: ContinuousCollection, Y: ContinuousCollection>(_ x: X, _ y: Y) -> Double where X.Iterator.Element == Double, Y.Iterator.Element == Double {
precondition(x.count == y.count, "Vectors must have equal count")

View File

@ -26,7 +26,7 @@ import Accelerate
// Convolution of a signal [x], with a kernel [k]. The signal must be at least as long as the kernel.
public func conv<X: ContinuousCollection, K: ContinuousCollection>(_ x: X, _ k: K) -> [Float] where X.Iterator.Element == Float, K.Iterator.Element == Float {
precondition(x.count >= k.count, "Input vector [x] must have at least as many elements as the kernel, [k]")
let resultSize = numericCast(x.count) + numericCast(k.count) - 1
var result = [Float](repeating: 0, count: resultSize)
result.withUnsafeMutableBufferPointer { rbp in
@ -80,7 +80,7 @@ public func xcorr<X: ContinuousCollection, Y: ContinuousCollection>(_ x: X, _ y:
let padding = repeatElement(0 as Float, count: numericCast(x.count) - numericCast(y.count))
yPadded.append(contentsOf: padding)
}
let resultSize = numericCast(x.count) + yPadded.count - 1
var result = [Float](repeating: 0, count: resultSize)
let xPad = repeatElement(0 as Float, count: yPadded.count-1)
@ -94,7 +94,7 @@ public func xcorr<X: ContinuousCollection, Y: ContinuousCollection>(_ x: X, _ y:
result.withUnsafeMutableBufferPointer { rbp in
vDSP_conv(xPadded, 1, yPadded, 1, rbp.baseAddress!, 1, vDSP_Length(resultSize), vDSP_Length(yPadded.count))
}
return result
}
@ -107,7 +107,7 @@ public func xcorr<X: ContinuousCollection, Y: ContinuousCollection>(_ x: X, _ y:
let padding = repeatElement(0 as Double, count: numericCast(x.count) - numericCast(y.count))
yPadded.append(contentsOf: padding)
}
let resultSize = numericCast(x.count) + yPadded.count - 1
var result = [Double](repeating: 0, count: resultSize)
let xPad = repeatElement(0 as Double, count: yPadded.count-1)
@ -121,7 +121,7 @@ public func xcorr<X: ContinuousCollection, Y: ContinuousCollection>(_ x: X, _ y:
result.withUnsafeMutableBufferPointer { rbp in
vDSP_convD(xPadded, 1, yPadded, 1, rbp.baseAddress!, 1, vDSP_Length(resultSize), vDSP_Length(yPadded.count))
}
return result
}
@ -165,6 +165,6 @@ public func xcorr<X: ContinuousCollection>(_ x: X) -> [Double] where X.Iterator.
vDSP_convD(xPadded, 1, xp, 1, rbp.baseAddress!, 1, vDSP_Length(resultSize), vDSP_Length(xc))
}
}
return result
}

View File

@ -57,7 +57,7 @@ public struct Matrix<Scalar> where Scalar: FloatingPoint, Scalar: ExpressibleByF
grid[(row * columns) + column] = newValue
}
}
public subscript(row row: Int) -> [Scalar] {
get {
assert(row < rows)
@ -65,7 +65,7 @@ public struct Matrix<Scalar> where Scalar: FloatingPoint, Scalar: ExpressibleByF
let endIndex = row * columns + columns
return Array(grid[startIndex..<endIndex])
}
set {
assert(row < rows)
assert(newValue.count == columns)
@ -74,7 +74,7 @@ public struct Matrix<Scalar> where Scalar: FloatingPoint, Scalar: ExpressibleByF
grid.replaceSubrange(startIndex..<endIndex, with: newValue)
}
}
public subscript(column column: Int) -> [Scalar] {
get {
var result = [Scalar](repeating: 0.0, count: rows)
@ -84,7 +84,7 @@ public struct Matrix<Scalar> where Scalar: FloatingPoint, Scalar: ExpressibleByF
}
return result
}
set {
assert(column < columns)
assert(newValue.count == rows)
@ -107,7 +107,7 @@ extension Matrix: CustomStringConvertible {
var description = ""
for i in 0..<rows {
let contents = (0..<columns).map{"\(self[i, $0])"}.joined(separator: "\t")
let contents = (0..<columns).map({ "\(self[i, $0])" }).joined(separator: "\t")
switch (i, rows) {
case (0, 1):
@ -152,7 +152,6 @@ public func ==<T> (lhs: Matrix<T>, rhs: Matrix<T>) -> Bool {
return lhs.rows == rhs.rows && lhs.columns == rhs.columns && lhs.grid == rhs.grid
}
// MARK: -
public func add(_ x: Matrix<Float>, _ y: Matrix<Float>) -> Matrix<Float> {
@ -268,7 +267,7 @@ public func exp(_ x: Matrix<Float>) -> Matrix<Float> {
}
public func sum(_ x: Matrix<Double>, axies: MatrixAxies = .column) -> Matrix<Double> {
switch axies {
case .column:
var result = Matrix<Double>(rows: 1, columns: x.columns, repeatedValue: 0.0)
@ -276,7 +275,7 @@ public func sum(_ x: Matrix<Double>, axies: MatrixAxies = .column) -> Matrix<Dou
result.grid[i] = sum(x[column: i])
}
return result
case .row:
var result = Matrix<Double>(rows: x.rows, columns: 1, repeatedValue: 0.0)
for i in 0..<x.rows {
@ -286,7 +285,7 @@ public func sum(_ x: Matrix<Double>, axies: MatrixAxies = .column) -> Matrix<Dou
}
}
public func inv(_ x : Matrix<Float>) -> Matrix<Float> {
public func inv(_ x: Matrix<Float>) -> Matrix<Float> {
precondition(x.rows == x.columns, "Matrix must be square")
var results = x
@ -386,14 +385,14 @@ public func / (lhs: Matrix<Float>, rhs: Matrix<Float>) -> Matrix<Float> {
public func / (lhs: Matrix<Double>, rhs: Double) -> Matrix<Double> {
var result = Matrix<Double>(rows: lhs.rows, columns: lhs.columns, repeatedValue: 0.0)
result.grid = lhs.grid / rhs;
return result;
result.grid = lhs.grid / rhs
return result
}
public func / (lhs: Matrix<Float>, rhs: Float) -> Matrix<Float> {
var result = Matrix<Float>(rows: lhs.rows, columns: lhs.columns, repeatedValue: 0.0)
result.grid = lhs.grid / rhs;
return result;
result.grid = lhs.grid / rhs
return result
}
postfix operator

View File

@ -52,7 +52,6 @@ public func withUnsafeMutablePointersAndCountsTo<A: ContinuousMutableCollection>
}
}
// MARK: 2 Parameter
/// Invokes the given closure with pointers to the given arguments (2 parameter version).
@ -129,7 +128,6 @@ public func withUnsafeMutablePointersAndCountsTo<A: ContinuousMutableCollection,
}
}
// MARK: 3 Parameter
/// Invokes the given closure with pointers to the given arguments (3 parameter version).

View File

@ -25,7 +25,7 @@ import Accelerate
public func pow<X: ContinuousCollection, Y: ContinuousCollection>(_ x: X, _ y: Y) -> [Float] where X.Iterator.Element == Float, Y.Iterator.Element == Float {
var results = [Float](repeating: 0.0, count: numericCast(x.count))
results.withUnsafeMutableBufferPointer { pointer in
withUnsafePointersAndCountsTo(x, y) { xp, xc, yp, yc in
withUnsafePointersAndCountsTo(x, y) { xp, xc, yp, _ in
vvpowf(pointer.baseAddress!, xp, yp, [Int32(xc)])
}
}
@ -35,7 +35,7 @@ public func pow<X: ContinuousCollection, Y: ContinuousCollection>(_ x: X, _ y: Y
public func pow<X: ContinuousCollection, Y: ContinuousCollection>(_ x: X, _ y: Y) -> [Double] where X.Iterator.Element == Double, Y.Iterator.Element == Double {
var results = [Double](repeating: 0.0, count: numericCast(x.count))
results.withUnsafeMutableBufferPointer { pointer in
withUnsafePointersAndCountsTo(x, y) { xp, xc, yp, yc in
withUnsafePointersAndCountsTo(x, y) { xp, xc, yp, _ in
vvpow(pointer.baseAddress!, xp, yp, [Int32(xc)])
}
}

View File

@ -27,7 +27,7 @@ let count = 64
let frequency = 4.0
let amplitude = 3.0
let x = (0..<count).map{ 2.0 * Double.pi / Double(count) * Double($0) * frequency }
let x = (0..<count).map { 2.0 * Double.pi / Double(count) * Double($0) * frequency }
for value in sin(x) {
value

View File

@ -235,6 +235,7 @@
F8A1E1A619917A79009735E2 /* Frameworks */,
F8A1E1A719917A79009735E2 /* Headers */,
F8A1E1A819917A79009735E2 /* Resources */,
61AA2D731F78CF4E00B28C43 /* Lint */,
);
buildRules = (
);
@ -302,6 +303,24 @@
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXShellScriptBuildPhase section */
61AA2D731F78CF4E00B28C43 /* Lint */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = Lint;
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, `brew install swiftlint` or download from https://github.com/realm/SwiftLint\"\nfi";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
F84A6AAA19A9A72F007B53E1 /* Sources */ = {
isa = PBXSourcesBuildPhase;

View File

@ -37,6 +37,19 @@
</dict>
</dict>
</dict>
<key>AuxiliaryTests</key>
<dict>
<key>test_copysign()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>3.4883e-05</real>
<key>baselineIntegrationDisplayName</key>
<string>Sep 24, 2017, 11:04:17 PM</string>
</dict>
</dict>
</dict>
<key>ExponentialTests</key>
<dict>
<key>test_exp()</key>
@ -62,6 +75,36 @@
</dict>
<key>HyperbolicTests</key>
<dict>
<key>test_acosh()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.00014254</real>
<key>baselineIntegrationDisplayName</key>
<string>Sep 24, 2017, 11:04:17 PM</string>
</dict>
</dict>
<key>test_asinh()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>8.0483e-05</real>
<key>baselineIntegrationDisplayName</key>
<string>Sep 24, 2017, 11:04:17 PM</string>
</dict>
</dict>
<key>test_atanh()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.00012056</real>
<key>baselineIntegrationDisplayName</key>
<string>Sep 24, 2017, 11:04:17 PM</string>
</dict>
</dict>
<key>test_cosh()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
@ -95,6 +138,36 @@
</dict>
<key>TrigonometricTests</key>
<dict>
<key>test_acos()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.00015664</real>
<key>baselineIntegrationDisplayName</key>
<string>Sep 24, 2017, 11:06:27 PM</string>
</dict>
</dict>
<key>test_asin()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.00012768</real>
<key>baselineIntegrationDisplayName</key>
<string>Sep 24, 2017, 11:06:27 PM</string>
</dict>
</dict>
<key>test_atan()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
<dict>
<key>baselineAverage</key>
<real>0.00014418</real>
<key>baselineIntegrationDisplayName</key>
<string>Sep 24, 2017, 11:06:27 PM</string>
</dict>
</dict>
<key>test_cos()</key>
<dict>
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>

View File

@ -26,7 +26,7 @@ class ArithmeticTests: XCTestCase {
let n = 100000
func test_sum() {
let values = (0...n).map{ _ in Double(arc4random()) / Double(UInt32.max) }
let values = (0...n).map { _ in Double(arc4random()) / Double(UInt32.max) }
var actual = 0.0
measure {
actual = sum(values)
@ -41,7 +41,7 @@ class ArithmeticTests: XCTestCase {
}
func test_sum_slice() {
let values = (0...n).map{ _ in Double(arc4random()) / Double(UInt32.max) }
let values = (0...n).map { _ in Double(arc4random()) / Double(UInt32.max) }
var actual = 0.0
measure {
actual = sum(values[0..<n/2])
@ -56,7 +56,7 @@ class ArithmeticTests: XCTestCase {
}
func test_sqrt() {
let values = (0...n).map{ _ in Double(arc4random()) }
let values = (0...n).map { _ in Double(arc4random()) }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: sqrt, mapped: sqrt, accuracy: 0.0001)
}

View File

@ -26,7 +26,7 @@ class AuxiliaryTests: XCTestCase {
let n = 10000
func test_copysign() {
let signs = [Double]((0..<n).map {$0 % 2 == 0 ? 1.0 : -1.0})
let signs = Array((0..<n).map({ $0 % 2 == 0 ? 1.0 : -1.0 }))
var magnitudes = [Double]()
for _ in (0..<n).enumerated() {

View File

@ -24,9 +24,9 @@ import Surge
import XCTest
class ConvolutionTests: XCTestCase {
let floatAccuracy:Float = 0.00000001
let doubleAccuracy:Double = 0.00000000001
let floatAccuracy: Float = 0.00000001
let doubleAccuracy: Double = 0.00000000001
// MARK: Test Arrays - Float
let a1f: [Float] = [0, 0, 1, 0, 0]
let a2f: [Float] = [1, 0, 0]
@ -38,7 +38,7 @@ class ConvolutionTests: XCTestCase {
let d2f: [Float] = [0, 1]
let e1f: [Float] = [0, 0, 0, 0, 0]
let e2f: [Float] = [0, 0, 0]
// MARK: Test Arrays - Double
let a1d: [Double] = [0, 0, 1, 0, 0]
let a2d: [Double] = [1, 0, 0]
@ -50,31 +50,30 @@ class ConvolutionTests: XCTestCase {
let d2d: [Double] = [0, 1]
let e1d: [Double] = [0, 0, 0, 0, 0]
let e2d: [Double] = [0, 0, 0]
// MARK: Convolution - Float
func test_conv_float() {
let a3f: [Float] = [0, 0, 1, 0, 0, 0, 0]
let b3f: [Float] = [0, 0, 0, -2, -3, -1, -5, -6]
let c3f: [Float] = [0, 0, 0, 0, -1, 0, -2, 1, 3]
let b3f: [Float] = [0, 0, 0, -2, -3, -1, -5, -6]
let c3f: [Float] = [0, 0, 0, 0, -1, 0, -2, 1, 3]
let d3f: [Float] = [0, 1, 1]
let e3f: [Float] = [0, 0, 0, 0, 0, 0, 0]
XCTAssertArrayFloatEqualWithAccuracy(calcArray: conv(a1f, a2f), a3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: conv(b1f, b2f), b3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: conv(c1f, c2f), c3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: conv(d1f, d2f), d3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: conv(e1f, e2f), e3f, floatAccuracy)
}
// MARK: Convolution - Double
func test_conv_double() {
let a3d: [Double] = [0, 0, 1, 0, 0, 0, 0]
let b3d: [Double] = [0, 0, 0, -2, -3, -1, -5, -6]
let c3d: [Double] = [0, 0, 0, 0, -1, 0, -2, 1, 3]
let b3d: [Double] = [0, 0, 0, -2, -3, -1, -5, -6]
let c3d: [Double] = [0, 0, 0, 0, -1, 0, -2, 1, 3]
let d3d: [Double] = [0, 1, 1]
let e3d: [Double] = [0, 0, 0, 0, 0, 0, 0]
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: conv(a1d, a2d), a3d, doubleAccuracy)
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: conv(b1d, b2d), b3d, doubleAccuracy)
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: conv(c1d, c2d), c3d, doubleAccuracy)
@ -84,62 +83,62 @@ class ConvolutionTests: XCTestCase {
// MARK: Cross-Correlation - Float
func test_xcorr_float() {
let a3f: [Float] = [0, 0, 0, 0, 0, 0, 1, 0, 0]
let b3f: [Float] = [0, 0, 0, 0, -2, -3, -1, -5, -6, 0, 0]
let c3f: [Float] = [-1, 0, -2, 1, 3, 0, 0, 0, 0]
let a3f: [Float] = [0, 0, 0, 0, 0, 0, 1, 0, 0]
let b3f: [Float] = [0, 0, 0, 0, -2, -3, -1, -5, -6, 0, 0]
let c3f: [Float] = [-1, 0, -2, 1, 3, 0, 0, 0, 0]
let d3f: [Float] = [1, 1, 0]
let e3f: [Float] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
XCTAssertArrayFloatEqualWithAccuracy(calcArray: xcorr(a1f, a2f), a3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: xcorr(b1f, b2f), b3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: xcorr(c1f, c2f), c3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: xcorr(d1f, d2f), d3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: xcorr(e1f, e2f), e3f, floatAccuracy)
}
// MARK: Cross-Correlation - Double
func test_xcorr_double() {
let a3d: [Double] = [0, 0, 0, 0, 0, 0, 1, 0, 0]
let b3d: [Double] = [0, 0, 0, 0, -2, -3, -1, -5, -6, 0, 0]
let c3d: [Double] = [-1, 0, -2, 1, 3, 0, 0, 0, 0]
let a3d: [Double] = [0, 0, 0, 0, 0, 0, 1, 0, 0]
let b3d: [Double] = [0, 0, 0, 0, -2, -3, -1, -5, -6, 0, 0]
let c3d: [Double] = [-1, 0, -2, 1, 3, 0, 0, 0, 0]
let d3d: [Double] = [1, 1, 0]
let e3d: [Double] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: xcorr(a1d, a2d), a3d, doubleAccuracy)
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: xcorr(b1d, b2d), b3d, doubleAccuracy)
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: xcorr(c1d, c2d), c3d, doubleAccuracy)
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: xcorr(d1d, d2d), d3d, doubleAccuracy)
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: xcorr(e1d, e2d), e3d, doubleAccuracy)
}
// MARK: Auto-Correlation - Float
func test_acorr_float() {
let a3f: [Float] = [0, 0, 0, 0, 1, 0, 0, 0, 0]
let b3f: [Float] = [0, 12, 28, 23, 44, 75, 44, 23, 28, 12, 0]
let c3f: [Float] = [-3, -1, -4, 1, 15, 1, -4, -1, -3]
let b3f: [Float] = [0, 12, 28, 23, 44, 75, 44, 23, 28, 12, 0]
let c3f: [Float] = [-3, -1, -4, 1, 15, 1, -4, -1, -3]
let d3f: [Float] = [1, 2, 1]
let e3f: [Float] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
XCTAssertArrayFloatEqualWithAccuracy(calcArray: xcorr(a1f), a3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: xcorr(b1f), b3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: xcorr(c1f), c3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: xcorr(d1f), d3f, floatAccuracy)
XCTAssertArrayFloatEqualWithAccuracy(calcArray: xcorr(e1f), e3f, floatAccuracy)
}
// MARK: Auto-Correlation - Double
func test_acorr_double() {
let a3d: [Double] = [0, 0, 0, 0, 1, 0, 0, 0, 0]
let b3d: [Double] = [0, 12, 28, 23, 44, 75, 44, 23, 28, 12, 0]
let c3d: [Double] = [-3, -1, -4, 1, 15, 1, -4, -1, -3]
let b3d: [Double] = [0, 12, 28, 23, 44, 75, 44, 23, 28, 12, 0]
let c3d: [Double] = [-3, -1, -4, 1, 15, 1, -4, -1, -3]
let d3d: [Double] = [1, 2, 1]
let e3d: [Double] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: xcorr(a1d), a3d, doubleAccuracy)
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: xcorr(b1d), b3d, doubleAccuracy)
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: xcorr(c1d), c3d, doubleAccuracy)
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: xcorr(d1d), d3d, doubleAccuracy)
XCTAssertArrayDoubleEqualWithAccuracy(calcArray: xcorr(e1d), e3d, doubleAccuracy)
}
}

View File

@ -26,12 +26,12 @@ class ExponentialTests: XCTestCase {
let n = 10000
func test_exp() {
let values = (0...n).map{_ in Double(arc4random_uniform(10))}
let values = (0...n).map { _ in Double(arc4random_uniform(10)) }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: exp, mapped: exp, accuracy: 0.0001)
}
func test_exp2() {
let values = (0...n).map{_ in Double(arc4random_uniform(10))}
let values = (0...n).map { _ in Double(arc4random_uniform(10)) }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: exp2, mapped: exp2, accuracy: 0.0001)
}
}

View File

@ -26,32 +26,32 @@ class HyperbolicTests: XCTestCase {
let n = 10000
func test_sinh() {
let values = (0...n).map{_ in drand48() * Double.pi}
let values = (0...n).map { _ in drand48() * Double.pi }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: sinh, mapped: sinh, accuracy: 0.0001)
}
func test_cosh() {
let values = (0...n).map{_ in drand48() * Double.pi}
let values = (0...n).map { _ in drand48() * Double.pi }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: cosh, mapped: cosh, accuracy: 0.0001)
}
func test_tanh() {
let values = (0...n).map{_ in drand48() * Double.pi}
let values = (0...n).map { _ in drand48() * Double.pi }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: tanh, mapped: tanh, accuracy: 0.0001)
}
// func test_asinh() {
// let values = map(0...n){_ in drand48()}
// measureAndValidateMappedFunctionWithAccuracy(values, member: asinh, mapped: asinh, accuracy: 0.0001)
// }
//
// func test_acosh() {
// let values = map(0...n){_ in drand48()}
// measureAndValidateMappedFunctionWithAccuracy(values, member: acosh, mapped: acosh, accuracy: 0.0001)
// }
//
// func test_atanh() {
// let values = map(0...n){_ in drand48()}
// measureAndValidateMappedFunctionWithAccuracy(values, member: atanh, mapped: atanh, accuracy: 0.0001)
// }
func test_asinh() {
let values = (0...n).map { _ in drand48() }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: asinh, mapped: asinh, accuracy: 0.0001)
}
func test_acosh() {
let values = (0...n).map { _ in 1 + drand48() }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: acosh, mapped: acosh, accuracy: 0.0001)
}
func test_atanh() {
let values = (0...n).map { _ in drand48() }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: atanh, mapped: atanh, accuracy: 0.0001)
}
}

View File

@ -23,8 +23,8 @@ import XCTest
@testable import Surge
class MatrixTests: XCTestCase {
var matrix : Matrix<Double> = Matrix<Double>([[1, 2, 3, 4], [5,6,7,8], [9, 10, 11, 12]])
var matrix: Matrix<Double> = Matrix<Double>([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
func testInit() {
let m1 = Matrix([[1.0, 2.0]])
@ -35,32 +35,32 @@ class MatrixTests: XCTestCase {
}
func testSubscriptRow() {
XCTAssertEqual(matrix[row: 0], [1,2,3,4])
XCTAssertEqual(matrix[row: 0], [1, 2, 3, 4])
XCTAssertEqual(matrix[row: 1], [5, 6, 7, 8])
}
func testSubscriptColumn() {
XCTAssertEqual(matrix[column: 0], [1,5,9])
XCTAssertEqual(matrix[column: 1], [2,6,10])
XCTAssertEqual(matrix[column: 0], [1, 5, 9])
XCTAssertEqual(matrix[column: 1], [2, 6, 10])
}
func testSetRow() {
matrix[row: 0] = [13.0, 14.0, 15.0, 16.0]
XCTAssertTrue(matrix==Matrix<Double>([[13,14,15,16],[5,6,7,8], [9, 10, 11, 12]]))
XCTAssertTrue(matrix==Matrix<Double>([[13, 14, 15, 16], [5, 6, 7, 8], [9, 10, 11, 12]]))
}
func testSetColumn() {
matrix[column: 0] = [20,30,40]
XCTAssertEqual(matrix, Matrix<Double>([[20,2,3,4],[30,6,7,8], [40, 10, 11, 12]]))
matrix[column: 0] = [20, 30, 40]
XCTAssertEqual(matrix, Matrix<Double>([[20, 2, 3, 4], [30, 6, 7, 8], [40, 10, 11, 12]]))
}
func testMatrixPower() {
let expectedResult = Matrix<Double>([[1, 4, 9, 16], [25, 36, 49, 64], [81, 100, 121, 144]])
XCTAssertEqual(pow(matrix, 2), expectedResult)
}
func testElementWiseMultiplication() {
let matrix2 = Matrix<Double>([[2,3,4,5], [6,7,8,9], [10, 11, 12, 13]])
XCTAssertEqual(elmul(matrix, matrix2), Matrix<Double>([[2,6,12,20], [30,42,56,72], [90, 110, 132,156]]))
let matrix2 = Matrix<Double>([[2, 3, 4, 5], [6, 7, 8, 9], [10, 11, 12, 13]])
XCTAssertEqual(elmul(matrix, matrix2), Matrix<Double>([[2, 6, 12, 20], [30, 42, 56, 72], [90, 110, 132, 156]]))
}
}

View File

@ -23,9 +23,9 @@ import XCTest
import Surge
class PowerTests: XCTestCase {
let vector = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
func testPower() {
let powered = pow(vector, 2.0)
XCTAssertEqual(powered, [1.0, 4.0, 9.0, 16.0, 25.0, 36.0])

View File

@ -26,32 +26,32 @@ class TrigonometricTests: XCTestCase {
let n = 10000
func test_sin() {
let values = (0...n).map{_ in drand48() * Double.pi}
let values = (0...n).map { _ in drand48() * Double.pi }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: sin, mapped: sin, accuracy: 0.0001)
}
func test_cos() {
let values = (0...n).map{_ in drand48() * Double.pi}
let values = (0...n).map { _ in drand48() * Double.pi }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: cos, mapped: cos, accuracy: 0.0001)
}
func test_tan() {
let values = (0...n).map{_ in drand48() * Double.pi}
let values = (0...n).map { _ in drand48() * Double.pi }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: tan, mapped: tan, accuracy: 0.0001)
}
// func test_asin() {
// let values = map(0...n){_ in drand48()}
// measureAndValidateMappedFunctionWithAccuracy(values, member: asin, mapped: asin, accuracy: 0.0001)
// }
//
// func test_acos() {
// let values = map(0...n){_ in drand48()}
// measureAndValidateMappedFunctionWithAccuracy(values, member: acos, mapped: acos, accuracy: 0.0001)
// }
//
// func test_atan() {
// let values = map(0...n){_ in drand48()}
// measureAndValidateMappedFunctionWithAccuracy(values, member: atan, mapped: atan, accuracy: 0.0001)
// }
func test_asin() {
let values = (0...n).map { _ in drand48() }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: asin, mapped: asin, accuracy: 0.0001)
}
func test_acos() {
let values = (0...n).map { _ in drand48() }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: acos, mapped: acos, accuracy: 0.0001)
}
func test_atan() {
let values = (0...n).map { _ in drand48() }
measureAndValidateMappedFunctionWithAccuracy(source: values, member: atan, mapped: atan, accuracy: 0.0001)
}
}

View File

@ -22,29 +22,29 @@ import Foundation
import XCTest
extension XCTestCase {
func measureAndValidateMappedFunctionWithAccuracy<C : Collection>(source: C, member: (C.Iterator.Element) -> (C.Iterator.Element), mapped: @escaping (C) -> ([C.Iterator.Element]), accuracy: C.Iterator.Element) where C.Iterator.Element: ExpressibleByFloatLiteral & FloatingPoint {
func measureAndValidateMappedFunctionWithAccuracy<C: Collection>(source: C, member: (C.Iterator.Element) -> (C.Iterator.Element), mapped: @escaping (C) -> ([C.Iterator.Element]), accuracy: C.Iterator.Element) where C.Iterator.Element: ExpressibleByFloatLiteral & FloatingPoint {
var expected = source.map(member)
var actual: [C.Iterator.Element] = []
self.measure {
actual = mapped(source)
}
for (i, _) in source.enumerated() {
XCTAssertEqual(actual[i], expected[i], accuracy: accuracy)
for (i, j) in zip(actual.indices, expected.indices) {
XCTAssertEqual(actual[i], expected[j], accuracy: accuracy)
}
}
func XCTAssertArrayFloatEqualWithAccuracy(calcArray: [Float], _ testArray: [Float], _ accuracy: Float) {
assert(calcArray.count == testArray.count, "XCTAssertArrayFloatEqualWithAccuracy arrays must be same size")
for i:Int in 0..<calcArray.count {
for i: Int in 0..<calcArray.count {
XCTAssertEqual(calcArray[i], testArray[i], accuracy: accuracy)
}
}
func XCTAssertArrayDoubleEqualWithAccuracy(calcArray: [Double], _ testArray: [Double], _ accuracy: Double) {
assert(calcArray.count == testArray.count, "XCTAssertArrayFloatEqualWithAccuracy arrays must be same size")
for i:Int in 0..<calcArray.count {
for i: Int in 0..<calcArray.count {
XCTAssertEqual(calcArray[i], testArray[i], accuracy: accuracy)
}
}