Skip to content

Commit

Permalink
Reformat all Swift source using SwiftFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyarnold committed Nov 23, 2016
1 parent 2c6468a commit c0506cb
Show file tree
Hide file tree
Showing 27 changed files with 406 additions and 444 deletions.
26 changes: 13 additions & 13 deletions DiffTests/DiffTests.swift
Expand Up @@ -16,7 +16,7 @@ extension Trace: Hashable {
}

class DiffTests: XCTestCase {

let expectations = [
("kitten", "sitting", "D(0)I(0)D(4)I(4)I(6)"),
("🐩itt🍨ng", "kitten", "D(0)I(0)D(4)I(4)D(6)"),
Expand All @@ -31,9 +31,9 @@ class DiffTests: XCTestCase {
("1234", "1234", ""),
("", "", ""),
("Oh Hi", "Hi Oh", "D(0)D(1)D(2)I(2)I(3)I(4)"),
("1362", "31526", "D(0)D(2)I(1)I(2)I(4)")
("1362", "31526", "D(0)D(2)I(1)I(2)I(4)"),
]

let extendedExpectations = [
("sitting", "kitten", "D(0)I(0)D(4)I(4)D(6)"),
("🐩itt🍨ng", "kitten", "D(0)I(0)D(4)I(4)D(6)"),
Expand All @@ -51,62 +51,62 @@ class DiffTests: XCTestCase {
("Oh Hi", "Hi Oh", "M(03)M(14)M(22)"),
("Hi Oh", "Oh Hi", "M(03)M(14)M(22)"),
("12345", "12435", "M(23)"),
("1362", "31526", "M(01)M(24)I(2)")
("1362", "31526", "M(01)M(24)I(2)"),
]

func testDiffOutputs() {
for expectation in expectations {
XCTAssertEqual(
_test(from: expectation.0, to: expectation.1),
expectation.2)
}
}

func testExtendedDiffOutputs() {
for expectation in extendedExpectations {
XCTAssertEqual(
_testExtended(from: expectation.0, to: expectation.1),
expectation.2)
}
}

// The tests below check efficiency of the algorithm

func testDuplicateTraces() {
for expectation in expectations {
XCTAssertFalse(duplicateTraces(from: expectation.0, to: expectation.1))
}
}

func testTracesOutOfBounds() {
for expectation in expectations {
if tracesOutOfBounds(from: expectation.0, to: expectation.1) != [] {
XCTFail("traces out of bounds for \(expectation.0) -> \(expectation.1)")
}
}
}

func duplicateTraces(from: String, to: String) -> Bool {
let traces = from.characters.diffTraces(to: to.characters)
let tracesSet = Set(traces)
return !(traces.count == tracesSet.count)
}

func tracesOutOfBounds(from: String, to: String) -> [Trace] {
let ac = from.characters
let bc = to.characters
return ac.diffTraces(to: bc)
.filter { $0.to.y > bc.count || $0.to.x > ac.count }
}

func _test(
from: String,
to: String) -> String {
return from
.diff(to: to)
.reduce("") { $0 + $1.debugDescription }
}

func _testExtended(
from: String,
to: String) -> String {
Expand Down
40 changes: 20 additions & 20 deletions DiffTests/ExtendedPatchSortTests.swift
Expand Up @@ -3,29 +3,29 @@ import XCTest
import Diff

class ExtendedPatchSortTests: XCTestCase {

func testDefaultOrder() {
let expectations = [
("gitten", "sitting", "M(0,5)I(0,s)D(4)I(4,i)"),
("Oh Hi", "Hi Oh", "M(0,4)M(0,4)M(0,2)"),
("12345", "12435", "M(2,3)"),
("1362", "31526", "M(0,2)M(1,3)I(2,5)"),
("221", "122", "M(2,0)")
("221", "122", "M(2,0)"),
]

for expectation in expectations {
XCTAssertEqual(
_extendedTest(from: expectation.0, to: expectation.1),
expectation.2)
}
}

func testInsertionDeletionMove() {
let expectations = [
("gitten", "sitting", "I(5,i)I(1,s)D(5)M(0,6)"),
("1362", "31526", "I(3,5)M(0,2)M(1,4)")
("1362", "31526", "I(3,5)M(0,2)M(1,4)"),
]

let sort: ExtendedSortingFunction = { fst, snd in
switch (fst, snd) {
case (.insert, _):
Expand All @@ -38,7 +38,7 @@ class ExtendedPatchSortTests: XCTestCase {
return false
}
}

for expectation in expectations {
XCTAssertEqual(
_extendedTest(
Expand All @@ -48,13 +48,13 @@ class ExtendedPatchSortTests: XCTestCase {
expectation.2)
}
}

func testDeletionMoveInsertion() {
let expectations = [
("gitten", "sitting", "D(4)M(0,4)I(0,s)I(4,i)"),
("1362", "31526", "M(0,2)M(1,3)I(2,5)")
("1362", "31526", "M(0,2)M(1,3)I(2,5)"),
]

let sort: ExtendedSortingFunction = { fst, snd in
switch (fst, snd) {
case (.delete, _):
Expand All @@ -67,7 +67,7 @@ class ExtendedPatchSortTests: XCTestCase {
return false
}
}

for expectation in expectations {
XCTAssertEqual(
_extendedTest(
Expand All @@ -77,18 +77,18 @@ class ExtendedPatchSortTests: XCTestCase {
expectation.2)
}
}

func testRandomStringPermutationRandomPatchSort() {

let sort: ExtendedSortingFunction = { _, _ in arc4random_uniform(2) == 0
}
for _ in 0..<20 {
for _ in 0 ..< 20 {
let string1 = "eakjnrsignambmcbdcdhdkmhkolpdgfedcpgabtldjkaqkoobomuhpepirdcrdrgmrmaefesoiildmtnbronpmmbuuplnfnjgdhadkbmprensshiekknhskognpbknpbepmlakducnfktjeookncjpcnpklfedrebstisalskigsuojkookhbmkdafiaftrkrccupgjapqrigbanfbboapmicabeclhentlabourhtqmlboqctgorajirchesaorsgnigattkdrenquffcutffopbjrebegbfmkeikstqsut"
let string2 = "mdjqtbchphncsjdkjtutagahmdtfcnjliipmqgrhgajsgotcdgidlghithdgrcmfuausmjnbtjghqblaiuldirulhllidbpcpglfbnfbkbddhdskdplsgjjsusractdplajrctgrcebhesbeneidsititlalsqkhliontgpesglkoorjqeniqaetatamneonhbhunqlfkbmfsjallnejhkcfaeapdnacqdtukcuiheiabqpudmgosssabisrrlmhcmpkgerhesqihdnfjmqgfnmulnfkmpqrsghutfsckurr"
let patch = string1.extendedDiff(string2).patch(
from: string1.characters,
to: string2.characters,
sort:sort)
sort: sort)
let result = string1.apply(patch)
XCTAssertEqual(result, string2)
}
Expand All @@ -103,13 +103,13 @@ func _extendedTest(
sortingFunction: ExtendedSortingFunction? = nil) -> String {
guard let sort = sortingFunction else {
return extendedPatch(
from: from.characters,
to: to.characters)
from: from.characters,
to: to.characters)
.reduce("") { $0 + $1.debugDescription }
}
return extendedPatch(
from: from.characters,
to: to.characters,
sort: sort)
from: from.characters,
to: to.characters,
sort: sort)
.reduce("") { $0 + $1.debugDescription }
}
18 changes: 9 additions & 9 deletions DiffTests/PatchApplyTests.swift
Expand Up @@ -3,31 +3,31 @@ import XCTest

class PatchApplyTests: XCTestCase {
func testString() {

let testCases: [(String, String, String)] = [
("", "I(0,A)I(0,B)I(0,C)", "CBA"),
("", "I(0,A)I(1,B)I(1,C)", "ACB"),
("AB", "D(1)I(1,B)I(1,C)", "ACB"),
("AB", "I(1,B)D(0)I(1,C)", "BCB"),
("A", "I(0,B)D(0)", "A")
("A", "I(0,B)D(0)", "A"),
]
testCases.forEach { (seed, patchString, result) in

testCases.forEach { seed, patchString, result in
XCTAssertEqual(seed.apply(stringPatch(from: patchString)), result)
}
}

func testCollection() {

let testCases: [([Int], String, [Int])] = [
([], "I(0,0)I(0,1)I(0,2)", [2, 1, 0]),
([], "I(0,0)I(1,1)I(1,2)", [0, 2, 1]),
([0, 1], "D(1)I(1,1)I(1,2)", [0, 2, 1]),
([0, 1], "I(1,1)D(0)I(1,2)", [1, 2, 1]),
([0], "I(0,1)D(0)", [0])
([0], "I(0,1)D(0)", [0]),
]
testCases.forEach { (seed, patchString, result) in

testCases.forEach { seed, patchString, result in
XCTAssertEqual(seed.apply(intPatch(from: patchString)), result)
}
}
Expand Down
55 changes: 26 additions & 29 deletions DiffTests/PatchSortTests.swift
Expand Up @@ -4,7 +4,7 @@ import XCTest
class PatchTests: XCTestCase {

func testDefaultOrder() {

let defaultOrder = [
("kitten", "sitting", "D(0)I(0,s)D(4)I(4,i)I(6,g)"),
("🐩itt🍨ng", "kitten", "D(0)I(0,k)D(4)I(4,e)D(6)"),
Expand All @@ -19,18 +19,18 @@ class PatchTests: XCTestCase {
("", "", ""),
("Oh Hi", "Hi Oh", "D(0)D(0)D(0)I(2, )I(3,O)I(4,h)"),
("1362", "31526", "D(0)D(1)I(1,1)I(2,5)I(4,6)"),
("1234b2", "ab", "D(0)D(0)D(0)D(0)I(0,a)D(2)")
("1234b2", "ab", "D(0)D(0)D(0)D(0)I(0,a)D(2)"),
]

for expectation in defaultOrder {
XCTAssertEqual(
_test(from: expectation.0, to: expectation.1),
expectation.2)
}
}

func testInsertionsFirst() {

let insertionsFirst = [
("kitten", "sitting", "I(1,s)I(6,i)I(8,g)D(0)D(4)"),
("🐩itt🍨ng", "kitten", "I(1,k)I(6,e)D(0)D(4)D(6)"),
Expand All @@ -44,9 +44,9 @@ class PatchTests: XCTestCase {
("1234", "1234", ""),
("", "", ""),
("Oh Hi", "Hi Oh", "I(5, )I(6,O)I(7,h)D(0)D(0)D(0)"),
("1362", "31526", "I(3,1)I(4,5)I(6,6)D(0)D(1)")
("1362", "31526", "I(3,1)I(4,5)I(6,6)D(0)D(1)"),
]

let insertionsFirstSort = { (element1: Diff.Element, element2: Diff.Element) -> Bool in
switch (element1, element2) {
case (.insert(let at1), .insert(let at2)):
Expand All @@ -60,7 +60,7 @@ class PatchTests: XCTestCase {
default: fatalError()
}
}

for expectation in insertionsFirst {
XCTAssertEqual(
_test(
Expand All @@ -70,9 +70,9 @@ class PatchTests: XCTestCase {
expectation.2)
}
}

func testDeletionsFirst() {

let deletionsFirst = [
("kitten", "sitting", "D(0)D(3)I(0,s)I(4,i)I(6,g)"),
("🐩itt🍨ng", "kitten", "D(0)D(3)D(4)I(0,k)I(4,e)"),
Expand All @@ -86,9 +86,9 @@ class PatchTests: XCTestCase {
("1234", "1234", ""),
("", "", ""),
("Oh Hi", "Hi Oh", "D(0)D(0)D(0)I(2, )I(3,O)I(4,h)"),
("1362", "31526", "D(0)D(1)I(1,1)I(2,5)I(4,6)")
("1362", "31526", "D(0)D(1)I(1,1)I(2,5)I(4,6)"),
]

let deletionsFirstSort = { (element1: Diff.Element, element2: Diff.Element) -> Bool in
switch (element1, element2) {
case (.insert(let at1), .insert(let at2)):
Expand All @@ -102,7 +102,7 @@ class PatchTests: XCTestCase {
default: fatalError()
}
}

for expectation in deletionsFirst {
XCTAssertEqual(
_test(
Expand All @@ -112,35 +112,35 @@ class PatchTests: XCTestCase {
expectation.2)
}
}

func testRandomStringPermutationRandomPatchSort() {

let sort = { (element1: Diff.Element, element2: Diff.Element) -> Bool in
return arc4random_uniform(2) == 0
}
for _ in 0..<200 {
for _ in 0 ..< 200 {
let randomString = randomAlphaNumericString(length: 30)
let permutation = randomAlphaNumericString(length: 30)
let patch = randomString.diff(to: permutation).patch(from: randomString.characters, to: permutation.characters, sort:sort)
let patch = randomString.diff(to: permutation).patch(from: randomString.characters, to: permutation.characters, sort: sort)
let result = randomString.apply(patch)
XCTAssertEqual(result, permutation)
}
}
}

func randomAlphaNumericString(length: Int) -> String {

let allowedChars = "abcdefghijklmnopqrstu"
let allowedCharsCount = UInt32(allowedChars.characters.count)
var randomString = ""
for _ in 0..<length {

for _ in 0 ..< length {
let randomNum = Int(arc4random_uniform(allowedCharsCount))
let randomIndex = allowedChars.index(allowedChars.startIndex, offsetBy: randomNum)
let newCharacter = allowedChars[randomIndex]
randomString += String(newCharacter)
}

return randomString
}

Expand All @@ -152,16 +152,13 @@ func _test(
sortingFunction: SortingFunction? = nil) -> String {
if let sort = sortingFunction {
return patch(
from: from.characters,
to: to.characters,
sort: sort)
from: from.characters,
to: to.characters,
sort: sort)
.reduce("") { $0 + $1.debugDescription }
}
return patch(
from: from.characters,
to: to.characters)
from: from.characters,
to: to.characters)
.reduce("") { $0 + $1.debugDescription }
}



0 comments on commit c0506cb

Please sign in to comment.