File: swift-expected-execution.cpp

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 (134 lines) | stat: -rw-r--r-- 4,644 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend %S/swift-functions-errors.swift -typecheck -module-name Functions -Xcc -fno-exceptions -enable-experimental-cxx-interop -clang-header-expose-decls=has-expose-attr-or-stdlib -enable-experimental-feature GenerateBindingsForThrowingFunctionsInCXX -emit-clang-header-path %t/functions.h

// RUN: %target-interop-build-clangxx -c %s -I %t -fno-exceptions -o %t/swift-expected-execution.o -DSWIFT_CXX_INTEROP_EXPERIMENTAL_SWIFT_ERROR
// RUN: %target-interop-build-swift %S/swift-functions-errors.swift -o %t/swift-expected-execution -Xlinker %t/swift-expected-execution.o -module-name Functions -Xfrontend -entry-point-function-name -Xfrontend swiftMain -enable-experimental-feature GenerateBindingsForThrowingFunctionsInCXX

// RUN: %target-codesign %t/swift-expected-execution
// RUN: %target-run %t/swift-expected-execution | %FileCheck %s

// REQUIRES: executable_test
// UNSUPPORTED: OS=windows-msvc
// UNSUPPORTED: CPU=arm64e

// for experimental feature GenerateBindingsForThrowingFunctionsInCXX:
// REQUIRES: asserts

#include <cassert>
#include <cstdio>
#include "functions.h"

int main() {

  // Test Empty Constructor
  auto testIntEmpty = swift::Expected<int>();

  // Test Error Constructor
  swift::Error e;
  auto testIntError = swift::Expected<int>(e);

  // Test Value Constructor
  auto testIntValue = swift::Expected<int>(42);

  // Test Copy Constructor
  auto testCopy = testIntEmpty;

  // TODO: Test Move Constructor

  // Test Destructor
  auto testDestEmpty = swift::Expected<int>();
  auto testDestInt = swift::Expected<int>(42);
  auto testDestError = swift::Expected<int>(e);
  testDestEmpty.~Expected();
  testDestInt.~Expected();
  testDestError.~Expected();

  // TODO: Test Assignment (Move)

  // Test Access to T's members (const)
  const auto exp = testIntValue;
  if (*exp.operator->() == 42)
    printf("Test Access to T's members (const)\n");

  // Test Access to T's members
  if (*testIntValue.operator->() == 42)
    printf("Test Access to T's members\n");

  // Test Reference to T's members (const)
  const auto refExp = testIntValue;
  if (*refExp == 42)
    printf("Test Reference to T's members (const)\n");

  // Test Reference to T's members
  if (*testIntValue == 42)
    printf("Test Reference to T's members\n");

  // Test bool operator
  if (testIntValue) {
    printf("Test operator bool\n");
  }

  const auto constExpectedResult = Functions::throwFunctionWithPossibleReturn(0);
  if (!constExpectedResult.has_value()) {
    auto constError = constExpectedResult.error();
    auto optionalError = constError.as<Functions::NaiveErrors>();
    assert(optionalError.isSome());
    auto valueError = optionalError.get();
    assert(valueError == Functions::NaiveErrors::returnError);
    valueError.getMessage();
  }

  auto expectedResult = Functions::throwFunctionWithPossibleReturn(0);
  if (!expectedResult.has_value()) {
    auto error = expectedResult.error();
    auto optionalError = error.as<Functions::NaiveErrors>();
    assert(optionalError.isSome());
    auto valueError = optionalError.get();
    assert(valueError == Functions::NaiveErrors::returnError);
    valueError.getMessage();
  }

  auto expectedResult2 = Functions::throwFunctionWithNeverReturn();
  if (!expectedResult2.has_value()) {
    auto error = expectedResult2.error();
    auto optionalError = error.as<Functions::NaiveErrors>();
    assert(optionalError.isSome());
    auto valueError = optionalError.get();
    assert(valueError == Functions::NaiveErrors::returnError);
    valueError.getMessage();
  }

  // Test get T's Value (const)
  const auto valueExp = testIntValue;
  if (valueExp.value() == 42)
    printf("Test get T's Value (const)\n");

  // Test get T's Value
  if (testIntValue.value() == 42)
    printf("Test get T's Value\n");

  // Test has Value
  if (testIntValue.has_value())
    printf("testIntValue has a value\n");
  if (!testIntError.has_value())
    printf("testIntError doesn't have a value\n");

  return 0;
}

// CHECK: Test Access to T's members (const)
// CHECK-NEXT: Test Access to T's members
// CHECK-NEXT: Test Reference to T's members (const)
// CHECK-NEXT: Test Reference to T's members
// CHECK-NEXT: Test operator bool
// CHECK-NEXT: passThrowFunctionWithPossibleReturn
// CHECK-NEXT: returnError
// CHECK-NEXT: passThrowFunctionWithPossibleReturn
// CHECK-NEXT: returnError
// CHECK-NEXT: passThrowFunctionWithNeverReturn
// CHECK-NEXT: returnError
// CHECK-NEXT: Test get T's Value (const)
// CHECK-NEXT: Test get T's Value
// CHECK-NEXT: testIntValue has a value
// CHECK-NEXT: testIntError doesn't have a value