• 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/tests/test_helper.h"
17 #include "common_interfaces/objects/base_string_table.h"
18 #include "common_components/objects/string_table_internal.h"
19 #include "common_interfaces/thread/mutator_base.h"
20 #include "common_interfaces/objects/base_string.h"
21 #include "common_interfaces/thread/thread_holder.h"
22 #include "common_interfaces/base_runtime.h"
23 #include "common_interfaces/heap/heap_allocator.h"
24 #include "common_interfaces/objects/string/base_string-inl2.h"
25 
26 
27 namespace common {
28 
29 struct DummyMutator : public MutatorBase {
DummyMutatorcommon::DummyMutator30     explicit DummyMutator(LanguageType lang) : lang_(lang) {}
31     LanguageType lang_;
32 };
33 
34 class BaseStringTableTest : public common::test::BaseTestWithScope {
35 protected:
36     using TableType = BaseStringTableInternal<false>;
37     BaseRuntime* runtime_;
38     std::unique_ptr<MutatorBase> mutator_;
39     std::unique_ptr<TableType> table_;
40     ThreadHolder* threadHolder_;
41 
SetUp()42     void SetUp() override
43     {
44         mutator_ = std::make_unique<DummyMutator>(LanguageType::DYNAMIC);
45         threadHolder_ = new ThreadHolder(mutator_.get());
46 
47         runtime_ = BaseRuntime::GetInstance();
48         ASSERT_TRUE(runtime_ != nullptr);
49 
50         runtime_->Init();
51 
52         table_ = std::make_unique<TableType>();
53     }
54 
TearDown()55     void TearDown() override
56     {
57         table_.reset();
58 
59         if (runtime_) {
60             runtime_->Fini();
61         }
62 
63         mutator_.reset();
64         threadHolder_ = nullptr;
65     }
66 
CreateUtf8String(const char * utf8Data,uint32_t length,bool canBeCompress)67     BaseString* CreateUtf8String(const char* utf8Data, uint32_t length, bool canBeCompress)
68     {
69         auto allocator = [](size_t size, CommonType type) -> BaseString* {
70             void* mem = reinterpret_cast<void*>(HeapAllocator::AllocateInOldOrHuge(size, LanguageType::DYNAMIC));
71             if (mem == nullptr) {
72                 return nullptr;
73             }
74             return reinterpret_cast<BaseString*>(mem);
75         };
76 
77         BaseString* str = BaseString::CreateFromUtf8(allocator,
78             reinterpret_cast<const uint8_t*>(utf8Data), length, canBeCompress);
79 
80         if (str == nullptr) {
81             return nullptr;
82         }
83         return str;
84     }
85 
MockHandleCreator(ThreadHolder * holder,BaseString * str)86     static ReadOnlyHandle<BaseString> MockHandleCreator(ThreadHolder* holder, BaseString* str)
87     {
88         uintptr_t handleValue = reinterpret_cast<uintptr_t>(str);
89         return ReadOnlyHandle<BaseString>(handleValue);
90     }
91 };
92 
HWTEST_F_L0(BaseStringTableTest,SweepWeakRef)93 HWTEST_F_L0(BaseStringTableTest, SweepWeakRef)
94 {
95     WeakRefFieldVisitor mockVisitor = [](RefField<false>& field) {
96         return true;
97     };
98 
99     table_->GetHashTrieMap().StartSweeping();
100     table_->SweepWeakRef(mockVisitor);
101     table_->GetHashTrieMap().FinishSweeping();
102 
103     EXPECT_TRUE(true);
104 }
105 
HWTEST_F_L0(BaseStringTableTest,CleanUp)106 HWTEST_F_L0(BaseStringTableTest, CleanUp)
107 {
108     table_->GetHashTrieMap().Clear();
109     EXPECT_TRUE(true);
110 }
111 
112 }