Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests & benchmarks for several arithmetic functions #105

Merged
merged 4 commits into from
Aug 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
283 changes: 278 additions & 5 deletions Tests/SurgeTests/ArithmeticTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,269 @@ import Foundation
import Surge
import XCTest

// swiftlint:disable nesting
// swiftlint:disable nesting type_body_length

class ArithmeticTests: XCTestCase {
let n = 100_000

func test_add_array_array_float() {
typealias Scalar = Float

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = (0..<n).map { Scalar($0 * 3) }

var actual: [Scalar] = []
measure {
actual = Surge.add(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { $0 + $1 }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_add_array_array_double() {
typealias Scalar = Double

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = (0..<n).map { Scalar($0 * 3) }

var actual: [Scalar] = []
measure {
actual = Surge.add(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { $0 + $1 }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_sub_array_array_float() {
typealias Scalar = Float

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = (0..<n).map { Scalar($0 * 3) }

var actual: [Scalar] = []
measure {
actual = Surge.sub(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { $0 - $1 }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_sub_array_array_double() {
typealias Scalar = Double

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = (0..<n).map { Scalar($0 * 3) }

var actual: [Scalar] = []
measure {
actual = Surge.sub(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { $0 - $1 }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_mul_array_array_float() {
typealias Scalar = Float

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = (0..<n).map { Scalar($0 / 1_000) }

var actual: [Scalar] = []
measure {
actual = Surge.mul(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { $0 * $1 }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_mul_array_array_double() {
typealias Scalar = Double

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = (0..<n).map { Scalar($0 / 1_000) }

var actual: [Scalar] = []
measure {
actual = Surge.mul(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { $0 * $1 }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_div_array_array_float() {
typealias Scalar = Float

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = (0..<n).map { Scalar($0 + 1) }

var actual: [Scalar] = []
measure {
actual = Surge.div(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { $0 / $1 }

XCTAssertEqual(actual, expected, accuracy: 1e-6)
}

func test_div_array_array_double() {
typealias Scalar = Double

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = (0..<n).map { Scalar($0 + 1) }

var actual: [Scalar] = []
measure {
actual = Surge.div(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { $0 / $1 }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_mod_array_array_float() {
typealias Scalar = Float

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = Array(repeating: 42, count: n)

var actual: [Scalar] = []
measure {
actual = Surge.mod(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { fmod($0, $1) }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_mod_array_array_double() {
typealias Scalar = Double

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = Array(repeating: 42, count: n)

var actual: [Scalar] = []
measure {
actual = Surge.mod(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { fmod($0, $1) }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_remainder_array_array_float() {
typealias Scalar = Float

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = Array(repeating: -42, count: n)

var actual: [Scalar] = []
measure {
actual = Surge.remainder(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { remainder($0, $1) }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_remainder_array_array_double() {
typealias Scalar = Double

let lhs: [Scalar] = (0..<n).map { Scalar($0) }
let rhs: [Scalar] = Array(repeating: -42, count: n)

var actual: [Scalar] = []
measure {
actual = Surge.remainder(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).map { remainder($0, $1) }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_sqrt_array_array_float() {
typealias Scalar = Float

let lhs: [Scalar] = (0..<n).map { Scalar($0) }

var actual: [Scalar] = []
measure {
actual = Surge.sqrt(lhs)
}

let expected = lhs.map { sqrt($0) }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_sqrt_array_array_double() {
typealias Scalar = Double

let lhs: [Scalar] = (0..<n).map { Scalar($0) }

var actual: [Scalar] = []
measure {
actual = Surge.sqrt(lhs)
}

let expected = lhs.map { sqrt($0) }

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_dot_array_array_float() {
typealias Scalar = Float

let lhs: [Scalar] = Array(repeating: 0.25, count: n)
let rhs: [Scalar] = Array(repeating: 0.75, count: n)

var actual: Scalar = 0
measure {
actual = Surge.dot(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).reduce(0) {
$0 + $1.0 * $1.1
}

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_dot_array_array_double() {
typealias Scalar = Double

let lhs: [Scalar] = Array(repeating: 0.25, count: n)
let rhs: [Scalar] = Array(repeating: 0.75, count: n)

var actual: Scalar = 0
measure {
actual = Surge.dot(lhs, rhs)
}

let expected = Swift.zip(lhs, rhs).reduce(0) {
$0 + $1.0 * $1.1
}

XCTAssertEqual(actual, expected, accuracy: 1e-8)
}

func test_sum() {
let values = (0...n).map { _ in Double(arc4random()) / Double(UInt32.max) }
var actual = 0.0
Expand Down Expand Up @@ -73,7 +331,11 @@ class ArithmeticTests: XCTestCase {
let lhs: [Scalar] = [1, 2, 3]
let rhs: [Scalar] = [9, 8, 7]

let actual = dist(lhs, rhs)
var actual: Scalar = 0.0
measure {
actual = dist(lhs, rhs)
}

let expected: Scalar = sqrt(Swift.zip(lhs, rhs).map({ $0 - $1 }).map({ $0 * $0 }).reduce(0.0, +))

XCTAssertEqual(actual, expected, accuracy: 1e-6)
Expand All @@ -85,7 +347,11 @@ class ArithmeticTests: XCTestCase {
let lhs: [Scalar] = [1, 2, 3, 4]
let rhs: [Scalar] = [9, 8, 7, 6]

let actual = dist(lhs, rhs)
var actual: Scalar = 0.0
measure {
actual = dist(lhs, rhs)
}

let expected: Scalar = sqrt(Swift.zip(lhs, rhs).map({ $0 - $1 }).map({ $0 * $0 }).reduce(0.0, +))

XCTAssertEqual(actual, expected, accuracy: 1e-6)
Expand All @@ -97,7 +363,10 @@ class ArithmeticTests: XCTestCase {
let lhs: [Scalar] = [1, 2, 3]
let rhs: [Scalar] = [9, 8, 7]

let actual = distSq(lhs, rhs)
var actual: Scalar = 0.0
measure {
actual = distSq(lhs, rhs)
}

let expected: Scalar = Swift.zip(lhs, rhs).map({ $0 - $1 }).map({ $0 * $0 }).reduce(0.0, +)

Expand All @@ -110,7 +379,11 @@ class ArithmeticTests: XCTestCase {
let lhs: [Scalar] = [1, 2, 3, 4]
let rhs: [Scalar] = [9, 8, 7, 6]

let actual = distSq(lhs, rhs)
var actual: Scalar = 0.0
measure {
actual = distSq(lhs, rhs)
}

let expected: Scalar = Swift.zip(lhs, rhs).map({ $0 - $1 }).map({ $0 * $0 }).reduce(0.0, +)

XCTAssertEqual(actual, expected, accuracy: 1e-6)
Expand Down