Skip to content

Commit

Permalink
Fix behavior when hashValue is duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1028 committed Aug 27, 2018
1 parent 42a7d4c commit 39d2092
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Sources/Algorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,7 @@ private struct TableKey<T: Hashable>: Hashable {

static func == (lhs: TableKey, rhs: TableKey) -> Bool {
return lhs.hashValue == rhs.hashValue
|| lhs.pointer.distance(to: rhs.pointer) == 0
|| lhs.pointer.pointee == rhs.pointer.pointee
&& (lhs.pointer.distance(to: rhs.pointer) == 0 || lhs.pointer.pointee == rhs.pointer.pointee)
}
}

Expand Down
36 changes: 36 additions & 0 deletions Tests/AlgorithmTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,42 @@ extension AlgorithmTestCase {
]
)
}

func testSameHashValue() {
struct A: Hashable, Differentiable {
let hashValue = 0
let actualValue: Int

init(_ actualValue: Int) {
self.actualValue = actualValue
}

func isContentEqual(to source: A) -> Bool {
return actualValue == source.actualValue
}
}

let section = 0

let source = [A(0), A(1)]
let target = [A(0), A(2)]

XCTAssertExactDifferences(
source: source,
target: target,
section: section,
expected: [
Changeset(
data: [A(0)],
elementDeleted: [ElementPath(element: 1, section: section)]
),
Changeset(
data: target,
elementInserted: [ElementPath(element: 1, section: section)]
)
]
)
}
}

/// Test algorithm for sectioned collection.
Expand Down

0 comments on commit 39d2092

Please sign in to comment.