@@ -12,20 +12,20 @@ import XCTest
12
12
class TrieTests : XCTestCase {
13
13
var wordArray : [ String ] ?
14
14
var trie = Trie ( )
15
-
15
+
16
16
/// Makes sure that the wordArray and trie are initialized before each test.
17
17
override func setUp( ) {
18
18
super. setUp ( )
19
19
createWordArray ( )
20
20
insertWordsIntoTrie ( )
21
21
}
22
-
23
- /// Don't need to do anything here because the wordArrayu and trie should
22
+
23
+ /// Don't need to do anything here because the wordArray and trie should
24
24
/// stay around.
25
25
override func tearDown( ) {
26
26
super. tearDown ( )
27
27
}
28
-
28
+
29
29
/// Reads words from the dictionary file and inserts them into an array. If
30
30
/// the word array already has words, do nothing. This allows running all
31
31
/// tests without repeatedly filling the array with the same values.
@@ -36,7 +36,7 @@ class TrieTests: XCTestCase {
36
36
let resourcePath = Bundle . main. resourcePath! as NSString
37
37
let fileName = " dictionary.txt "
38
38
let filePath = resourcePath. appendingPathComponent ( fileName)
39
-
39
+
40
40
var data : String ?
41
41
do {
42
42
data = try String ( contentsOfFile: filePath, encoding: String . Encoding. utf8)
@@ -48,24 +48,23 @@ class TrieTests: XCTestCase {
48
48
wordArray = data!. components ( separatedBy: " \n " )
49
49
XCTAssertEqual ( wordArray!. count, dictionarySize)
50
50
}
51
-
51
+
52
52
/// Inserts words into a trie. If the trie is non-empty, don't do anything.
53
53
func insertWordsIntoTrie( ) {
54
54
guard trie. count == 0 else {
55
55
return
56
56
}
57
- let numberOfWordsToInsert = wordArray!. count
58
- for i in 0 ..< numberOfWordsToInsert {
59
- trie. insert ( word: wordArray![ i] )
57
+ for word in self . wordArray! {
58
+ trie. insert ( word: word)
60
59
}
61
60
}
62
-
61
+
63
62
/// Tests that a newly created trie has zero words.
64
63
func testCreate( ) {
65
64
let trie = Trie ( )
66
65
XCTAssertEqual ( trie. count, 0 )
67
66
}
68
-
67
+
69
68
/// Tests the insert method
70
69
func testInsert( ) {
71
70
let trie = Trie ( )
@@ -78,7 +77,7 @@ class TrieTests: XCTestCase {
78
77
XCTAssertTrue ( trie. contains ( word: " cut " ) )
79
78
XCTAssertEqual ( trie. count, 4 )
80
79
}
81
-
80
+
82
81
/// Tests the remove method
83
82
func testRemove( ) {
84
83
let trie = Trie ( )
@@ -90,7 +89,7 @@ class TrieTests: XCTestCase {
90
89
XCTAssertFalse ( trie. contains ( word: " cute " ) )
91
90
XCTAssertEqual ( trie. count, 1 )
92
91
}
93
-
92
+
94
93
/// Tests the words property
95
94
func testWords( ) {
96
95
let trie = Trie ( )
@@ -101,52 +100,53 @@ class TrieTests: XCTestCase {
101
100
XCTAssertEqual ( words [ 0 ] , " foobar " )
102
101
XCTAssertEqual ( words. count, 1 )
103
102
}
104
-
103
+
105
104
/// Tests the performance of the insert method.
106
105
func testInsertPerformance( ) {
107
- let trie = Trie ( )
108
106
self . measure ( ) {
109
- let numberOfWordsToInsert = self . wordArray! . count
110
- for i in 0 ..< numberOfWordsToInsert {
111
- trie. insert ( word: self . wordArray! [ i ] )
107
+ let trie = Trie ( )
108
+ for word in self . wordArray! {
109
+ trie. insert ( word: word )
112
110
}
113
111
}
114
112
XCTAssertGreaterThan ( trie. count, 0 )
115
113
XCTAssertEqual ( trie. count, wordArray? . count)
116
114
}
117
-
115
+
118
116
/// Tests the performance of the insert method when the words are already
119
117
/// present.
120
118
func testInsertAgainPerformance( ) {
121
119
self . measure ( ) {
122
- let numberOfWordsToInsert = self . wordArray!. count
123
- for i in 0 ..< numberOfWordsToInsert {
124
- self . trie. insert ( word: self . wordArray![ i] )
120
+ for word in self . wordArray! {
121
+ self . trie. insert ( word: word)
125
122
}
126
123
}
127
124
}
128
-
125
+
129
126
/// Tests the performance of the contains method.
130
127
func testContainsPerformance( ) {
131
128
self . measure ( ) {
132
- for i in 0 ..< self . wordArray!. count {
133
- XCTAssertTrue ( self . trie. contains ( word: self . wordArray! [ i ] ) )
129
+ for word in self . wordArray! {
130
+ XCTAssertTrue ( self . trie. contains ( word: word ) )
134
131
}
135
-
136
132
}
137
133
}
138
-
139
- /// Tests the performance of the remove method.
134
+
135
+ /// Tests the performance of the remove method. Since setup has already put
136
+ /// words into the trie, remove them before measuring performance.
140
137
func testRemovePerformance( ) {
138
+ for word in self . wordArray! {
139
+ self . trie. remove ( word: word)
140
+ }
141
141
self . measure ( ) {
142
- let numberOfWordsToRemove = self . wordArray! . count
143
- for i in 0 ..< numberOfWordsToRemove {
144
- self . trie. remove ( word: self . wordArray! [ i ] )
142
+ self . insertWordsIntoTrie ( )
143
+ for word in self . wordArray! {
144
+ self . trie. remove ( word: word )
145
145
}
146
146
}
147
147
XCTAssertEqual ( trie. count, 0 )
148
148
}
149
-
149
+
150
150
/// Tests the performance of the words computed property. Also tests to see
151
151
/// if it worked properly.
152
152
func testWordsPerformance( ) {
@@ -159,4 +159,15 @@ class TrieTests: XCTestCase {
159
159
XCTAssertTrue ( self . trie. contains ( word: word) )
160
160
}
161
161
}
162
+
163
+ /// Tests the archiving and unarchiving of the trie.
164
+ func testArchiveAndUnarchive( ) {
165
+ let resourcePath = Bundle . main. resourcePath! as NSString
166
+ let fileName = " dictionary-archive "
167
+ let filePath = resourcePath. appendingPathComponent ( fileName)
168
+ NSKeyedArchiver . archiveRootObject ( trie, toFile: filePath)
169
+ let trieCopy = NSKeyedUnarchiver . unarchiveObject ( withFile: filePath) as! Trie
170
+ XCTAssertEqual ( trieCopy. count, trie. count)
171
+
172
+ }
162
173
}
0 commit comments