forked from ra1028/DifferenceKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeasurementTest.swift
51 lines (43 loc) 路 1.64 KB
/
MeasurementTest.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
import XCTest
import DifferenceKit
final class MeasurementTestCase: XCTestCase {
func testMeasureAlgorithmForLinearCollection() {
let source = Array(1...100000)
let target = source.mutated(removeAt: IndexSet(10000..<20000), insertAt: IndexSet(30000..<40000))
measure {
_ = StagedChangeset(source: source, target: target)
}
}
func testMeasureAlgorithmForSectionedCollection() {
let source: [ArraySection<D, Int>] = D.allCases.enumerated().map { o, d in
let lowerBound = o * 30000 + 1
let upperBound = lowerBound + 20000
return ArraySection(model: d, elements: Array(lowerBound...upperBound))
}
let target = source.map { section in
ArraySection(
model: section.model,
elements: section.elements.mutated(removeAt: IndexSet(2000..<5000), insertAt: IndexSet(5000..<8000))
)
}
measure {
_ = StagedChangeset(source: source, target: target)
}
}
}
private extension RangeReplaceableCollection where Element == Int {
func mutated(removeAt: IndexSet, insertAt: IndexSet) -> Self {
var subject = ContiguousArray(self)
var max = subject.max(by: <) ?? 0
for range in removeAt.rangeView.reversed() {
subject.removeSubrange(range)
}
for range in insertAt.rangeView {
let lowerBound = max + 1
let upperBound = lowerBound + range.count
max += range.count
subject.insert(contentsOf: lowerBound..<upperBound, at: range.lowerBound)
}
return Self(subject)
}
}