• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "common_components/heap/allocator/allocator.h"
17 #include "common_components/tests/test_helper.h"
18 #include <cstdint>
19 #include <gtest/gtest.h>
20 
21 using namespace common;
22 
23 namespace common::test {
24 
25 class TestAllocator : public Allocator {
26 public:
GetIsAsyncAllocationEnable() const27     bool GetIsAsyncAllocationEnable() const
28     {
29         return isAsyncAllocationEnable_;
30     }
Allocate(size_t size,AllocType allocType)31     HeapAddress Allocate(size_t size, AllocType allocType) override { return 0; }
AllocateNoGC(size_t size,AllocType allocType)32     HeapAddress AllocateNoGC(size_t size, AllocType allocType) override { return 0; }
ForEachObject(const std::function<void (BaseObject *)> &,bool safe) const33     bool ForEachObject(const std::function<void(BaseObject*)>&, bool safe) const override { return true; }
ReclaimGarbageMemory(bool releaseAll)34     size_t ReclaimGarbageMemory(bool releaseAll) override { return 0; }
LargeObjectSize() const35     size_t LargeObjectSize() const override { return 0; }
GetAllocatedBytes() const36     size_t GetAllocatedBytes() const override { return 0; }
Init(const RuntimeParam & param)37     void Init(const RuntimeParam& param) override {}
GetMaxCapacity() const38     size_t GetMaxCapacity() const override { return 0; }
GetCurrentCapacity() const39     size_t GetCurrentCapacity() const override { return 0; }
GetUsedPageSize() const40     size_t GetUsedPageSize() const override { return 0; }
GetSpaceStartAddress() const41     HeapAddress GetSpaceStartAddress() const override { return 0; }
GetSpaceEndAddress() const42     HeapAddress GetSpaceEndAddress() const override { return 0; }
43 #ifndef NDEBUG
IsHeapObject(HeapAddress) const44     bool IsHeapObject(HeapAddress) const override { return false; }
45 #endif
FeedHungryBuffers()46     void FeedHungryBuffers() override {}
GetSurvivedSize() const47     size_t GetSurvivedSize() const override { return 0; }
48 };
49 class AllocatorTest : public common::test::BaseTestWithScope {
50 };
51 
HWTEST_F_L0(AllocatorTest,EnvNotSet)52 HWTEST_F_L0(AllocatorTest, EnvNotSet)
53 {
54     unsetenv("arkEnableAsyncAllocation");
55 
56     TestAllocator allocator;
57     bool result = allocator.GetIsAsyncAllocationEnable();
58 #if defined(PANDA_TARGET_OHOS)
59     EXPECT_TRUE(result);
60 #else
61     EXPECT_FALSE(result);
62 #endif
63 }
64 
HWTEST_F_L0(AllocatorTest,InvalidLength)65 HWTEST_F_L0(AllocatorTest, InvalidLength)
66 {
67     setenv("arkEnableAsyncAllocation", "12", 1);
68 
69     TestAllocator allocator;
70     bool result = allocator.GetIsAsyncAllocationEnable();
71 
72 #if defined(PANDA_TARGET_OHOS)
73     EXPECT_TRUE(result);
74 #else
75     EXPECT_FALSE(result);
76 #endif
77 }
78 
HWTEST_F_L0(AllocatorTest,InitAsyncAllocation_ValidLength)79 HWTEST_F_L0(AllocatorTest, InitAsyncAllocation_ValidLength)
80 {
81     setenv("arkEnableAsyncAllocation", "1", 1);
82     TestAllocator allocator;
83     bool result = allocator.GetIsAsyncAllocationEnable();
84     EXPECT_TRUE(result);
85 }
86 
HWTEST_F_L0(AllocatorTest,InitAsyncAllocation_Zero)87 HWTEST_F_L0(AllocatorTest, InitAsyncAllocation_Zero)
88 {
89     setenv("arkEnableAsyncAllocation", "0", 1);
90     TestAllocator allocator;
91     bool result = allocator.GetIsAsyncAllocationEnable();
92     EXPECT_FALSE(result);
93 }
94 
HWTEST_F_L0(AllocatorTest,InitAsyncAllocation_InvalidChar)95 HWTEST_F_L0(AllocatorTest, InitAsyncAllocation_InvalidChar)
96 {
97     setenv("arkEnableAsyncAllocation", "a", 1);
98     TestAllocator allocator;
99     bool result = allocator.GetIsAsyncAllocationEnable();
100     EXPECT_TRUE(result);
101 }
102 }
103