1 //===--- status_test.cpp - Tests for the Status and Expected classes ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "status.h"
10
11 #include "gtest/gtest.h"
12
13 #include <memory>
14
15 namespace {
16
17 struct RefCounter {
18 static int Count;
19
RefCounter__anona6904e430111::RefCounter20 RefCounter() { ++Count; }
~RefCounter__anona6904e430111::RefCounter21 ~RefCounter() { --Count; }
22 RefCounter(const RefCounter &) = delete;
23 RefCounter &operator=(const RefCounter &) = delete;
24 };
25
26 int RefCounter::Count;
27
TEST(Expected,RefCounter)28 TEST(Expected, RefCounter) {
29 RefCounter::Count = 0;
30 using uptr = std::unique_ptr<RefCounter>;
31
32 acxxel::Expected<uptr> E0(uptr(new RefCounter));
33 EXPECT_FALSE(E0.isError());
34 EXPECT_EQ(1, RefCounter::Count);
35
36 acxxel::Expected<uptr> E1(std::move(E0));
37 EXPECT_FALSE(E1.isError());
38 EXPECT_EQ(1, RefCounter::Count);
39
40 acxxel::Expected<uptr> E2(acxxel::Status("nothing in here yet"));
41 EXPECT_TRUE(E2.isError());
42 EXPECT_EQ(1, RefCounter::Count);
43 E2 = std::move(E1);
44 EXPECT_FALSE(E2.isError());
45 EXPECT_EQ(1, RefCounter::Count);
46
47 EXPECT_EQ(1, E2.getValue()->Count);
48 EXPECT_FALSE(E2.isError());
49 EXPECT_EQ(1, RefCounter::Count);
50
51 EXPECT_EQ(1, E2.takeValue()->Count);
52 EXPECT_EQ(0, RefCounter::Count);
53 }
54
55 } // namespace
56