1 //===- unittests/ErrorOrTest.cpp - ErrorOr.h tests ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/ErrorOr.h"
11 #include "llvm/Support/Errc.h"
12 #include "gtest/gtest.h"
19 ErrorOr<int> t1() {return 1;}
20 ErrorOr<int> t2() { return errc::invalid_argument; }
22 TEST(ErrorOr, SimpleValue) {
23 ErrorOr<int> a = t1();
24 // FIXME: This is probably a bug in gtest. EXPECT_TRUE should expand to
25 // include the !! to make it friendly to explicit bool operators.
34 EXPECT_EQ(a.getError(), errc::invalid_argument);
35 #ifdef EXPECT_DEBUG_DEATH
36 EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists");
40 ErrorOr<std::unique_ptr<int> > t3() {
41 return std::unique_ptr<int>(new int(3));
44 TEST(ErrorOr, Types) {
57 TEST(ErrorOr, Covariant) {
58 ErrorOr<B*> b(ErrorOr<D*>(nullptr));
59 b = ErrorOr<D*>(nullptr);
61 ErrorOr<std::unique_ptr<B> > b1(ErrorOr<std::unique_ptr<D> >(nullptr));
62 b1 = ErrorOr<std::unique_ptr<D> >(nullptr);
64 ErrorOr<std::unique_ptr<int>> b2(ErrorOr<int *>(nullptr));
65 ErrorOr<int *> b3(nullptr);
66 ErrorOr<std::unique_ptr<int>> b4(b3);
69 TEST(ErrorOr, Comparison) {
70 ErrorOr<int> x(errc::no_such_file_or_directory);
71 EXPECT_EQ(x, errc::no_such_file_or_directory);
74 // ErrorOr<int*> x(nullptr);
75 // ErrorOr<std::unique_ptr<int>> y = x; // invalid conversion
77 !std::is_convertible<const ErrorOr<int *> &,
78 ErrorOr<std::unique_ptr<int>>>::value,
79 "do not invoke explicit ctors in implicit conversion from lvalue");
81 // ErrorOr<std::unique_ptr<int>> y = ErrorOr<int*>(nullptr); // invalid
84 !std::is_convertible<ErrorOr<int *> &&,
85 ErrorOr<std::unique_ptr<int>>>::value,
86 "do not invoke explicit ctors in implicit conversion from rvalue");
88 // ErrorOr<int*> x(nullptr);
89 // ErrorOr<std::unique_ptr<int>> y;
90 // y = x; // invalid conversion
91 static_assert(!std::is_assignable<ErrorOr<std::unique_ptr<int>>,
92 const ErrorOr<int *> &>::value,
93 "do not invoke explicit ctors in assignment");
95 // ErrorOr<std::unique_ptr<int>> x;
96 // x = ErrorOr<int*>(nullptr); // invalid conversion
97 static_assert(!std::is_assignable<ErrorOr<std::unique_ptr<int>>,
98 ErrorOr<int *> &&>::value,
99 "do not invoke explicit ctors in assignment");
100 } // end anon namespace