//
// AsynchronousTaskTests.swift
// LLVSTests
//
// Created by Drew McCormack on 04/03/2019.
//
import XCTest
import LLVS
class AsynchronousTaskTests: XCTestCase {
var task1: AsynchronousTask!
var task2: AsynchronousTask!
override func setUp() {
super.setUp()
task1 = AsynchronousTask { finish in
finish(.success(()))
}
task2 = AsynchronousTask { finish in
finish(.success(()))
}
}
func testNextTask() {
let expect = XCTestExpectation(description: "Completed basic")
task1.next = task2
task2.completionBlock = { result in
XCTAssert(result.isSuccess)
expect.fulfill()
}
task1.execute()
wait(for: [expect], timeout: 1.0)
}
func testChain() {
let expect = XCTestExpectation(description: "Completed basic")
[task1, task2].chain()
task2.completionBlock = { result in
XCTAssert(result.isSuccess)
expect.fulfill()
}
task1.execute()
wait(for: [expect], timeout: 1.0)
}
func testExecuteInOrder() {
let expect = XCTestExpectation(description: "Completed Execute in Order")
let task3 = AsynchronousTask { finish in
finish(.success(()))
}
let task4 = AsynchronousTask { finish in
finish(.success(()))
}
[task1, task2, task3, task4].executeInOrder { result in
XCTAssert(result.isSuccess)
expect.fulfill()
}
task1.execute()
wait(for: [expect], timeout: 1.0)
}
func testWithAsyncDelay() {
let expect = XCTestExpectation(description: "Completed with Async Delay")
let task3 = AsynchronousTask { finish in
DispatchQueue.main.async {
finish(.success(()))
}
}
[task1, task2, task3].executeInOrder { result in
XCTAssert(result.isSuccess)
expect.fulfill()
}
task1.execute()
wait(for: [expect], timeout: 1.0)
}
enum Error: Swift.Error {
case testError
}
func testWithFailure() {
let expect = XCTestExpectation(description: "Test with failure")
let task3 = AsynchronousTask { finish in
finish(.failure(Error.testError))
}
var task4Executed = false
let task4 = AsynchronousTask { finish in
task4Executed = true
finish(.success(()))
}
[task1, task2, task3, task4].executeInOrder { result in
XCTAssertFalse(task4Executed)
XCTAssertFalse(result.isSuccess)
expect.fulfill()
}
task1.execute()
wait(for: [expect], timeout: 1.0)
}
static var allTests = [
("testNextTask", testNextTask),
("testChain", testChain),
("testExecuteInOrder", testExecuteInOrder),
("testWithFailure", testWithFailure),
]
}
import XCTest
@testable import Concurrency
import Dispatch
import Foundation
class ConcurrencyTests: XCTestCase {
enum Error: Swift.Error {
case er
}
func testSend() {
let expectation = self.expectation(description: "expectation")
var value = 0
let task = Task<Int>()
.done { value = $0 * 2 }
.always { _ in expectation.fulfill() }
task.send(10)
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(20, value)
}
}
func testThrow() {
let expectation = self.expectation(description: "expectation")
var taskError: Swift.Error?
let task = Task<Int>()
.catch { taskError = $0 }
.always { _ in expectation.fulfill() }
task.throw(Error.er)
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertNotNil(taskError)
}
}
func testRecover() {
var expectation = self.expectation(description: "expectation")
var taskError: Swift.Error?
var value = 0
var task = Task<Int>()
task.recover { _ in return 10 }
.then { $0 }
.done { value = $0 * 2 }
.catch { taskError = $0 }
.always { _ in expectation.fulfill() }
task.throw(Error.er)
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertNil(taskError)
XCTAssertEqual(20, value)
}
expectation = self.expectation(description: "expectation")
value = 0
task = Task<Int>()
task.done { value = $0 * 2 }
.catch { taskError = $0 }
.recover { throw $0 }
.always { _ in expectation.fulfill() }
task.throw(Error.er)
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertNotNil(taskError)
XCTAssertEqual(0, value)
}
}
func testInitializerBuild() {
let expectation = self.expectation(description: "expectation")
var value = 0
Task<Int> { task in
task.send(10)
}
.done { value = $0 * 2 }
.always { _ in expectation.fulfill() }
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(20, value)
}
}
func testInitializerValue() {
let expectation = self.expectation(description: "expectation")
var value = 0
Task<Int>(value: 10)
.done { value = $0 * 2 }
.always { _ in expectation.fulfill() }
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(20, value)
}
}
func testInitializerState() {
let expectation = self.expectation(description: "expectation")
var value = 0
Task<Int>(state: .success(10))
.done { value = $0 * 2 }
.always { _ in expectation.fulfill() }
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(20, value)
}
}
func testChain() {
var expectation = self.expectation(description: "expectation")
var value = 0
Task<Int>(value: 10)
.then { $0 + 5 }
.then { $0 * 2 }
.recover { _ in 40 }
.then { value = $0 }
.always { _ in expectation.fulfill() }
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(30, value)
}
expectation = self.expectation(description: "expectation")
value = 0
Task<Int>(value: 10)
.then { $0 + 5 }
.then { $0 * 2 }
.then { _ in throw Error.er }
.recover { _ in 40 }
.then { value = $0 }
.always { _ in expectation.fulfill() }
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(40, value)
}
}
func testQueue() {
var expectation = self.expectation(description: "expectation")
var value = 0
Task<Int>(value: 10)
.then { $0 + 5 }
.then(in: .global()) { return $0 * 2 }
.then { value = $0 }
.always { _ in expectation.fulfill() }
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(30, value)
}
expectation = self.expectation(description: "expectation")
value = 0
Task<Int>(value: 10)
.then { $0 + 5 }
.then { $0 * 2 }
.then { _ in throw Error.er }
.recover { _ in 40 }
.then(in: .global()) { value = $0 }
.always { _ in expectation.fulfill() }
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(40, value)
}
}
// TODO: hangs
func testDelay() {
let expectation = self.expectation(description: "expectation")
var value = 0
// let delay = { DispatchTime
Task<Int>(value: 10)
.then(delay: { .now() + 2 }) { $0 * 2 }
.done { print("delay \($0)"); value = $0 }
.always { _ in print("delay"); expectation.fulfill() }
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(20, value)
}
}
func testUpdate() {
let expectation = self.expectation(description: "expectation")
var value = 0
let task = Task<Int>(value: 10)
.then(delay: { .now() + 3 }) { $0 * 2 }
DispatchQueue.global().async {
task.then { $0 * 2 }
.then { $0 + 5 }
.done { value = $0 }
.always { _ in expectation.fulfill() }
}
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(45, value)
}
}
func testMultiple() {
let expectation = self.expectation(description: "expectation")
let e1 = self.expectation(description: "expectation1")
let e2 = self.expectation(description: "expectation2")
let task = Task<Int>(in: .global(), value: 10)
.then(in: .global()) { _ in expectation.fulfill() }
task.then(in: .global()) { e1.fulfill() }
task.then(in: .global()) { e2.fulfill() }
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
}
}
func testWait() {
var value = try? Task<Int>(value: 10)
.then { $0 * 2 }
.then { $0 + 5 }
.wait()
XCTAssertEqual(25, value)
value = try? Task<Int>(value: 10)
.then { $0 * 2 }
.then(delay: { .now() + 10 }) { $0 + 5 }
.wait()
XCTAssertEqual(25, value)
}
func testWaitTimeout() {
var value = 0
do {
value = try Task<Int>(value: 10)
.then { $0 * 2 }
.then(in: .global(), delay: { .now() + .seconds(5) }) { $0 + 5 }
.wait(for: { .now() + 10 })
// try Task<Int>(value: 10)
// .then { $0 * 2 }
// .then(delay: { .now() + .seconds(5) }) { $0 + 5 }
//// .wait()
// .wait(for: { .now() + .seconds(10) })
XCTAssertEqual(25, value)
} catch {
XCTFail("error: \(error)")
}
// value = 0
//
// do {
// value = try Task<Int>(value: 10)
// .then { $0 * 2 }
// .then(delay: { .now() + 5 }) { $0 + 5 }
// .wait(for: { .now() + .seconds(1) })
//
// XCTFail("should throw")
// } catch {
// switch error {
// // case let error as TaskError where error == .timeout:
// // XCTAssertTrue(true)
// default:
// XCTFail("wrong error \(error)")
// }
// }
//
// XCTAssertEqual(0, value)
}
func testCombine() {
var expectation = self.expectation(description: "e")
var intTask = Task<Int>()
var stringTask = Task<String>()
var boolTask = Task<Bool>()
var result = [Any]()
[intTask.as(Any.self), stringTask.as(Any.self), boolTask.as(Any.self)]
.combine()
.done { result = $0 }
.always { _ in expectation.fulfill() }
intTask.send(10)
stringTask.send("task string")
boolTask.send(true)
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(3, result.count)
guard result.count == 3 else {
XCTFail()
return
}
XCTAssertEqual(10, result[0] as? Int)
XCTAssertEqual("task string", result[1] as? String)
XCTAssertEqual(true, result[2] as? Bool)
}
expectation = self.expectation(description: "e")
intTask = Task<Int>()
stringTask = Task<String>()
boolTask = Task<Bool>()
result = [Any]()
[intTask.as(Any.self), stringTask.as(Any.self), boolTask.as(Any.self)]
.combine()
.done { result = $0 }
.always { _ in expectation.fulfill() }
intTask.send(10)
stringTask.throw(Error.er)
boolTask.send(true)
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(0, result.count)
}
}
func testMapReduce() {
let task = Task<[Any]>(value: [10, 30, "ss", true, "aa"])
let filterArray = try? task.filter { $0 is String }.wait()
XCTAssertEqual(["ss", "aa"], (filterArray ?? []).flatMap { $0 as? String })
let mapArray = try? task.map { String.init(describing: $0) }.wait()
XCTAssertEqual(["10", "30", "ss", "true", "aa"], mapArray ?? [])
let flatMapArray = try? task.flatMap { $0 as? String }.wait()
XCTAssertEqual(["ss", "aa"], (flatMapArray ?? []))
let reduce = try? task.reduce(0) { $0 + (($1 as? Int) ?? 1) }.wait()
XCTAssertEqual(43, reduce)
}
func testUnwrap() {
let e = self.expectation(description: "e")
var value = 0
Task<Void>().finish()
.then { _ in return Task<Int>(value: 10) }
.then { $0.then { $0 * 2 } }
.unwrap()
.then { $0 + 5 }
.then { value = $0 }
.always(in: .global()) { _ in e.fulfill() }
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual(25, value)
}
}
func testThenOnState() {
var e = self.expectation(description: "e")
var value = ""
func foo(_ string: String) {
value = string
}
var task = Task<Int>()
task.then(on: .success) { _ in foo("success") }
.always { _ in e.fulfill() }
task.then(on: .failure) { _ in foo("fail") }
.always { _ in e.fulfill() }
task.send(10)
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual("success", value)
}
e = self.expectation(description: "e")
task = Task<Int>()
task.then(on: .success) { _ in foo("success") }
.always { _ in e.fulfill() }
task.then(on: .failure) { $0 }
.catch { _ in foo("fail") }
.always { _ in e.fulfill() }
task.throw(Error.er)
self.waitForExpectations(timeout: 5) { error in
XCTAssertNil(error)
XCTAssertEqual("fail", value)
}
}
static var allTests : [(String, (ConcurrencyTests) -> () throws -> Void)] {
return [
("testSend", testSend),
("testThrow", testThrow),
("testRecover", testRecover),
("testInitializerBuild", testInitializerBuild),
("testInitializerValue", testInitializerValue),
("testInitializerState", testInitializerState),
("testChain", testChain),
("testQueue", testQueue),
("testDelay", testDelay),
("testUpdate", testUpdate),
("testMultiple", testMultiple),
("testWait", testWait),
("testWaitTimeout", testWaitTimeout),
("testCombine", testCombine),
("testUnwrap", testUnwrap),
]
}
}
//
// E14Tests.swift
// E14Tests
//
// Created by stephen weber on 5/4/22.
//
import XCTest
@testable import E14
class E14Tests: XCTestCase {
var collatz : Collatz!
override func setUpWithError() throws {
collatz = Collatz()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
}
func testarray() {
print(collatz.sieve.count)
print(collatz.possible.count)
}
func testCollatzodd() {
XCTAssertEqual(collatz.EvenReverseCollatz(14),7)
XCTAssertEqual(collatz.OddReverseCollatz(15),46)
}
func testChain() {
collatz.chain(8)
XCTAssertEqual(collatz.theList[8],4)
}
func testAllChain() {
for i in 1...100000000 {
collatz.chain(i)
}
print(collatz.maxCount)
print(collatz.maxInt)
/*
525
837799
*/
//took 1.421 seconds without memorizing data
/*
686
8400511
14.075
*/
/*
3GB
100000000
950
63728127
162.230 secs
without any dictionary 30mb total mem. 128 sec
*/
}
func testMEmeorizationSolution() {
..not helpful
for i in 1...100000000 {
collatz.chain(i)
}
print(collatz.maxCount)
print(collatz.maxInt)
/*
525
837799 at 1.44 sec
*/
/*
5000000
597
3732423 at 6.852 sec
*/
/*
686
8400511
14.083
*/
/*
100000000
950
63728127
161.637
3Gb
*/
// collatz.theSolution(13)
// print(collatz.theList[13] ?? 999)
// print(collatz.maxCount)
// print(collatz.maxInt)
// XCTAssertEqual(collatz.theSolution(12),collatz.theList[12] )
// XCTAssertEqual(collatz.theSolution(2),2)
// XCTAssertEqual(collatz.theSolution(4),3)
// // XCTAssertEqual(collatz.theSolution(1000000),837799)
// XCTAssertEqual(collatz.theSolution(1000000),837799)
}
func testRevatureSuits() {
XCTAssertEqual(collatz.revatureSuits(10,2),19)
XCTAssertEqual(collatz.revatureSuits(3,0),3)
}
}