1 /*
2 * Copyright (c) 2024 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 "gtest/gtest.h"
17 #include <iostream>
18 #include <unistd.h>
19 #include <climits>
20
21 #include "cgbb.h"
22 #include "mempool.h"
23 #include "cgfunc.h"
24
25 using namespace maplebe;
26 using namespace std;
27 namespace {
CreateInsnObj(std::string poolName,maple::uint32 opc)28 static maplebe::Insn CreateInsnObj(std::string poolName, maple::uint32 opc)
29 {
30 MemPoolCtrler memPoolCtrler;
31 MemPool memPool(memPoolCtrler, poolName);
32 maplebe::Insn Insn_obj(memPool, opc);
33 return Insn_obj;
34 }
CreateBBObj(uint32 bbID,std::string poolName)35 static maplebe::BB CreateBBObj(uint32 bbID, std::string poolName)
36 {
37 // BBID bbID, MapleAllocator &mallocator
38 MemPoolCtrler memPoolCtrler;
39 MemPool memPool(memPoolCtrler, poolName);
40 MapleAllocator mallocator(&memPool);
41 maplebe::BB BB_obj(bbID, mallocator);
42 return BB_obj;
43 }
44
TEST(InsertInsnBefore_FUNC,t01)45 TEST(InsertInsnBefore_FUNC, t01)
46 {
47 maplebe::BB test_bb = CreateBBObj(10, "BB_pool");
48 maplebe::Insn existing = CreateInsnObj("existing", 32);
49 maplebe::Insn newInsn = CreateInsnObj("newInsn", 3);
50
51 test_bb.SetFirstInsn(&existing);
52 EXPECT_EQ(existing.GetPrev(), nullptr);
53 maplebe::Insn *ans = test_bb.InsertInsnBefore(existing, newInsn);
54 EXPECT_EQ(ans, &newInsn);
55
56 maplebe::Insn newInsn1 = CreateInsnObj("newInsn1", 3);
57 EXPECT_NE(existing.GetPrev(), nullptr);
58 ans = test_bb.InsertInsnBefore(existing, newInsn1);
59 EXPECT_EQ(ans, &newInsn1);
60 EXPECT_EQ(newInsn.GetNext(), &newInsn1);
61 }
62
TEST(InsertInsnAfter_FUNC,t02)63 TEST(InsertInsnAfter_FUNC, t02)
64 {
65 maplebe::BB test_bb = CreateBBObj(2, "BB_pool");
66 maplebe::Insn existing = CreateInsnObj("existing", 32);
67 maplebe::Insn newInsn1 = CreateInsnObj("newInsn1", 1), newInsn2 = CreateInsnObj("newInsn2", 2);
68
69 maplebe::Insn *ans;
70 test_bb.SetFirstInsn(&existing);
71 ans = test_bb.InsertInsnAfter(existing, newInsn1);
72 EXPECT_EQ(ans, &newInsn1);
73 EXPECT_EQ(newInsn1.GetNext(), nullptr);
74
75 ans = test_bb.InsertInsnAfter(existing, newInsn2);
76 EXPECT_EQ(ans, &newInsn2);
77 EXPECT_EQ(newInsn2.GetNext(), &newInsn1);
78 }
79
TEST(RemoveInsn_FUNC,t03)80 TEST(RemoveInsn_FUNC, t03)
81 {
82 /*void BB::RemoveInsn(Insn &insn)*/
83 maplebe::BB test_bb = CreateBBObj(10, "BB_pool");
84 maplebe::Insn insn = CreateInsnObj("insn", 0);
85 maplebe::Insn insn1 = CreateInsnObj("insn1", 1);
86 maplebe::Insn insn2 = CreateInsnObj("insn2", 2);
87
88 test_bb.SetFirstInsn(&insn);
89 test_bb.RemoveInsn(insn);
90 EXPECT_EQ(test_bb.GetFirstInsn(), nullptr);
91
92 test_bb.SetFirstInsn(&insn);
93 test_bb.InsertInsnAfter(insn, insn1);
94 test_bb.InsertInsnAfter(insn1, insn2);
95 test_bb.RemoveInsn(insn);
96 EXPECT_EQ(test_bb.GetFirstInsn(), &insn1);
97 EXPECT_EQ((*(insn.GetNext())).GetPrev(), nullptr);
98 }
99 } // namespace