-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from Infomaniak/xor-collection-id
feat: Identifiable collection
- Loading branch information
Showing
2 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
Sources/InfomaniakCore/Extensions/Collection+Identity.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
Infomaniak Core - iOS | ||
Copyright (C) 2023 Infomaniak Network SA | ||
|
||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
@available(iOS 13.0, *) | ||
public extension Collection where Element: Identifiable { | ||
/// Compute a stable id for the given collection | ||
func collectionId(baseId: AnyHashable? = nil) -> Int { | ||
var hasher = Hasher() | ||
hasher.combine(baseId) | ||
forEach { hasher.combine($0.id) } | ||
return hasher.finalize() | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
Tests/InfomaniakCoreTests/Extensions/UTCollection+Identity.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
Infomaniak Core - iOS | ||
Copyright (C) 2023 Infomaniak Network SA | ||
|
||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import InfomaniakCore | ||
import XCTest | ||
|
||
extension Int: Identifiable { | ||
public var id: Int { | ||
return self | ||
} | ||
} | ||
|
||
@available(iOS 13.0, *) | ||
final class UTCollectionIdentity: XCTestCase { | ||
func testIntSameArraySameHash() { | ||
// GIVEN | ||
let lhsArray = [1, 2, 3, 4] | ||
let rhsArray = [1, 2, 3, 4] | ||
|
||
// WHEN | ||
let lhsId = lhsArray.collectionId() | ||
let rhsId = rhsArray.collectionId() | ||
|
||
// THEN | ||
XCTAssertEqual(lhsId, rhsId, "We expect the ids to be the same") | ||
} | ||
|
||
func testIntSameArraySameHashWithBase() { | ||
// GIVEN | ||
let lhsArray = [1, 2, 3, 4] | ||
let rhsArray = [1, 2, 3, 4] | ||
|
||
// WHEN | ||
let lhsId = lhsArray.collectionId(baseId: 10) | ||
let rhsId = rhsArray.collectionId(baseId: 10) | ||
|
||
// THEN | ||
XCTAssertEqual(lhsId, rhsId, "We expect the ids to be the same") | ||
} | ||
|
||
func testIntReversedArrayDifferentHash() { | ||
// GIVEN | ||
let lhsArray = [1, 2, 3, 4] | ||
let rhsArray = [1, 2, 3, 4].reversed() | ||
|
||
// WHEN | ||
let lhsId = lhsArray.collectionId() | ||
let rhsId = rhsArray.collectionId() | ||
|
||
// THEN | ||
XCTAssertNotEqual(lhsId, rhsId, "We expect the ids to not be the same") | ||
} | ||
|
||
func testIntDifferentArrayDifferentHash() { | ||
// GIVEN | ||
let lhsArray = [1, 2, 3, 4] | ||
let rhsArray = [1, 2, 3, 5] | ||
|
||
// WHEN | ||
let lhsId = lhsArray.collectionId() | ||
let rhsId = rhsArray.collectionId() | ||
|
||
// THEN | ||
XCTAssertNotEqual(lhsId, rhsId, "We expect the ids to not be the same") | ||
} | ||
|
||
func testIntDifferentArrayDifferentHashWithBase() { | ||
// GIVEN | ||
let lhsArray = [1, 2, 3, 4] | ||
let rhsArray = [1, 2, 3, 5] | ||
|
||
// WHEN | ||
let lhsId = lhsArray.collectionId(baseId: 10) | ||
let rhsId = rhsArray.collectionId(baseId: 10) | ||
|
||
// THEN | ||
XCTAssertNotEqual(lhsId, rhsId, "We expect the ids to not be the same") | ||
} | ||
|
||
func testIntDifferentArrayDifferentHashWithDifferentBase() { | ||
// GIVEN | ||
let lhsArray = [1, 2, 3, 4] | ||
let rhsArray = [1, 2, 3, 5] | ||
|
||
// WHEN | ||
let lhsId = lhsArray.collectionId(baseId: 10) | ||
let rhsId = rhsArray.collectionId(baseId: 11) | ||
|
||
// THEN | ||
XCTAssertNotEqual(lhsId, rhsId, "We expect the ids to not be the same") | ||
} | ||
} |