aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/compiler-rt/lib/orc/tests/unit/error_test.cpp
blob: 5251d788e01b05fc1d460a289a6d6f3272c1a0fc (plain) (blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//===-- error_test.cpp --sssssssss-----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of the ORC runtime.
//
// Note:
//   This unit test was adapted from
//   llvm/unittests/Support/ExtensibleRTTITest.cpp
//
//===----------------------------------------------------------------------===//

#include "error.h"
#include "gtest/gtest.h"

using namespace __orc_rt;

namespace {

class CustomError : public RTTIExtends<CustomError, ErrorInfoBase> {
public:
  CustomError(int V1) : V1(V1) {}
  std::string toString() const override {
    return "CustomError V1 = " + std::to_string(V1);
  }
  int getV1() const { return V1; }

protected:
  int V1;
};

class CustomSubError : public RTTIExtends<CustomSubError, CustomError> {
public:
  CustomSubError(int V1, std::string V2)
      : RTTIExtends<CustomSubError, CustomError>(V1), V2(std::move(V2)) {}
  std::string toString() const override {
    return "CustomSubError V1 = " + std::to_string(V1) + ", " + V2;
  }
  const std::string &getV2() const { return V2; }

protected:
  std::string V2;
};

} // end anonymous namespace

// Test that a checked success value doesn't cause any issues.
TEST(Error, CheckedSuccess) {
  Error E = Error::success();
  EXPECT_FALSE(E) << "Unexpected error while testing Error 'Success'";
}

// Check that a consumed success value doesn't cause any issues.
TEST(Error, ConsumeSuccess) { consumeError(Error::success()); }

TEST(Error, ConsumeError) {
  Error E = make_error<CustomError>(42);
  if (E) {
    consumeError(std::move(E));
  } else
    ADD_FAILURE() << "Error failure value should convert to true";
}

// Test that unchecked success values cause an abort.
TEST(Error, UncheckedSuccess) {
  EXPECT_DEATH({ Error E = Error::success(); },
               "Error must be checked prior to destruction")
      << "Unchecked Error Succes value did not cause abort()";
}

// Test that a checked but unhandled error causes an abort.
TEST(Error, CheckedButUnhandledError) {
  auto DropUnhandledError = []() {
    Error E = make_error<CustomError>(42);
    (void)!E;
  };
  EXPECT_DEATH(DropUnhandledError(),
               "Error must be checked prior to destruction")
      << "Unhandled Error failure value did not cause an abort()";
}

// Test that error_cast works as expected.
TEST(Error, BasicErrorCast) {
  {
    // Check casting base error value to base error type.
    auto E = make_error<CustomError>(42);
    if (auto CSE = error_cast<CustomSubError>(E)) {
      ADD_FAILURE() << "Derived cast incorrectly matched base error";
    } else if (auto CE = error_cast<CustomError>(E)) {
      EXPECT_EQ(CE->getV1(), 42) << "Unexpected wrapped value";
    } else
      ADD_FAILURE() << "Unexpected error value";
  }

  {
    // Check casting derived error value to base error type.
    auto E = make_error<CustomSubError>(42, "foo");
    if (auto CE = error_cast<CustomError>(E)) {
      EXPECT_EQ(CE->getV1(), 42) << "Unexpected wrapped value";
    } else
      ADD_FAILURE() << "Unexpected error value";
  }

  {
    // Check casting derived error value to derived error type.
    auto E = make_error<CustomSubError>(42, "foo");
    if (auto CSE = error_cast<CustomSubError>(E)) {
      EXPECT_EQ(CSE->getV1(), 42) << "Unexpected wrapped value";
      EXPECT_EQ(CSE->getV2(), "foo") << "Unexpected wrapped value";
    } else
      ADD_FAILURE() << "Unexpected error value";
  }
}

// ErrorAsOutParameter tester.
static void errAsOutParamHelper(Error &Err) {
  ErrorAsOutParameter ErrAsOutParam(&Err);
  // Verify that checked flag is raised - assignment should not crash.
  Err = Error::success();
  // Raise the checked bit manually - caller should still have to test the
  // error.
  (void)!!Err;
}

// Test that ErrorAsOutParameter sets the checked flag on construction.
TEST(Error, ErrorAsOutParameterChecked) {
  Error E = Error::success();
  errAsOutParamHelper(E);
  (void)!!E;
}

// Test that ErrorAsOutParameter clears the checked flag on destruction.
TEST(Error, ErrorAsOutParameterUnchecked) {
  EXPECT_DEATH(
      {
        Error E = Error::success();
        errAsOutParamHelper(E);
      },
      "Error must be checked prior to destruction")
      << "ErrorAsOutParameter did not clear the checked flag on destruction.";
}

// Check 'Error::isA<T>' method handling.
TEST(Error, IsAHandling) {
  // Check 'isA' handling.
  Error E = make_error<CustomError>(42);
  Error F = make_error<CustomSubError>(42, "foo");
  Error G = Error::success();

  EXPECT_TRUE(E.isA<CustomError>());
  EXPECT_FALSE(E.isA<CustomSubError>());
  EXPECT_TRUE(F.isA<CustomError>());
  EXPECT_TRUE(F.isA<CustomSubError>());
  EXPECT_FALSE(G.isA<CustomError>());

  consumeError(std::move(E));
  consumeError(std::move(F));
  consumeError(std::move(G));
}

TEST(Error, StringError) {
  auto E = make_error<StringError>("foo");
  if (auto SE = error_cast<StringError>(E)) {
    EXPECT_EQ(SE->toString(), "foo") << "Unexpected StringError value";
  } else
    ADD_FAILURE() << "Expected StringError value";
}

// Test Checked Expected<T> in success mode.
TEST(Error, CheckedExpectedInSuccessMode) {
  Expected<int> A = 7;
  EXPECT_TRUE(!!A) << "Expected with non-error value doesn't convert to 'true'";
  // Access is safe in second test, since we checked the error in the first.
  EXPECT_EQ(*A, 7) << "Incorrect Expected non-error value";
}

// Test Expected with reference type.
TEST(Error, ExpectedWithReferenceType) {
  int A = 7;
  Expected<int &> B = A;
  // 'Check' B.
  (void)!!B;
  int &C = *B;
  EXPECT_EQ(&A, &C) << "Expected failed to propagate reference";
}

// Test Unchecked Expected<T> in success mode.
// We expect this to blow up the same way Error would.
// Test runs in debug mode only.
TEST(Error, UncheckedExpectedInSuccessModeDestruction) {
  EXPECT_DEATH({ Expected<int> A = 7; },
               "Expected<T> must be checked before access or destruction.")
      << "Unchecekd Expected<T> success value did not cause an abort().";
}

// Test Unchecked Expected<T> in success mode.
// We expect this to blow up the same way Error would.
// Test runs in debug mode only.
TEST(Error, UncheckedExpectedInSuccessModeAccess) {
  EXPECT_DEATH(
      {
        Expected<int> A = 7;
        *A;
      },
      "Expected<T> must be checked before access or destruction.")
      << "Unchecekd Expected<T> success value did not cause an abort().";
}

// Test Unchecked Expected<T> in success mode.
// We expect this to blow up the same way Error would.
// Test runs in debug mode only.
TEST(Error, UncheckedExpectedInSuccessModeAssignment) {
  EXPECT_DEATH(
      {
        Expected<int> A = 7;
        A = 7;
      },
      "Expected<T> must be checked before access or destruction.")
      << "Unchecekd Expected<T> success value did not cause an abort().";
}

// Test Expected<T> in failure mode.
TEST(Error, ExpectedInFailureMode) {
  Expected<int> A = make_error<CustomError>(42);
  EXPECT_FALSE(!!A) << "Expected with error value doesn't convert to 'false'";
  Error E = A.takeError();
  EXPECT_TRUE(E.isA<CustomError>()) << "Incorrect Expected error value";
  consumeError(std::move(E));
}

// Check that an Expected instance with an error value doesn't allow access to
// operator*.
// Test runs in debug mode only.
TEST(Error, AccessExpectedInFailureMode) {
  Expected<int> A = make_error<CustomError>(42);
  EXPECT_DEATH(*A, "Expected<T> must be checked before access or destruction.")
      << "Incorrect Expected error value";
  consumeError(A.takeError());
}

// Check that an Expected instance with an error triggers an abort if
// unhandled.
// Test runs in debug mode only.
TEST(Error, UnhandledExpectedInFailureMode) {
  EXPECT_DEATH({ Expected<int> A = make_error<CustomError>(42); },
               "Expected<T> must be checked before access or destruction.")
      << "Unchecked Expected<T> failure value did not cause an abort()";
}

// Test covariance of Expected.
TEST(Error, ExpectedCovariance) {
  class B {};
  class D : public B {};

  Expected<B *> A1(Expected<D *>(nullptr));
  // Check A1 by converting to bool before assigning to it.
  (void)!!A1;
  A1 = Expected<D *>(nullptr);
  // Check A1 again before destruction.
  (void)!!A1;

  Expected<std::unique_ptr<B>> A2(Expected<std::unique_ptr<D>>(nullptr));
  // Check A2 by converting to bool before assigning to it.
  (void)!!A2;
  A2 = Expected<std::unique_ptr<D>>(nullptr);
  // Check A2 again before destruction.
  (void)!!A2;
}

// Test that the ExitOnError utility works as expected.
TEST(Error, CantFailSuccess) {
  cantFail(Error::success());

  int X = cantFail(Expected<int>(42));
  EXPECT_EQ(X, 42) << "Expected value modified by cantFail";

  int Dummy = 42;
  int &Y = cantFail(Expected<int &>(Dummy));
  EXPECT_EQ(&Dummy, &Y) << "Reference mangled by cantFail";
}

// Test that cantFail results in a crash if you pass it a failure value.
TEST(Error, CantFailDeath) {
  EXPECT_DEATH(cantFail(make_error<StringError>("foo")),
               "cantFail called on failure value")
      << "cantFail(Error) did not cause an abort for failure value";

  EXPECT_DEATH(cantFail(Expected<int>(make_error<StringError>("foo"))),
               "cantFail called on failure value")
      << "cantFail(Expected<int>) did not cause an abort for failure value";
}