1 //===- unittests/ErrorOrTest.cpp - ErrorOr.h tests ------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/ErrorOr.h" 11 12 #include "gtest/gtest.h" 13 14 #include <memory> 15 16 using namespace llvm; 17 18 namespace { 19 20 ErrorOr<int> t1() {return 1;} 21 ErrorOr<int> t2() { return errc::invalid_argument; } 22 23 TEST(ErrorOr, SimpleValue) { 24 ErrorOr<int> a = t1(); 25 EXPECT_TRUE(a); 26 EXPECT_EQ(1, *a); 27 28 a = t2(); 29 EXPECT_FALSE(a); 30 EXPECT_EQ(errc::invalid_argument, a); 31 #ifdef EXPECT_DEBUG_DEATH 32 EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists"); 33 #endif 34 } 35 36 #if LLVM_HAS_CXX11_STDLIB 37 ErrorOr<std::unique_ptr<int> > t3() { 38 return std::unique_ptr<int>(new int(3)); 39 } 40 #endif 41 42 TEST(ErrorOr, Types) { 43 int x; 44 ErrorOr<int&> a(x); 45 *a = 42; 46 EXPECT_EQ(42, x); 47 48 #if LLVM_HAS_CXX11_STDLIB 49 // Move only types. 50 EXPECT_EQ(3, **t3()); 51 #endif 52 } 53 54 struct B {}; 55 struct D : B {}; 56 57 TEST(ErrorOr, Covariant) { 58 ErrorOr<B*> b(ErrorOr<D*>(0)); 59 b = ErrorOr<D*>(0); 60 61 #if LLVM_HAS_CXX11_STDLIB 62 ErrorOr<std::unique_ptr<B> > b1(ErrorOr<std::unique_ptr<D> >(0)); 63 b1 = ErrorOr<std::unique_ptr<D> >(0); 64 #endif 65 } 66 } // end anon namespace 67