From 7cc7cc1da73102a4d7ce73e08e66b3bb70b08128 Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 31 Oct 2023 01:28:13 +0000 Subject: [PATCH 1/3] Add LockIsolated test cases --- .../LockIsolatedTests.swift | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift diff --git a/Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift b/Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift new file mode 100644 index 0000000..151332a --- /dev/null +++ b/Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift @@ -0,0 +1,100 @@ +import ConcurrencyExtras +import XCTest + +final class LockIsolatedTests: XCTestCase { + func testLockThreadSafety() { + let value = LockIsolated(0) + let iterations = 100_000 + let group = DispatchGroup() + + for _ in 1 ... iterations { + group.enter() + DispatchQueue.global().async { + value.withValue { value in + value += 1 + } + group.leave() + } + } + + for _ in 1 ... iterations { + group.enter() + DispatchQueue.global().async { + _ = value.value + group.leave() + } + } + + group.wait() + XCTAssertEqual(value.value, iterations) + } + + func testInitializationWithValue() { + let value = 10 + let lockIsolated = LockIsolated(value) + XCTAssertEqual(lockIsolated.value, value) + } + + func testInitializationWithClosure() { + let lockIsolated = LockIsolated( + 1 + 1 + ) + XCTAssertEqual(lockIsolated.value, 2) + } + + func testDynamicMemberLookup() { + struct TestValue: Sendable { + var x = 0 + var y = 10 + } + + let testValue = TestValue() + let lockIsolated = LockIsolated(testValue) + + XCTAssertEqual(lockIsolated.x, testValue.x) + XCTAssertEqual(lockIsolated.y, testValue.y) + } + + func testWithValue() { + let initialValue = 0 + let lockIsolated = LockIsolated(initialValue) + let result = lockIsolated.withValue { value in + value += 1 + return String(value) + } + + XCTAssertEqual(result, "1") + XCTAssertEqual(lockIsolated.value, 1) + } + + func testSetValue() { + let initialValue = 0 + let lockIsolated = LockIsolated(initialValue) + lockIsolated.setValue(2) + XCTAssertEqual(lockIsolated.value, 2) + } + + func testEquatable() { + let firstValue = 2 + let secondValue = 2 + let thirdValue = 3 + let firstLockIsolated = LockIsolated(firstValue) + let secondLockIsolated = LockIsolated(secondValue) + let thirdLockIsolated = LockIsolated(thirdValue) + + XCTAssertTrue(firstLockIsolated == secondLockIsolated) + XCTAssertFalse(firstLockIsolated == thirdLockIsolated) + } + + func testHashable() { + let firstValue = 1 + let secondValue = 1 + let thirdValue = 2 + let firstLockIsolated = LockIsolated(firstValue) + let secondLockIsolated = LockIsolated(secondValue) + let thirdLockIsolated = LockIsolated(thirdValue) + + XCTAssertEqual(firstLockIsolated.hashValue, secondLockIsolated.hashValue) + XCTAssertNotEqual(firstLockIsolated.hashValue, thirdLockIsolated.hashValue) + } +} From 708d2d450911fe1913265f651380bf0b50a847bb Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Thu, 13 Jun 2024 10:26:24 -0700 Subject: [PATCH 2/3] wip --- .../LockIsolatedTests.swift | 178 ++++++++---------- 1 file changed, 79 insertions(+), 99 deletions(-) diff --git a/Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift b/Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift index 151332a..a2764bc 100644 --- a/Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift +++ b/Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift @@ -1,100 +1,80 @@ -import ConcurrencyExtras -import XCTest +#if canImport(Dispatch) + import ConcurrencyExtras + import Dispatch + import XCTest + + final class LockIsolatedTests: XCTestCase { + func testLockThreadSafety() { + let value = LockIsolated(0) + let iterations = 100_000 + let group = DispatchGroup() + + for _ in 1...iterations { + group.enter() + DispatchQueue.global().async { + value.withValue { value in + value += 1 + } + group.leave() + } + } + + for _ in 1...iterations { + group.enter() + DispatchQueue.global().async { + _ = value.value + group.leave() + } + } + + group.wait() + XCTAssertEqual(value.value, iterations) + } + + func testInitializationWithValue() { + let value = 10 + let lockIsolated = LockIsolated(value) + XCTAssertEqual(lockIsolated.value, value) + } + + func testInitializationWithClosure() { + let lockIsolated = LockIsolated( + 1 + 1 + ) + XCTAssertEqual(lockIsolated.value, 2) + } + + func testDynamicMemberLookup() { + struct TestValue: Sendable { + var x = 0 + var y = 10 + } + + let testValue = TestValue() + let lockIsolated = LockIsolated(testValue) + + XCTAssertEqual(lockIsolated.x, testValue.x) + XCTAssertEqual(lockIsolated.y, testValue.y) + } + + func testWithValue() { + let initialValue = 0 + let lockIsolated = LockIsolated(initialValue) + let result = lockIsolated.withValue { value in + value += 1 + return String(value) + } + + XCTAssertEqual(result, "1") + XCTAssertEqual(lockIsolated.value, 1) + } + + func testSetValue() { + let initialValue = 0 + let lockIsolated = LockIsolated(initialValue) + lockIsolated.setValue(2) + XCTAssertEqual(lockIsolated.value, 2) + } + } +#endif -final class LockIsolatedTests: XCTestCase { - func testLockThreadSafety() { - let value = LockIsolated(0) - let iterations = 100_000 - let group = DispatchGroup() - - for _ in 1 ... iterations { - group.enter() - DispatchQueue.global().async { - value.withValue { value in - value += 1 - } - group.leave() - } - } - - for _ in 1 ... iterations { - group.enter() - DispatchQueue.global().async { - _ = value.value - group.leave() - } - } - - group.wait() - XCTAssertEqual(value.value, iterations) - } - - func testInitializationWithValue() { - let value = 10 - let lockIsolated = LockIsolated(value) - XCTAssertEqual(lockIsolated.value, value) - } - - func testInitializationWithClosure() { - let lockIsolated = LockIsolated( - 1 + 1 - ) - XCTAssertEqual(lockIsolated.value, 2) - } - - func testDynamicMemberLookup() { - struct TestValue: Sendable { - var x = 0 - var y = 10 - } - - let testValue = TestValue() - let lockIsolated = LockIsolated(testValue) - - XCTAssertEqual(lockIsolated.x, testValue.x) - XCTAssertEqual(lockIsolated.y, testValue.y) - } - - func testWithValue() { - let initialValue = 0 - let lockIsolated = LockIsolated(initialValue) - let result = lockIsolated.withValue { value in - value += 1 - return String(value) - } - - XCTAssertEqual(result, "1") - XCTAssertEqual(lockIsolated.value, 1) - } - - func testSetValue() { - let initialValue = 0 - let lockIsolated = LockIsolated(initialValue) - lockIsolated.setValue(2) - XCTAssertEqual(lockIsolated.value, 2) - } - - func testEquatable() { - let firstValue = 2 - let secondValue = 2 - let thirdValue = 3 - let firstLockIsolated = LockIsolated(firstValue) - let secondLockIsolated = LockIsolated(secondValue) - let thirdLockIsolated = LockIsolated(thirdValue) - - XCTAssertTrue(firstLockIsolated == secondLockIsolated) - XCTAssertFalse(firstLockIsolated == thirdLockIsolated) - } - - func testHashable() { - let firstValue = 1 - let secondValue = 1 - let thirdValue = 2 - let firstLockIsolated = LockIsolated(firstValue) - let secondLockIsolated = LockIsolated(secondValue) - let thirdLockIsolated = LockIsolated(thirdValue) - - XCTAssertEqual(firstLockIsolated.hashValue, secondLockIsolated.hashValue) - XCTAssertNotEqual(firstLockIsolated.hashValue, thirdLockIsolated.hashValue) - } -} From d1341a191fa98361e130e8df2243ffaf9fc82921 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Thu, 13 Jun 2024 10:29:16 -0700 Subject: [PATCH 3/3] wip --- Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift b/Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift index a2764bc..296f43c 100644 --- a/Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift +++ b/Tests/ConcurrencyExtrasTests/LockIsolatedTests.swift @@ -1,4 +1,4 @@ -#if canImport(Dispatch) +#if !os(WASI) && canImport(Dispatch) import ConcurrencyExtras import Dispatch import XCTest