Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Frugghi committed Feb 27, 2018
1 parent 1e6ba5d commit 69b2692
Show file tree
Hide file tree
Showing 4 changed files with 329 additions and 0 deletions.
12 changes: 12 additions & 0 deletions SwiftLCS Tests/ArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,17 @@ class ArrayTests: XCTestCase {
XCTAssertEqual(old.longestCommonSubsequence(new), [1, 2, 3])
XCTAssertEqual(new.longestCommonSubsequence(old), [1, 2, 3])
}

func testAppend() {
let old = [1, 2, 3, 4, 5, 6, 7]
let new = old + [8, 9, 10, 11]
XCTAssertEqual(old.longestCommonSubsequence(new), [1, 2, 3, 4, 5, 6, 7])
}

func testPrependAndAppend() {
let old = [1, 2, 3, 4, 5, 6, 7]
let new = [0] + old + [8, 9, 10, 11]
XCTAssertEqual(old.longestCommonSubsequence(new), [1, 2, 3, 4, 5, 6, 7])
}

}
56 changes: 56 additions & 0 deletions SwiftLCS Tests/BenchmarkTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2018 Tommaso Madonia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import XCTest
@testable import SwiftLCS

class BenchmarkTests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testBenchmark100() {
let old: [Int] = (0...100).map({ _ in Int(arc4random_uniform(10)) })
let new: [Int] = (0...100).map({ _ in Int(arc4random_uniform(10)) })
measure {
let _ = old.longestCommonSubsequence(new)
}
}

func testBenchmark1000() {
let old: [Int] = (0...1000).map({ _ in Int(arc4random_uniform(100)) })
let new: [Int] = (0...1000).map({ _ in Int(arc4random_uniform(100)) })
measure {
let _ = old.longestCommonSubsequence(new)
}
}

}
253 changes: 253 additions & 0 deletions SwiftLCS Tests/LinkedListTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2018 Tommaso Madonia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import XCTest
@testable import SwiftLCS

class LinkedListTests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testSimple() {
let old: LinkedList<Int> = [1, 2, 3, 4, 5, 6, 7]
let new: LinkedList<Int> = [8, 9, 2, 10, 4, 11, 6, 12]
let diff = Diff(old, new)

XCTAssertEqual(diff.commonIndexes, [1, 3, 5])
XCTAssertEqual(diff.addedIndexes, [0, 1, 3, 5, 7])
XCTAssertEqual(diff.removedIndexes, [0, 2, 4, 6])

XCTAssertEqual(diff.commonIndexes.count + diff.removedIndexes.count, old.count)
XCTAssertEqual(diff.commonIndexes.count + diff.addedIndexes.count, new.count)
}

func testPrefix() {
let old: LinkedList<Int> = [1, 2, 3, 4, 5]
let new: LinkedList<Int> = [1, 2, 3, 6, 7]
let diff = Diff(old, new)

XCTAssertEqual(diff.commonIndexes, [0, 1, 2])
XCTAssertEqual(diff.addedIndexes, [3, 4])
XCTAssertEqual(diff.removedIndexes, [3, 4])

XCTAssertEqual(diff.commonIndexes.count + diff.removedIndexes.count, old.count)
XCTAssertEqual(diff.commonIndexes.count + diff.addedIndexes.count, new.count)
}

func testSuffix() {
let old: LinkedList<Int> = [1, 2, 3, 4, 5]
let new: LinkedList<Int> = [6, 7, 3, 4, 5]
let diff = Diff(old, new)

XCTAssertEqual(diff.commonIndexes, [2, 3, 4])
XCTAssertEqual(diff.addedIndexes, [0, 1])
XCTAssertEqual(diff.removedIndexes, [0, 1])

XCTAssertEqual(diff.commonIndexes.count + diff.removedIndexes.count, old.count)
XCTAssertEqual(diff.commonIndexes.count + diff.addedIndexes.count, new.count)
}

func testContains() {
let old: LinkedList<Int> = [1, 2, 3, 4, 5]
let new: LinkedList<Int> = [6, 7, 1, 2, 3, 4, 5]
let diff = Diff(old, new)

XCTAssertEqual(diff.commonIndexes, [0, 1, 2, 3, 4])
XCTAssertEqual(diff.addedIndexes, [0, 1])
XCTAssertEqual(diff.removedIndexes, [])

XCTAssertEqual(diff.commonIndexes.count + diff.removedIndexes.count, old.count)
XCTAssertEqual(diff.commonIndexes.count + diff.addedIndexes.count, new.count)
}

func testEqual() {
let old: LinkedList<Int> = [1, 2, 3, 4, 5]
let new: LinkedList<Int> = [1, 2, 3, 4, 5]
let diff = Diff(old, new)

XCTAssertEqual(diff.commonIndexes, [0, 1, 2, 3, 4])
XCTAssertEqual(diff.addedIndexes, [])
XCTAssertEqual(diff.removedIndexes, [])

XCTAssertEqual(diff.commonIndexes.count + diff.removedIndexes.count, old.count)
XCTAssertEqual(diff.commonIndexes.count + diff.addedIndexes.count, new.count)
}

func testEmpty() {
let old: LinkedList<Int> = [1, 2, 3, 4, 5]
let new: LinkedList<Int> = []
let diff = Diff(old, new)

XCTAssertEqual(diff.commonIndexes, [])
XCTAssertEqual(diff.addedIndexes, [])
XCTAssertEqual(diff.removedIndexes, [0, 1, 2, 3, 4])

XCTAssertEqual(diff.commonIndexes.count + diff.removedIndexes.count, old.count)
XCTAssertEqual(diff.commonIndexes.count + diff.addedIndexes.count, new.count)
}

func testBothEmpty() {
let old: LinkedList<Int> = []
let new: LinkedList<Int> = []
let diff = Diff(old, new)

XCTAssertEqual(diff.commonIndexes, [])
XCTAssertEqual(diff.addedIndexes, [])
XCTAssertEqual(diff.removedIndexes, [])

XCTAssertEqual(diff.commonIndexes.count + diff.removedIndexes.count, old.count)
XCTAssertEqual(diff.commonIndexes.count + diff.addedIndexes.count, new.count)
}

func testDifferentLengths() {
let old: LinkedList<Int> = [1, 2, 3, 4, 5, 6]
let new: LinkedList<Int> = [1, 6, 7, 2, 3]
let diff = Diff(old, new)

XCTAssertEqual(diff.commonIndexes, [0, 1, 2])
XCTAssertEqual(diff.addedIndexes, [1, 2])
XCTAssertEqual(diff.removedIndexes, [3, 4, 5])

XCTAssertEqual(diff.commonIndexes.count + diff.removedIndexes.count, old.count)
XCTAssertEqual(diff.commonIndexes.count + diff.addedIndexes.count, new.count)
}

func testAddMultipleCopiesStart() {
let old: LinkedList<Int> = [0, 1, 2]
let new: LinkedList<Int> = [3, 3, 0, 1, 2]
let diff = Diff(old, new)

XCTAssertEqual(diff.commonIndexes, [0, 1, 2])
XCTAssertEqual(diff.addedIndexes, [0, 1])
XCTAssertEqual(diff.removedIndexes, [])

XCTAssertEqual(diff.commonIndexes.count + diff.removedIndexes.count, old.count)
XCTAssertEqual(diff.commonIndexes.count + diff.addedIndexes.count, new.count)
}

func testAddMultipleCopies() {
let old: LinkedList<Int> = [0, 1, 2]
let new: LinkedList<Int> = [3, 0, 3, 2, 3]
let diff = Diff(old, new)

XCTAssertEqual(diff.commonIndexes, [0, 2])
XCTAssertEqual(diff.addedIndexes, [0, 2, 4])
XCTAssertEqual(diff.removedIndexes, [1])

XCTAssertEqual(diff.commonIndexes.count + diff.removedIndexes.count, old.count)
XCTAssertEqual(diff.commonIndexes.count + diff.addedIndexes.count, new.count)
}

func testAddMultipleCopiesEnd() {
let old: LinkedList<Int> = [0, 1, 2]
let new: LinkedList<Int> = [0, 1, 2, 3, 3]
let diff = Diff(old, new)

XCTAssertEqual(diff.commonIndexes, [0, 1, 2])
XCTAssertEqual(diff.addedIndexes, [3, 4])
XCTAssertEqual(diff.removedIndexes, [])

XCTAssertEqual(diff.commonIndexes.count + diff.removedIndexes.count, old.count)
XCTAssertEqual(diff.commonIndexes.count + diff.addedIndexes.count, new.count)
}

}

// MARK: - Linked list -

/// Ugly implementation of a linked list, used for testing non bidirectional collections
fileprivate class LinkedList<T> : Collection, ExpressibleByArrayLiteral {
typealias Index = Int
typealias SubSequence = LinkedList<T>

private var head: LinkedListNode<T>?

var startIndex: Int {
return 0
}

var endIndex: Int {
return self.count
}

var count: Int {
var count = 0
var nextNode = self.head
while nextNode != nil {
nextNode = nextNode?.next
count += 1
}
return count
}

func index(after i: Int) -> Int {
return i + 1
}

subscript(index: Int) -> T {
var count = 0
var nextNode = self.head
while count != index || nextNode == nil {
nextNode = nextNode?.next
count += 1
}
return nextNode!.element
}

required init(arrayLiteral elements: T...) {
var lastNode = self.head
for element in elements {
let node = LinkedListNode(element: element)
if lastNode != nil {
lastNode?.next = node
} else {
self.head = node
}
lastNode = node
}
}


private class LinkedListNode<T> {

var element: T
var next: LinkedListNode<T>?

init(element: T, next: LinkedListNode<T>? = nil) {
self.element = element
self.next = next
}

}

}

8 changes: 8 additions & 0 deletions SwiftLCS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
18BCD1091D8B740200B6E4B5 /* SwiftLCS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182E30DB1BD4F46A008EC94E /* SwiftLCS.swift */; };
18BCD1191D8B757B00B6E4B5 /* SwiftLCS.h in Headers */ = {isa = PBXBuildFile; fileRef = 182E30D11BD4F44A008EC94E /* SwiftLCS.h */; settings = {ATTRIBUTES = (Public, ); }; };
18BCD11A1D8B758200B6E4B5 /* SwiftLCS.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182E30DB1BD4F46A008EC94E /* SwiftLCS.swift */; };
7D4B06042044EA7600C02F0E /* BenchmarkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D4B06032044EA7600C02F0E /* BenchmarkTests.swift */; };
7D4B06092044F85400C02F0E /* LinkedListTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D4B06082044F85400C02F0E /* LinkedListTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -45,6 +47,8 @@
18BCD0FE1D8B72F400B6E4B5 /* SwiftLCS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftLCS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
18BCD1061D8B73E000B6E4B5 /* Info-tvOS.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-tvOS.plist"; sourceTree = "<group>"; };
18BCD1111D8B752200B6E4B5 /* SwiftLCS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftLCS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
7D4B06032044EA7600C02F0E /* BenchmarkTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BenchmarkTests.swift; sourceTree = "<group>"; };
7D4B06082044F85400C02F0E /* LinkedListTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkedListTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -114,6 +118,8 @@
1886DD131BCEC60900E67CD7 /* StringTests.swift */,
1886DD151BCEC7FB00E67CD7 /* ArrayTests.swift */,
1886DD171BCECA3000E67CD7 /* IndexTests.swift */,
7D4B06032044EA7600C02F0E /* BenchmarkTests.swift */,
7D4B06082044F85400C02F0E /* LinkedListTests.swift */,
1804B66C1BCDE58000BD250F /* Info.plist */,
);
path = "SwiftLCS Tests";
Expand Down Expand Up @@ -365,6 +371,8 @@
buildActionMask = 2147483647;
files = (
1886DD161BCEC7FB00E67CD7 /* ArrayTests.swift in Sources */,
7D4B06092044F85400C02F0E /* LinkedListTests.swift in Sources */,
7D4B06042044EA7600C02F0E /* BenchmarkTests.swift in Sources */,
1886DD141BCEC60900E67CD7 /* StringTests.swift in Sources */,
1886DD181BCECA3000E67CD7 /* IndexTests.swift in Sources */,
);
Expand Down

0 comments on commit 69b2692

Please sign in to comment.