-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinkedTest.swift
159 lines (95 loc) · 3.59 KB
/
LinkedTest.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// LinkedTest.swift
// SwiftStructures
//
// Created by Wayne Bishop on 9/23/14.
// Copyright (c) 2014 Arbutus Software Inc. All rights reserved.
//
import UIKit
import XCTest
@testable import SwiftStructures
//struct for testing indicies
struct keyIndex {
public var key: Int
public var index: Int
}
class LinkedTest: XCTestCase {
var numberList : Array<Int>!
override func setUp() {
super.setUp()
numberList = [8, 2, 10, 9, 7, 5]
}
//retrieve specific links
func testLinkAtIndex() {
let list = buildLinkedList()
let nodecount: Int = list.count
//test lower-bound
let lbound = list[0]
if ((lbound == nil) || (lbound?.key != numberList[0])) {
XCTFail("lowest bound retrieve fail..")
}
//upper bound
let ubound = list[nodecount - 1]
if ((ubound == nil) || (ubound?.key != numberList[nodecount - 1])) {
XCTFail("upper bound retrieve fail..")
}
//establish random index
let range: UInt32 = UInt32(numberList.count)
let randomIndex = Int(arc4random_uniform(range))
//retrieve random index
let randomlink = list[randomIndex]
if ((randomlink == nil) || (randomlink?.key != numberList[randomIndex])) {
XCTFail("random index retrieve fail..")
}
}
//test nodes at a specific index
func testAddLinkAtIndex() {
//create list and test pair
let list = buildLinkedList()
let testPair: keyIndex = keyIndex(key: 4, index: 3)
list.insert(testPair.key, at: testPair.index)
list.printAllKeys()
let current = list[testPair.index]
//test condition
if current == nil || current?.key != testPair.key {
XCTFail("linked list addition at index failed..")
}
//remove test item
list.remove(at: testPair.index)
list.printAllKeys()
//retrieve value at same position
let removed = list[testPair.index] as LLNode?
if removed == nil || removed?.key == testPair.key {
XCTFail("test failed: removed linked list element not found")
}
} //end function
//reverse a linked list
func testReverseLinkedList() {
let list = buildLinkedList()
//find (subscript) syntax
guard let flink = list[0] else {
XCTFail("link for reversal not found..")
return
}
//reverse the list
list.reverse()
list.printAllKeys()
if let blink = list[0] {
//evaluate keys
if (flink.key == blink.key) {
XCTFail("reversed list failed..")
}
}
}
//MARK: helper functions
func buildLinkedList() ->LinkedList<Int> {
let list = LinkedList<Int>()
//append items
for number in numberList {
list.append(element: number)
}
list.printAllKeys()
XCTAssertFalse(list.count != numberList.count, "test failed: linked list count doesn't match number list..")
return list
}
}