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 <cstddef>
17 #include <cstdint>
18 #include <gtest/gtest.h>
19 #include <sstream>
20 #include <type_traits>
21 
22 #include "bytecode_instruction-inl.h"
23 
24 namespace panda::test {
25 
TEST(BytecodeInstruction, IsJumpInstruction)26 TEST(BytecodeInstruction, IsJumpInstruction)
27 {
28     {
29         // jmp -22
30         const uint8_t bytecode[] = {0x4d, 0xea};
31         BytecodeInstruction inst(bytecode);
32         EXPECT_EQ(inst.IsJumpInstruction(), true);
33     }
34     {
35         // a non-jump instruction
36         const uint8_t bytecode[] = {0x08, 0x0b, 0x03, 0x06};
37         BytecodeInstruction inst(bytecode);
38         EXPECT_EQ(inst.IsJumpInstruction(), false);
39     }
40 }
41 
TEST(BytecodeInstruction, IsRangeInstruction)42 TEST(BytecodeInstruction, IsRangeInstruction)
43 {
44     {
45         // newobjrange 0xb, 0x3, v6
46         const uint8_t bytecode[] = {0x08, 0x0b, 0x03, 0x06};
47         BytecodeInstruction inst(bytecode);
48         EXPECT_EQ(inst.IsRangeInstruction(), true);
49     }
50     {
51         // a non-range instruction
52         const uint8_t bytecode[] = {0x44, 0x58};
53         BytecodeInstruction inst(bytecode);
54         EXPECT_EQ(inst.IsRangeInstruction(), false);
55     }
56 }
57 
TEST(BytecodeInstruction, GetRangeInsLastRegIdx)58 TEST(BytecodeInstruction, GetRangeInsLastRegIdx) {
59     {
60         // a non-range instruction
61         const uint8_t bytecode[] = {0x3f, 0x0a, 0x00, 0x00};
62         BytecodeInstruction inst(bytecode);
63         EXPECT_EQ(inst.GetRangeInsLastRegIdx(), std::nullopt);
64     }
65     {
66         // newobjrange 0xb, 0x3, v6
67         const uint8_t bytecode[] = {0x08, 0x0b, 0x03, 0x06};
68         BytecodeInstruction inst(bytecode);
69         EXPECT_EQ(inst.GetRangeInsLastRegIdx().value(), 8);
70     }
71 }
72 }