1 /**
2 * Copyright (c) 2021-2022 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 "runtime/include/coretypes/string.h"
18 #include "runtime/include/mem/panda_smart_pointers.h"
19 #include "runtime/include/runtime.h"
20
21 namespace panda::mem::test {
22
23 class PandaSmartPointersTest : public testing::Test {
24 public:
PandaSmartPointersTest()25 PandaSmartPointersTest()
26 {
27 RuntimeOptions options;
28 options.SetShouldLoadBootPandaFiles(false);
29 options.SetShouldInitializeIntrinsics(false);
30 options.SetLimitStandardAlloc(true);
31 Runtime::Create(options);
32 thread_ = panda::MTManagedThread::GetCurrent();
33 thread_->ManagedCodeBegin();
34 }
35
~PandaSmartPointersTest()36 ~PandaSmartPointersTest() override
37 {
38 thread_->ManagedCodeEnd();
39 Runtime::Destroy();
40 }
41
42 protected:
43 panda::MTManagedThread *thread_;
44 };
45
ReturnValueFromUniqPtr(PandaUniquePtr<int> ptr)46 int ReturnValueFromUniqPtr(PandaUniquePtr<int> ptr)
47 {
48 return *ptr.get();
49 }
50
TEST_F(PandaSmartPointersTest,MakePandaUniqueTest)51 TEST_F(PandaSmartPointersTest, MakePandaUniqueTest)
52 {
53 // Not array type
54
55 static constexpr int POINTER_VALUE = 5;
56
57 auto uniq_ptr = MakePandaUnique<int>(POINTER_VALUE);
58 ASSERT_NE(uniq_ptr.get(), nullptr);
59
60 int res = ReturnValueFromUniqPtr(std::move(uniq_ptr));
61 ASSERT_EQ(res, 5);
62 ASSERT_EQ(uniq_ptr.get(), nullptr);
63
64 // Unbounded array type
65
66 static constexpr size_t SIZE = 3;
67
68 auto uniq_ptr_2 = MakePandaUnique<int[]>(SIZE);
69 ASSERT_NE(uniq_ptr_2.get(), nullptr);
70
71 for (size_t i = 0; i < SIZE; ++i) {
72 uniq_ptr_2[i] = i;
73 }
74
75 auto uniq_ptr_3 = std::move(uniq_ptr_2);
76 for (size_t i = 0; i < SIZE; ++i) {
77 ASSERT_EQ(uniq_ptr_3[i], i);
78 }
79 ASSERT_EQ(uniq_ptr_2.get(), nullptr);
80 }
81
82 } // namespace panda::mem::test
83