From 1deea5d599ef3136cb4d53483a4cb4e17d11a964 Mon Sep 17 00:00:00 2001 From: Robert Widmann Date: Fri, 22 Sep 2017 14:26:27 -0400 Subject: [PATCH] Adjust documentation for dependently atomic op --- Sources/Concurrent/MVar.swift | 4 +++- Tests/ConcurrentTests/ChanSpec.swift | 8 +++++++- Tests/ConcurrentTests/ConcurrentSpec.swift | 13 +++++++++++++ Tests/ConcurrentTests/IVarSpec.swift | 9 +++++++-- Tests/ConcurrentTests/MVarSpec.swift | 11 ++++++++--- Tests/ConcurrentTests/QSemSpec.swift | 9 +++++++-- Tests/ConcurrentTests/STMSpec.swift | 7 ++++++- Tests/ConcurrentTests/SVarSpec.swift | 9 +++++++-- Tests/ConcurrentTests/TMVarSpec.swift | 11 ++++++++--- Tests/LinuxMain.swift | 15 +++++++++++++++ 10 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 Tests/LinuxMain.swift diff --git a/Sources/Concurrent/MVar.swift b/Sources/Concurrent/MVar.swift index b3f93b6..b9e5e77 100644 --- a/Sources/Concurrent/MVar.swift +++ b/Sources/Concurrent/MVar.swift @@ -148,8 +148,10 @@ public final class MVar { return (self.val == nil) } - /// Atomically, take a value from the `MVar`, put a given new value in the + /// Take a value from the `MVar`, put a given new value in the /// `MVar`, then return the `MVar`'s old value. + /// + /// This operation is atomic only if no writes occur during its execution. public func swap(_ x : A) -> A { let old = self.take() self.put(x) diff --git a/Tests/ConcurrentTests/ChanSpec.swift b/Tests/ConcurrentTests/ChanSpec.swift index c6bc28e..19b671f 100644 --- a/Tests/ConcurrentTests/ChanSpec.swift +++ b/Tests/ConcurrentTests/ChanSpec.swift @@ -72,7 +72,7 @@ class ChanSpec : XCTestCase { var result = [Action]() while empty != 0 { empty -= 1 - let branch = arc4random() % 3 + let branch = randomInteger() % 3 if branch == 0 { return Gen.pure(ArrayOf(Array(repeating: .readChan, count: empty) + result)) } else if branch == 1 { @@ -144,4 +144,10 @@ class ChanSpec : XCTestCase { ((l1 == l2) "MVar Values Match") } } + + #if !os(macOS) && !os(iOS) && !os(tvOS) + static var allTests = testCase([ + ("testProperties", testProperties), + ]) + #endif } diff --git a/Tests/ConcurrentTests/ConcurrentSpec.swift b/Tests/ConcurrentTests/ConcurrentSpec.swift index a0a9aa8..2b388c6 100644 --- a/Tests/ConcurrentTests/ConcurrentSpec.swift +++ b/Tests/ConcurrentTests/ConcurrentSpec.swift @@ -11,6 +11,19 @@ import Concurrent @testable import SwiftCheck import Dispatch +#if os(Linux) + import Glibc + public func randomInteger() -> UInt32 { + return UInt32(rand()) + } +#else + import Darwin + + public func randomInteger() -> UInt32 { + return arc4random() + } +#endif + public func error(_ x : String) -> A { XCTFail(x) fatalError(x) diff --git a/Tests/ConcurrentTests/IVarSpec.swift b/Tests/ConcurrentTests/IVarSpec.swift index 7f1c7c5..cee8c90 100644 --- a/Tests/ConcurrentTests/IVarSpec.swift +++ b/Tests/ConcurrentTests/IVarSpec.swift @@ -10,7 +10,6 @@ import Foundation import Concurrent import XCTest import SwiftCheck -import func Darwin.C.stdlib.arc4random private enum Action { case newEmptyIVar @@ -73,7 +72,7 @@ class IVarSpec : XCTestCase { if n == 0 { return Gen.pure(ArrayOf(result)) } - while (arc4random() % UInt32(n)) != 0 { + while (randomInteger() % UInt32(n)) != 0 { if empty { result = result + [.putIVar(Int.arbitrary.generate), .readIVar] empty = false @@ -147,4 +146,10 @@ class IVarSpec : XCTestCase { ((l1 == l2) "IVar Values Match") } } + + #if !os(macOS) && !os(iOS) && !os(tvOS) + static var allTests = testCase([ + ("testProperties", testProperties), + ]) + #endif } diff --git a/Tests/ConcurrentTests/MVarSpec.swift b/Tests/ConcurrentTests/MVarSpec.swift index 0c1a73e..b626281 100644 --- a/Tests/ConcurrentTests/MVarSpec.swift +++ b/Tests/ConcurrentTests/MVarSpec.swift @@ -9,7 +9,6 @@ import Concurrent import XCTest import SwiftCheck -import func Darwin.C.stdlib.arc4random private enum Action { case newEmptyMVar @@ -93,9 +92,9 @@ class MVarSpec : XCTestCase { if n == 0 { return Gen.pure(ArrayOf(result)) } - while (arc4random() % UInt32(n)) != 0 { + while (randomInteger() % UInt32(n)) != 0 { if empty { - result = result + [.putMVar(Int.arbitrary.generate)] + ((arc4random() % 2) == 0 ? [.swapMVar(Int.arbitrary.generate)] : [.readMVar]) + result = result + [.putMVar(Int.arbitrary.generate)] + ((randomInteger() % 2) == 0 ? [.swapMVar(Int.arbitrary.generate)] : [.readMVar]) empty = false } else { result = result + [.takeMVar] @@ -175,4 +174,10 @@ class MVarSpec : XCTestCase { ((l1 == l2) "MVar Values Match") } } + + #if !os(macOS) && !os(iOS) && !os(tvOS) + static var allTests = testCase([ + ("testProperties", testProperties), + ]) + #endif } diff --git a/Tests/ConcurrentTests/QSemSpec.swift b/Tests/ConcurrentTests/QSemSpec.swift index 5f4171e..793f0b9 100644 --- a/Tests/ConcurrentTests/QSemSpec.swift +++ b/Tests/ConcurrentTests/QSemSpec.swift @@ -10,7 +10,6 @@ import Foundation import Concurrent import XCTest import SwiftCheck -import func Darwin.C.stdlib.arc4random private enum Action { case newQSem(UInt) @@ -62,7 +61,7 @@ class QSemSpec : XCTestCase { if n == 0 { return Gen.pure(ArrayOf(result)) } - while (arc4random() % UInt32(n)) != 0 { + while (randomInteger() % UInt32(n)) != 0 { if quantity <= 0 { result.append(.signalQSem) quantity += 1 @@ -116,4 +115,10 @@ class QSemSpec : XCTestCase { self.setupPerformance(d + suff.getArray) } } + + #if !os(macOS) && !os(iOS) && !os(tvOS) + static var allTests = testCase([ + ("testProperties", testProperties), + ]) + #endif } diff --git a/Tests/ConcurrentTests/STMSpec.swift b/Tests/ConcurrentTests/STMSpec.swift index f7e1230..8e57561 100644 --- a/Tests/ConcurrentTests/STMSpec.swift +++ b/Tests/ConcurrentTests/STMSpec.swift @@ -53,7 +53,6 @@ func snapshot(_ v1 : TVar, _ v2 : TVar) -> STM<(Int, Int)> { class STMSpec : XCTestCase { func testMain() { - let (sv1, sv2) = initTVars.atomically() _ = elseTestA(sv1, sv2).atomically() @@ -91,4 +90,10 @@ class STMSpec : XCTestCase { XCTAssert(vs.1 == 50) }() } + + #if !os(macOS) && !os(iOS) && !os(tvOS) + static var allTests = testCase([ + ("testMain", testMain), + ]) + #endif } diff --git a/Tests/ConcurrentTests/SVarSpec.swift b/Tests/ConcurrentTests/SVarSpec.swift index 7b7e7eb..661ff06 100644 --- a/Tests/ConcurrentTests/SVarSpec.swift +++ b/Tests/ConcurrentTests/SVarSpec.swift @@ -9,7 +9,6 @@ import Concurrent import XCTest import SwiftCheck -import func Darwin.C.stdlib.arc4random private enum Action { case newEmptySVar @@ -84,7 +83,7 @@ class SVarSpec : XCTestCase { if n == 0 { return Gen.pure(ArrayOf(result)) } - while (arc4random() % UInt32(n)) != 0 { + while (randomInteger() % UInt32(n)) != 0 { if empty { result = result + [.putSVar(Int.arbitrary.generate)] empty = false @@ -159,4 +158,10 @@ class SVarSpec : XCTestCase { ((l1 == l2) "SVar Values Match") } } + + #if !os(macOS) && !os(iOS) && !os(tvOS) + static var allTests = testCase([ + ("testProperties", testProperties), + ]) + #endif } diff --git a/Tests/ConcurrentTests/TMVarSpec.swift b/Tests/ConcurrentTests/TMVarSpec.swift index a576f3b..adeb7ec 100644 --- a/Tests/ConcurrentTests/TMVarSpec.swift +++ b/Tests/ConcurrentTests/TMVarSpec.swift @@ -9,7 +9,6 @@ import Concurrent import XCTest import SwiftCheck -import func Darwin.C.stdlib.arc4random private enum Action { case newEmptyTMVar @@ -93,9 +92,9 @@ class TMVarSpec : XCTestCase { if n == 0 { return Gen.pure(ArrayOf(result)) } - while (arc4random() % UInt32(n)) != 0 { + while (randomInteger() % UInt32(n)) != 0 { if empty { - result = result + [.putTMVar(Int.arbitrary.generate)] + ((arc4random() % 2) == 0 ? [.swapTMVar(Int.arbitrary.generate)] : [.readTMVar]) + result = result + [.putTMVar(Int.arbitrary.generate)] + ((randomInteger() % 2) == 0 ? [.swapTMVar(Int.arbitrary.generate)] : [.readTMVar]) empty = false } else { result = result + [.takeTMVar] @@ -183,4 +182,10 @@ class TMVarSpec : XCTestCase { ((l1 == l2) "TMVar Values Match") } } + + #if !os(macOS) && !os(iOS) && !os(tvOS) + static var allTests = testCase([ + ("testProperties", testProperties), + ]) + #endif } diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift new file mode 100644 index 0000000..9e1bf09 --- /dev/null +++ b/Tests/LinuxMain.swift @@ -0,0 +1,15 @@ +import XCTest + +@testable import ConcurrentTests + +#if !os(macOS) +XCTMain([ + ChanSpec.allTests, + IVarSpec.allTests, + MVarSpec.allTests, + QSemSpec.allTests, + STMSpec.allTests, + SVarSpec.allTests, + TMVarSpec.allTests, +]) +#endif