-
Notifications
You must be signed in to change notification settings - Fork 124
/
ExtendedPatchSortTests.swift
115 lines (101 loc) · 3.81 KB
/
ExtendedPatchSortTests.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
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)"),
]
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)"),
]
let sort: ExtendedSortingFunction = { fst, snd in
switch (fst, snd) {
case (.insert, _):
return true
case (.delete, .insert):
return false
case (.delete, _):
return true
case (.move, _):
return false
}
}
for expectation in expectations {
XCTAssertEqual(
_extendedTest(
from: expectation.0,
to: expectation.1,
sortingFunction: sort),
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)"),
]
let sort: ExtendedSortingFunction = { fst, snd in
switch (fst, snd) {
case (.delete, _):
return true
case (.insert, _):
return false
case (.move, .insert):
return true
case (.move, _):
return false
}
}
for expectation in expectations {
XCTAssertEqual(
_extendedTest(
from: expectation.0,
to: expectation.1,
sortingFunction: sort),
expectation.2)
}
}
func testRandomStringPermutationRandomPatchSort() {
let sort: ExtendedSortingFunction = { _, _ in arc4random_uniform(2) == 0
}
for _ in 0 ..< 20 {
let string1 = "eakjnrsignambmcbdcdhdkmhkolpdgfedcpgabtldjkaqkoobomuhpepirdcrdrgmrmaefesoiildmtnbronpmmbuuplnfnjgdhadkbmprensshiekknhskognpbknpbepmlakducnfktjeookncjpcnpklfedrebstisalskigsuojkookhbmkdafiaftrkrccupgjapqrigbanfbboapmicabeclhentlabourhtqmlboqctgorajirchesaorsgnigattkdrenquffcutffopbjrebegbfmkeikstqsut"
let string2 = "mdjqtbchphncsjdkjtutagahmdtfcnjliipmqgrhgajsgotcdgidlghithdgrcmfuausmjnbtjghqblaiuldirulhllidbpcpglfbnfbkbddhdskdplsgjjsusractdplajrctgrcebhesbeneidsititlalsqkhliontgpesglkoorjqeniqaetatamneonhbhunqlfkbmfsjallnejhkcfaeapdnacqdtukcuiheiabqpudmgosssabisrrlmhcmpkgerhesqihdnfjmqgfnmulnfkmpqrsghutfsckurr"
let patch = string1.extendedDiff(string2).patch(
from: string1.characters,
to: string2.characters,
sort: sort)
let result = string1.apply(patch)
XCTAssertEqual(result, string2)
}
}
}
typealias ExtendedSortingFunction = (ExtendedDiff.Element, ExtendedDiff.Element) -> Bool
func _extendedTest(
from: String,
to: String,
sortingFunction: ExtendedSortingFunction? = nil) -> String {
guard let sort = sortingFunction else {
return extendedPatch(
from: from.characters,
to: to.characters)
.reduce("") { $0 + $1.debugDescription }
}
return extendedPatch(
from: from.characters,
to: to.characters,
sort: sort)
.reduce("") { $0 + $1.debugDescription }
}