Skip to content

Commit 9f30b59

Browse files
authored
Add tests (#22)
1 parent d85791b commit 9f30b59

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ fastlane/test_output
8686
iOSInjectionProject/
8787

8888
.DS_Store
89-
.swiftpm
89+
.swiftpm/xcode
9090
# End of https://www.toptal.com/developers/gitignore/api/swift
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"configurations" : [
3+
{
4+
"id" : "1194A67B-74F2-4259-BCF4-4C67B5C7FAE7",
5+
"name" : "Test Scheme Action",
6+
"options" : {
7+
8+
}
9+
}
10+
],
11+
"defaultOptions" : {
12+
13+
},
14+
"testTargets" : [
15+
{
16+
"target" : {
17+
"containerPath" : "container:",
18+
"identifier" : "StateStructMacroTests",
19+
"name" : "StateStructMacroTests"
20+
}
21+
},
22+
{
23+
"target" : {
24+
"containerPath" : "container:",
25+
"identifier" : "StateStructTests",
26+
"name" : "StateStructTests"
27+
}
28+
}
29+
],
30+
"version" : 1
31+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import Testing
2+
@testable import StateStruct
3+
4+
@Suite("CopyOnWrite Tests")
5+
struct BackingStorageTests {
6+
7+
@Test("Concurrent access should be thread-safe")
8+
func testConcurrentAccess() async throws {
9+
let storage = _BackingStorage(0)
10+
let iterations = 1000
11+
let taskCount = 10
12+
13+
await withTaskGroup(of: Void.self) { group in
14+
for taskCount in 0..<taskCount {
15+
group.addTask {
16+
for i in 0..<iterations {
17+
print(i, taskCount)
18+
storage.value += 1
19+
}
20+
}
21+
}
22+
await group.waitForAll()
23+
}
24+
25+
#expect(storage.value == iterations * taskCount)
26+
}
27+
28+
@Test("Concurrent read-write operations should be safe")
29+
func testConcurrentReadWrite() async throws {
30+
let storage = _BackingStorage(0)
31+
let iterations = 1000
32+
33+
async let writeTask: () = Task {
34+
for i in 0..<iterations {
35+
storage.value = i
36+
}
37+
}.value
38+
39+
async let readTask: () = Task {
40+
for _ in 0..<iterations {
41+
#expect(storage.value >= 0)
42+
}
43+
}.value
44+
45+
await writeTask
46+
await readTask
47+
48+
#expect(storage.value == iterations - 1)
49+
}
50+
51+
@Test("Copy on write behavior")
52+
func testCopyOnWrite() {
53+
let storage1 = _BackingStorage(42)
54+
let storage2 = storage1.copy()
55+
56+
#expect(storage1.value == storage2.value)
57+
58+
storage2.value = 100
59+
#expect(storage1.value == 42)
60+
#expect(storage2.value == 100)
61+
}
62+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import Testing
2+
import StateStruct
3+
4+
@Suite
5+
struct TrackingExistential {
6+
7+
@Tracking
8+
struct UI {
9+
var count: Int
10+
11+
init(count: Int) {
12+
self.count = count
13+
}
14+
}
15+
16+
@Tracking
17+
struct Root {
18+
19+
var _any: Any?
20+
21+
var ui: UI? {
22+
get {
23+
_any as? UI
24+
}
25+
set {
26+
_any = newValue
27+
}
28+
}
29+
30+
init(ui: UI) {
31+
self.ui = ui
32+
}
33+
}
34+
35+
36+
@Test func tracking() {
37+
38+
let original = Root(ui: UI(count: 1))
39+
40+
let reading = original.tracking {
41+
_ = original.ui?.count
42+
}
43+
44+
#expect(
45+
reading.graph.prettyPrint() == """
46+
StateStructTests.TrackingExistential.Root {
47+
_any-(1) {
48+
count-(1)
49+
}
50+
}
51+
"""
52+
)
53+
54+
}
55+
56+
}

0 commit comments

Comments
 (0)