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