/
DiffTests.swift
117 lines (103 loc) · 3.55 KB
/
DiffTests.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
//
// DiffTests.swift
// DiffTests
//
// Created by Wojciech Czekalski on 03.07.2016.
// Copyright © 2016 wczekalski. All rights reserved.
//
import XCTest
@testable import Diff
extension Trace: Hashable {
public var hashValue: Int {
return (((51 + from.x.hashValue) * 51 + from.y.hashValue) * 51 + to.x.hashValue) * 51 + to.y.hashValue
}
}
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)"),
("1234", "ABCD", "D(0)D(1)D(2)D(3)I(0)I(1)I(2)I(3)"),
("1234", "", "D(0)D(1)D(2)D(3)"),
("", "1234", "I(0)I(1)I(2)I(3)"),
("Hi", "Oh Hi", "I(0)I(1)I(2)"),
("Hi", "Hi O", "I(2)I(3)"),
("Oh Hi", "Hi", "D(0)D(1)D(2)"),
("Hi O", "Hi", "D(2)D(3)"),
("Wojtek", "Wojciech", "D(3)I(3)I(4)D(5)I(6)I(7)"),
("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)"),
]
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)"),
("1234", "ABCD", "D(0)D(1)D(2)D(3)I(0)I(1)I(2)I(3)"),
("1234", "", "D(0)D(1)D(2)D(3)"),
("", "1234", "I(0)I(1)I(2)I(3)"),
("Hi", "Oh Hi", "I(0)I(1)I(2)"),
("Hi", "Hi O", "I(2)I(3)"),
("Oh Hi", "Hi", "D(0)D(1)D(2)"),
("Hi O", "Hi", "D(2)D(3)"),
("Wojtek", "Wojciech", "D(3)I(3)I(4)D(5)I(6)I(7)"),
("1234", "1234", ""),
("", "", ""),
("gitten", "sitting", "M(06)I(0)D(4)I(4)"),
("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)"),
]
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 {
return from
.extendedDiff(to)
.reduce("") { $0 + $1.debugDescription }
}
}