Skip to content

[test] Merge hash determinism/randomisation tests #15269

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

Merged
merged 1 commit into from
Mar 16, 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
6 changes: 0 additions & 6 deletions validation-test/stdlib/Hashing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,4 @@ HashingTestSuite.test("_Hasher/DefaultKey") {
expectEqual(customHasher.finalize(), defaultHash)
}

HashingTestSuite.test("_Hasher/determinism") {
// By defaults, tests are configured to run with deterministic hashing.
expectTrue(_Hasher._isDeterministic)
expectEqual((0, 0), _Hasher._seed)
}

runAllTests()
41 changes: 25 additions & 16 deletions validation-test/stdlib/HashingRandomization.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -module-name main %s -o %t/hash
// RUN: (export -n %env-SWIFT_DETERMINISTIC_HASHING; %target-run %t/hash && %target-run %t/hash) | %FileCheck %s
// REQUIRES: executable_test
// RUN: (export -n %env-SWIFT_DETERMINISTIC_HASHING; %target-run %t/hash && %target-run %t/hash) | %FileCheck --check-prefixes=RANDOM %s
// RUN: (export %env-SWIFT_DETERMINISTIC_HASHING=1; %target-run %t/hash && %target-run %t/hash) | %FileCheck --check-prefixes=STABLE %s

// This check verifies that the hash seed is randomly generated on every
// execution of a Swift program. There is a minuscule chance that the same seed
// is generated on two separate executions; however, a test failure here is more
// likely to indicate an issue with the random number generator or the testing
// environment.
// execution of a Swift program unless the SWIFT_DETERMINISTIC_HASHING
// environment variable is set.

print("Deterministic: \(_Hasher._isDeterministic)")
print("Seed: \(_Hasher._seed)")
print("Hash values: <\(0.hashValue), \(1.hashValue)>")

// On the first run, remember the seed and hash value.
// CHECK: Deterministic: false
// CHECK-NEXT: Seed: [[SEED0:\([0-9]+, [0-9]+\)]]
// CHECK-NEXT: Hash values: [[HASH0:<-?[0-9]+, -?[0-9]+>]]
// With randomized hashing, we get a new seed and a new set of hash values on
// each run. There is a minuscule chance that the same seed is generated on two
// separate executions; however, a test failure here is more likely to indicate
// an issue with the random number generator or the testing environment.
// RANDOM: Deterministic: false
// RANDOM-NEXT: Seed: [[SEED0:\([0-9]+, [0-9]+\)]]
// RANDOM-NEXT: Hash values: [[HASH0:<-?[0-9]+, -?[0-9]+>]]
// RANDOM-NEXT: Deterministic: false
// RANDOM-NEXT: Seed:
// RANDOM-NOT: [[SEED0]]
// RANDOM-NEXT: Hash values:
// RANDOM-NOT: [[HASH0]]

// Check that the values are different on the second run.
// CHECK-NEXT: Deterministic: false
// CHECK-NEXT: Seed:
// CHECK-NOT: [[SEED0]]
// CHECK-NEXT: Hash values:
// CHECK-NOT: [[HASH0]]
// Stable runs have known seeds, and generate the same hash values. A test
// failure here indicates that the seed override mechanism isn't working
// correctly.
// STABLE: Deterministic: true
// STABLE-NEXT: Seed: (0, 0)
// STABLE-NEXT: Hash values: [[HASH1:<-?[0-9]+, -?[0-9]+>]]
// STABLE-NEXT: Deterministic: true
// STABLE-NEXT: Seed: (0, 0)
// STABLE-NEXT: Hash values: [[HASH1]]