1
+ // RUN: %empty-directory(%t)
2
+
3
+ // RUN: %target-swift-frontend %S/swift-functions-errors.swift -typecheck -module-name Functions -clang-header-expose-decls=has-expose-attr -emit-clang-header-path %t/functions.h
4
+
5
+ // RUN: %target-interop-build-clangxx -c %s -I %t -o %t/swift-expected-execution.o
6
+ // 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
7
+
8
+ // RUN: %target-codesign %t/swift-expected-execution
9
+ // RUN: %target-run %t/swift-expected-execution | %FileCheck %s
10
+
11
+ // REQUIRES: executable_test
12
+
13
+ #include < cstdio>
14
+ #include " functions.h"
15
+
16
+ int main () {
17
+
18
+ // Test Empty Constructor
19
+ auto testIntEmpty = swift::Expected<int >();
20
+
21
+ // Test Error Constructor
22
+ swift::Error e;
23
+ auto testIntError = swift::Expected<int >(e);
24
+
25
+ // Test Value Constructor
26
+ auto testIntValue = swift::Expected<int >(42 );
27
+
28
+ // Test Copy Constructor
29
+ auto testCopy = testIntEmpty;
30
+
31
+ // TODO: Test Move Constructor
32
+
33
+ // Test Destructor
34
+ auto testDestEmpty = swift::Expected<int >();
35
+ auto testDestInt = swift::Expected<int >(42 );
36
+ auto testDestError = swift::Expected<int >(e);
37
+ testDestEmpty.~Expected ();
38
+ testDestInt.~Expected ();
39
+ testDestError.~Expected ();
40
+
41
+ // TODO: Test Assignment (Move)
42
+
43
+ // Test Access to T's members (const)
44
+ const auto exp = testIntValue;
45
+ if (*exp.operator ->() == 42 )
46
+ printf (" Test Access to T's members (const)\n " );
47
+
48
+ // Test Access to T's members
49
+ if (*testIntValue.operator ->() == 42 )
50
+ printf (" Test Access to T's members\n " );
51
+
52
+ // Test Reference to T's members (const)
53
+ const auto refExp = testIntValue;
54
+ if (*refExp == 42 )
55
+ printf (" Test Reference to T's members (const)\n " );
56
+
57
+ // Test Reference to T's members
58
+ if (*testIntValue == 42 )
59
+ printf (" Test Reference to T's members (const)\n " );
60
+
61
+ // Test bool operator
62
+ if (testIntValue) {
63
+ printf (" Test operator bool\n " );
64
+ }
65
+
66
+ // Test get T's Value (const)
67
+ const auto valueExp = testIntValue;
68
+ if (valueExp.value () == 42 )
69
+ printf (" Test get T's Value (const)\n " );
70
+
71
+ // Test get T's Value
72
+ if (testIntValue.value () == 42 )
73
+ printf (" Test get T's Value\n " );
74
+
75
+ // Test get swift::Error (const)
76
+ try {
77
+ Functions::throwFunction ();
78
+ } catch (swift::Error& e) {
79
+ const auto errorExp = swift::Expected<int >(e);
80
+ auto err = errorExp.error (); // using const function
81
+ auto errorVal = err.as <Functions::NaiveErrors>();
82
+ errorVal.getMessage ();
83
+ }
84
+
85
+ // Test get swift::Error
86
+ try {
87
+ Functions::throwFunction ();
88
+ } catch (swift::Error& e) {
89
+ auto err = swift::Expected<int >(e).error ();
90
+ auto errorVal = err.as <Functions::NaiveErrors>();
91
+ errorVal.getMessage ();
92
+ }
93
+
94
+ // Test has Value
95
+ if (testIntValue.has_value ())
96
+ printf (" testIntValue has a value\n " );
97
+ if (!testIntError.has_value ())
98
+ printf (" testIntError doesn't have a value\n " );
99
+
100
+ return 0 ;
101
+ }
102
+
103
+ // CHECK: Test Access to T's members (const)
104
+ // CHECK-NEXT: Test Access to T's members
105
+ // CHECK-NEXT: Test Reference to T's members (const)
106
+ // CHECK-NEXT: Test Reference to T's members (const)
107
+ // CHECK-NEXT: Test operator bool
108
+ // CHECK-NEXT: Test get T's Value (const)
109
+ // CHECK-NEXT: Test get T's Value
110
+ // CHECK-NEXT: passThrowFunction
111
+ // CHECK-NEXT: throwError
112
+ // CHECK-NEXT: passThrowFunction
113
+ // CHECK-NEXT: throwError
114
+ // CHECK-NEXT: testIntValue has a value
115
+ // CHECK-NEXT: testIntError doesn't have a value
0 commit comments