File: HashingRandomization.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (45 lines) | stat: -rw-r--r-- 2,203 bytes parent folder | download
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]]