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 <iostream>
17 #include <string>
18
19 #include <gtest/gtest.h>
20 #include "disassembler.h"
21
22 using namespace panda::disasm;
23
24 std::string g_bin_path_abs {};
25
TEST(record_test,empty_record)26 TEST(record_test, empty_record)
27 {
28 Disassembler d {};
29
30 std::stringstream ss {};
31 d.Disassemble(g_bin_path_abs + "empty_record.bc");
32 d.Serialize(ss, false);
33
34 EXPECT_TRUE(ss.str().find(".record A {\n}") != std::string::npos) << "record translated incorrectly";
35 }
36
TEST(record_test,record_with_fields)37 TEST(record_test, record_with_fields)
38 {
39 Disassembler d {};
40
41 std::stringstream ss {};
42 d.Disassemble(g_bin_path_abs + "record_with_fields.bc");
43 d.Serialize(ss, false);
44
45 EXPECT_TRUE(ss.str().find("u1 a") != std::string::npos) << "u1 translated incorrectly";
46 EXPECT_TRUE(ss.str().find("i8 b") != std::string::npos) << "i8 translated incorrectly";
47 EXPECT_TRUE(ss.str().find("u8 c") != std::string::npos) << "u8 translated incorrectly";
48 EXPECT_TRUE(ss.str().find("i16 d") != std::string::npos) << "i16 translated incorrectly";
49 EXPECT_TRUE(ss.str().find("u16 e") != std::string::npos) << "u16 translated incorrectly";
50 EXPECT_TRUE(ss.str().find("i32 f") != std::string::npos) << "i32 translated incorrectly";
51 EXPECT_TRUE(ss.str().find("u32 g") != std::string::npos) << "u32 translated incorrectly";
52 EXPECT_TRUE(ss.str().find("f32 h") != std::string::npos) << "f32 translated incorrectly";
53 EXPECT_TRUE(ss.str().find("f64 i") != std::string::npos) << "f64 translated incorrectly";
54 EXPECT_TRUE(ss.str().find("i64 j") != std::string::npos) << "i64 translated incorrectly";
55 EXPECT_TRUE(ss.str().find("u64 k") != std::string::npos) << "u64 translated incorrectly";
56 }
57
TEST(record_test,record_with_record)58 TEST(record_test, record_with_record)
59 {
60 Disassembler d {};
61
62 std::stringstream ss {};
63 d.Disassemble(g_bin_path_abs + "record_in_record.bc");
64 d.Serialize(ss, false);
65
66 size_t beg_a = ss.str().find(".record A");
67 size_t end_a = ss.str().find('}', beg_a);
68 size_t beg_b = ss.str().find(".record B");
69 size_t end_b = ss.str().find("}", beg_b);
70
71 ASSERT_TRUE(beg_a != std::string::npos && end_a != std::string::npos) << "record A not found";
72 ASSERT_TRUE(beg_b != std::string::npos && end_b != std::string::npos) << "record B not found";
73
74 std::string rec_a = ss.str().substr(beg_a, end_a + 1);
75 std::string rec_b = ss.str().substr(beg_b, end_b + 1);
76
77 EXPECT_TRUE(rec_a.find("i64 aw") != std::string::npos);
78
79 EXPECT_TRUE(rec_b.find("A a") != std::string::npos);
80 EXPECT_TRUE(rec_b.find("i32 c") != std::string::npos);
81 }
82
main(int argc,char ** argv)83 int main(int argc, char **argv)
84 {
85 std::string dir_basename {};
86 std::string glob_argv0 = argv[0];
87
88 size_t last_slash_idx = glob_argv0.rfind('/');
89
90 if (std::string::npos != last_slash_idx) {
91 dir_basename = glob_argv0.substr(0, last_slash_idx + 1);
92 }
93
94 g_bin_path_abs = dir_basename + "../disassembler/bin/";
95
96 ::testing::InitGoogleTest(&argc, argv);
97 return RUN_ALL_TESTS();
98 }
99