10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- import XCTest
13
+ import Testing
14
14
15
15
@testable import PackageCollections
16
16
17
- class TrieTests : XCTestCase {
18
- func testContains( ) {
17
+ @ Suite struct TrieTests {
18
+ @ Test func testContains( ) {
19
19
let trie = Trie < Int > ( )
20
20
21
21
let doc1 = " THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG "
22
22
self . indexDocument ( id: 1 , contents: doc1, trie: trie)
23
23
24
24
// 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 ) )
28
28
29
29
// 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 ) )
33
33
}
34
34
35
- func testFind( ) {
35
+ @ Test func testFind( ) throws {
36
36
let trie = Trie < Int > ( )
37
37
38
38
let doc1 = " the quick brown fox jumps over the lazy dog and the dog does not notice the fox jumps over it "
39
39
let doc2 = " the quick brown fox jumps over the lazy dog for the lazy dog has blocked its way for far too long "
40
40
self . indexDocument ( id: 1 , contents: doc1, trie: trie)
41
41
self . indexDocument ( id: 2 , contents: doc2, trie: trie)
42
42
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 " )
47
47
}
48
48
}
49
49
50
- func testFindWithPrefix( ) {
50
+ func testFindWithPrefix( ) throws {
51
51
let trie = Trie < Int > ( )
52
52
53
53
let doc1 = " the quick brown fox jumps over the lazy dog and the dog does not notice the fox jumps over it "
54
54
let doc2 = " the quick brown fox jumps over the lazy dog for the lazy dog has blocked its way for far too long "
55
55
self . indexDocument ( id: 1 , contents: doc1, trie: trie)
56
56
self . indexDocument ( id: 2 , contents: doc2, trie: trie)
57
57
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 " )
63
63
}
64
64
}
65
65
66
- func testRemoveDocument( ) {
66
+ @ Test func testRemoveDocument( ) throws {
67
67
let trie = Trie < Int > ( )
68
68
69
69
let doc1 = " the quick brown fox jumps over the lazy dog "
@@ -73,60 +73,60 @@ class TrieTests: XCTestCase {
73
73
self . indexDocument ( id: 2 , contents: doc2, trie: trie)
74
74
self . indexDocument ( id: 3 , contents: doc3, trie: trie)
75
75
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 ] )
82
82
83
83
trie. remove ( document: 3 )
84
84
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 " )
92
92
}
93
93
94
94
trie. remove ( document: 1 )
95
95
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 " )
101
101
}
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 " )
105
105
}
106
106
107
107
trie. remove ( document: 2 )
108
108
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 " )
111
111
}
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 " )
114
114
}
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 " )
117
117
}
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 " )
120
120
}
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 " )
123
123
}
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 " )
126
126
}
127
127
}
128
128
129
- func testRemoveDocumentsWithPredicate( ) {
129
+ func testRemoveDocumentsWithPredicate( ) throws {
130
130
let trie = Trie < Int > ( )
131
131
132
132
let doc1 = " the quick brown fox jumps over the lazy dog "
@@ -136,56 +136,56 @@ class TrieTests: XCTestCase {
136
136
self . indexDocument ( id: 2 , contents: doc2, trie: trie)
137
137
self . indexDocument ( id: 3 , contents: doc3, trie: trie)
138
138
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 ] )
145
145
146
146
trie. remove { $0 == 3 }
147
147
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 " )
155
155
}
156
156
157
157
trie. remove { $0 == 1 }
158
158
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 " )
164
164
}
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 " )
168
168
}
169
169
170
170
trie. remove { $0 == 2 }
171
171
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 " )
174
174
}
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 " )
177
177
}
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 " )
180
180
}
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 " )
183
183
}
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 " )
186
186
}
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 " )
189
189
}
190
190
}
191
191
@@ -196,33 +196,25 @@ class TrieTests: XCTestCase {
196
196
}
197
197
}
198
198
199
- func testThreadSafe( ) {
200
- let queue = DispatchQueue ( label: " TrieTests " , attributes: . concurrent)
199
+ @Test func testThreadSafe( ) async throws {
201
200
let trie = Trie < Int > ( )
202
- let docCount = 100
203
-
204
- for _ in 0 ..< 100 {
205
- let sync = DispatchGroup ( )
206
201
202
+ let docCount = 100
203
+ await withTaskGroup { group in
207
204
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 ) ) )
210
207
211
208
trie. remove { $0 == i }
212
209
trie. insert ( word: " word- \( i) " , foundIn: i)
213
210
trie. insert ( word: " test " , foundIn: i)
214
211
}
215
212
}
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)
226
218
}
227
219
}
228
220
}
0 commit comments