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 #include "gtest/gtest.h" 12 #include <memory> 13 14 using namespace llvm; 15 16 namespace { 17 18 ErrorOr<int> t1() {return 1;} 19 ErrorOr<int> t2() { return errc::invalid_argument; } 20 21 TEST(ErrorOr, SimpleValue) { 22 ErrorOr<int> a = t1(); 23 EXPECT_TRUE(a); 24 EXPECT_EQ(1, *a); 25 26 a = t2(); 27 EXPECT_FALSE(a); 28 EXPECT_EQ(errc::invalid_argument, a); 29 #ifdef EXPECT_DEBUG_DEATH 30 EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists"); 31 #endif 32 } 33 34 #if LLVM_HAS_CXX11_STDLIB 35 ErrorOr<std::unique_ptr<int> > t3() { 36 return std::unique_ptr<int>(new int(3)); 37 } 38 #endif 39 40 TEST(ErrorOr, Types) { 41 int x; 42 ErrorOr<int&> a(x); 43 *a = 42; 44 EXPECT_EQ(42, x); 45 46 #if LLVM_HAS_CXX11_STDLIB 47 // Move only types. 48 EXPECT_EQ(3, **t3()); 49 #endif 50 } 51 52 struct B {}; 53 struct D : B {}; 54 55 TEST(ErrorOr, Covariant) { 56 ErrorOr<B*> b(ErrorOr<D*>(0)); 57 b = ErrorOr<D*>(0); 58 59 #if LLVM_HAS_CXX11_STDLIB 60 ErrorOr<std::unique_ptr<B> > b1(ErrorOr<std::unique_ptr<D> >(0)); 61 b1 = ErrorOr<std::unique_ptr<D> >(0); 62 #endif 63 } 64 } // end anon namespace 65