Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] Arrays: Include the count as a discriminator value #16030

Merged
merged 1 commit into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -2270,6 +2270,7 @@ extension ${Self}: Hashable where Element: Hashable {

@inlinable // FIXME(sil-serialize-all)
public func _hash(into hasher: inout _Hasher) {
hasher.combine(count) // discriminator
for element in self {
hasher.combine(element)
}
Expand Down
64 changes: 49 additions & 15 deletions validation-test/stdlib/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,55 @@ ArrayTestSuite.test("init(repeating:count:)") {
}

ArrayTestSuite.test("Hashable") {
let a1: Array<Int> = [1, 2, 3]
let a2: Array<Int> = [1, 3, 2]
let a3: Array<Int> = [3, 1, 2]
let a4: Array<Int> = [1, 2]
let a5: Array<Int> = [1]
let a6: Array<Int> = []
let a7: Array<Int> = [1, 1, 1]
checkHashable([a1, a2, a3, a4, a5, a6, a7], equalityOracle: { $0 == $1 })

let aa1: Array<Array<Int>> = [[], [1], [1, 2], [2, 1]]
let aa2: Array<Array<Int>> = [[], [1], [2, 1], [2, 1]]
let aa3: Array<Array<Int>> = [[1], [], [2, 1], [2, 1]]
let aa4: Array<Array<Int>> = [[1], [], [2, 1], [2]]
let aa5: Array<Array<Int>> = [[1], [], [2, 1]]
checkHashable([aa1, aa2, aa3, aa4, aa5], equalityOracle: { $0 == $1 })
let a1: [Array<Int>] = [
[1, 2, 3],
[1, 3, 2],
[3, 1, 2],
[1, 2],
[1],
[],
[1, 1, 1]
]
checkHashable(a1, equalityOracle: { $0 == $1 })

let a2: [Array<Array<Int>>] = [
[[], [1], [1, 2], [2, 1]],
[[], [1], [2, 1], [2, 1]],
[[1], [], [2, 1], [2, 1]],
[[1], [], [2, 1], [2]],
[[1], [], [2, 1]]
]
checkHashable(a2, equalityOracle: { $0 == $1 })

// These arrays share the same sequence of leaf integers, but they must
// still all hash differently.
let a3: [Array<Array<Int>>] = [
// Grouping changes must perturb the hash.
[[1], [2], [3], [4], [5]],
[[1, 2], [3], [4], [5]],

[[1], [2, 3], [4], [5]],
[[1], [2], [3, 4], [5]],
[[1], [2], [3], [4, 5]],

[[1, 2, 3], [4], [5]],
[[1], [2, 3, 4], [5]],
[[1], [2], [3, 4, 5]],

[[1, 2, 3, 4], [5]],
[[1], [2, 3, 4, 5]],

[[1, 2], [3, 4], [5]],
[[1], [2, 3], [4, 5]],
[[1, 2, 3, 4, 5]],

// Empty arrays must perturb the hash.
[[], [1], [2], [3], [4], [5]],
[[1], [], [2], [3], [4], [5]],
[[1], [2], [3], [4], [5], []],
[[1], [], [], [2], [3], [], [4], [], [5]],
]
checkHashable(a3, equalityOracle: { $0 == $1 })
}


Expand Down