Skip to content

Commit 598a053

Browse files
authored
Move to TrieTest to swift-testing (#8742)
1 parent 4a33cc7 commit 598a053

File tree

1 file changed

+98
-106
lines changed

1 file changed

+98
-106
lines changed

Tests/PackageCollectionsTests/TrieTests.swift

Lines changed: 98 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -10,60 +10,60 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import XCTest
13+
import Testing
1414

1515
@testable import PackageCollections
1616

17-
class TrieTests: XCTestCase {
18-
func testContains() {
17+
@Suite struct TrieTests {
18+
@Test func testContains() {
1919
let trie = Trie<Int>()
2020

2121
let doc1 = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
2222
self.indexDocument(id: 1, contents: doc1, trie: trie)
2323

2424
// Whole word match
25-
XCTAssertTrue(trie.contains(word: "brown", prefixMatch: false))
26-
XCTAssertTrue(trie.contains(word: "Fox", prefixMatch: false))
27-
XCTAssertFalse(trie.contains(word: "foobar", prefixMatch: false))
25+
#expect(trie.contains(word: "brown", prefixMatch: false))
26+
#expect(trie.contains(word: "Fox", prefixMatch: false))
27+
#expect(!trie.contains(word: "foobar", prefixMatch: false))
2828

2929
// Prefix match
30-
XCTAssertTrue(trie.contains(word: "d", prefixMatch: true))
31-
XCTAssertTrue(trie.contains(word: "Do", prefixMatch: true))
32-
XCTAssertFalse(trie.contains(word: "doo", prefixMatch: true))
30+
#expect(trie.contains(word: "d", prefixMatch: true))
31+
#expect(trie.contains(word: "Do", prefixMatch: true))
32+
#expect(!trie.contains(word: "doo", prefixMatch: true))
3333
}
3434

35-
func testFind() {
35+
@Test func testFind() throws {
3636
let trie = Trie<Int>()
3737

3838
let doc1 = "the quick brown fox jumps over the lazy dog and the dog does not notice the fox jumps over it"
3939
let doc2 = "the quick brown fox jumps over the lazy dog for the lazy dog has blocked its way for far too long"
4040
self.indexDocument(id: 1, contents: doc1, trie: trie)
4141
self.indexDocument(id: 2, contents: doc2, trie: trie)
4242

43-
XCTAssertEqual(try trie.find(word: "brown"), [1, 2])
44-
XCTAssertEqual(try trie.find(word: "blocked"), [2])
45-
XCTAssertThrowsError(try trie.find(word: "fo"), "expected error") { error in
46-
XCTAssert(error is NotFoundError)
43+
#expect(try trie.find(word: "brown") == [1, 2])
44+
#expect(try trie.find(word: "blocked") == [2])
45+
#expect(throws:NotFoundError.self) {
46+
try trie.find(word: "fo")
4747
}
4848
}
4949

50-
func testFindWithPrefix() {
50+
func testFindWithPrefix() throws {
5151
let trie = Trie<Int>()
5252

5353
let doc1 = "the quick brown fox jumps over the lazy dog and the dog does not notice the fox jumps over it"
5454
let doc2 = "the quick brown fox jumps over the lazy dog for the lazy dog has blocked its way for far too long"
5555
self.indexDocument(id: 1, contents: doc1, trie: trie)
5656
self.indexDocument(id: 2, contents: doc2, trie: trie)
5757

58-
XCTAssertEqual(try trie.findWithPrefix("f"), ["fox": [1, 2], "for": [2], "far": [2]])
59-
XCTAssertEqual(try trie.findWithPrefix("fo"), ["fox": [1, 2], "for": [2]])
60-
XCTAssertEqual(try trie.findWithPrefix("far"), ["far": [2]])
61-
XCTAssertThrowsError(try trie.findWithPrefix("foo"), "expected error") { error in
62-
XCTAssert(error is NotFoundError)
58+
#expect(try trie.findWithPrefix("f") == ["fox": [1, 2], "for": [2], "far": [2]])
59+
#expect(try trie.findWithPrefix("fo") == ["fox": [1, 2], "for": [2]])
60+
#expect(try trie.findWithPrefix("far") == ["far": [2]])
61+
#expect(throws: NotFoundError.self) {
62+
try trie.findWithPrefix("foo")
6363
}
6464
}
6565

66-
func testRemoveDocument() {
66+
@Test func testRemoveDocument() throws {
6767
let trie = Trie<Int>()
6868

6969
let doc1 = "the quick brown fox jumps over the lazy dog"
@@ -73,60 +73,60 @@ class TrieTests: XCTestCase {
7373
self.indexDocument(id: 2, contents: doc2, trie: trie)
7474
self.indexDocument(id: 3, contents: doc3, trie: trie)
7575

76-
XCTAssertEqual(try trie.find(word: "fox"), [1, 2, 3])
77-
XCTAssertEqual(try trie.find(word: "dog"), [1, 2])
78-
XCTAssertEqual(try trie.find(word: "it"), [2, 3])
79-
XCTAssertEqual(try trie.find(word: "lazy"), [1])
80-
XCTAssertEqual(try trie.find(word: "notice"), [2])
81-
XCTAssertEqual(try trie.find(word: "blocked"), [3])
76+
#expect(try trie.find(word: "fox") == [1, 2, 3])
77+
#expect(try trie.find(word: "dog") == [1, 2])
78+
#expect(try trie.find(word: "it") == [2, 3])
79+
#expect(try trie.find(word: "lazy") == [1])
80+
#expect(try trie.find(word: "notice") == [2])
81+
#expect(try trie.find(word: "blocked") == [3])
8282

8383
trie.remove(document: 3)
8484

85-
XCTAssertEqual(try trie.find(word: "fox"), [1, 2])
86-
XCTAssertEqual(try trie.find(word: "dog"), [1, 2])
87-
XCTAssertEqual(try trie.find(word: "it"), [2])
88-
XCTAssertEqual(try trie.find(word: "lazy"), [1])
89-
XCTAssertEqual(try trie.find(word: "notice"), [2])
90-
XCTAssertThrowsError(try trie.find(word: "blocked"), "expected error") { error in
91-
XCTAssert(error is NotFoundError)
85+
#expect(try trie.find(word: "fox") == [1, 2])
86+
#expect(try trie.find(word: "dog") == [1, 2])
87+
#expect(try trie.find(word: "it") == [2])
88+
#expect(try trie.find(word: "lazy") == [1])
89+
#expect(try trie.find(word: "notice") == [2])
90+
#expect(throws: NotFoundError.self) {
91+
try trie.find(word: "blocked")
9292
}
9393

9494
trie.remove(document: 1)
9595

96-
XCTAssertEqual(try trie.find(word: "fox"), [2])
97-
XCTAssertEqual(try trie.find(word: "dog"), [2])
98-
XCTAssertEqual(try trie.find(word: "it"), [2])
99-
XCTAssertThrowsError(try trie.find(word: "lazy"), "expected error") { error in
100-
XCTAssert(error is NotFoundError)
96+
#expect(try trie.find(word: "fox") == [2])
97+
#expect(try trie.find(word: "dog") == [2])
98+
#expect(try trie.find(word: "it") == [2])
99+
#expect(throws: NotFoundError.self) {
100+
try trie.find(word: "lazy")
101101
}
102-
XCTAssertEqual(try trie.find(word: "notice"), [2])
103-
XCTAssertThrowsError(try trie.find(word: "blocked"), "expected error") { error in
104-
XCTAssert(error is NotFoundError)
102+
#expect(try trie.find(word: "notice") == [2])
103+
#expect(throws: NotFoundError.self) {
104+
try trie.find(word: "blocked")
105105
}
106106

107107
trie.remove(document: 2)
108108

109-
XCTAssertThrowsError(try trie.find(word: "fox"), "expected error") { error in
110-
XCTAssert(error is NotFoundError)
109+
#expect(throws: NotFoundError.self) {
110+
try trie.find(word: "fox")
111111
}
112-
XCTAssertThrowsError(try trie.find(word: "dog"), "expected error") { error in
113-
XCTAssert(error is NotFoundError)
112+
#expect(throws: NotFoundError.self) {
113+
try trie.find(word: "dog")
114114
}
115-
XCTAssertThrowsError(try trie.find(word: "it"), "expected error") { error in
116-
XCTAssert(error is NotFoundError)
115+
#expect(throws: NotFoundError.self) {
116+
try trie.find(word: "it")
117117
}
118-
XCTAssertThrowsError(try trie.find(word: "lazy"), "expected error") { error in
119-
XCTAssert(error is NotFoundError)
118+
#expect(throws: NotFoundError.self) {
119+
try trie.find(word: "lazy")
120120
}
121-
XCTAssertThrowsError(try trie.find(word: "notice"), "expected error") { error in
122-
XCTAssert(error is NotFoundError)
121+
#expect(throws: NotFoundError.self) {
122+
try trie.find(word: "notice")
123123
}
124-
XCTAssertThrowsError(try trie.find(word: "blocked"), "expected error") { error in
125-
XCTAssert(error is NotFoundError)
124+
#expect(throws: NotFoundError.self) {
125+
try trie.find(word: "blocked")
126126
}
127127
}
128128

129-
func testRemoveDocumentsWithPredicate() {
129+
func testRemoveDocumentsWithPredicate() throws {
130130
let trie = Trie<Int>()
131131

132132
let doc1 = "the quick brown fox jumps over the lazy dog"
@@ -136,56 +136,56 @@ class TrieTests: XCTestCase {
136136
self.indexDocument(id: 2, contents: doc2, trie: trie)
137137
self.indexDocument(id: 3, contents: doc3, trie: trie)
138138

139-
XCTAssertEqual(try trie.find(word: "fox"), [1, 2, 3])
140-
XCTAssertEqual(try trie.find(word: "dog"), [1, 2])
141-
XCTAssertEqual(try trie.find(word: "it"), [2, 3])
142-
XCTAssertEqual(try trie.find(word: "lazy"), [1])
143-
XCTAssertEqual(try trie.find(word: "notice"), [2])
144-
XCTAssertEqual(try trie.find(word: "blocked"), [3])
139+
#expect(try trie.find(word: "fox") == [1, 2, 3])
140+
#expect(try trie.find(word: "dog") == [1, 2])
141+
#expect(try trie.find(word: "it") == [2, 3])
142+
#expect(try trie.find(word: "lazy") == [1])
143+
#expect(try trie.find(word: "notice") == [2])
144+
#expect(try trie.find(word: "blocked") == [3])
145145

146146
trie.remove { $0 == 3 }
147147

148-
XCTAssertEqual(try trie.find(word: "fox"), [1, 2])
149-
XCTAssertEqual(try trie.find(word: "dog"), [1, 2])
150-
XCTAssertEqual(try trie.find(word: "it"), [2])
151-
XCTAssertEqual(try trie.find(word: "lazy"), [1])
152-
XCTAssertEqual(try trie.find(word: "notice"), [2])
153-
XCTAssertThrowsError(try trie.find(word: "blocked"), "expected error") { error in
154-
XCTAssert(error is NotFoundError)
148+
#expect(try trie.find(word: "fox") == [1, 2])
149+
#expect(try trie.find(word: "dog") == [1, 2])
150+
#expect(try trie.find(word: "it") == [2])
151+
#expect(try trie.find(word: "lazy") == [1])
152+
#expect(try trie.find(word: "notice") == [2])
153+
#expect(throws: NotFoundError.self) {
154+
try trie.find(word: "blocked")
155155
}
156156

157157
trie.remove { $0 == 1 }
158158

159-
XCTAssertEqual(try trie.find(word: "fox"), [2])
160-
XCTAssertEqual(try trie.find(word: "dog"), [2])
161-
XCTAssertEqual(try trie.find(word: "it"), [2])
162-
XCTAssertThrowsError(try trie.find(word: "lazy"), "expected error") { error in
163-
XCTAssert(error is NotFoundError)
159+
#expect(try trie.find(word: "fox") == [2])
160+
#expect(try trie.find(word: "dog") == [2])
161+
#expect(try trie.find(word: "it") == [2])
162+
#expect(throws: NotFoundError.self) {
163+
try trie.find(word: "lazy")
164164
}
165-
XCTAssertEqual(try trie.find(word: "notice"), [2])
166-
XCTAssertThrowsError(try trie.find(word: "blocked"), "expected error") { error in
167-
XCTAssert(error is NotFoundError)
165+
#expect(try trie.find(word: "notice") == [2])
166+
#expect(throws: NotFoundError.self) {
167+
try trie.find(word: "blocked")
168168
}
169169

170170
trie.remove { $0 == 2 }
171171

172-
XCTAssertThrowsError(try trie.find(word: "fox"), "expected error") { error in
173-
XCTAssert(error is NotFoundError)
172+
#expect(throws: NotFoundError.self) {
173+
try trie.find(word: "fox")
174174
}
175-
XCTAssertThrowsError(try trie.find(word: "dog"), "expected error") { error in
176-
XCTAssert(error is NotFoundError)
175+
#expect(throws: NotFoundError.self) {
176+
try trie.find(word: "dog")
177177
}
178-
XCTAssertThrowsError(try trie.find(word: "it"), "expected error") { error in
179-
XCTAssert(error is NotFoundError)
178+
#expect(throws: NotFoundError.self) {
179+
try trie.find(word: "it")
180180
}
181-
XCTAssertThrowsError(try trie.find(word: "lazy"), "expected error") { error in
182-
XCTAssert(error is NotFoundError)
181+
#expect(throws: NotFoundError.self) {
182+
try trie.find(word: "lazy")
183183
}
184-
XCTAssertThrowsError(try trie.find(word: "notice"), "expected error") { error in
185-
XCTAssert(error is NotFoundError)
184+
#expect(throws: NotFoundError.self) {
185+
try trie.find(word: "notice")
186186
}
187-
XCTAssertThrowsError(try trie.find(word: "blocked"), "expected error") { error in
188-
XCTAssert(error is NotFoundError)
187+
#expect(throws: NotFoundError.self) {
188+
try trie.find(word: "blocked")
189189
}
190190
}
191191

@@ -196,33 +196,25 @@ class TrieTests: XCTestCase {
196196
}
197197
}
198198

199-
func testThreadSafe() {
200-
let queue = DispatchQueue(label: "TrieTests", attributes: .concurrent)
199+
@Test func testThreadSafe() async throws {
201200
let trie = Trie<Int>()
202-
let docCount = 100
203-
204-
for _ in 0 ..< 100 {
205-
let sync = DispatchGroup()
206201

202+
let docCount = 100
203+
await withTaskGroup { group in
207204
for i in 0 ..< docCount {
208-
queue.async(group: sync) {
209-
Thread.sleep(forTimeInterval: Double.random(in: 100 ... 300) * 1.0e-6)
205+
group.addTask {
206+
try? await Task.sleep(for: .milliseconds(Double.random(in: 100...300)))
210207

211208
trie.remove { $0 == i }
212209
trie.insert(word: "word-\(i)", foundIn: i)
213210
trie.insert(word: "test", foundIn: i)
214211
}
215212
}
216-
217-
switch sync.wait(timeout: .now() + 1) {
218-
case .timedOut:
219-
XCTFail("timeout")
220-
case .success:
221-
for doc in 0 ..< docCount {
222-
XCTAssertEqual(try trie.find(word: "word-\(doc)"), [doc])
223-
XCTAssertEqual(try trie.find(word: "test").count, docCount)
224-
}
225-
}
213+
await group.waitForAll()
214+
}
215+
for doc in 0 ..< docCount {
216+
#expect(try trie.find(word: "word-\(doc)") == [doc])
217+
#expect(try trie.find(word: "test").count == docCount)
226218
}
227219
}
228220
}

0 commit comments

Comments
 (0)