Skip to content

Commit

Permalink
finalize migration tests (kodecocodes#381)
Browse files Browse the repository at this point in the history
* finalize migration tests

* fix

* fixes

* test fixes

* fix bounded priority queue
  • Loading branch information
JaapWijnen authored and vincentngo committed Feb 18, 2017
1 parent 7cee0b6 commit c1952ec
Show file tree
Hide file tree
Showing 26 changed files with 202 additions and 130 deletions.
23 changes: 11 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,29 @@ script:
- xcodebuild test -project ./Boyer-Moore/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Binary\ Search\ Tree/Solution\ 1/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Bloom\ Filter/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Bounded\ Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Breadth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Bounded\ Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Breadth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Bucket\ Sort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./B-Tree/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Comb\ Sort/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Counting\ Sort/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Depth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Graph/Graph.xcodeproj -scheme GraphTests
# - xcodebuild test -project ./Heap/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Counting\ Sort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Depth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Graph/Graph.xcodeproj -scheme GraphTests
- xcodebuild test -project ./Heap/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Insertion\ Sort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./K-Means/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Linked\ List/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Linked\ List/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Longest\ Common\ Subsequence/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Minimum\ Spanning\ Tree\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Queue/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Quicksort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Quicksort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Radix\ Sort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Rootish\ Array\ Stack/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Run-Length\ Encoding/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Select\ Minimum\ Maximum/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Select\ Minimum\ Maximum/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Selection\ Sort/Tests/Tests.xcodeproj -scheme Tests
# - xcodebuild test -project ./Shell\ Sort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Shell\ Sort/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Shortest\ Path\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
- xcodebuild test -project ./Single-Source\ Shortest\ Paths\ \(Weighted\)/SSSP.xcodeproj -scheme SSSPTests
- xcodebuild test -project ./Stack/Tests/Tests.xcodeproj -scheme Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class LinkedListNode<T: Comparable> {
}

public class BoundedPriorityQueue<T: Comparable> {
private typealias Node = LinkedListNode<T>
fileprivate typealias Node = LinkedListNode<T>

private(set) public var count = 0
fileprivate var head: Node?
Expand Down
30 changes: 15 additions & 15 deletions Bounded Priority Queue/BoundedPriorityQueue.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class LinkedListNode<T: Comparable> {
open class LinkedListNode<T: Comparable> {
var value: T
var next: LinkedListNode?
var previous: LinkedListNode?
Expand All @@ -8,27 +8,27 @@ public class LinkedListNode<T: Comparable> {
}
}

public class BoundedPriorityQueue<T: Comparable> {
private typealias Node = LinkedListNode<T>
open class BoundedPriorityQueue<T: Comparable> {
fileprivate typealias Node = LinkedListNode<T>

private(set) public var count = 0
private var head: Node?
private var tail: Node?
private var maxElements: Int
fileprivate(set) open var count = 0
fileprivate var head: Node?
fileprivate var tail: Node?
fileprivate var maxElements: Int

public init(maxElements: Int) {
self.maxElements = maxElements
}

public var isEmpty: Bool {
open var isEmpty: Bool {
return count == 0
}

public func peek() -> T? {
open func peek() -> T? {
return head?.value
}

public func enqueue(value: T) {
open func enqueue(_ value: T) {
if let node = insert(value, after: findInsertionPoint(value)) {
// If the newly inserted node is the last one in the list, then update
// the tail pointer.
Expand All @@ -44,7 +44,7 @@ public class BoundedPriorityQueue<T: Comparable> {
}
}

private func insert(value: T, after: Node?) -> Node? {
fileprivate func insert(_ value: T, after: Node?) -> Node? {
if let previous = after {

// If the queue is full and we have to insert at the end of the list,
Expand Down Expand Up @@ -78,18 +78,18 @@ public class BoundedPriorityQueue<T: Comparable> {

/* Find the node after which to insert the new value. If this returns nil,
the new value should be inserted at the head of the list. */
private func findInsertionPoint(value: T) -> Node? {
fileprivate func findInsertionPoint(_ value: T) -> Node? {
var node = head
var prev: Node? = nil

while let current = node where value < current.value {
while let current = node, value < current.value {
prev = node
node = current.next
}
return prev
}

private func removeLeastImportantElement() {
fileprivate func removeLeastImportantElement() {
if let last = tail {
tail = last.previous
tail?.next = nil
Expand All @@ -101,7 +101,7 @@ public class BoundedPriorityQueue<T: Comparable> {
// this is much slower on large lists.
}

public func dequeue() -> T? {
open func dequeue() -> T? {
if let first = head {
count -= 1
if count == 0 {
Expand Down
42 changes: 41 additions & 1 deletion Bounded Priority Queue/Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0720;
LastUpgradeCheck = 0820;
TargetAttributes = {
B80004B21C83E342001FE2D7 = {
CreatedOnToolsVersion = 7.2.1;
LastSwiftMigration = 0820;
};
};
};
Expand Down Expand Up @@ -132,12 +133,49 @@
B80004AD1C83E324001FE2D7 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
ONLY_ACTIVE_ARCH = YES;
};
name = Debug;
};
B80004AE1C83E324001FE2D7 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
};
name = Release;
};
Expand Down Expand Up @@ -188,6 +226,7 @@
SDKROOT = macosx;
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand Down Expand Up @@ -230,6 +269,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx;
SWIFT_OBJC_BRIDGING_HEADER = "";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0730"
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
66 changes: 32 additions & 34 deletions Counting Sort/CountingSort.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,39 @@
// Copyright © 2016 Ali Hafizji. All rights reserved.
//

import Foundation

enum CountingSortError: Error {
case arrayEmpty
case arrayEmpty
}

func countingSort(array: [Int]) throws -> [Int] {
guard array.count > 0 else {
throw CountingSortError.arrayEmpty
}
// Step 1
// Create an array to store the count of each element
let maxElement = array.max() ?? 0
var countArray = [Int](repeating: 0, count: Int(maxElement + 1))
for element in array {
countArray[element] += 1
}
// Step 2
// Set each value to be the sum of the previous two values
for index in 1 ..< countArray.count {
let sum = countArray[index] + countArray[index - 1]
countArray[index] = sum
}
print(countArray)
// Step 3
// Place the element in the final array as per the number of elements before it
var sortedArray = [Int](repeating: 0, count: array.count)
for element in array {
countArray[element] -= 1
sortedArray[countArray[element]] = element
}
return sortedArray
func countingSort(_ array: [Int]) throws -> [Int] {
guard array.count > 0 else {
throw CountingSortError.arrayEmpty
}

// Step 1
// Create an array to store the count of each element
let maxElement = array.max() ?? 0

var countArray = [Int](repeating: 0, count: Int(maxElement + 1))
for element in array {
countArray[element] += 1
}

// Step 2
// Set each value to be the sum of the previous two values
for index in 1 ..< countArray.count {
let sum = countArray[index] + countArray[index - 1]
countArray[index] = sum
}

print(countArray)

// Step 3
// Place the element in the final array as per the number of elements before it
var sortedArray = [Int](repeating: 0, count: array.count)
for element in array {
countArray[element] -= 1
sortedArray[countArray[element]] = element
}
return sortedArray
}
10 changes: 9 additions & 1 deletion Counting Sort/Tests/Tests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0720;
LastUpgradeCheck = 0820;
ORGANIZATIONNAME = "Swift Algorithm Club";
TargetAttributes = {
7B2BBC7F1C779D720067B71D = {
CreatedOnToolsVersion = 7.2;
LastSwiftMigration = 0820;
};
};
};
Expand Down Expand Up @@ -145,8 +146,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand Down Expand Up @@ -189,8 +192,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand All @@ -209,6 +214,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
};
name = Release;
};
Expand All @@ -220,6 +226,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -231,6 +238,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0720"
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Loading

0 comments on commit c1952ec

Please sign in to comment.