• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <cstdlib>
17 #include "disassembler.h"
18 #include <gtest/gtest.h>
19 #include <string>
20 
21 using namespace testing::ext;
22 namespace panda::disasm {
23 
24 static const std::string ANNOTATION_TEST_FILE_NAME_001 = GRAPH_TEST_ABC_DIR "script-string1.abc";
25 static const std::string ANNOTATION_TEST_FILE_NAME_002 = GRAPH_TEST_ABC_DIR "script-string2.abc";
26 
27 class DisassemblerStringTest : public testing::Test {
28 public:
SetUpTestCase(void)29     static void SetUpTestCase(void) {};
TearDownTestCase(void)30     static void TearDownTestCase(void) {};
SetUp()31     void SetUp() {};
TearDown()32     void TearDown() {};
33 
ValidateString(const std::vector<std::string> & strings,const std::vector<std::string> & expected_strings)34     bool ValidateString(const std::vector<std::string> &strings,
35                         const std::vector<std::string> &expected_strings)
36     {
37         for (const auto &expected_string : expected_strings) {
38             const auto string_iter = std::find(strings.begin(), strings.end(), expected_string);
39             if (string_iter == strings.end()) {
40                 return false;
41             }
42         }
43 
44         return true;
45     }
46 };
47 
48 /**
49 * @tc.name: disassembler_string_test_001
50 * @tc.desc: get existed string.
51 * @tc.type: FUNC
52 * @tc.require: file path and name
53 */
54 HWTEST_F(DisassemblerStringTest, disassembler_string_test_001, TestSize.Level1)
55 {
56     std::vector<std::string> expected_strings = {"ClassA", "prototype", "str", "test"};
57     panda::disasm::Disassembler disasm {};
58     disasm.Disassemble(ANNOTATION_TEST_FILE_NAME_001, false, false);
59     std::vector<std::string> strings = disasm.GetStrings();
60     bool has_string = ValidateString(strings, expected_strings);
61     EXPECT_TRUE(has_string);
62 }
63 
64 /**
65 * @tc.name: disassembler_string_test_002
66 * @tc.desc: get not existed string.
67 * @tc.type: FUNC
68 * @tc.require: file path and name
69 */
70 HWTEST_F(DisassemblerStringTest, disassembler_string_test_002, TestSize.Level1)
71 {
72     std::vector<std::string> expected_strings = {"Student", "name", "prototype", "student_name"};
73     panda::disasm::Disassembler disasm {};
74     disasm.Disassemble(ANNOTATION_TEST_FILE_NAME_002, false, false);
75     std::vector<std::string> strings = disasm.GetStrings();
76     bool has_string = ValidateString(strings, expected_strings);
77     EXPECT_TRUE(has_string);
78 }
79 
80 };
81