• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "gtest/gtest.h"
17 #include "libpandabase/utils/utils.h"
18 #include "runtime/include/coretypes/string.h"
19 #include "runtime/include/mem/panda_smart_pointers.h"
20 #include "runtime/include/runtime.h"
21 
22 namespace ark::mem::test {
23 
24 class PandaSmartPointersTest : public testing::Test {
25 public:
PandaSmartPointersTest()26     PandaSmartPointersTest()
27     {
28         RuntimeOptions options;
29         options.SetShouldLoadBootPandaFiles(false);
30         options.SetShouldInitializeIntrinsics(false);
31         options.SetLimitStandardAlloc(true);
32         Runtime::Create(options);
33         thread_ = ark::MTManagedThread::GetCurrent();
34         thread_->ManagedCodeBegin();
35     }
36 
~PandaSmartPointersTest()37     ~PandaSmartPointersTest() override
38     {
39         thread_->ManagedCodeEnd();
40         Runtime::Destroy();
41     }
42 
43     NO_COPY_SEMANTIC(PandaSmartPointersTest);
44     NO_MOVE_SEMANTIC(PandaSmartPointersTest);
45 
46 private:
47     ark::MTManagedThread *thread_ {};
48 };
49 
ReturnValueFromUniqPtr(PandaUniquePtr<int> ptr)50 int ReturnValueFromUniqPtr(PandaUniquePtr<int> ptr)
51 {
52     return *ptr.get();
53 }
54 
TEST_F(PandaSmartPointersTest,MakePandaUniqueTest)55 TEST_F(PandaSmartPointersTest, MakePandaUniqueTest)
56 {
57     // Not array type
58 
59     static constexpr int POINTER_VALUE = 5;
60 
61     auto uniqPtr = MakePandaUnique<int>(POINTER_VALUE);
62     ASSERT_NE(uniqPtr.get(), nullptr);
63 
64     int res = ReturnValueFromUniqPtr(std::move(uniqPtr));
65     ASSERT_EQ(res, 5_I);
66     // NOLINTNEXTLINE(clang-analyzer-cplusplus.Move)
67     ASSERT_EQ(uniqPtr.get(), nullptr);
68 
69     // Unbounded array type
70 
71     static constexpr size_t SIZE = 3;
72 
73     // NOLINTNEXTLINE(modernize-avoid-c-arrays)
74     auto uniqPtr2 = MakePandaUnique<int[]>(SIZE);
75     ASSERT_NE(uniqPtr2.get(), nullptr);
76 
77     for (size_t i = 0; i < SIZE; ++i) {
78         uniqPtr2[i] = i;
79     }
80 
81     auto uniqPtr3 = std::move(uniqPtr2);
82     for (size_t i = 0; i < SIZE; ++i) {
83         ASSERT_EQ(uniqPtr3[i], i);
84     }
85     // NOLINTNEXTLINE(clang-analyzer-cplusplus.Move)
86     ASSERT_EQ(uniqPtr2.get(), nullptr);
87 }
88 
89 }  // namespace ark::mem::test
90