From 1639c1e2c36aa0a0d8a53de71bb99b66e3337931 Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Wed, 7 Apr 2021 11:42:28 -0300 Subject: [PATCH] [tests] Ensure correct implementation of hash for ChunkedByCount (#123) --- Sources/Algorithms/Chunked.swift | 5 +++-- Tests/SwiftAlgorithmsTests/ChunkedTests.swift | 12 ++++++++++++ Tests/SwiftAlgorithmsTests/TestUtilities.swift | 13 +++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Sources/Algorithms/Chunked.swift b/Sources/Algorithms/Chunked.swift index 14d0894..e64fde8 100644 --- a/Sources/Algorithms/Chunked.swift +++ b/Sources/Algorithms/Chunked.swift @@ -515,12 +515,13 @@ extension ChunkedByCount: Equatable where Base: Equatable {} // Since we have another stored property of type `Index` on the // collection, synthesis of `Hashble` conformace would require // a `Base.Index: Hashable` constraint, so we implement the hasher -// only in terms of `base`. Since the computed index is based on it, -// it should not make a difference here. +// only in terms of `base` and `chunkCount`. Since the computed +// index is based on it, it should not make a difference here. extension ChunkedByCount: Hashable where Base: Hashable { @inlinable public func hash(into hasher: inout Hasher) { hasher.combine(base) + hasher.combine(chunkCount) } } extension ChunkedByCount.Index: Hashable where Base.Index: Hashable {} diff --git a/Tests/SwiftAlgorithmsTests/ChunkedTests.swift b/Tests/SwiftAlgorithmsTests/ChunkedTests.swift index 10fcc13..aa78a7a 100644 --- a/Tests/SwiftAlgorithmsTests/ChunkedTests.swift +++ b/Tests/SwiftAlgorithmsTests/ChunkedTests.swift @@ -145,4 +145,16 @@ final class ChunkedTests: XCTestCase { validateIndexTraversals(chunks) } } + + func testChunksOfCountHash() { + let collection1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + let collection2 = [1, 2, 3, 4, 5] + + XCTAssertEqualHashValue( + collection1.chunks(ofCount: 1), collection1.chunks(ofCount: 1)) + XCTAssertNotEqualHashValue( + collection1.chunks(ofCount: 1), collection2.chunks(ofCount: 1)) + XCTAssertNotEqualHashValue( + collection1.chunks(ofCount: 2), collection2.chunks(ofCount: 4)) + } } diff --git a/Tests/SwiftAlgorithmsTests/TestUtilities.swift b/Tests/SwiftAlgorithmsTests/TestUtilities.swift index b4068dd..26f005a 100644 --- a/Tests/SwiftAlgorithmsTests/TestUtilities.swift +++ b/Tests/SwiftAlgorithmsTests/TestUtilities.swift @@ -203,6 +203,19 @@ func XCTAssertEqualHashValue( ) } +/// Asserts that two hashable instances don't produce the same hash value. +func XCTAssertNotEqualHashValue( + _ expression1: @autoclosure () throws -> T, + _ expression2: @autoclosure () throws -> U, + _ message: @autoclosure () -> String = "", + file: StaticString = #file, line: UInt = #line +) { + XCTAssertNotEqual( + hash(try expression1()), hash(try expression2()), + message(), file: file, line: line + ) +} + /// Tests that all index traversal methods behave as expected. /// /// Verifies the correctness of the implementations of `startIndex`, `endIndex`,