File: HashingRandomization.swift

package info (click to toggle)
swiftlang 6.1.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,644 kB
  • sloc: cpp: 9,901,738; ansic: 2,201,433; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (45 lines) | stat: -rw-r--r-- 2,203 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -Xfrontend -disable-access-control -module-name main %s -o %t/hash
// RUN: %target-codesign %t/hash
// RUN: env %env-SWIFT_DETERMINISTIC_HASHING='' %target-run %t/hash > %t/nondeterministic.log
// RUN: env %env-SWIFT_DETERMINISTIC_HASHING='' %target-run %t/hash >> %t/nondeterministic.log
// RUN: %FileCheck --check-prefixes=RANDOM %s < %t/nondeterministic.log
// RUN: env %env-SWIFT_DETERMINISTIC_HASHING=1 %target-run %t/hash > %t/deterministic.log
// RUN: env %env-SWIFT_DETERMINISTIC_HASHING=1 %target-run %t/hash >> %t/deterministic.log
// RUN: %FileCheck --check-prefixes=STABLE %s < %t/deterministic.log

// REQUIRES: executable_test

// Freestanding doesn't support environment variables, and this test depends on SWIFT_DETERMINISTIC_HASHING=1
// UNSUPPORTED: freestanding

// This check verifies that the hash seed is randomly generated on every
// execution of a Swift program unless the SWIFT_DETERMINISTIC_HASHING
// environment variable is set.

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

// 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]]

// 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]]