Use LLVM_EXPLICIT instead of a function pointer as bool.
[oota-llvm.git] / unittests / Support / ErrorOrTest.cpp
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   // FIXME: This is probably a bug in gtest. EXPECT_TRUE should expand to
24   // include the !! to make it friendly to explicit bool operators.
25   EXPECT_TRUE(!!a);
26   EXPECT_EQ(1, *a);
27
28   ErrorOr<int> b = a;
29   EXPECT_EQ(1, *b);
30
31   a = t2();
32   EXPECT_FALSE(a);
33   EXPECT_EQ(errc::invalid_argument, a.getError());
34 #ifdef EXPECT_DEBUG_DEATH
35   EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists");
36 #endif
37 }
38
39 #if LLVM_HAS_CXX11_STDLIB
40 ErrorOr<std::unique_ptr<int> > t3() {
41   return std::unique_ptr<int>(new int(3));
42 }
43 #endif
44
45 TEST(ErrorOr, Types) {
46   int x;
47   ErrorOr<int&> a(x);
48   *a = 42;
49   EXPECT_EQ(42, x);
50
51 #if LLVM_HAS_CXX11_STDLIB
52   // Move only types.
53   EXPECT_EQ(3, **t3());
54 #endif
55 }
56
57 struct B {};
58 struct D : B {};
59
60 TEST(ErrorOr, Covariant) {
61   ErrorOr<B*> b(ErrorOr<D*>(0));
62   b = ErrorOr<D*>(0);
63
64 #if LLVM_HAS_CXX11_STDLIB
65   ErrorOr<std::unique_ptr<B> > b1(ErrorOr<std::unique_ptr<D> >(0));
66   b1 = ErrorOr<std::unique_ptr<D> >(0);
67 #endif
68 }
69 } // end anon namespace