1//===-- SharedClusterTest.cpp ---------------------------------------------===//
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 "lldb/Utility/SharedCluster.h"
10#include "gmock/gmock.h"
11#include "gtest/gtest.h"
12
13using namespace lldb_private;
14
15namespace {
16class DestructNotifier {
17public:
18 DestructNotifier(std::vector<int> &Queue, int Key) : Queue(Queue), Key(Key) {}
19 ~DestructNotifier() { Queue.push_back(x: Key); }
20
21 std::vector<int> &Queue;
22 const int Key;
23};
24} // namespace
25
26TEST(SharedCluster, ClusterManager) {
27 std::vector<int> Queue;
28 {
29 auto CM = ClusterManager<DestructNotifier>::Create();
30 auto *One = new DestructNotifier(Queue, 1);
31 auto *Two = new DestructNotifier(Queue, 2);
32 CM->ManageObject(new_object: One);
33 CM->ManageObject(new_object: Two);
34
35 ASSERT_THAT(Queue, testing::IsEmpty());
36 {
37 std::shared_ptr<DestructNotifier> OnePtr = CM->GetSharedPointer(desired_object: One);
38 ASSERT_EQ(OnePtr->Key, 1);
39 ASSERT_THAT(Queue, testing::IsEmpty());
40
41 {
42 std::shared_ptr<DestructNotifier> OnePtrCopy = OnePtr;
43 ASSERT_EQ(OnePtrCopy->Key, 1);
44 ASSERT_THAT(Queue, testing::IsEmpty());
45 }
46
47 {
48 std::shared_ptr<DestructNotifier> TwoPtr = CM->GetSharedPointer(desired_object: Two);
49 ASSERT_EQ(TwoPtr->Key, 2);
50 ASSERT_THAT(Queue, testing::IsEmpty());
51 }
52
53 ASSERT_THAT(Queue, testing::IsEmpty());
54 }
55 ASSERT_THAT(Queue, testing::IsEmpty());
56 }
57 ASSERT_THAT(Queue, testing::ElementsAre(1, 2));
58}
59

source code of lldb/unittests/Utility/SharedClusterTest.cpp