summaryrefslogtreecommitdiffstats
path: root/parallel-libs/acxxel/tests/status_test.cpp
blob: edd0f2a44639e94044c62a84331bd9472db4d47c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//===--- status_test.cpp - Tests for the Status and Expected classes ------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "status.h"

#include "gtest/gtest.h"

#include <memory>

namespace {

struct RefCounter {
  static int Count;

  RefCounter() { ++Count; }
  ~RefCounter() { --Count; }
  RefCounter(const RefCounter &) = delete;
  RefCounter &operator=(const RefCounter &) = delete;
};

int RefCounter::Count;

TEST(Expected, RefCounter) {
  RefCounter::Count = 0;
  using uptr = std::unique_ptr<RefCounter>;

  acxxel::Expected<uptr> E0(uptr(new RefCounter));
  EXPECT_FALSE(E0.isError());
  EXPECT_EQ(1, RefCounter::Count);

  acxxel::Expected<uptr> E1(std::move(E0));
  EXPECT_FALSE(E1.isError());
  EXPECT_EQ(1, RefCounter::Count);

  acxxel::Expected<uptr> E2(acxxel::Status("nothing in here yet"));
  EXPECT_TRUE(E2.isError());
  EXPECT_EQ(1, RefCounter::Count);
  E2 = std::move(E1);
  EXPECT_FALSE(E2.isError());
  EXPECT_EQ(1, RefCounter::Count);

  EXPECT_EQ(1, E2.getValue()->Count);
  EXPECT_FALSE(E2.isError());
  EXPECT_EQ(1, RefCounter::Count);

  EXPECT_EQ(1, E2.takeValue()->Count);
  EXPECT_EQ(0, RefCounter::Count);
}

} // namespace
OpenPOWER on IntegriCloud