Skip to content

Commit 1deea5d

Browse files
committed
Adjust documentation for dependently atomic op
1 parent b93b844 commit 1deea5d

File tree

10 files changed

+81
-15
lines changed

10 files changed

+81
-15
lines changed

Sources/Concurrent/MVar.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ public final class MVar<A> {
148148
return (self.val == nil)
149149
}
150150

151-
/// Atomically, take a value from the `MVar`, put a given new value in the
151+
/// Take a value from the `MVar`, put a given new value in the
152152
/// `MVar`, then return the `MVar`'s old value.
153+
///
154+
/// This operation is atomic only if no writes occur during its execution.
153155
public func swap(_ x : A) -> A {
154156
let old = self.take()
155157
self.put(x)

Tests/ConcurrentTests/ChanSpec.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ChanSpec : XCTestCase {
7272
var result = [Action]()
7373
while empty != 0 {
7474
empty -= 1
75-
let branch = arc4random() % 3
75+
let branch = randomInteger() % 3
7676
if branch == 0 {
7777
return Gen.pure(ArrayOf(Array(repeating: .readChan, count: empty) + result))
7878
} else if branch == 1 {
@@ -144,4 +144,10 @@ class ChanSpec : XCTestCase {
144144
((l1 == l2) <?> "MVar Values Match")
145145
}
146146
}
147+
148+
#if !os(macOS) && !os(iOS) && !os(tvOS)
149+
static var allTests = testCase([
150+
("testProperties", testProperties),
151+
])
152+
#endif
147153
}

Tests/ConcurrentTests/ConcurrentSpec.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ import Concurrent
1111
@testable import SwiftCheck
1212
import Dispatch
1313

14+
#if os(Linux)
15+
import Glibc
16+
public func randomInteger() -> UInt32 {
17+
return UInt32(rand())
18+
}
19+
#else
20+
import Darwin
21+
22+
public func randomInteger() -> UInt32 {
23+
return arc4random()
24+
}
25+
#endif
26+
1427
public func error<A>(_ x : String) -> A {
1528
XCTFail(x)
1629
fatalError(x)

Tests/ConcurrentTests/IVarSpec.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Foundation
1010
import Concurrent
1111
import XCTest
1212
import SwiftCheck
13-
import func Darwin.C.stdlib.arc4random
1413

1514
private enum Action {
1615
case newEmptyIVar
@@ -73,7 +72,7 @@ class IVarSpec : XCTestCase {
7372
if n == 0 {
7473
return Gen.pure(ArrayOf(result))
7574
}
76-
while (arc4random() % UInt32(n)) != 0 {
75+
while (randomInteger() % UInt32(n)) != 0 {
7776
if empty {
7877
result = result + [.putIVar(Int.arbitrary.generate), .readIVar]
7978
empty = false
@@ -147,4 +146,10 @@ class IVarSpec : XCTestCase {
147146
((l1 == l2) <?> "IVar Values Match")
148147
}
149148
}
149+
150+
#if !os(macOS) && !os(iOS) && !os(tvOS)
151+
static var allTests = testCase([
152+
("testProperties", testProperties),
153+
])
154+
#endif
150155
}

Tests/ConcurrentTests/MVarSpec.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import Concurrent
1010
import XCTest
1111
import SwiftCheck
12-
import func Darwin.C.stdlib.arc4random
1312

1413
private enum Action {
1514
case newEmptyMVar
@@ -93,9 +92,9 @@ class MVarSpec : XCTestCase {
9392
if n == 0 {
9493
return Gen.pure(ArrayOf(result))
9594
}
96-
while (arc4random() % UInt32(n)) != 0 {
95+
while (randomInteger() % UInt32(n)) != 0 {
9796
if empty {
98-
result = result + [.putMVar(Int.arbitrary.generate)] + ((arc4random() % 2) == 0 ? [.swapMVar(Int.arbitrary.generate)] : [.readMVar])
97+
result = result + [.putMVar(Int.arbitrary.generate)] + ((randomInteger() % 2) == 0 ? [.swapMVar(Int.arbitrary.generate)] : [.readMVar])
9998
empty = false
10099
} else {
101100
result = result + [.takeMVar]
@@ -175,4 +174,10 @@ class MVarSpec : XCTestCase {
175174
((l1 == l2) <?> "MVar Values Match")
176175
}
177176
}
177+
178+
#if !os(macOS) && !os(iOS) && !os(tvOS)
179+
static var allTests = testCase([
180+
("testProperties", testProperties),
181+
])
182+
#endif
178183
}

Tests/ConcurrentTests/QSemSpec.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Foundation
1010
import Concurrent
1111
import XCTest
1212
import SwiftCheck
13-
import func Darwin.C.stdlib.arc4random
1413

1514
private enum Action {
1615
case newQSem(UInt)
@@ -62,7 +61,7 @@ class QSemSpec : XCTestCase {
6261
if n == 0 {
6362
return Gen.pure(ArrayOf(result))
6463
}
65-
while (arc4random() % UInt32(n)) != 0 {
64+
while (randomInteger() % UInt32(n)) != 0 {
6665
if quantity <= 0 {
6766
result.append(.signalQSem)
6867
quantity += 1
@@ -116,4 +115,10 @@ class QSemSpec : XCTestCase {
116115
self.setupPerformance(d + suff.getArray)
117116
}
118117
}
118+
119+
#if !os(macOS) && !os(iOS) && !os(tvOS)
120+
static var allTests = testCase([
121+
("testProperties", testProperties),
122+
])
123+
#endif
119124
}

Tests/ConcurrentTests/STMSpec.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func snapshot(_ v1 : TVar<Int>, _ v2 : TVar<Int>) -> STM<(Int, Int)> {
5353

5454
class STMSpec : XCTestCase {
5555
func testMain() {
56-
5756
let (sv1, sv2) = initTVars.atomically()
5857

5958
_ = elseTestA(sv1, sv2).atomically()
@@ -91,4 +90,10 @@ class STMSpec : XCTestCase {
9190
XCTAssert(vs.1 == 50)
9291
}()
9392
}
93+
94+
#if !os(macOS) && !os(iOS) && !os(tvOS)
95+
static var allTests = testCase([
96+
("testMain", testMain),
97+
])
98+
#endif
9499
}

Tests/ConcurrentTests/SVarSpec.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import Concurrent
1010
import XCTest
1111
import SwiftCheck
12-
import func Darwin.C.stdlib.arc4random
1312

1413
private enum Action {
1514
case newEmptySVar
@@ -84,7 +83,7 @@ class SVarSpec : XCTestCase {
8483
if n == 0 {
8584
return Gen.pure(ArrayOf(result))
8685
}
87-
while (arc4random() % UInt32(n)) != 0 {
86+
while (randomInteger() % UInt32(n)) != 0 {
8887
if empty {
8988
result = result + [.putSVar(Int.arbitrary.generate)]
9089
empty = false
@@ -159,4 +158,10 @@ class SVarSpec : XCTestCase {
159158
((l1 == l2) <?> "SVar Values Match")
160159
}
161160
}
161+
162+
#if !os(macOS) && !os(iOS) && !os(tvOS)
163+
static var allTests = testCase([
164+
("testProperties", testProperties),
165+
])
166+
#endif
162167
}

Tests/ConcurrentTests/TMVarSpec.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import Concurrent
1010
import XCTest
1111
import SwiftCheck
12-
import func Darwin.C.stdlib.arc4random
1312

1413
private enum Action {
1514
case newEmptyTMVar
@@ -93,9 +92,9 @@ class TMVarSpec : XCTestCase {
9392
if n == 0 {
9493
return Gen.pure(ArrayOf(result))
9594
}
96-
while (arc4random() % UInt32(n)) != 0 {
95+
while (randomInteger() % UInt32(n)) != 0 {
9796
if empty {
98-
result = result + [.putTMVar(Int.arbitrary.generate)] + ((arc4random() % 2) == 0 ? [.swapTMVar(Int.arbitrary.generate)] : [.readTMVar])
97+
result = result + [.putTMVar(Int.arbitrary.generate)] + ((randomInteger() % 2) == 0 ? [.swapTMVar(Int.arbitrary.generate)] : [.readTMVar])
9998
empty = false
10099
} else {
101100
result = result + [.takeTMVar]
@@ -183,4 +182,10 @@ class TMVarSpec : XCTestCase {
183182
((l1 == l2) <?> "TMVar Values Match")
184183
}
185184
}
185+
186+
#if !os(macOS) && !os(iOS) && !os(tvOS)
187+
static var allTests = testCase([
188+
("testProperties", testProperties),
189+
])
190+
#endif
186191
}

Tests/LinuxMain.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import XCTest
2+
3+
@testable import ConcurrentTests
4+
5+
#if !os(macOS)
6+
XCTMain([
7+
ChanSpec.allTests,
8+
IVarSpec.allTests,
9+
MVarSpec.allTests,
10+
QSemSpec.allTests,
11+
STMSpec.allTests,
12+
SVarSpec.allTests,
13+
TMVarSpec.allTests,
14+
])
15+
#endif

0 commit comments

Comments
 (0)