Skip to content

Commit ca9c9ab

Browse files
author
Yuya Horita
committed
🔨 Add Test, Fix snakeCount
1 parent e673cb2 commit ca9c9ab

File tree

5 files changed

+123
-35
lines changed

5 files changed

+123
-35
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#
33
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
44

5+
**/.DS_Store
6+
57
## Build generated
68
build/
79
DerivedData/

DifferenceAlgorithmComparison.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
8CF98E712033286000EE3096 /* MyersTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CF98E702033286000EE3096 /* MyersTest.swift */; };
2222
8CF98E742033398100EE3096 /* originalMyers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8CF98E732033398100EE3096 /* originalMyers.swift */; };
2323
CCCD5211CE6C9968A1E8D310 /* Pods_DifferenceAlgorithmComparisonTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B94B832DF79C22777907EF04 /* Pods_DifferenceAlgorithmComparisonTests.framework */; };
24+
FABB8F932054CF5500956C11 /* WuTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = FABB8F922054CF5500956C11 /* WuTest.swift */; };
2425
/* End PBXBuildFile section */
2526

2627
/* Begin PBXContainerItemProxy section */
@@ -56,6 +57,7 @@
5657
9C4CEF1502477842CD0E679D /* Pods-DifferenceAlgorithmComparisonTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DifferenceAlgorithmComparisonTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-DifferenceAlgorithmComparisonTests/Pods-DifferenceAlgorithmComparisonTests.release.xcconfig"; sourceTree = "<group>"; };
5758
B94B832DF79C22777907EF04 /* Pods_DifferenceAlgorithmComparisonTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DifferenceAlgorithmComparisonTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5859
C5ACEFB3D0998062611E351E /* Pods-DifferenceAlgorithmComparison.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DifferenceAlgorithmComparison.debug.xcconfig"; path = "Pods/Target Support Files/Pods-DifferenceAlgorithmComparison/Pods-DifferenceAlgorithmComparison.debug.xcconfig"; sourceTree = "<group>"; };
60+
FABB8F922054CF5500956C11 /* WuTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WuTest.swift; sourceTree = "<group>"; };
5961
/* End PBXFileReference section */
6062

6163
/* Begin PBXFrameworksBuildPhase section */
@@ -129,6 +131,7 @@
129131
8C9CCD31201202C60055DEFE /* HeckelTest.swift */,
130132
8C9CCD22201033480055DEFE /* Info.plist */,
131133
8CF98E702033286000EE3096 /* MyersTest.swift */,
134+
FABB8F922054CF5500956C11 /* WuTest.swift */,
132135
8C0C117620345D84003AADCA /* Utility.swift */,
133136
);
134137
path = DifferenceAlgorithmComparisonTests;
@@ -372,6 +375,7 @@
372375
8CF98E712033286000EE3096 /* MyersTest.swift in Sources */,
373376
8C9CCD32201202C60055DEFE /* HeckelTest.swift in Sources */,
374377
8C0C117720345D84003AADCA /* Utility.swift in Sources */,
378+
FABB8F932054CF5500956C11 /* WuTest.swift in Sources */,
375379
);
376380
runOnlyForDeploymentPostprocessing = 0;
377381
};

DifferenceAlgorithmComparison/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private extension ViewController {
7676
//print(Myers.diff(from:k, to: k))
7777

7878
let a = [1, 2, 3, 4, 5]
79-
let b = [1, 2, 3, 4, 5, 6, 7]
79+
let b = [1, 2, 3, 4, 5, 6]
8080
print(Myers.diff(from: a, to: b))
8181
print(Wu.diff(from: a, to: b))
8282
//print(originalMyers.diff(from: [], to: []))

DifferenceAlgorithmComparison/Wu.swift

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -107,33 +107,34 @@ private extension Wu {
107107
}
108108

109109
for p in 0...fromCount {
110-
let lowerRange = -p...delta - 1
111-
112-
for k in lowerRange {
113-
let index = k + fromCount
114-
var fromVertice = Vertice.vertice(x: 0, y: 0)
115-
var toVertice = Vertice.vertice(x: fromCount, y: toCount)
116-
var script = Script.sourceScript
117-
var _y = 0
118-
119-
// moving bottom, means delete script
120-
// thinking about it on Wu's EditGraph, not myers'
121-
if p == 0 && k == 0 {}
122-
else if furthestReaching[index - 1] + 1 < furthestReaching[index + 1] {
123-
_y = furthestReaching[index + 1]
124-
fromVertice = .vertice(x: _y - k, y: _y)
125-
toVertice = .vertice(x: _y - k + 1, y: _y)
126-
script = .delete(at: _y - k)
127-
} else {
128-
_y = furthestReaching[index - 1] + 1
129-
fromVertice = .vertice(x: _y - k, y: _y - 1)
130-
toVertice = .vertice(x: _y - k, y: _y)
131-
script = Script.insert(from: _y, to: _y - k)
132-
}
133-
134-
furthestReaching[index] = snake(k, _y)
135-
if p != 0 || k != 0 {
136-
path.append((P: p, from: fromVertice, to: toVertice, script: script, snakeCount: 100))
110+
if delta > 0 {
111+
let lowerRange = -p...delta - 1
112+
for k in lowerRange {
113+
let index = k + fromCount
114+
var fromVertice = Vertice.vertice(x: 0, y: 0)
115+
var toVertice = Vertice.vertice(x: fromCount, y: toCount)
116+
var script = Script.sourceScript
117+
var _y = 0
118+
119+
// moving bottom, means delete script
120+
// thinking about it on Wu's EditGraph, not myers'
121+
if p == 0 && k == 0 {}
122+
else if furthestReaching[index - 1] + 1 < furthestReaching[index + 1] {
123+
_y = furthestReaching[index + 1]
124+
fromVertice = .vertice(x: _y - k, y: _y)
125+
toVertice = .vertice(x: _y - k + 1, y: _y)
126+
script = .delete(at: _y - k)
127+
} else {
128+
_y = furthestReaching[index - 1] + 1
129+
fromVertice = .vertice(x: _y - k, y: _y - 1)
130+
toVertice = .vertice(x: _y - k, y: _y)
131+
script = Script.insert(from: _y - 1, to: _y - k - 1)
132+
}
133+
134+
furthestReaching[index] = snake(k, _y)
135+
if p != 0 || k != 0 {
136+
path.append((P: p, from: fromVertice, to: toVertice, script: script, snakeCount: furthestReaching[index] - _y))
137+
}
137138
}
138139
}
139140

@@ -159,7 +160,7 @@ private extension Wu {
159160
toVertice = .vertice(x: _y - k, y: _y)
160161
script = Script.insert(from: _y, to: _y - k)
161162
}
162-
//let m = max(furthestReaching[index - 1] + 1, furthestReaching[index + 1])
163+
163164
furthestReaching[index] = snake(k, _y)
164165
path.append((P: p, from: fromVertice, to: toVertice, script: script, snakeCount: 0))
165166
}
@@ -180,17 +181,14 @@ private extension Wu {
180181
_y = furthestReaching[deltaIndex - 1] + 1
181182
fromVertice = .vertice(x: _y - delta, y: _y - 1)
182183
toVertice = .vertice(x: _y - delta, y: _y)
183-
script = Script.insert(from: _y, to: _y - delta)
184+
script = Script.insert(from: _y - 1, to: _y - delta - 1)
184185
}
185186

186187
furthestReaching[deltaIndex] = snake(delta, _y)
187-
path.append((P: p, from: fromVertice, to: toVertice, script: script, snakeCount: 100))
188-
188+
path.append((P: p, from: fromVertice, to: toVertice, script: script, snakeCount: furthestReaching[deltaIndex] - _y))
189189

190190
if furthestReaching[deltaIndex] == toCount {
191-
print(2 * p + delta)
192-
print(path)
193-
return []
191+
return path
194192
}
195193
}
196194

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// WuTest.swift
3+
// DifferenceAlgorithmComparisonTests
4+
//
5+
// Created by Yuya Horita on 2018/03/11.
6+
// Copyright © 2018 yuyahorita. All rights reserved.
7+
//
8+
@testable import DifferenceAlgorithmComparison
9+
10+
import XCTest
11+
12+
final class WuTest: XCTestCase {
13+
func testSameAsDiffer() {
14+
let expectations = [
15+
("kitten", "sitting", "I(6, 5)I(4, 4)D(4)I(0, 0)D(0)"),
16+
("🐩itt🍨ng", "kitten", "D(6)I(4, 4)D(4)I(0, 0)D(0)"),
17+
("1234", "ABCD", "I(3, 3)I(2, 3)I(1, 3)I(0, 3)D(3)D(2)D(1)D(0)"),
18+
("1234", "", "D(0)D(1)D(2)D(3)"),
19+
("", "1234", "IH(3)IH(2)IH(1)IH(0)"),
20+
("Hi", "Oh Hi", "IH(2)IH(1)IH(0)"),
21+
("Hi", "Hi O", "I(3, 1)I(2, 1)"),
22+
("Oh Hi", "Hi", "D(2)D(1)D(0)"),
23+
("Hi O", "Hi", "D(3)D(2)"),
24+
("Wojtek", "Wojciech", "I(7, 5)I(6, 5)D(5)I(4, 3)I(3, 3)D(3)"),
25+
("1234", "1234", ""),
26+
("", "", ""),
27+
("Oh Hi", "Hi Oh", "I(4, 4)I(3, 4)I(2, 4)D(2)D(1)D(0)"),
28+
("1362", "31526", "I(4, 3)I(2, 2)I(1, 2)D(2)D(0)")
29+
]
30+
31+
expectations.forEach { from, to, expectation in
32+
XCTAssertTrue(expectation == Wu.diff(from: .init(from), to: .init(to)).reduce("") { $0 + $1.description })
33+
}
34+
}
35+
36+
func testAccuracy() {
37+
let patterns: [(from: [Int], to: [Int], expect: [Wu<Int>.Script])] = [
38+
// normal insert
39+
(from: [1, 2, 3, 4, 5], to: [1, 2, 3, 4, 5, 6], expect: [.insert(from: 5, to: 4)]),
40+
// empty
41+
(from: [], to: [], expect: []),
42+
// delete to empty
43+
(from: [1], to: [], expect: [.delete(at: 0)]),
44+
// empty to insert
45+
(from: [], to: [1], expect: [.insertToHead(from: 0)]),
46+
// exchange
47+
(from: [0], to: [1], expect:[
48+
.insert(from: 0, to: 0),
49+
.delete(at: 0)
50+
]
51+
),
52+
// same sequence
53+
(from: [1, 2, 3, 4, 5], to: [1, 2, 3, 4, 5], expect: []),
54+
// random, the other having several same symbols
55+
(from: [1, 2, 3, 4, 5], to: [2, 6, 4, 5, 6, 7], expect: [
56+
.insert(from: 5, to: 4),
57+
.insert(from: 4, to: 4),
58+
.insert(from: 1, to: 2),
59+
.delete(at: 2),
60+
.delete(at: 0)
61+
]
62+
),
63+
// random, both having several same symbols
64+
(from: [1, 1, 3, 2, 5], to: [1, 6, 1, 3, 5], expect: [
65+
.delete(at: 3),
66+
.insert(from: 1, to: 0)
67+
]
68+
)]
69+
70+
patterns.forEach { old, new, expect in
71+
XCTAssertTrue(expect == Wu.diff(from: old, to: new))
72+
}
73+
}
74+
75+
let (old, new) = generate(count: 20000, removeRange: 0..<0, addRange: 19999..<21000)
76+
77+
func testPerformanceOriginalModel() {
78+
measure { _ = Wu.diff(from: old, to: new) }
79+
}
80+
81+
func testPerformanceSnakeCountModel() {
82+
measure { _ = Wu.diff(from: old, to: new) }
83+
}
84+
}

0 commit comments

Comments
 (0)