133f65f39SPavel Labath //===-- SharedClusterTest.cpp ---------------------------------------------===//
233f65f39SPavel Labath //
333f65f39SPavel Labath // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
433f65f39SPavel Labath // See https://llvm.org/LICENSE.txt for license information.
533f65f39SPavel Labath // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
633f65f39SPavel Labath //
733f65f39SPavel Labath //===----------------------------------------------------------------------===//
833f65f39SPavel Labath
933f65f39SPavel Labath #include "lldb/Utility/SharedCluster.h"
1033f65f39SPavel Labath #include "gmock/gmock.h"
1133f65f39SPavel Labath #include "gtest/gtest.h"
1233f65f39SPavel Labath
1333f65f39SPavel Labath using namespace lldb_private;
1433f65f39SPavel Labath
1533f65f39SPavel Labath namespace {
1633f65f39SPavel Labath class DestructNotifier {
1733f65f39SPavel Labath public:
DestructNotifier(std::vector<int> & Queue,int Key)1833f65f39SPavel Labath DestructNotifier(std::vector<int> &Queue, int Key) : Queue(Queue), Key(Key) {}
~DestructNotifier()1933f65f39SPavel Labath ~DestructNotifier() { Queue.push_back(Key); }
2033f65f39SPavel Labath
2133f65f39SPavel Labath std::vector<int> &Queue;
2233f65f39SPavel Labath const int Key;
2333f65f39SPavel Labath };
2433f65f39SPavel Labath } // namespace
2533f65f39SPavel Labath
TEST(SharedCluster,ClusterManager)2633f65f39SPavel Labath TEST(SharedCluster, ClusterManager) {
2733f65f39SPavel Labath std::vector<int> Queue;
28*363f05b8SPavel Labath {
29*363f05b8SPavel Labath auto CM = ClusterManager<DestructNotifier>::Create();
3033f65f39SPavel Labath auto *One = new DestructNotifier(Queue, 1);
3133f65f39SPavel Labath auto *Two = new DestructNotifier(Queue, 2);
3233f65f39SPavel Labath CM->ManageObject(One);
3333f65f39SPavel Labath CM->ManageObject(Two);
3433f65f39SPavel Labath
3533f65f39SPavel Labath ASSERT_THAT(Queue, testing::IsEmpty());
3633f65f39SPavel Labath {
37*363f05b8SPavel Labath std::shared_ptr<DestructNotifier> OnePtr = CM->GetSharedPointer(One);
3833f65f39SPavel Labath ASSERT_EQ(OnePtr->Key, 1);
3933f65f39SPavel Labath ASSERT_THAT(Queue, testing::IsEmpty());
4033f65f39SPavel Labath
4133f65f39SPavel Labath {
42*363f05b8SPavel Labath std::shared_ptr<DestructNotifier> OnePtrCopy = OnePtr;
4333f65f39SPavel Labath ASSERT_EQ(OnePtrCopy->Key, 1);
4433f65f39SPavel Labath ASSERT_THAT(Queue, testing::IsEmpty());
4533f65f39SPavel Labath }
4633f65f39SPavel Labath
4733f65f39SPavel Labath {
48*363f05b8SPavel Labath std::shared_ptr<DestructNotifier> TwoPtr = CM->GetSharedPointer(Two);
4933f65f39SPavel Labath ASSERT_EQ(TwoPtr->Key, 2);
5033f65f39SPavel Labath ASSERT_THAT(Queue, testing::IsEmpty());
5133f65f39SPavel Labath }
5233f65f39SPavel Labath
5333f65f39SPavel Labath ASSERT_THAT(Queue, testing::IsEmpty());
5433f65f39SPavel Labath }
55*363f05b8SPavel Labath ASSERT_THAT(Queue, testing::IsEmpty());
56*363f05b8SPavel Labath }
5733f65f39SPavel Labath ASSERT_THAT(Queue, testing::ElementsAre(1, 2));
5833f65f39SPavel Labath }
59