forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed singly linked list; this discussion covers both.
- Loading branch information
Showing
18 changed files
with
1,380 additions
and
495 deletions.
There are no files selected for viewing
210 changes: 105 additions & 105 deletions
210
...LinkedListTests.xcodeproj/project.pbxproj → ...ests/LinkedList.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// AppDelegate.swift | ||
// LinkedList | ||
// | ||
// Created by Matthijs Hollemans on 03-02-16. | ||
// Copyright © 2016 Swift Algorithm Club. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
|
||
@NSApplicationMain | ||
class AppDelegate: NSObject, NSApplicationDelegate { | ||
|
||
@IBOutlet weak var window: NSWindow! | ||
|
||
|
||
func applicationDidFinishLaunching(aNotification: NSNotification) { | ||
// Insert code here to initialize your application | ||
} | ||
|
||
func applicationWillTerminate(aNotification: NSNotification) { | ||
// Insert code here to tear down your application | ||
} | ||
|
||
|
||
} | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
252 changes: 252 additions & 0 deletions
252
Linked List/LinkedList Tests/LinkedListTests/LinkedListTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
import XCTest | ||
@testable import LinkedList | ||
|
||
class LinkedListTest: XCTestCase { | ||
let numbers = [8, 2, 10, 9, 7, 5] | ||
|
||
private func buildList() -> LinkedList<Int> { | ||
let list = LinkedList<Int>() | ||
for number in numbers { | ||
list.append(number) | ||
} | ||
return list | ||
} | ||
|
||
func testEmptyList() { | ||
let list = LinkedList<Int>() | ||
XCTAssertTrue(list.isEmpty) | ||
XCTAssertEqual(list.count, 0) | ||
XCTAssertNil(list.first) | ||
XCTAssertNil(list.last) | ||
} | ||
|
||
func testListWithOneElement() { | ||
let list = LinkedList<Int>() | ||
list.append(123) | ||
|
||
XCTAssertFalse(list.isEmpty) | ||
XCTAssertEqual(list.count, 1) | ||
|
||
XCTAssertNotNil(list.first) | ||
XCTAssertNil(list.first!.previous) | ||
XCTAssertNil(list.first!.next) | ||
XCTAssertEqual(list.first!.value, 123) | ||
|
||
XCTAssertNotNil(list.last) | ||
XCTAssertNil(list.last!.previous) | ||
XCTAssertNil(list.last!.next) | ||
XCTAssertEqual(list.last!.value, 123) | ||
|
||
XCTAssertTrue(list.first === list.last) | ||
} | ||
|
||
func testListWithTwoElements() { | ||
let list = LinkedList<Int>() | ||
list.append(123) | ||
list.append(456) | ||
|
||
XCTAssertEqual(list.count, 2) | ||
|
||
XCTAssertNotNil(list.first) | ||
XCTAssertEqual(list.first!.value, 123) | ||
|
||
XCTAssertNotNil(list.last) | ||
XCTAssertEqual(list.last!.value, 456) | ||
|
||
XCTAssertTrue(list.first !== list.last) | ||
|
||
XCTAssertNil(list.first!.previous) | ||
XCTAssertTrue(list.first!.next === list.last) | ||
XCTAssertTrue(list.last!.previous === list.first) | ||
XCTAssertNil(list.last!.next) | ||
} | ||
|
||
func testListWithThreeElements() { | ||
let list = LinkedList<Int>() | ||
list.append(123) | ||
list.append(456) | ||
list.append(789) | ||
|
||
XCTAssertEqual(list.count, 3) | ||
|
||
XCTAssertNotNil(list.first) | ||
XCTAssertEqual(list.first!.value, 123) | ||
|
||
let second = list.first!.next | ||
XCTAssertNotNil(second) | ||
XCTAssertEqual(second!.value, 456) | ||
|
||
XCTAssertNotNil(list.last) | ||
XCTAssertEqual(list.last!.value, 789) | ||
|
||
XCTAssertNil(list.first!.previous) | ||
XCTAssertTrue(list.first!.next === second) | ||
XCTAssertTrue(second!.previous === list.first) | ||
XCTAssertTrue(second!.next === list.last) | ||
XCTAssertTrue(list.last!.previous === second) | ||
XCTAssertNil(list.last!.next) | ||
} | ||
|
||
func testNodeAtIndexInEmptyList() { | ||
let list = LinkedList<Int>() | ||
let node = list.nodeAtIndex(0) | ||
XCTAssertNil(node) | ||
} | ||
|
||
func testNodeAtIndexInListWithOneElement() { | ||
let list = LinkedList<Int>() | ||
list.append(123) | ||
|
||
let node = list.nodeAtIndex(0) | ||
XCTAssertNotNil(node) | ||
XCTAssertEqual(node!.value, 123) | ||
XCTAssertTrue(node === list.first) | ||
} | ||
|
||
func testNodeAtIndex() { | ||
let list = buildList() | ||
|
||
let nodeCount = list.count | ||
XCTAssertEqual(nodeCount, numbers.count) | ||
|
||
XCTAssertNil(list.nodeAtIndex(-1)) | ||
XCTAssertNil(list.nodeAtIndex(nodeCount)) | ||
|
||
let first = list.nodeAtIndex(0) | ||
XCTAssertNotNil(first) | ||
XCTAssertTrue(first === list.first) | ||
XCTAssertEqual(first!.value, numbers[0]) | ||
|
||
let last = list.nodeAtIndex(nodeCount - 1) | ||
XCTAssertNotNil(last) | ||
XCTAssertTrue(last === list.last) | ||
XCTAssertEqual(last!.value, numbers[nodeCount - 1]) | ||
|
||
for i in 0..<nodeCount { | ||
let node = list.nodeAtIndex(i) | ||
XCTAssertNotNil(node) | ||
XCTAssertEqual(node!.value, numbers[i]) | ||
} | ||
} | ||
|
||
func testSubscript() { | ||
let list = buildList() | ||
for i in 0 ..< list.count { | ||
XCTAssertEqual(list[i], numbers[i]) | ||
} | ||
} | ||
|
||
func testInsertAtIndexInEmptyList() { | ||
let list = LinkedList<Int>() | ||
list.insert(123, atIndex: 0) | ||
|
||
XCTAssertFalse(list.isEmpty) | ||
XCTAssertEqual(list.count, 1) | ||
|
||
let node = list.nodeAtIndex(0) | ||
XCTAssertNotNil(node) | ||
XCTAssertEqual(node!.value, 123) | ||
} | ||
|
||
func testInsertAtIndex() { | ||
let list = buildList() | ||
let prev = list.nodeAtIndex(2) | ||
let next = list.nodeAtIndex(3) | ||
let nodeCount = list.count | ||
|
||
list.insert(444, atIndex: 3) | ||
|
||
let node = list.nodeAtIndex(3) | ||
XCTAssertNotNil(node) | ||
XCTAssertEqual(node!.value, 444) | ||
XCTAssertEqual(nodeCount + 1, list.count) | ||
|
||
XCTAssertFalse(prev === node) | ||
XCTAssertFalse(next === node) | ||
XCTAssertTrue(prev!.next === node) | ||
XCTAssertTrue(next!.previous === node) | ||
} | ||
|
||
func testRemoveAtIndexOnListWithOneElement() { | ||
let list = LinkedList<Int>() | ||
list.append(123) | ||
|
||
let value = list.removeAtIndex(0) | ||
XCTAssertEqual(value, 123) | ||
|
||
XCTAssertTrue(list.isEmpty) | ||
XCTAssertEqual(list.count, 0) | ||
XCTAssertNil(list.first) | ||
XCTAssertNil(list.last) | ||
} | ||
|
||
func testRemoveAtIndex() { | ||
let list = buildList() | ||
let prev = list.nodeAtIndex(2) | ||
let next = list.nodeAtIndex(3) | ||
let nodeCount = list.count | ||
|
||
list.insert(444, atIndex: 3) | ||
|
||
let value = list.removeAtIndex(3) | ||
XCTAssertEqual(value, 444) | ||
|
||
let node = list.nodeAtIndex(3) | ||
XCTAssertTrue(next === node) | ||
XCTAssertTrue(prev!.next === node) | ||
XCTAssertTrue(node!.previous === prev) | ||
XCTAssertEqual(nodeCount, list.count) | ||
} | ||
|
||
func testRemoveLastOnListWithOneElement() { | ||
let list = LinkedList<Int>() | ||
list.append(123) | ||
|
||
let value = list.removeLast() | ||
XCTAssertEqual(value, 123) | ||
|
||
XCTAssertTrue(list.isEmpty) | ||
XCTAssertEqual(list.count, 0) | ||
XCTAssertNil(list.first) | ||
XCTAssertNil(list.last) | ||
} | ||
|
||
func testRemoveLast() { | ||
let list = buildList() | ||
let last = list.last | ||
let prev = last!.previous | ||
let nodeCount = list.count | ||
|
||
let value = list.removeLast() | ||
XCTAssertEqual(value, 5) | ||
|
||
XCTAssertNil(last!.previous) | ||
XCTAssertNil(last!.next) | ||
|
||
XCTAssertNil(prev!.next) | ||
XCTAssertTrue(list.last === prev) | ||
XCTAssertEqual(nodeCount - 1, list.count) | ||
} | ||
|
||
func testRemoveAll() { | ||
let list = buildList() | ||
list.removeAll() | ||
XCTAssertTrue(list.isEmpty) | ||
XCTAssertEqual(list.count, 0) | ||
XCTAssertNil(list.first) | ||
XCTAssertNil(list.last) | ||
} | ||
|
||
func testReverseLinkedList() { | ||
let list = buildList() | ||
let first = list.first | ||
let last = list.last | ||
let nodeCount = list.count | ||
|
||
list.reverse() | ||
|
||
XCTAssertTrue(first === list.last) | ||
XCTAssertTrue(last === list.first) | ||
XCTAssertEqual(nodeCount, list.count) | ||
} | ||
} |
Oops, something went wrong.