1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include <gtest/gtest.h>
17800b99b8Sopenharmony_ci#include <memory>
18800b99b8Sopenharmony_ci#include <sys/types.h>
19800b99b8Sopenharmony_ci
20800b99b8Sopenharmony_ci#include "unique_stack_table.h"
21800b99b8Sopenharmony_ci
22800b99b8Sopenharmony_ciusing namespace OHOS::HiviewDFX;
23800b99b8Sopenharmony_ciusing namespace std;
24800b99b8Sopenharmony_ciusing namespace testing::ext;
25800b99b8Sopenharmony_ci
26800b99b8Sopenharmony_cinamespace OHOS {
27800b99b8Sopenharmony_cinamespace HiviewDFX {
28800b99b8Sopenharmony_cinamespace {
29800b99b8Sopenharmony_cistatic const int BUF_LEN = 100;
30800b99b8Sopenharmony_ci}
31800b99b8Sopenharmony_ci
32800b99b8Sopenharmony_ciclass StackTableTest : public testing::Test {
33800b99b8Sopenharmony_cipublic:
34800b99b8Sopenharmony_ci    static void SetUpTestCase(void) {}
35800b99b8Sopenharmony_ci    static void TearDownTestCase(void) {}
36800b99b8Sopenharmony_ci    void SetUp() {}
37800b99b8Sopenharmony_ci    void TearDown() {}
38800b99b8Sopenharmony_ci};
39800b99b8Sopenharmony_ci
40800b99b8Sopenharmony_cinamespace {
41800b99b8Sopenharmony_ci/**
42800b99b8Sopenharmony_ci * @tc.name: StackTableTest001
43800b99b8Sopenharmony_ci * @tc.desc: test StackTable functions
44800b99b8Sopenharmony_ci * @tc.type: FUNC
45800b99b8Sopenharmony_ci */
46800b99b8Sopenharmony_ciHWTEST_F(StackTableTest, StackTableTest001, TestSize.Level2)
47800b99b8Sopenharmony_ci{
48800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest001: start.";
49800b99b8Sopenharmony_ci    Node node;
50800b99b8Sopenharmony_ci    node.value = 1;
51800b99b8Sopenharmony_ci    char buf[BUF_LEN] = "6fd1bf000-6fd2bf000 rw-p 00000000 b3:07 1882 [anon:async_stack_table]";
52800b99b8Sopenharmony_ci    std::shared_ptr<UniqueStackTable> stackTable = make_shared<UniqueStackTable>(buf, sizeof(buf));
53800b99b8Sopenharmony_ci    ASSERT_EQ(stackTable->ImportNode(0, node), true);
54800b99b8Sopenharmony_ci    ASSERT_EQ(stackTable->GetHeadNode()->value, node.value);
55800b99b8Sopenharmony_ci    size_t functionsSize = stackTable->GetWriteSize();
56800b99b8Sopenharmony_ci    uint32_t oldSize = stackTable->GetTabelSize();
57800b99b8Sopenharmony_ci    uint32_t usedNodes = stackTable->GetUsedIndexes().size();
58800b99b8Sopenharmony_ci    auto pid = stackTable->GetPid();
59800b99b8Sopenharmony_ci    size_t resultSize = sizeof(pid) + sizeof(stackTable->GetTabelSize());
60800b99b8Sopenharmony_ci    size_t usedNodesSize = sizeof(usedNodes) + usedNodes * sizeof(uint32_t) + usedNodes * sizeof(uint64_t);
61800b99b8Sopenharmony_ci    ASSERT_EQ(usedNodesSize + resultSize, functionsSize);
62800b99b8Sopenharmony_ci    ASSERT_EQ(stackTable->Resize(), true);
63800b99b8Sopenharmony_ci    ASSERT_NE(stackTable->GetTabelSize(), oldSize);
64800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest001: end.";
65800b99b8Sopenharmony_ci}
66800b99b8Sopenharmony_ci
67800b99b8Sopenharmony_ci/**
68800b99b8Sopenharmony_ci * @tc.name: StackTableTest002
69800b99b8Sopenharmony_ci * @tc.desc: test StackTable functions
70800b99b8Sopenharmony_ci * @tc.type: FUNC
71800b99b8Sopenharmony_ci */
72800b99b8Sopenharmony_ciHWTEST_F(StackTableTest, StackTableTest002, TestSize.Level2)
73800b99b8Sopenharmony_ci{
74800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest002: start.";
75800b99b8Sopenharmony_ci    std::shared_ptr<UniqueStackTable> stackTable = make_shared<UniqueStackTable>(nullptr,
76800b99b8Sopenharmony_ci                                                                                 MAX_NODES_CNT * MAX_NODES_CNT);
77800b99b8Sopenharmony_ci    bool ret = stackTable->Init();
78800b99b8Sopenharmony_ci    ASSERT_EQ(ret, false);
79800b99b8Sopenharmony_ci    ret = stackTable->Resize();
80800b99b8Sopenharmony_ci    ASSERT_EQ(ret, false);
81800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest002: end.";
82800b99b8Sopenharmony_ci}
83800b99b8Sopenharmony_ci
84800b99b8Sopenharmony_ci/**
85800b99b8Sopenharmony_ci * @tc.name: StackTableTest003
86800b99b8Sopenharmony_ci * @tc.desc: test StackTable functions
87800b99b8Sopenharmony_ci * @tc.type: FUNC
88800b99b8Sopenharmony_ci */
89800b99b8Sopenharmony_ciHWTEST_F(StackTableTest, StackTableTest003, TestSize.Level2)
90800b99b8Sopenharmony_ci{
91800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest003: start.";
92800b99b8Sopenharmony_ci    std::shared_ptr<UniqueStackTable> stackTable = make_shared<UniqueStackTable>(nullptr, 0);
93800b99b8Sopenharmony_ci    bool ret = stackTable->Init();
94800b99b8Sopenharmony_ci    ASSERT_EQ(ret, false);
95800b99b8Sopenharmony_ci    ret = stackTable->Resize();
96800b99b8Sopenharmony_ci    ASSERT_EQ(ret, false);
97800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest003: end.";
98800b99b8Sopenharmony_ci}
99800b99b8Sopenharmony_ci
100800b99b8Sopenharmony_ci/**
101800b99b8Sopenharmony_ci * @tc.name: StackTableTest004
102800b99b8Sopenharmony_ci * @tc.desc: test StackTable functions
103800b99b8Sopenharmony_ci * @tc.type: FUNC
104800b99b8Sopenharmony_ci */
105800b99b8Sopenharmony_ciHWTEST_F(StackTableTest, StackTableTest004, TestSize.Level2)
106800b99b8Sopenharmony_ci{
107800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest004: start.";
108800b99b8Sopenharmony_ci    std::shared_ptr<UniqueStackTable> stackTable = make_shared<UniqueStackTable>(nullptr,
109800b99b8Sopenharmony_ci                                                                                 MAX_NODES_CNT * MAX_NODES_CNT);
110800b99b8Sopenharmony_ci    uint64_t ret = stackTable->PutPcsInTable(nullptr, nullptr, 0);
111800b99b8Sopenharmony_ci    ASSERT_EQ(ret, 0);
112800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest004: end.";
113800b99b8Sopenharmony_ci}
114800b99b8Sopenharmony_ci
115800b99b8Sopenharmony_ci/**
116800b99b8Sopenharmony_ci * @tc.name: StackTableTest005
117800b99b8Sopenharmony_ci * @tc.desc: test StackTable functions
118800b99b8Sopenharmony_ci * @tc.type: FUNC
119800b99b8Sopenharmony_ci */
120800b99b8Sopenharmony_ciHWTEST_F(StackTableTest, StackTableTest005, TestSize.Level2)
121800b99b8Sopenharmony_ci{
122800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest005: start.";
123800b99b8Sopenharmony_ci    std::shared_ptr<UniqueStackTable> stackTable = make_shared<UniqueStackTable>(nullptr,
124800b99b8Sopenharmony_ci                                                                                 MAX_NODES_CNT * MAX_NODES_CNT);
125800b99b8Sopenharmony_ci    size_t ret = stackTable->GetWriteSize();
126800b99b8Sopenharmony_ci    ASSERT_EQ(ret, 0);
127800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest005: end.";
128800b99b8Sopenharmony_ci}
129800b99b8Sopenharmony_ci
130800b99b8Sopenharmony_ci/**
131800b99b8Sopenharmony_ci * @tc.name: StackTableTest006
132800b99b8Sopenharmony_ci * @tc.desc: test StackTable functions
133800b99b8Sopenharmony_ci * @tc.type: FUNC
134800b99b8Sopenharmony_ci */
135800b99b8Sopenharmony_ciHWTEST_F(StackTableTest, StackTableTest006, TestSize.Level2)
136800b99b8Sopenharmony_ci{
137800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest006: start.";
138800b99b8Sopenharmony_ci    std::shared_ptr<UniqueStackTable> stackTable = make_shared<UniqueStackTable>(nullptr, 0);
139800b99b8Sopenharmony_ci    StackId stackId;
140800b99b8Sopenharmony_ci    std::vector<uintptr_t> pcs;
141800b99b8Sopenharmony_ci    bool ret = stackTable->GetPcsByStackId(stackId, pcs);
142800b99b8Sopenharmony_ci    ASSERT_EQ(ret, false);
143800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest006: end.";
144800b99b8Sopenharmony_ci}
145800b99b8Sopenharmony_ci
146800b99b8Sopenharmony_ci/**
147800b99b8Sopenharmony_ci * @tc.name: StackTableTest007
148800b99b8Sopenharmony_ci * @tc.desc: test StackTable functions
149800b99b8Sopenharmony_ci * @tc.type: FUNC
150800b99b8Sopenharmony_ci */
151800b99b8Sopenharmony_ciHWTEST_F(StackTableTest, StackTableTest007, TestSize.Level2)
152800b99b8Sopenharmony_ci{
153800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest007: start.";
154800b99b8Sopenharmony_ci    std::shared_ptr<UniqueStackTable> stackTable = make_shared<UniqueStackTable>(0, 0);
155800b99b8Sopenharmony_ci    Node node;
156800b99b8Sopenharmony_ci    bool ret = stackTable->ImportNode(MAX_NODES_CNT, node);
157800b99b8Sopenharmony_ci    ASSERT_EQ(ret, false);
158800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest007: end.";
159800b99b8Sopenharmony_ci}
160800b99b8Sopenharmony_ci
161800b99b8Sopenharmony_ci/**
162800b99b8Sopenharmony_ci * @tc.name: StackTableTest008
163800b99b8Sopenharmony_ci * @tc.desc: test StackTable functions
164800b99b8Sopenharmony_ci * @tc.type: FUNC
165800b99b8Sopenharmony_ci */
166800b99b8Sopenharmony_ciHWTEST_F(StackTableTest, StackTableTest008, TestSize.Level2)
167800b99b8Sopenharmony_ci{
168800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest008: start.";
169800b99b8Sopenharmony_ci    std::shared_ptr<UniqueStackTable> stackTable = make_shared<UniqueStackTable>(0, 0);
170800b99b8Sopenharmony_ci    uint64_t ret = stackTable->PutPcInSlot(0, 0);
171800b99b8Sopenharmony_ci    ASSERT_EQ(ret, 0);
172800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest008: end.";
173800b99b8Sopenharmony_ci}
174800b99b8Sopenharmony_ci
175800b99b8Sopenharmony_ci/**
176800b99b8Sopenharmony_ci * @tc.name: StackTableTest009
177800b99b8Sopenharmony_ci * @tc.desc: test StackTable functions
178800b99b8Sopenharmony_ci * @tc.type: FUNC
179800b99b8Sopenharmony_ci */
180800b99b8Sopenharmony_ciHWTEST_F(StackTableTest, StackTableTest009, TestSize.Level2)
181800b99b8Sopenharmony_ci{
182800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest009: start.";
183800b99b8Sopenharmony_ci    std::shared_ptr<UniqueStackTable> stackTable = make_shared<UniqueStackTable>(0, 0);
184800b99b8Sopenharmony_ci    Node* ret = stackTable->GetFrame(MAX_NODES_CNT);
185800b99b8Sopenharmony_ci    ASSERT_EQ(ret, nullptr);
186800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest009: end.";
187800b99b8Sopenharmony_ci}
188800b99b8Sopenharmony_ci
189800b99b8Sopenharmony_ci
190800b99b8Sopenharmony_ci/**
191800b99b8Sopenharmony_ci * @tc.name: StackTableTest010
192800b99b8Sopenharmony_ci * @tc.desc: test StackTable functions
193800b99b8Sopenharmony_ci * @tc.type: FUNC
194800b99b8Sopenharmony_ci */
195800b99b8Sopenharmony_ciHWTEST_F(StackTableTest, StackTableTest010, TestSize.Level2)
196800b99b8Sopenharmony_ci{
197800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest010: start.";
198800b99b8Sopenharmony_ci    const size_t size = (MAX_NODES_CNT + 10) * sizeof(Node);
199800b99b8Sopenharmony_ci    std::shared_ptr<UniqueStackTable> stackTable = make_shared<UniqueStackTable>(nullptr,
200800b99b8Sopenharmony_ci                                                                                 size);
201800b99b8Sopenharmony_ci    bool ret = stackTable->Init();
202800b99b8Sopenharmony_ci    ASSERT_EQ(ret, false);
203800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "StackTableTest010: end.";
204800b99b8Sopenharmony_ci}
205800b99b8Sopenharmony_ci}
206800b99b8Sopenharmony_ci} // namespace HiviewDFX
207800b99b8Sopenharmony_ci} // namespace OHOS