Skip to content

Commit

Permalink
Merge pull request #96 from Infomaniak/xor-collection-id
Browse files Browse the repository at this point in the history
feat: Identifiable collection
  • Loading branch information
PhilippeWeidmann authored Jan 18, 2024
2 parents a1341fb + c826980 commit 162b153
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Sources/InfomaniakCore/Extensions/Collection+Identity.swift
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 Tests/InfomaniakCoreTests/Extensions/UTCollection+Identity.swift
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")
}
}

0 comments on commit 162b153

Please sign in to comment.