1 /*
2 * Copyright (c) 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 "ecmascript/waiter_list.h"
17 #include "ecmascript/tests/test_helper.h"
18
19 using namespace panda;
20 using namespace panda::ecmascript;
21
22 namespace panda::test {
23 class WaiterListTest : public testing::Test {
24 public:
SetUpTestCase()25 static void SetUpTestCase()
26 {
27 GTEST_LOG_(INFO) << "SetUpTestCase";
28 }
29
TearDownTestCase()30 static void TearDownTestCase()
31 {
32 GTEST_LOG_(INFO) << "TearDownCase";
33 }
34
SetUp()35 void SetUp() override
36 {
37 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
38 }
39
TearDown()40 void TearDown() override
41 {
42 TestHelper::DestroyEcmaVMWithScope(instance, scope);
43 }
44
45 EcmaVM *instance {nullptr};
46 EcmaHandleScope *scope {nullptr};
47 JSThread *thread {nullptr};
48 };
49
CreateListNode(JSThread * thread,uint32_t bufferLength)50 static WaiterListNode *CreateListNode(JSThread *thread, uint32_t bufferLength)
51 {
52 EcmaVM *ecmaVm = thread->GetEcmaVM();
53 WaiterListNode *listNode = new WaiterListNode();
54 void *toBuffer = ecmaVm->GetNativeAreaAllocator()->AllocateBuffer(bufferLength);
55 EXPECT_TRUE(toBuffer != nullptr && listNode != nullptr);
56 listNode->date_ = toBuffer;
57 listNode->index_ = bufferLength;
58 listNode->waitPointer_ = reinterpret_cast<int8_t *>(toBuffer) + bufferLength;
59 listNode->waiting_ = true;
60 listNode->prev_ = nullptr;
61 listNode->next_ = nullptr;
62 return listNode;
63 }
64
DeleteListNode(JSThread * thread,WaiterListNode * listNode)65 static void DeleteListNode(JSThread *thread, WaiterListNode *listNode)
66 {
67 if (listNode != nullptr) {
68 thread->GetEcmaVM()->GetNativeAreaAllocator()->Delete(listNode->date_);
69 delete listNode;
70 listNode = nullptr;
71 }
72 return;
73 }
74
HWTEST_F_L0(WaiterListTest,CreateWaiterList)75 HWTEST_F_L0(WaiterListTest, CreateWaiterList)
76 {
77 WaiterList *waitLists = Singleton<WaiterList>::GetInstance();
78 EXPECT_TRUE(waitLists != nullptr);
79 }
80
HWTEST_F_L0(WaiterListTest,AddNode)81 HWTEST_F_L0(WaiterListTest, AddNode)
82 {
83 uint32_t bufferLength = 5;
84 WaiterList *waitLists = Singleton<WaiterList>::GetInstance();
85 WaiterListNode *listNode = CreateListNode(thread, bufferLength);
86 // add the listNode to waitlists
87 waitLists->AddNode(listNode);
88 auto indexOneIter = waitLists->locationListMap_.find(listNode->waitPointer_);
89 EXPECT_EQ(indexOneIter->second.pTail, listNode);
90 EXPECT_EQ(indexOneIter->second.pHead, listNode);
91 // change listNode property and add
92 listNode->waiting_ = false;
93 waitLists->AddNode(listNode);
94 auto indexOneIter1 = waitLists->locationListMap_.find(listNode->waitPointer_);
95 EXPECT_EQ(indexOneIter1->second.pTail, listNode);
96 EXPECT_EQ(indexOneIter1->second.pTail->next_, listNode);
97 EXPECT_EQ(indexOneIter1->second.pHead, listNode);
98 EXPECT_EQ(listNode->prev_, listNode);
99 EXPECT_EQ(listNode->next_, listNode);
100 EXPECT_EQ(listNode->prev_->waiting_, false);
101 DeleteListNode(thread, listNode);
102 }
103 } // namespace panda::test
104