Skip to content

Commit b0baae9

Browse files
committed
Updated tests to swift 3 and fixed playground
There was a better way to do the naming. Instead of doing nodeAt(index: Int), I changed it to nodeAt(_ index: Int) which allows you to call nodeAt(3) instead of nodeAt(index: 3)
1 parent b2fe7f3 commit b0baae9

File tree

4 files changed

+54
-51
lines changed

4 files changed

+54
-51
lines changed

Linked List/LinkedList.playground/Contents.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class LinkedList<T> {
4747
}
4848
}
4949

50-
public func nodeAt(index: Int) -> Node? {
50+
public func nodeAt(_ index: Int) -> Node? {
5151
if index >= 0 {
5252
var node = head
5353
var i = index
@@ -61,7 +61,7 @@ public class LinkedList<T> {
6161
}
6262

6363
public subscript(index: Int) -> T {
64-
let node = nodeAt(index: index)
64+
let node = nodeAt(index)
6565
assert(node != nil)
6666
return node!.value
6767
}
@@ -132,8 +132,8 @@ public class LinkedList<T> {
132132
return remove(node: last!)
133133
}
134134

135-
public func removeAt(index: Int) -> T {
136-
let node = nodeAt(index: index)
135+
public func removeAt(_ index: Int) -> T {
136+
let node = nodeAt(index)
137137
assert(node != nil)
138138
return remove(node: node!)
139139
}
@@ -211,9 +211,9 @@ list.first!.next!.value // "World"
211211
list.last!.previous!.value // "Hello"
212212
list.last!.next // nil
213213

214-
list.nodeAt(index: 0)!.value // "Hello"
215-
list.nodeAt(index: 1)!.value // "World"
216-
list.nodeAt(index: 2) // nil
214+
list.nodeAt(0)!.value // "Hello"
215+
list.nodeAt(1)!.value // "World"
216+
list.nodeAt(2) // nil
217217

218218
list[0] // "Hello"
219219
list[1] // "World"
@@ -227,8 +227,8 @@ print(list)
227227

228228
list.reverse() // [World, Swift, Hello]
229229

230-
list.nodeAt(index: 0)!.value = "Universe"
231-
list.nodeAt(index: 1)!.value = "Swifty"
230+
list.nodeAt(0)!.value = "Universe"
231+
list.nodeAt(1)!.value = "Swifty"
232232
let m = list.map { s in s.characters.count }
233233
m // [8, 6, 5]
234234
let f = list.filter { s in s.characters.count > 5 }
@@ -246,5 +246,5 @@ list.removeLast() // "World"
246246
list.count // 1
247247
list[0] // "Swift"
248248

249-
list.removeAt(index: 0) // "Swift"
249+
list.removeAt(0) // "Swift"
250250
list.count // 0

Linked List/LinkedList.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Most operations on the linked list have complexity O(n).
55
*/
6-
public class LinkedListNode<T> {
6+
open class LinkedListNode<T> {
77
var value: T
88
var next: LinkedListNode?
99
weak var previous: LinkedListNode?
@@ -13,20 +13,20 @@ public class LinkedListNode<T> {
1313
}
1414
}
1515

16-
public class LinkedList<T> {
16+
open class LinkedList<T> {
1717
public typealias Node = LinkedListNode<T>
1818

1919
fileprivate var head: Node?
2020

21-
public var isEmpty: Bool {
21+
open var isEmpty: Bool {
2222
return head == nil
2323
}
2424

25-
public var first: Node? {
25+
open var first: Node? {
2626
return head
2727
}
2828

29-
public var last: Node? {
29+
open var last: Node? {
3030
if var node = head {
3131
while case let next? = node.next {
3232
node = next
@@ -37,7 +37,7 @@ public class LinkedList<T> {
3737
}
3838
}
3939

40-
public var count: Int {
40+
open var count: Int {
4141
if var node = head {
4242
var c = 1
4343
while case let next? = node.next {
@@ -50,7 +50,7 @@ public class LinkedList<T> {
5050
}
5151
}
5252

53-
public func nodeAt(index: Int) -> Node? {
53+
open func nodeAt(_ index: Int) -> Node? {
5454
if index >= 0 {
5555
var node = head
5656
var i = index
@@ -63,13 +63,13 @@ public class LinkedList<T> {
6363
return nil
6464
}
6565

66-
public subscript(index: Int) -> T {
67-
let node = nodeAt(index: index)
66+
open subscript(index: Int) -> T {
67+
let node = nodeAt(index)
6868
assert(node != nil)
6969
return node!.value
7070
}
7171

72-
public func append(value: T) {
72+
open func append(_ value: T) {
7373
let newNode = Node(value: value)
7474
if let lastNode = last {
7575
newNode.previous = lastNode
@@ -79,7 +79,7 @@ public class LinkedList<T> {
7979
}
8080
}
8181

82-
private func nodesBeforeAndAfter(index: Int) -> (Node?, Node?) {
82+
fileprivate func nodesBeforeAndAfter(_ index: Int) -> (Node?, Node?) {
8383
assert(index >= 0)
8484

8585
var i = index
@@ -96,8 +96,8 @@ public class LinkedList<T> {
9696
return (prev, next)
9797
}
9898

99-
public func insert(value: T, atIndex index: Int) {
100-
let (prev, next) = nodesBeforeAndAfter(index: index)
99+
open func insert(_ value: T, atIndex index: Int) {
100+
let (prev, next) = nodesBeforeAndAfter(index)
101101

102102
let newNode = Node(value: value)
103103
newNode.previous = prev
@@ -110,11 +110,11 @@ public class LinkedList<T> {
110110
}
111111
}
112112

113-
public func removeAll() {
113+
open func removeAll() {
114114
head = nil
115115
}
116116

117-
public func remove(node: Node) -> T {
117+
open func remove(_ node: Node) -> T {
118118
let prev = node.previous
119119
let next = node.next
120120

@@ -130,15 +130,15 @@ public class LinkedList<T> {
130130
return node.value
131131
}
132132

133-
public func removeLast() -> T {
133+
open func removeLast() -> T {
134134
assert(!isEmpty)
135-
return remove(node: last!)
135+
return remove(last!)
136136
}
137137

138-
public func removeAt(index: Int) -> T {
139-
let node = nodeAt(index: index)
138+
open func removeAt(_ index: Int) -> T {
139+
let node = nodeAt(index)
140140
assert(node != nil)
141-
return remove(node: node!)
141+
return remove(node!)
142142
}
143143
}
144144

@@ -167,22 +167,22 @@ extension LinkedList {
167167
}
168168

169169
extension LinkedList {
170-
public func map<U>(transform: (T)-> U) -> LinkedList<U> {
170+
public func map<U>(_ transform: (T)-> U) -> LinkedList<U> {
171171
let result = LinkedList<U>()
172172
var node = head
173173
while node != nil {
174-
result.append(value: transform(node!.value))
174+
result.append(transform(node!.value))
175175
node = node!.next
176176
}
177177
return result
178178
}
179179

180-
public func filter(predicate: (T)-> Bool) -> LinkedList<T> {
180+
public func filter(_ predicate: (T)-> Bool) -> LinkedList<T> {
181181
let result = LinkedList<T>()
182182
var node = head
183183
while node != nil {
184184
if predicate(node!.value) {
185-
result.append(value: node!.value)
185+
result.append(node!.value)
186186
}
187187
node = node!.next
188188
}

Linked List/Tests/LinkedListTests.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import XCTest
33
class LinkedListTest: XCTestCase {
44
let numbers = [8, 2, 10, 9, 7, 5]
55

6-
private func buildList() -> LinkedList<Int> {
6+
fileprivate func buildList() -> LinkedList<Int> {
77
let list = LinkedList<Int>()
88
for number in numbers {
99
list.append(number)
@@ -88,15 +88,15 @@ class LinkedListTest: XCTestCase {
8888

8989
func testNodeAtIndexInEmptyList() {
9090
let list = LinkedList<Int>()
91-
let node = list.nodeAtIndex(0)
91+
let node = list.nodeAt(0)
9292
XCTAssertNil(node)
9393
}
9494

9595
func testNodeAtIndexInListWithOneElement() {
9696
let list = LinkedList<Int>()
9797
list.append(123)
9898

99-
let node = list.nodeAtIndex(0)
99+
let node = list.nodeAt(0)
100100
XCTAssertNotNil(node)
101101
XCTAssertEqual(node!.value, 123)
102102
XCTAssertTrue(node === list.first)
@@ -108,21 +108,21 @@ class LinkedListTest: XCTestCase {
108108
let nodeCount = list.count
109109
XCTAssertEqual(nodeCount, numbers.count)
110110

111-
XCTAssertNil(list.nodeAtIndex(-1))
112-
XCTAssertNil(list.nodeAtIndex(nodeCount))
111+
XCTAssertNil(list.nodeAt(-1))
112+
XCTAssertNil(list.nodeAt(nodeCount))
113113

114-
let first = list.nodeAtIndex(0)
114+
let first = list.nodeAt(0)
115115
XCTAssertNotNil(first)
116116
XCTAssertTrue(first === list.first)
117117
XCTAssertEqual(first!.value, numbers[0])
118118

119-
let last = list.nodeAtIndex(nodeCount - 1)
119+
let last = list.nodeAt(nodeCount - 1)
120120
XCTAssertNotNil(last)
121121
XCTAssertTrue(last === list.last)
122122
XCTAssertEqual(last!.value, numbers[nodeCount - 1])
123123

124124
for i in 0..<nodeCount {
125-
let node = list.nodeAtIndex(i)
125+
let node = list.nodeAt(i)
126126
XCTAssertNotNil(node)
127127
XCTAssertEqual(node!.value, numbers[i])
128128
}
@@ -142,20 +142,20 @@ class LinkedListTest: XCTestCase {
142142
XCTAssertFalse(list.isEmpty)
143143
XCTAssertEqual(list.count, 1)
144144

145-
let node = list.nodeAtIndex(0)
145+
let node = list.nodeAt(0)
146146
XCTAssertNotNil(node)
147147
XCTAssertEqual(node!.value, 123)
148148
}
149149

150150
func testInsertAtIndex() {
151151
let list = buildList()
152-
let prev = list.nodeAtIndex(2)
153-
let next = list.nodeAtIndex(3)
152+
let prev = list.nodeAt(2)
153+
let next = list.nodeAt(3)
154154
let nodeCount = list.count
155155

156156
list.insert(444, atIndex: 3)
157157

158-
let node = list.nodeAtIndex(3)
158+
let node = list.nodeAt(3)
159159
XCTAssertNotNil(node)
160160
XCTAssertEqual(node!.value, 444)
161161
XCTAssertEqual(nodeCount + 1, list.count)
@@ -170,7 +170,7 @@ class LinkedListTest: XCTestCase {
170170
let list = LinkedList<Int>()
171171
list.append(123)
172172

173-
let value = list.removeAtIndex(0)
173+
let value = list.removeAt(0)
174174
XCTAssertEqual(value, 123)
175175

176176
XCTAssertTrue(list.isEmpty)
@@ -181,16 +181,16 @@ class LinkedListTest: XCTestCase {
181181

182182
func testRemoveAtIndex() {
183183
let list = buildList()
184-
let prev = list.nodeAtIndex(2)
185-
let next = list.nodeAtIndex(3)
184+
let prev = list.nodeAt(2)
185+
let next = list.nodeAt(3)
186186
let nodeCount = list.count
187187

188188
list.insert(444, atIndex: 3)
189189

190-
let value = list.removeAtIndex(3)
190+
let value = list.removeAt(3)
191191
XCTAssertEqual(value, 444)
192192

193-
let node = list.nodeAtIndex(3)
193+
let node = list.nodeAt(3)
194194
XCTAssertTrue(next === node)
195195
XCTAssertTrue(prev!.next === node)
196196
XCTAssertTrue(node!.previous === prev)

Linked List/Tests/Tests.xcodeproj/project.pbxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
TargetAttributes = {
8989
7B2BBC7F1C779D720067B71D = {
9090
CreatedOnToolsVersion = 7.2;
91+
LastSwiftMigration = 0800;
9192
};
9293
};
9394
};
@@ -220,6 +221,7 @@
220221
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
221222
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
222223
PRODUCT_NAME = "$(TARGET_NAME)";
224+
SWIFT_VERSION = 3.0;
223225
};
224226
name = Debug;
225227
};
@@ -231,6 +233,7 @@
231233
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
232234
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
233235
PRODUCT_NAME = "$(TARGET_NAME)";
236+
SWIFT_VERSION = 3.0;
234237
};
235238
name = Release;
236239
};

0 commit comments

Comments
 (0)