-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSortingTest.swift
134 lines (83 loc) · 3.71 KB
/
SortingTest.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
//
// BinarySearchTest.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
/*
notes: this test class adopts the Sortable protocol.
the isSorted function originates from the protocol extension.
*/
class SortingTest: XCTestCase, Sortable {
//test input types for algorithms
var numberList = [8, 2, 10, 9, 7, 5]
var searchList = [0,4,7,9,13,16,34]
var trivialNumberList = [1]
var emptyNumberList: Array<Int> = []
var textList = ["Dog", "Cat", "Dinasour", "Lion", "Cheetah", "Gazelle", "Elephant", "Aardvark"]
var triviaTextList = ["Dog"]
var emptyTextList: Array<String> = []
//string extension
var dateList: Array<Date> = ["12-10-2016".datevalue, "12-08-2016".datevalue, "12-09-2016".datevalue]
override func setUp() {
super.setUp()
}
func testLongestSequence() {
let list = [0,1,0,1,1,1,0,1,1,1,1,1]
let results = list.longestSequence(of: 1)
print("the longest sequence is \(results)")
}
//MARK: - Binary Search Algorithms
func testBinarySearch() {
var searchList = Array<Int>()
let key: Int = 235
//populate collection..
for number in 0...500 {
searchList.append(number)
}
XCTAssertTrue(self.isSorted(searchList), "search list of values not sorted..")
//perform test search
XCTAssertTrue(searchList.binarySearch(forElement: key), "binary key value \(key) not found..")
}
func testBinaryNotFound() {
let key: Int = 8
//test for false positive
XCTAssertFalse(searchList.binarySearch(forElement: key), "binary key value \(key) found..")
}
func testBinaryFound() {
let key: Int = 9
XCTAssertTrue(searchList.binarySearch(forElement: key), "binary key value \(key) not found..")
}
//MARK: General Sorting Algorithms
func testInsertionSort() {
XCTAssertTrue(isSorted(numberList.insertionSort()))
XCTAssertTrue(isSorted(trivialNumberList.insertionSort()))
XCTAssertTrue(isSorted(emptyNumberList.insertionSort()))
XCTAssertTrue(isSorted(textList.insertionSort()))
XCTAssertTrue(isSorted(triviaTextList.insertionSort()))
XCTAssertTrue(isSorted(emptyTextList.insertionSort()))
XCTAssert(isSorted(dateList.insertionSort()))
}
func testBubbleSort() {
XCTAssertTrue(isSorted(numberList.bubbleSort()))
XCTAssertTrue(isSorted(trivialNumberList.bubbleSort()))
XCTAssertTrue(isSorted(emptyNumberList.bubbleSort()))
XCTAssertTrue(isSorted(textList.bubbleSort()))
XCTAssertTrue(isSorted(triviaTextList.bubbleSort()))
XCTAssertTrue(isSorted(emptyTextList.bubbleSort()))
XCTAssert(isSorted(dateList.bubbleSort()))
}
func testSelectionSort() {
XCTAssertTrue(isSorted(numberList.selectionSort()))
XCTAssertTrue(isSorted(trivialNumberList.selectionSort()))
XCTAssertTrue(isSorted(emptyNumberList.selectionSort()))
XCTAssertTrue(isSorted(textList.selectionSort()))
XCTAssertTrue(isSorted(triviaTextList.selectionSort()))
XCTAssertTrue(isSorted(emptyTextList.selectionSort()))
XCTAssert(isSorted(dateList.selectionSort()))
}
}