Skip to content

Commit

Permalink
Adjust documentation for dependently atomic op
Browse files Browse the repository at this point in the history
  • Loading branch information
CodaFi committed Sep 23, 2017
1 parent b93b844 commit 1deea5d
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Sources/Concurrent/MVar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ public final class MVar<A> {
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)
Expand Down
8 changes: 7 additions & 1 deletion Tests/ConcurrentTests/ChanSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions Tests/ConcurrentTests/ConcurrentSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<A>(_ x : String) -> A {
XCTFail(x)
fatalError(x)
Expand Down
9 changes: 7 additions & 2 deletions Tests/ConcurrentTests/IVarSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Foundation
import Concurrent
import XCTest
import SwiftCheck
import func Darwin.C.stdlib.arc4random

private enum Action {
case newEmptyIVar
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
11 changes: 8 additions & 3 deletions Tests/ConcurrentTests/MVarSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Concurrent
import XCTest
import SwiftCheck
import func Darwin.C.stdlib.arc4random

private enum Action {
case newEmptyMVar
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
}
9 changes: 7 additions & 2 deletions Tests/ConcurrentTests/QSemSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Foundation
import Concurrent
import XCTest
import SwiftCheck
import func Darwin.C.stdlib.arc4random

private enum Action {
case newQSem(UInt)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
7 changes: 6 additions & 1 deletion Tests/ConcurrentTests/STMSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func snapshot(_ v1 : TVar<Int>, _ v2 : TVar<Int>) -> STM<(Int, Int)> {

class STMSpec : XCTestCase {
func testMain() {

let (sv1, sv2) = initTVars.atomically()

_ = elseTestA(sv1, sv2).atomically()
Expand Down Expand Up @@ -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
}
9 changes: 7 additions & 2 deletions Tests/ConcurrentTests/SVarSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Concurrent
import XCTest
import SwiftCheck
import func Darwin.C.stdlib.arc4random

private enum Action {
case newEmptySVar
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
11 changes: 8 additions & 3 deletions Tests/ConcurrentTests/TMVarSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import Concurrent
import XCTest
import SwiftCheck
import func Darwin.C.stdlib.arc4random

private enum Action {
case newEmptyTMVar
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1deea5d

Please sign in to comment.