-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrieTest.swift
86 lines (56 loc) · 1.96 KB
/
TrieTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// TrieTest.swift
// SwiftStructures
//
// Created by Wayne Bishop on 10/14/14.
// Copyright (c) 2014 Arbutus Software Inc. All rights reserved.
//
import UIKit
import XCTest
@testable import SwiftStructures
class TrieTest: XCTestCase {
var testTrie = Trie()
override func setUp() {
super.setUp()
XCTAssertNotNil(testTrie, "Trie instance not correctly intialized..")
//add words to data structure
testTrie.append(word: "Ball")
testTrie.append(word: "Balls")
testTrie.append(word: "Ballard")
testTrie.append(word: "Bat")
testTrie.append(word: "Bar")
}
/*
the findWord algorithm will only return strings identified as words. For example, the prefix "Ba" has children,
but only 2 are marked as final. Even though the phrase "Bal" is found in the trie, it is not
identified as a word.
*/
func testFindWithPrefix() {
guard let list = testTrie["Ba"] else {
XCTFail("test failed: no words found..")
return
}
for word in list {
print("\(word) found in trie..")
}
}
//note: the findWord algorthim will identify both parents and children identified as words
func testFindWithWord() {
guard let list = testTrie["Ball"] else {
XCTFail("test failed: no words found")
return
}
for word in list {
print("\(word) found in trie..")
}
}
//testing false search results
func testFindNoExist() {
let keyword = "Barstool"
//attempt to find word
guard let _ = testTrie[keyword] else {
return
}
XCTFail("test failed: \(keyword) incorrectly found in trie..")
}
}