blob: 7a0c31f484de6bdb8db2e9f4ff9706d4b18f4796 [file] [log] [blame]
Michael J. Spencer779c4242013-01-20 20:32:30 +00001//===- 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"
Michael J. Spencer779c4242013-01-20 20:32:30 +000011#include "gtest/gtest.h"
Michael J. Spencer779c4242013-01-20 20:32:30 +000012#include <memory>
13
14using namespace llvm;
15
16namespace {
17
18ErrorOr<int> t1() {return 1;}
Michael J. Spencer12e3dfc2013-02-28 01:44:26 +000019ErrorOr<int> t2() { return errc::invalid_argument; }
Michael J. Spencer779c4242013-01-20 20:32:30 +000020
21TEST(ErrorOr, SimpleValue) {
22 ErrorOr<int> a = t1();
Rafael Espindola98d3c102014-01-16 23:37:23 +000023 // 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);
Michael J. Spencer779c4242013-01-20 20:32:30 +000026 EXPECT_EQ(1, *a);
27
Rafael Espindolacd56deb2014-01-09 19:47:39 +000028 ErrorOr<int> b = a;
29 EXPECT_EQ(1, *b);
30
Michael J. Spencer779c4242013-01-20 20:32:30 +000031 a = t2();
32 EXPECT_FALSE(a);
Rafael Espindola1c704b42014-01-08 22:03:39 +000033 EXPECT_EQ(errc::invalid_argument, a.getError());
NAKAMURA Takumi08e028e2013-01-22 04:02:41 +000034#ifdef EXPECT_DEBUG_DEATH
Michael J. Spencer779c4242013-01-20 20:32:30 +000035 EXPECT_DEBUG_DEATH(*a, "Cannot get value when an error exists");
NAKAMURA Takumi08e028e2013-01-22 04:02:41 +000036#endif
Michael J. Spencer779c4242013-01-20 20:32:30 +000037}
38
39#if LLVM_HAS_CXX11_STDLIB
40ErrorOr<std::unique_ptr<int> > t3() {
41 return std::unique_ptr<int>(new int(3));
42}
43#endif
44
45TEST(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}
Michael J. Spencer18131ae2013-02-06 22:28:53 +000056
57struct B {};
58struct D : B {};
59
60TEST(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}
Michael J. Spencer779c4242013-01-20 20:32:30 +000069} // end anon namespace