File: protocol_resilience.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; 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 (81 lines) | stat: -rw-r--r-- 2,130 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// RUN: %empty-directory(%t)

// RUN: %target-build-swift-dylib(%t/%target-library-name(resilient_protocol))  -target %target-swift-5.1-abi-triple -enable-library-evolution %S/Inputs/resilient_protocol.swift -emit-module -emit-module-path %t/resilient_protocol.swiftmodule -module-name resilient_protocol
// RUN: %target-codesign %t/%target-library-name(resilient_protocol)

// RUN: %target-build-swift -parse-as-library  -target %target-swift-5.1-abi-triple %s -lresilient_protocol -I %t -L %t -o %t/main %target-rpath(%t)
// RUN: %target-codesign %t/main

// RUN: %target-run %t/main %t/%target-library-name(resilient_protocol)

// REQUIRES: executable_test
// REQUIRES: concurrency
// UNSUPPORTED: freestanding

// rdar://76038845
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime

import StdlibUnittest
import resilient_protocol

enum MyError : Error {
	case bad
}

struct IntAwaitable : Awaitable {
  func waitForNothing() async {}

  func wait() async -> Int {
    return 123
  }

  func waitForInt() async -> Int {
    return 321
  }

  func wait(orThrow: Bool) async throws {
    if (orThrow) {
      throw MyError.bad
    }
  }
}

func genericWaitForNothing<T : Awaitable>(_ t: T) async {
  await t.waitForNothing()
}

func genericWait<T : Awaitable>(_ t: T) async -> T.Result {
  return await t.wait()
}

func genericWaitForInt<T : Awaitable>(_ t: T) async -> Int {
  return await t.waitForInt()
}

func genericWait<T : Awaitable>(orThrow: Bool, _ t: T) async throws {
  return try await t.wait(orThrow: orThrow)
}

@main struct Main {
  static func main() async {
    let task = Task.detached {
      var AsyncProtocolRequirementSuite = TestSuite("ResilientProtocol")

      AsyncProtocolRequirementSuite.test("AsyncProtocolRequirement") {
        let x = IntAwaitable()

        await genericWaitForNothing(x)

        expectEqual(123, await genericWait(x))
        expectEqual(321, await genericWaitForInt(x))

        expectNil(try? await genericWait(orThrow: true, x))
        try! await genericWait(orThrow: false, x)
      }
      await runAllTestsAsync()
    }

    await task.value
  }
}