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 BaseTestWithScope<false> {
24 };
25
CreateListNode(JSThread * thread,uint32_t bufferLength)26 static WaiterListNode *CreateListNode(JSThread *thread, uint32_t bufferLength)
27 {
28 EcmaVM *ecmaVm = thread->GetEcmaVM();
29 WaiterListNode *listNode = new WaiterListNode();
30 void *toBuffer = ecmaVm->GetNativeAreaAllocator()->AllocateBuffer(bufferLength);
31 EXPECT_TRUE(toBuffer != nullptr && listNode != nullptr);
32 listNode->date_ = toBuffer;
33 listNode->index_ = bufferLength;
34 listNode->waitPointer_ = reinterpret_cast<int8_t *>(toBuffer) + bufferLength;
35 listNode->waiting_ = true;
36 listNode->prev_ = nullptr;
37 listNode->next_ = nullptr;
38 return listNode;
39 }
40
DeleteListNode(JSThread * thread,WaiterListNode * listNode)41 static void DeleteListNode(JSThread *thread, WaiterListNode *listNode)
42 {
43 if (listNode != nullptr) {
44 thread->GetEcmaVM()->GetNativeAreaAllocator()->Delete(listNode->date_);
45 delete listNode;
46 listNode = nullptr;
47 }
48 return;
49 }
50
HWTEST_F_L0(WaiterListTest,CreateWaiterList)51 HWTEST_F_L0(WaiterListTest, CreateWaiterList)
52 {
53 WaiterList *waitLists = Singleton<WaiterList>::GetInstance();
54 EXPECT_TRUE(waitLists != nullptr);
55 }
56
HWTEST_F_L0(WaiterListTest,AddNode)57 HWTEST_F_L0(WaiterListTest, AddNode)
58 {
59 uint32_t bufferLength = 5;
60 WaiterList *waitLists = Singleton<WaiterList>::GetInstance();
61 WaiterListNode *listNode = CreateListNode(thread, bufferLength);
62 // add the listNode to waitlists
63 waitLists->AddNode(listNode);
64 auto indexOneIter = waitLists->locationListMap_.find(listNode->waitPointer_);
65 EXPECT_EQ(indexOneIter->second.pTail, listNode);
66 EXPECT_EQ(indexOneIter->second.pHead, listNode);
67 // change listNode property and add
68 listNode->waiting_ = false;
69 waitLists->AddNode(listNode);
70 auto indexOneIter1 = waitLists->locationListMap_.find(listNode->waitPointer_);
71 EXPECT_EQ(indexOneIter1->second.pTail, listNode);
72 EXPECT_EQ(indexOneIter1->second.pTail->next_, listNode);
73 EXPECT_EQ(indexOneIter1->second.pHead, listNode);
74 EXPECT_EQ(listNode->prev_, listNode);
75 EXPECT_EQ(listNode->next_, listNode);
76 EXPECT_EQ(listNode->prev_->waiting_, false);
77 DeleteListNode(thread, listNode);
78 }
79 } // namespace panda::test
80