• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "file_items.h"
17 #include "file_writer.h"
18 
19 #include <cstdint>
20 
21 #include <utility>
22 #include <vector>
23 
24 #include <gtest/gtest.h>
25 
26 namespace panda::panda_file::test {
27 
TEST(LineNumberProgramItem,EmitSpecialOpcode)28 TEST(LineNumberProgramItem, EmitSpecialOpcode)
29 {
30     LineNumberProgramItem item;
31 
32     constexpr int32_t LINE_MAX_INC = LineNumberProgramItem::LINE_RANGE + LineNumberProgramItem::LINE_BASE - 1;
33     constexpr int32_t LINE_MIN_INC = LineNumberProgramItem::LINE_BASE;
34 
35     EXPECT_FALSE(item.EmitSpecialOpcode(0, LINE_MAX_INC + 1));
36     EXPECT_FALSE(item.EmitSpecialOpcode(0, LINE_MIN_INC - 1));
37     EXPECT_FALSE(item.EmitSpecialOpcode(100, LINE_MAX_INC));
38 
39     std::vector<std::pair<int32_t, uint32_t>> incs = {{1, LINE_MIN_INC}, {2, LINE_MAX_INC}};
40     std::vector<uint8_t> data;
41 
42     for (auto [pc_inc, line_inc] : incs) {
43         ASSERT_TRUE(item.EmitSpecialOpcode(pc_inc, line_inc));
44         data.push_back((line_inc - LineNumberProgramItem::LINE_BASE) + (pc_inc * LineNumberProgramItem::LINE_RANGE) +
45                        LineNumberProgramItem::OPCODE_BASE);
46     }
47 
48     MemoryWriter writer;
49     ASSERT_TRUE(item.Write(&writer));
50 
51     EXPECT_EQ(writer.GetData(), data);
52 }
53 
54 }  // namespace panda::panda_file::test
55