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 <cstdlib>
17 #include <gtest/gtest.h>
18 #include <iostream>
19 #include <string>
20 #include <vector>
21 #include "abc2program_driver.h"
22 #include "abc2program_test_utils.h"
23 #include "common/abc_file_utils.h"
24 #include "dump_utils.h"
25 #include "modifiers.h"
26
27 using namespace testing::ext;
28
29 namespace panda::abc2program {
30
31 struct FuncName {
32 std::string hello_world = ".#~@0=#HelloWorld";
33 std::string foo = ".#*#foo";
34 std::string goo = ".#*#goo";
35 std::string hoo = ".#*#hoo";
36 std::string main = ".func_main_0";
37 std::string method = ".#*@3*#method";
38 std::string lit = ".#~@1>#lit";
39 std::string add = ".#*#add";
40 std::string generate = ".#*#generateFunc";
41 std::string async_generate = ".#*#asyncGenerateFunc";
42 std::string async_arrow = ".#*#asyncArrowFunc";
43 std::string nested_literal_array = ".#~@2>#NestedLiteralArray";
44 std::string class_with_default_annotation = ".#~A=#A";
45 std::string class_with_spec_annotation = ".#~B=#B";
46 };
47
48 struct RecordName {
49 std::string annotation = "Anno";
50 };
51
52 const FuncName FUNC_NAME;
53 const RecordName RECORD_NAME;
54 const std::string HELLO_WORLD_ABC_TEST_FILE_NAME = GRAPH_TEST_ABC_DIR "HelloWorld.abc";
55 const std::string HELLO_WORLD_DEBUG_ABC_TEST_FILE_NAME = GRAPH_TEST_ABC_DIR "HelloWorldDebug.abc";
56 const std::string JSON_ABC_TEST_FILE_NAME = GRAPH_TEST_ABC_DIR "JsonTest.abc";
57 const std::string JSON_TEST_FILE_NAME = "JsonTest";
58 const bool REMOVE_DUMP_RESULT_FILES = true;
59 const std::string HELLO_WORLD_DUMP_RESULT_FILE_NAME = GRAPH_TEST_ABC_DUMP_DIR "HelloWorldDumpResult.txt";
60 const std::string HELLO_WORLD_DEBUG_DUMP_RESULT_FILE_NAME = GRAPH_TEST_ABC_DUMP_DIR "HelloWorldDebugDumpResult.txt";
61 const std::string HELLO_WORLD_DUMP_EXPECTED_FILE_NAME = GRAPH_TEST_ABC_DUMP_DIR "HelloWorldDumpExpected.txt";
62 const std::string HELLO_WORLD_DEBUG_DUMP_EXPECTED_FILE_NAME = GRAPH_TEST_ABC_DUMP_DIR "HelloWorldDebugDumpExpected.txt";
63 constexpr uint32_t NUM_OF_CODE_TEST_UT_FOO_METHOD_INS = 72;
64 constexpr uint8_t INS_SIZE_OF_FUNCTION_HOO = 4;
65 constexpr uint8_t IMMS_SIZE_OF_OPCODE_FLDAI = 1;
66 constexpr uint8_t SIZE_OF_LITERAL_ARRAY_TABLE = 7;
67 constexpr uint8_t TOTAL_NUM_OF_ASYNC_METHOD_LITERALS = 6;
68 constexpr uint8_t TOTAL_NUM_OF_MODULE_LITERALS = 21;
69 constexpr uint8_t TOTAL_NUM_OF_NESTED_LITERALS = 10;
70 constexpr uint8_t NUM_OF_MODULE_REQUESTS = 4;
71 constexpr uint8_t NUM_OF_REGULAR_IMPORTS = 1;
72 constexpr uint8_t NUM_OF_NAMESPACE_IMPORTS = 1;
73 constexpr uint8_t NUM_OF_LOCAL_EXPORTS = 1;
74 constexpr uint8_t NUM_OF_INDIRECT_EXPORTS = 1;
75 constexpr uint8_t NUM_OF_STAR_EXPORTS = 1;
76 const std::string ANNOTATIONS_ABC_TEST_FILE_NAME = GRAPH_TEST_ABC_DIR "Annotations.abc";
77
78
GetFunction(const std::string & name,const pandasm::Program & program)79 static const pandasm::Function *GetFunction(const std::string &name,
80 const pandasm::Program &program)
81 {
82 ASSERT(program.function_table.find(name) != program.function_table.end());
83 return &(program.function_table.find(name)->second);
84 }
85
GetRecord(const std::string & name,const pandasm::Program & program)86 static const pandasm::Record *GetRecord(const std::string &name,
87 const pandasm::Program &program)
88 {
89 ASSERT(program.record_table.find(name) != program.record_table.end());
90 return &(program.record_table.find(name)->second);
91 }
92
93 class Abc2ProgramHelloWorldTest : public testing::Test {
94 public:
SetUpTestCase(void)95 static void SetUpTestCase(void) {}
TearDownTestCase(void)96 static void TearDownTestCase(void) {}
SetUp()97 void SetUp()
98 {
99 (void)driver_.Compile(HELLO_WORLD_ABC_TEST_FILE_NAME);
100 prog_ = &(driver_.GetProgram());
101 hello_world_function_ = GetFunction(FUNC_NAME.hello_world, *prog_);
102 foo_function_ = GetFunction(FUNC_NAME.foo, *prog_);
103 goo_function_ = GetFunction(FUNC_NAME.goo, *prog_);
104 hoo_function_ = GetFunction(FUNC_NAME.hoo, *prog_);
105 main_function_ = GetFunction(FUNC_NAME.main, *prog_);
106 method_function_ = GetFunction(FUNC_NAME.method, *prog_);
107 lit_function_ = GetFunction(FUNC_NAME.lit, *prog_);
108 add_function_ = GetFunction(FUNC_NAME.add, *prog_);
109 generate_function_ = GetFunction(FUNC_NAME.generate, *prog_);
110 async_generate_function_ = GetFunction(FUNC_NAME.async_generate, *prog_);
111 async_arrow_function_ = GetFunction(FUNC_NAME.async_arrow, *prog_);
112 }
113
TearDown()114 void TearDown() {}
115
116 Abc2ProgramDriver driver_;
117 const pandasm::Program *prog_ = nullptr;
118 const pandasm::Function *hello_world_function_ = nullptr;
119 const pandasm::Function *foo_function_ = nullptr;
120 const pandasm::Function *goo_function_ = nullptr;
121 const pandasm::Function *hoo_function_ = nullptr;
122 const pandasm::Function *main_function_ = nullptr;
123 const pandasm::Function *method_function_ = nullptr;
124 const pandasm::Function *lit_function_ = nullptr;
125 const pandasm::Function *add_function_ = nullptr;
126 const pandasm::Function *generate_function_ = nullptr;
127 const pandasm::Function *async_generate_function_ = nullptr;
128 const pandasm::Function *async_arrow_function_ = nullptr;
129 };
130
131 class Abc2ProgramHelloWorldDebugTest : public testing::Test {
132 public:
SetUpTestCase(void)133 static void SetUpTestCase(void) {}
TearDownTestCase(void)134 static void TearDownTestCase(void) {}
SetUp()135 void SetUp()
136 {
137 (void)driver_.Compile(HELLO_WORLD_DEBUG_ABC_TEST_FILE_NAME);
138 prog_ = &(driver_.GetProgram());
139 foo_function_ = GetFunction(FUNC_NAME.foo, *prog_);
140 main_function_ = GetFunction(FUNC_NAME.main, *prog_);
141 }
142
TearDown()143 void TearDown() {}
144
145 Abc2ProgramDriver driver_;
146 const pandasm::Program *prog_ = nullptr;
147 const pandasm::Function *foo_function_ = nullptr;
148 const pandasm::Function *main_function_ = nullptr;
149 };
150
151 class Abc2ProgramJsonTest : public testing::Test {
152 public:
SetUpTestCase(void)153 static void SetUpTestCase(void) {}
TearDownTestCase(void)154 static void TearDownTestCase(void) {}
SetUp()155 void SetUp()
156 {
157 (void)driver_.Compile(JSON_ABC_TEST_FILE_NAME);
158 prog_ = &(driver_.GetProgram());
159 }
160
TearDown()161 void TearDown() {}
162
163 Abc2ProgramDriver driver_;
164 const pandasm::Program *prog_ = nullptr;
165 };
166
167 class Abc2ProgramAnnotationTest : public testing::Test {
168 public:
SetUpTestCase(void)169 static void SetUpTestCase(void) {}
TearDownTestCase(void)170 static void TearDownTestCase(void) {}
SetUp()171 void SetUp()
172 {
173 (void)driver_.Compile(ANNOTATIONS_ABC_TEST_FILE_NAME);
174 prog_ = &(driver_.GetProgram());
175 a_function_ = GetFunction(FUNC_NAME.class_with_default_annotation, *prog_);
176 b_function_ = GetFunction(FUNC_NAME.class_with_spec_annotation, *prog_);
177 anno_record_ = GetRecord(RECORD_NAME.annotation, *prog_);
178 }
179
TearDown()180 void TearDown() {}
181
182 Abc2ProgramDriver driver_;
183 const pandasm::Program *prog_ = nullptr;
184 const pandasm::Function *a_function_ = nullptr;
185 const pandasm::Function *b_function_ = nullptr;
186 const pandasm::Record *anno_record_ = nullptr;
187 };
188
189 /*------------------------------------- Cases of release mode below -------------------------------------*/
190
191 /**
192 * @tc.name: abc2program_hello_world_test_dump
193 * @tc.desc: check dump result in release mode.
194 * @tc.type: FUNC
195 * @tc.require: IADG92
196 */
197 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_hello_world_test_dump, TestSize.Level1)
198 {
199 driver_.Dump(HELLO_WORLD_DUMP_RESULT_FILE_NAME);
200 EXPECT_TRUE(Abc2ProgramTestUtils::ValidateDumpResult(HELLO_WORLD_DUMP_RESULT_FILE_NAME,
201 HELLO_WORLD_DUMP_EXPECTED_FILE_NAME));
202 if (REMOVE_DUMP_RESULT_FILES) {
203 Abc2ProgramTestUtils::RemoveDumpResultFile(HELLO_WORLD_DUMP_RESULT_FILE_NAME);
204 }
205 }
206
207 /**
208 * @tc.name: abc2program_hello_world_test_func_annotation
209 * @tc.desc: get program function annotation.
210 * @tc.type: FUNC
211 * @tc.require: #I9AQ3K
212 */
213 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_hello_world_test_func_annotation, TestSize.Level1)
214 {
215 constexpr uint32_t NUM_OF_HELLO_WORLD_TEST_UT_HELLO_WORLD_SLOTS_NUM = 2;
216 constexpr uint32_t NUM_OF_HELLO_WORLD_TEST_UT_FOO_SLOTS_NUM = 24;
217 constexpr uint32_t NUM_OF_HELLO_WORLD_TEST_UT_GOO_SLOTS_NUM = 0;
218 EXPECT_TRUE(hello_world_function_->GetSlotsNum() == NUM_OF_HELLO_WORLD_TEST_UT_HELLO_WORLD_SLOTS_NUM);
219 EXPECT_TRUE(foo_function_->GetSlotsNum() == NUM_OF_HELLO_WORLD_TEST_UT_FOO_SLOTS_NUM);
220 EXPECT_TRUE(goo_function_->GetSlotsNum() == NUM_OF_HELLO_WORLD_TEST_UT_GOO_SLOTS_NUM);
221 EXPECT_TRUE(hello_world_function_->concurrent_module_requests.empty());
222 EXPECT_TRUE(foo_function_->concurrent_module_requests.empty());
223 EXPECT_TRUE(goo_function_->concurrent_module_requests.empty());
224 }
225
226 /**
227 * @tc.name: abc2program_hello_world_test_field_metadata
228 * @tc.desc: get program field metadata.
229 * @tc.type: FUNC
230 * @tc.require: issueI98NGN
231 */
232 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_hello_world_test_field_metadata, TestSize.Level1)
233 {
234 for (const auto &it : prog_->record_table) {
235 if (it.first == "_ESModuleRecord") {
236 const pandasm::Record &record = it.second;
237 const std::vector<pandasm::Field> &field_list = record.field_list;
238 const pandasm::Field &field = field_list[0];
239 EXPECT_TRUE(field.type.GetPandasmName() == "u32");
240 EXPECT_TRUE(field.name.find("HelloWorld.ts") != std::string::npos);
241 }
242 }
243 }
244
245 /**
246 * @tc.name: abc2program_hello_world_test_lang
247 * @tc.desc: get program language.
248 * @tc.type: FUNC
249 * @tc.require: issueI96G2J
250 */
251 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_hello_world_test_lang, TestSize.Level1)
252 {
253 panda_file::SourceLang expected_lang = panda_file::SourceLang::ECMASCRIPT;
254 bool lang_matched = (prog_->lang == expected_lang);
255 EXPECT_TRUE(lang_matched);
256 }
257
258 /**
259 * @tc.name: abc2program_hello_world_test_literalarray_table
260 * @tc.desc: get program literalarray_table.
261 * @tc.type: FUNC
262 * @tc.require: issueI9DK5D
263 */
264 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_hello_world_test_literalarray_table, TestSize.Level1)
265 {
266 std::set<size_t> literals_sizes;
267 std::vector<std::string> literal_array_keys;
268 for (const auto &it : prog_->literalarray_table) {
269 const pandasm::LiteralArray &literal_array = it.second;
270 size_t literals_size = literal_array.literals_.size();
271 literals_sizes.insert(literals_size);
272 literal_array_keys.emplace_back(it.first);
273 }
274 EXPECT_TRUE(Abc2ProgramTestUtils::ValidateLiteralsSizes(literals_sizes));
275 EXPECT_TRUE(Abc2ProgramTestUtils::ValidateLiteralArrayKeys(literal_array_keys));
276 }
277
278 /**
279 * @tc.name: abc2program_async_method_literals
280 * @tc.desc: get and check literals of async generator method.
281 * @tc.type: FUNC
282 * @tc.require: issueI9SLHH
283 */
284 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_async_method_literals, TestSize.Level1)
285 {
286 auto &literal_array_table = prog_->literalarray_table;
287 EXPECT_EQ(literal_array_table.size(), SIZE_OF_LITERAL_ARRAY_TABLE);
288 panda::pandasm::LiteralArray async_literals;
289 for (auto &item : literal_array_table) {
290 for (auto &literal : item.second.literals_) {
291 if (literal.tag_ == panda_file::LiteralTag::ASYNCGENERATORMETHOD) {
292 async_literals = item.second;
293 break;
294 }
295 }
296 if (async_literals.literals_.size() != 0) {
297 break;
298 }
299 }
300 EXPECT_EQ(async_literals.literals_.size(), TOTAL_NUM_OF_ASYNC_METHOD_LITERALS);
301 auto it = async_literals.literals_.begin();
302 EXPECT_EQ(it->tag_, panda_file::LiteralTag::TAGVALUE);
303 ++it;
304 EXPECT_EQ(it->tag_, panda_file::LiteralTag::STRING);
305 ++it;
306 EXPECT_EQ(it->tag_, panda_file::LiteralTag::TAGVALUE);
307 ++it;
308 EXPECT_EQ(it->tag_, panda_file::LiteralTag::ASYNCGENERATORMETHOD);
309 EXPECT_EQ(std::get<std::string>(it->value_), FUNC_NAME.method);
310 ++it;
311 EXPECT_EQ(it->tag_, panda_file::LiteralTag::TAGVALUE);
312 ++it;
313 EXPECT_EQ(it->tag_, panda_file::LiteralTag::METHODAFFILIATE);
314 }
315
316 /**
317 * @tc.name: abc2program_hello_world_test_record_table
318 * @tc.desc: get program record_table.
319 * @tc.type: FUNC
320 * @tc.require: issueI96G2J
321 */
322 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_hello_world_test_record_table, TestSize.Level1)
323 {
324 panda_file::SourceLang expected_lang = panda_file::SourceLang::ECMASCRIPT;
325 std::vector<std::string> record_names;
326 for (const auto &it : prog_->record_table) {
327 EXPECT_TRUE(it.second.language == expected_lang);
328 EXPECT_TRUE(it.second.source_file == "");
329 EXPECT_TRUE(it.first == it.second.name);
330 record_names.emplace_back(it.first);
331 }
332 EXPECT_TRUE(Abc2ProgramTestUtils::ValidateRecordNames(record_names));
333 }
334
335 /**
336 * @tc.name: abc2program_hello_world_test_fields
337 * @tc.desc: get program record_table.
338 * @tc.type: FUNC
339 * @tc.require: issueI98NGN
340 */
341 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_hello_world_test_fields, TestSize.Level1)
342 {
343 for (const auto &it : prog_->record_table) {
344 if (it.first == "_ESModuleRecord" || it.first == "_ESScopeNamesRecord") {
345 const pandasm::Record &record = it.second;
346 const std::vector<pandasm::Field> &field_list = record.field_list;
347 EXPECT_EQ(field_list.size(), 1);
348 const pandasm::Field &field = field_list[0];
349 EXPECT_EQ(field.type.GetPandasmName(), "u32");
350 EXPECT_NE(field.name.find("HelloWorld.ts"), std::string::npos);
351 } else {
352 EXPECT_EQ(it.second.field_list.size(), 0);
353 }
354 }
355 }
356
357 /**
358 * @tc.name: abc2program_hello_world_test_strings
359 * @tc.desc: get existed string.
360 * @tc.type: FUNC
361 * @tc.require: issueI96G2J
362 */
363 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_hello_world_test_strings, TestSize.Level1)
364 {
365 bool string_matched = Abc2ProgramTestUtils::ValidateProgramStrings(prog_->strings);
366 EXPECT_TRUE(string_matched);
367 }
368
369 /**
370 * @tc.name: abc2program_hello_world_test_function_kind_access_flags
371 * @tc.desc: get existed function_kind.
372 * @tc.type: FUNC
373 * @tc.require: issueI9BPIO
374 */
375 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_hello_world_test_function_kind_access_flags, TestSize.Level1)
376 {
377 const pandasm::Function &function = *foo_function_;
378 panda_file::FunctionKind function_kind = function.function_kind;
379 EXPECT_TRUE(function_kind == panda_file::FunctionKind::FUNCTION);
380 uint32_t access_flags = function.metadata->GetAccessFlags();
381 EXPECT_TRUE((access_flags & ACC_STATIC) != 0);
382 EXPECT_TRUE(lit_function_->GetFunctionKind() == panda::panda_file::FunctionKind::NONE);
383 EXPECT_TRUE(foo_function_->GetFunctionKind() == panda::panda_file::FunctionKind::FUNCTION);
384 EXPECT_TRUE(add_function_->GetFunctionKind() == panda::panda_file::FunctionKind::NC_FUNCTION);
385 EXPECT_TRUE(generate_function_->GetFunctionKind() == panda::panda_file::FunctionKind::GENERATOR_FUNCTION);
386 EXPECT_TRUE(method_function_->GetFunctionKind() == panda::panda_file::FunctionKind::ASYNC_FUNCTION);
387 EXPECT_TRUE(async_generate_function_->GetFunctionKind() ==
388 panda::panda_file::FunctionKind::ASYNC_GENERATOR_FUNCTION);
389 EXPECT_TRUE(async_arrow_function_->GetFunctionKind() == panda::panda_file::FunctionKind::ASYNC_NC_FUNCTION);
390 }
391
392 /**
393 * @tc.name: abc2program_code_test_function_foo_part1
394 * @tc.desc: get program fuction foo.
395 * @tc.type: FUNC
396 * @tc.require: issueI989S6
397 */
398 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_code_test_function_foo_part1, TestSize.Level1)
399 {
400 const pandasm::Function &function = *foo_function_;
401 size_t regs_num = function.regs_num;
402 constexpr size_t NUM_OF_ARGS_FOR_FOO_METHOD = 3;
403 EXPECT_TRUE(function.params.size() == NUM_OF_ARGS_FOR_FOO_METHOD);
404 for (size_t i = 0; i < function.params.size(); ++i) {
405 EXPECT_TRUE(function.params[i].type.GetPandasmName() == ANY_TYPE_NAME);
406 }
407 EXPECT_TRUE(function.name == FUNC_NAME.foo);
408 EXPECT_TRUE(function.ins.size() == NUM_OF_CODE_TEST_UT_FOO_METHOD_INS);
409 // check ins[0]
410 const pandasm::Ins &ins0 = function.ins[0];
411 std::string pa_ins_str0 = ins0.ToString("", true, regs_num);
412 EXPECT_TRUE(pa_ins_str0 == "nop");
413 EXPECT_TRUE(ins0.label == "");
414 EXPECT_FALSE(ins0.set_label);
415 // check ins[3]
416 const pandasm::Ins &ins3 = function.ins[3];
417 std::string pa_ins_str3 = ins3.ToString("", true, regs_num);
418 EXPECT_TRUE(pa_ins_str3 == "label@3: ldai 0xb");
419 EXPECT_TRUE(ins3.label == "label@3");
420 EXPECT_TRUE(ins3.set_label);
421 // check ins[9]
422 const pandasm::Ins &ins9 = function.ins[9];
423 std::string pa_ins_str9 = ins9.ToString("", true, regs_num);
424 EXPECT_TRUE(pa_ins_str9 == "label@9: ldai 0x1");
425 EXPECT_TRUE(ins9.label == "label@9");
426 EXPECT_TRUE(ins9.set_label);
427 // check ins[11]
428 const pandasm::Ins &ins11 = function.ins[11];
429 std::string pa_ins_str11 = ins11.ToString("", true, regs_num);
430 EXPECT_TRUE(pa_ins_str11 == "label@11: jmp label@20");
431 EXPECT_TRUE(ins11.label == "label@11");
432 EXPECT_TRUE(ins11.set_label);
433 }
434
435 /**
436 * @tc.name: abc2program_code_test_function_foo_part2
437 * @tc.desc: get program fuction foo.
438 * @tc.type: FUNC
439 * @tc.require: issueI989S6
440 */
441 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_code_test_function_foo_part2, TestSize.Level1)
442 {
443 const pandasm::Function &function = *foo_function_;
444 size_t regs_num = function.regs_num;
445
446 // check ins[12]
447 const pandasm::Ins &ins12 = function.ins[12];
448 std::string pa_ins_str12 = ins12.ToString("", true, regs_num);
449 EXPECT_TRUE(pa_ins_str12 == "label@12: lda.str inner catch");
450 EXPECT_TRUE(ins12.label == "label@12");
451 EXPECT_TRUE(ins12.set_label);
452 // check ins[22]
453 const pandasm::Ins &ins22 = function.ins[22];
454 std::string pa_ins_str22 = ins22.ToString("", true, regs_num);
455 EXPECT_TRUE(pa_ins_str22 == "tryldglobalbyname 0x8, varA");
456 EXPECT_TRUE(ins22.label == "");
457 EXPECT_FALSE(ins22.set_label);
458 // check ins[26]
459 const pandasm::Ins &ins26 = function.ins[26];
460 std::string pa_ins_str26 = ins26.ToString("", true, regs_num);
461 EXPECT_TRUE(pa_ins_str26 == "jeqz label@29");
462 EXPECT_TRUE(ins26.label == "");
463 EXPECT_FALSE(ins26.set_label);
464 // check ins[29]
465 const pandasm::Ins &ins29 = function.ins[29];
466 std::string pa_ins_str29 = ins29.ToString("", true, regs_num);
467 EXPECT_TRUE(pa_ins_str29 == "label@29: tryldglobalbyname 0xa, x");
468 EXPECT_TRUE(ins29.label == "label@29");
469 EXPECT_TRUE(ins29.set_label);
470 // check ins[33]
471 const pandasm::Ins &ins33 = function.ins[33];
472 std::string pa_ins_str33 = ins33.ToString("", true, regs_num);
473 EXPECT_TRUE(pa_ins_str33 == "jeqz label@36");
474 EXPECT_TRUE(ins33.label == "");
475 EXPECT_FALSE(ins33.set_label);
476 // check ins[36]
477 const pandasm::Ins &ins36 = function.ins[36];
478 std::string pa_ins_str36 = ins36.ToString("", true, regs_num);
479 EXPECT_TRUE(pa_ins_str36 == "label@36: lda.str min");
480 EXPECT_TRUE(ins36.label == "label@36");
481 EXPECT_TRUE(ins36.set_label);
482 }
483
484 /**
485 * @tc.name: abc2program_code_test_function_foo_part3
486 * @tc.desc: get program fuction foo.
487 * @tc.type: FUNC
488 * @tc.require: issueI989S6
489 */
490 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_code_test_function_foo_part3, TestSize.Level1)
491 {
492 const pandasm::Function &function = *foo_function_;
493 size_t regs_num = function.regs_num;
494 // check ins[38]
495 const pandasm::Ins &ins38 = function.ins[38];
496 std::string pa_ins_str38 = ins38.ToString("", true, regs_num);
497 EXPECT_TRUE(pa_ins_str38 == "label@38: mov v1, v3");
498 EXPECT_TRUE(ins38.label == "label@38");
499 EXPECT_TRUE(ins38.set_label);
500 // check ins[44]
501 const pandasm::Ins &ins44 = function.ins[44];
502 std::string pa_ins_str44 = ins44.ToString("", true, regs_num);
503 EXPECT_TRUE(pa_ins_str44 == "sta v4");
504 EXPECT_TRUE(ins44.label == "");
505 EXPECT_FALSE(ins44.set_label);
506 // check ins[47]
507 const pandasm::Ins &ins47 = function.ins[47];
508 std::string pa_ins_str47 = ins47.ToString("", true, regs_num);
509 EXPECT_TRUE(pa_ins_str47 == "label@47: ldhole");
510 EXPECT_TRUE(ins47.label == "label@47");
511 EXPECT_TRUE(ins47.set_label);
512 // check ins[51]
513 const pandasm::Ins &ins51 = function.ins[51];
514 std::string pa_ins_str51 = ins51.ToString("", true, regs_num);
515 EXPECT_TRUE(pa_ins_str51 == "jmp label@53");
516 EXPECT_TRUE(ins51.label == "");
517 EXPECT_FALSE(ins51.set_label);
518 // check ins[52]
519 const pandasm::Ins &ins52 = function.ins[52];
520 std::string pa_ins_str52 = ins52.ToString("", true, regs_num);
521 EXPECT_TRUE(pa_ins_str52 == "label@52: sta v2");
522 EXPECT_TRUE(ins52.label == "label@52");
523 EXPECT_TRUE(ins52.set_label);
524 // check ins[53]
525 const pandasm::Ins &ins53 = function.ins[53];
526 std::string pa_ins_str53 = ins53.ToString("", true, regs_num);
527 EXPECT_TRUE(pa_ins_str53 == "label@53: ldundefined");
528 EXPECT_TRUE(ins53.label == "label@53");
529 EXPECT_TRUE(ins53.set_label);
530 // check ins[55]
531 const pandasm::Ins &ins55 = function.ins[55];
532 std::string pa_ins_str55 = ins55.ToString("", true, regs_num);
533 EXPECT_TRUE(pa_ins_str55 == "jeqz label@64");
534 EXPECT_TRUE(ins55.label == "");
535 EXPECT_FALSE(ins55.set_label);
536 // check ins[64]
537 const pandasm::Ins &ins64 = function.ins[64];
538 std::string pa_ins_str64 = ins64.ToString("", true, regs_num);
539 EXPECT_TRUE(pa_ins_str64 == "label@64: ldhole");
540 EXPECT_TRUE(ins64.label == "label@64");
541 EXPECT_TRUE(ins64.set_label);
542 }
543
544 /**
545 * @tc.name: abc2program_code_test_function_foo_part4
546 * @tc.desc: get program fuction foo.
547 * @tc.type: FUNC
548 * @tc.require: issueI989S6
549 */
550 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_code_test_function_foo_part4, TestSize.Level1)
551 {
552 const pandasm::Function &function = *foo_function_;
553 size_t regs_num = function.regs_num;
554 // check ins[68]
555 const pandasm::Ins &ins68 = function.ins[68];
556 std::string pa_ins_str68 = ins68.ToString("", true, regs_num);
557 EXPECT_TRUE(pa_ins_str68 == "jeqz label@71");
558 EXPECT_TRUE(ins68.label == "");
559 EXPECT_FALSE(ins68.set_label);
560 // check ins[71]
561 const pandasm::Ins &ins71 = function.ins[71];
562 std::string pa_ins_str71 = ins71.ToString("", true, regs_num);
563 EXPECT_TRUE(pa_ins_str71 == "label@71: returnundefined");
564 EXPECT_TRUE(ins71.label == "label@71");
565 EXPECT_TRUE(ins71.set_label);
566 // check catch blocks
567 constexpr uint32_t NUM_OF_CODE_TEST_UT_FOO_METHOD_CATCH_BLOCKS = 3;
568 EXPECT_TRUE(function.catch_blocks.size() == NUM_OF_CODE_TEST_UT_FOO_METHOD_CATCH_BLOCKS);
569 // catch_blocks[0]
570 const pandasm::Function::CatchBlock &pa_catch_block0 = function.catch_blocks[0];
571 EXPECT_TRUE(pa_catch_block0.try_begin_label == "label@9");
572 EXPECT_TRUE(pa_catch_block0.try_end_label == "label@11");
573 EXPECT_TRUE(pa_catch_block0.catch_begin_label == "label@12");
574 EXPECT_TRUE(pa_catch_block0.catch_end_label == "label@12");
575 // catch_blocks[1]
576 const pandasm::Function::CatchBlock &pa_catch_block1 = function.catch_blocks[1];
577 EXPECT_TRUE(pa_catch_block1.try_begin_label == "label@3");
578 EXPECT_TRUE(pa_catch_block1.try_end_label == "label@38");
579 EXPECT_TRUE(pa_catch_block1.catch_begin_label == "label@38");
580 EXPECT_TRUE(pa_catch_block1.catch_end_label == "label@38");
581 // catch_blocks[2]
582 const pandasm::Function::CatchBlock &pa_catch_block2 = function.catch_blocks[2];
583 EXPECT_TRUE(pa_catch_block2.try_begin_label == "label@3");
584 EXPECT_TRUE(pa_catch_block2.try_end_label == "label@47");
585 EXPECT_TRUE(pa_catch_block2.catch_begin_label == "label@52");
586 EXPECT_TRUE(pa_catch_block2.catch_end_label == "label@52");
587 }
588
589 /**
590 * @tc.name: abc2program_code_test_function_goo
591 * @tc.desc: get program fuction goo.
592 * @tc.type: FUNC
593 * @tc.require: issueI989S6
594 */
595 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_code_test_function_goo, TestSize.Level1)
596 {
597 const pandasm::Function &function = *goo_function_;
598 size_t regs_num = function.regs_num;
599 constexpr uint32_t NUM_OF_CODE_TEST_UT_GOO_METHOD_INS = 1;
600 EXPECT_TRUE(function.name == FUNC_NAME.goo);
601 EXPECT_TRUE(function.ins.size() == NUM_OF_CODE_TEST_UT_GOO_METHOD_INS);
602 // check ins[0]
603 constexpr uint32_t INDEX_OF_FUNC_RETURNUNDEFINED = 0;
604 const pandasm::Ins &ins1 = function.ins[INDEX_OF_FUNC_RETURNUNDEFINED];
605 std::string pa_ins_str1 = ins1.ToString("", true, regs_num);
606 EXPECT_TRUE(pa_ins_str1 == "returnundefined");
607 EXPECT_TRUE(ins1.label == "");
608 EXPECT_FALSE(ins1.set_label);
609 // check catch blocks
610 EXPECT_TRUE(function.catch_blocks.size() == 0);
611 }
612
613 /**
614 * @tc.name: abc2program_code_imm_of_FLDAI
615 * @tc.desc: get and check immediate number of opcode FLDAI.
616 * @tc.type: FUNC
617 * @tc.require: issueI9E5ZM
618 */
619 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_code_imm_of_FLDAI, TestSize.Level1)
620 {
621 const pandasm::Function &hoo = *hoo_function_;
622 EXPECT_EQ(hoo.ins.size(), INS_SIZE_OF_FUNCTION_HOO);
623 pandasm::Ins ins_fldai;
624 for (auto &ins : hoo.ins) {
625 if (ins.opcode == pandasm::Opcode::FLDAI) {
626 ins_fldai = ins;
627 break;
628 }
629 }
630 EXPECT_TRUE(ins_fldai.opcode == pandasm::Opcode::FLDAI);
631 // check imm of FLDAI
632 EXPECT_EQ(ins_fldai.imms.size(), IMMS_SIZE_OF_OPCODE_FLDAI);
633 auto p = std::get_if<double>(&ins_fldai.imms[0]);
634 EXPECT_NE(p, nullptr);
635 EXPECT_EQ(*p, 1.23);
636 }
637
638 /**
639 * @tc.name: abc2program_module_literal_entry_test
640 * @tc.desc: get and check number of modules.
641 * @tc.type: FUNC
642 * @tc.require: issueI9E5ZM
643 */
644 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_module_literal_entry_test, TestSize.Level1)
645 {
646 auto &mod_table = prog_->literalarray_table;
647 EXPECT_EQ(mod_table.size(), SIZE_OF_LITERAL_ARRAY_TABLE);
648 auto &module_literals = mod_table.begin()->second.literals_;
649 EXPECT_EQ(module_literals.size(), TOTAL_NUM_OF_MODULE_LITERALS);
__anoncd7fbe6d0102(size_t idx, panda::panda_file::LiteralTag expect_tag, uint32_t expect_value) 650 auto check_entry = [&module_literals](size_t idx, panda::panda_file::LiteralTag expect_tag, uint32_t expect_value) {
651 auto *literal = &(module_literals[idx]);
652 EXPECT_EQ(literal->tag_, expect_tag);
653 auto p = std::get_if<uint32_t>(&literal->value_);
654 EXPECT_TRUE(p != nullptr && *p == expect_value);
655 };
656 size_t idx = 0;
657 // check ModuleRequests
658 check_entry(idx, panda::panda_file::LiteralTag::INTEGER, NUM_OF_MODULE_REQUESTS);
659 // Each constant value '1' below stands for each entry. Each entry is also in the literal vector and shall be
660 // considered while calculating the position (index) of next entry.
661 // check RegularImportEntry
662 idx += NUM_OF_MODULE_REQUESTS + 1;
663 check_entry(idx, panda::panda_file::LiteralTag::INTEGER, NUM_OF_REGULAR_IMPORTS);
664 // check NamespaceImportEntry
665 idx += NUM_OF_REGULAR_IMPORTS * LITERAL_NUM_OF_REGULAR_IMPORT + 1;
666 check_entry(idx, panda::panda_file::LiteralTag::INTEGER, NUM_OF_NAMESPACE_IMPORTS);
667 // check LocalExportEntry
668 idx += NUM_OF_NAMESPACE_IMPORTS * LITERAL_NUM_OF_NAMESPACE_IMPORT + 1;
669 check_entry(idx, panda::panda_file::LiteralTag::INTEGER, NUM_OF_LOCAL_EXPORTS);
670 // check IndirectExportEntry
671 idx += NUM_OF_LOCAL_EXPORTS * LITERAL_NUM_OF_LOCAL_EXPORT + 1;
672 check_entry(idx, panda::panda_file::LiteralTag::INTEGER, NUM_OF_INDIRECT_EXPORTS);
673 // check StarExportEntry
674 idx += NUM_OF_INDIRECT_EXPORTS * LITERAL_NUM_OF_INDIRECT_EXPORT + 1;
675 check_entry(idx, panda::panda_file::LiteralTag::INTEGER, NUM_OF_STAR_EXPORTS);
676 // check idx
677 idx += NUM_OF_STAR_EXPORTS * LITERAL_NUM_OF_STAR_EXPORT + 1;
678 EXPECT_EQ(idx, TOTAL_NUM_OF_MODULE_LITERALS);
679 }
680
681 /**
682 * @tc.name: abc2program_hello_world_test_nested_literal_array
683 * @tc.desc: check contents of nested literal array
684 * @tc.type: FUNC
685 * @tc.require: issueIA7D9J
686 */
687 HWTEST_F(Abc2ProgramHelloWorldTest, abc2program_hello_world_test_nested_literal_array, TestSize.Level1)
688 {
689 auto &literal_array_table = prog_->literalarray_table;
690 EXPECT_EQ(literal_array_table.size(), SIZE_OF_LITERAL_ARRAY_TABLE);
691 /** current literal array table should be similar to below, each line indicates a literal array, which is
692 * formated as 'id_name : { literals }':
693 * _1 : { ... } // some other literal arrays
694 * _2 : { ... }
695 * _3 : { ... } // literal array that be nested, which id name is, for example, '_3'.
696 * _4 : { ..., literal_array:_3} // target literal array we are checking.
697 */
698 panda::pandasm::LiteralArray nested_literal_array;
699 for (auto &item : literal_array_table) {
700 for (auto &literal : item.second.literals_) {
701 if (literal.tag_ == panda_file::LiteralTag::LITERALARRAY) {
702 nested_literal_array = item.second;
703 break;
704 }
705 }
706 if (nested_literal_array.literals_.size() != 0) {
707 break;
708 }
709 }
710 EXPECT_EQ(nested_literal_array.literals_.size(), TOTAL_NUM_OF_NESTED_LITERALS);
711 auto it = nested_literal_array.literals_.begin();
712 EXPECT_EQ(it->tag_, panda_file::LiteralTag::TAGVALUE);
713 ++it;
714 EXPECT_EQ(it->tag_, panda_file::LiteralTag::STRING);
715 ++it;
716 EXPECT_EQ(it->tag_, panda_file::LiteralTag::TAGVALUE);
717 ++it;
718 EXPECT_EQ(it->tag_, panda_file::LiteralTag::METHOD);
719 EXPECT_EQ(std::get<std::string>(it->value_), FUNC_NAME.nested_literal_array);
720 ++it;
721 EXPECT_EQ(it->tag_, panda_file::LiteralTag::TAGVALUE);
722 ++it;
723 EXPECT_EQ(it->tag_, panda_file::LiteralTag::METHODAFFILIATE);
724 ++it;
725 EXPECT_EQ(it->tag_, panda_file::LiteralTag::TAGVALUE);
726 ++it;
727 EXPECT_EQ(it->tag_, panda_file::LiteralTag::INTEGER);
728 ++it;
729 EXPECT_EQ(it->tag_, panda_file::LiteralTag::TAGVALUE);
730 ++it;
731 EXPECT_EQ(it->tag_, panda_file::LiteralTag::LITERALARRAY);
732 EXPECT_NE(literal_array_table.find(std::get<std::string>(it->value_)), literal_array_table.end());
733 }
734
735 /*------------------------------------- Cases of release mode above -------------------------------------*/
736 /*-------------------------------------- Cases of debug mode below --------------------------------------*/
737
738 /**
739 * @tc.name: abc2program_hello_world_debug_test_dump
740 * @tc.desc: check dump result in debug mode.
741 * @tc.type: FUNC
742 * @tc.require: IADG92
743 */
744 HWTEST_F(Abc2ProgramHelloWorldDebugTest, abc2program_hello_world_debug_test_dump, TestSize.Level1)
745 {
746 driver_.Dump(HELLO_WORLD_DEBUG_DUMP_RESULT_FILE_NAME);
747 EXPECT_TRUE(Abc2ProgramTestUtils::ValidateDumpResult(HELLO_WORLD_DEBUG_DUMP_RESULT_FILE_NAME,
748 HELLO_WORLD_DEBUG_DUMP_EXPECTED_FILE_NAME));
749 if (REMOVE_DUMP_RESULT_FILES) {
750 Abc2ProgramTestUtils::RemoveDumpResultFile(HELLO_WORLD_DEBUG_DUMP_RESULT_FILE_NAME);
751 }
752 }
753
754 /**
755 * @tc.name: abc2program_hello_world_test_source_file
756 * @tc.desc: Verify the source file.
757 * @tc.type: FUNC
758 * @tc.require: issueI9CL5Z
759 */
760 HWTEST_F(Abc2ProgramHelloWorldDebugTest, abc2program_hello_world_test_source_file, TestSize.Level1)
761 {
762 const pandasm::Function &function = *foo_function_;
763 std::string source_file = function.source_file;
764 EXPECT_TRUE(source_file.find("HelloWorld.ts") != std::string::npos);
765 }
766
767 /**
768 * @tc.name: abc2program_hello_world_test_source_code
769 * @tc.desc: Verify the source code
770 * @tc.type: FUNC
771 * @tc.require: issueI9DT0V
772 */
773 HWTEST_F(Abc2ProgramHelloWorldDebugTest, abc2program_hello_world_test_source_code, TestSize.Level1)
774 {
775 const pandasm::Function &function = *main_function_;
776 std::string source_code = function.source_code;
777 EXPECT_TRUE(source_code.find("not supported") != std::string::npos);
778 }
779
780 /**
781 * @tc.name: abc2program_hello_world_test_local_variable
782 * @tc.desc: get local variables
783 * @tc.type: FUNC
784 * @tc.require: issueI9DT0V
785 */
786 HWTEST_F(Abc2ProgramHelloWorldDebugTest, abc2program_hello_world_test_local_variable, TestSize.Level1)
787 {
788 const pandasm::Function &function = *foo_function_;
789 EXPECT_FALSE(function.local_variable_debug.empty());
790 EXPECT_TRUE(function.local_variable_debug[0].name.find("4funcObj") != std::string::npos);
791 EXPECT_TRUE(function.local_variable_debug[0].signature.find("any") != std::string::npos);
792 EXPECT_TRUE(function.local_variable_debug[0].signature_type.find("any") != std::string::npos);
793 EXPECT_TRUE(function.local_variable_debug[0].reg == 0);
794 EXPECT_TRUE(function.local_variable_debug[0].start == 3);
795 EXPECT_TRUE(function.local_variable_debug[0].length == 74);
796 }
797
798 /**
799 * @tc.name: abc2program_hello_world_test_ins_debug
800 * @tc.desc: get ins_debug line number and column number
801 * @tc.type: FUNC
802 * @tc.require: issueI9DT0V
803 */
804 HWTEST_F(Abc2ProgramHelloWorldDebugTest, abc2program_hello_world_test_ins_debug, TestSize.Level1)
805 {
806 const pandasm::Function &function = *foo_function_;
807 size_t regs_num = function.regs_num;
808 EXPECT_FALSE(function.local_variable_debug.empty());
809
810 const pandasm::Ins &ins0 = function.ins[0];
811 std::string pa_ins_str0 = ins0.ToString("", true, regs_num);
812 EXPECT_TRUE(pa_ins_str0 == "mov v0, a0");
813 EXPECT_TRUE(ins0.ins_debug.line_number == -1);
814 EXPECT_TRUE(ins0.ins_debug.column_number == -1);
815
816 const pandasm::Ins &ins3 = function.ins[3];
817 std::string pa_ins_str3 = ins3.ToString("", true, regs_num);
818 EXPECT_TRUE(pa_ins_str3 == "ldundefined");
819 EXPECT_TRUE(ins3.ins_debug.line_number == 40);
820 EXPECT_TRUE(ins3.ins_debug.column_number == 2);
821
822 const pandasm::Ins &ins5 = function.ins[5];
823 std::string pa_ins_str5 = ins5.ToString("", true, regs_num);
824 EXPECT_TRUE(pa_ins_str5 == "label@5: ldai 0xb");
825 EXPECT_TRUE(ins5.ins_debug.line_number == 41);
826 EXPECT_TRUE(ins5.ins_debug.column_number == 11);
827 }
828
829 /**
830 * @tc.name: abc2program_hello_world_test_open_abc_file
831 * @tc.desc: open a non-existent abc file
832 * @tc.type: FUNC
833 * @tc.require: issueI9DT0V
834 */
835 HWTEST_F(Abc2ProgramHelloWorldDebugTest, abc2program_hello_world_test_open_abc_file, TestSize.Level1)
836 {
837 std::string file_path = "invalid_file_path";
838 EXPECT_FALSE(driver_.Compile(file_path));
839 }
840
841 /**
842 * @tc.name: abc2program_hello_world_test_driver_run
843 * @tc.desc: driver run different parameters
844 * @tc.type: FUNC
845 * @tc.require: issueI9DT0V
846 */
847 HWTEST_F(Abc2ProgramHelloWorldDebugTest, abc2program_hello_world_test_driver_run, TestSize.Level1)
848 {
849 EXPECT_TRUE(driver_.Run(0, nullptr));
850 int argc1 = 2;
851 const char* args1[] = {"--param1", "--param2"};
852 EXPECT_TRUE(driver_.Run(argc1, args1));
853 int argc2 = 2;
854 const char* args2[] = {"abc2program_options", "input_file_path"};
855 EXPECT_TRUE(driver_.Run(argc2, args2));
856 int argc3 = 3;
857 const char* args3[] = {"abc2program_options", "input_file_path", "output_file_path"};
858 EXPECT_TRUE(driver_.Run(argc3, args3));
859 int argc4 = 2;
860 const char* args4[] = {"abc2program_options", "--debug"};
861 EXPECT_TRUE(driver_.Run(argc4, args4));
862 int argc5 = 4;
863 const char* args5[] = {"abc2program_options", "--debug", "--debug-file", "debug_file_path"};
864 EXPECT_TRUE(driver_.Run(argc5, args5));
865 int argc6 = 3;
866 const char* args6[] = {"abc2program_options", HELLO_WORLD_DEBUG_ABC_TEST_FILE_NAME.c_str(),
867 HELLO_WORLD_DEBUG_DUMP_RESULT_FILE_NAME.c_str()};
868 EXPECT_FALSE(driver_.Run(argc6, args6));
869 if (REMOVE_DUMP_RESULT_FILES) {
870 Abc2ProgramTestUtils::RemoveDumpResultFile(HELLO_WORLD_DEBUG_DUMP_RESULT_FILE_NAME);
871 }
872 }
873
874 /**
875 * @tc.name: abc2program_hello_world_test_Label
876 * @tc.desc: label found in the map
877 * @tc.type: FUNC
878 * @tc.require: issueI9DT0V
879 */
880 HWTEST_F(Abc2ProgramHelloWorldDebugTest, abc2program_hello_world_test_Label, TestSize.Level1)
881 {
882 LabelMap label_map = {{"first_label", "first_mapped_label"}, {"second_label", "second_mapped_label"}};
883 std::string label1 = "second_label";
884 std::string label2 = "third_label";
885 PandasmDumperUtils test = PandasmDumperUtils();
886 std::string result_found = test.GetMappedLabel(label1, label_map);
887 std::string result_null = test.GetMappedLabel(label2, label_map);
888 EXPECT_EQ(result_found, "second_mapped_label");
889 EXPECT_EQ(result_null, "");
890 }
891
892 /*-------------------------------------- Cases of debug mode above --------------------------------------*/
893 /*-------------------------------------- Case of json file below ----------------------------------------*/
894
895 /**
896 * @tc.name: abc2program_json_field_test
897 * @tc.desc: get json field metadata
898 * @tc.type: FUNC
899 * @tc.require: issueIAJKTS
900 */
901 HWTEST_F(Abc2ProgramJsonTest, abc2program_json_field_test, TestSize.Level1)
902 {
903 auto it = prog_->record_table.find(JSON_TEST_FILE_NAME);
904 ASSERT(it != prog_->record_table.end());
905 const pandasm::Record &record = it->second;
906 const std::vector<pandasm::Field> &field_list = record.field_list;
907 EXPECT_EQ(field_list.size(), 1);
908 const pandasm::Field &field = field_list[0];
909 EXPECT_EQ(field.name, JSON_FILE_CONTENT);
910 std::optional<pandasm::ScalarValue> val = field.metadata->GetValue();
911 EXPECT_TRUE(val.has_value());
912 auto content = val.value().GetValue<std::string>();
913 EXPECT_EQ(content, "{\n \"name\" : \"Import json\"\n}");
914 }
915
916 /**
917 * @tc.name: abc2program_annotations_test_Record
918 * @tc.desc: record fields compare
919 * @tc.type: FUNC
920 * @tc.require: IARNAU
921 */
922 HWTEST_F(Abc2ProgramAnnotationTest, abc2program_annotations_test_Record, TestSize.Level1)
923 {
924 const auto &fields = anno_record_->field_list;
925 for (auto& field : fields) {
926 EXPECT_TRUE(field.name == "a" || field.name == "b" || field.name == "c" || field.name == "d" ||
927 field.name == "e" || field.name == "f");
928 if (field.name == "a") {
929 EXPECT_EQ(field.type.GetName(), "f64");
930 EXPECT_EQ(field.metadata->GetValue()->GetValue<double>(), 7);
931 }
932 if (field.name == "b") {
933 EXPECT_EQ(field.type.GetName(), "f64[]");
934 auto &litarr = prog_->literalarray_table.at(field.metadata->GetValue()->GetValue<std::string>());
935 EXPECT_EQ(litarr.literals_.size(), 4);
936 }
937 if (field.name == "c") {
938 EXPECT_EQ(field.type.GetName(), "panda.String");
939 EXPECT_EQ(field.metadata->GetValue()->GetValue<std::string>(), "abc");
940 }
941 if (field.name == "d") {
942 EXPECT_EQ(field.type.GetName(), "u1");
943 EXPECT_EQ(field.metadata->GetValue()->GetValue<bool>(), false);
944 }
945 if (field.name == "e") {
946 EXPECT_EQ(field.type.GetName(), "f64[]");
947 auto &litarr = prog_->literalarray_table.at(field.metadata->GetValue()->GetValue<std::string>());
948 EXPECT_EQ(litarr.literals_.size(), 6);
949 }
950 if (field.name == "f") {
951 EXPECT_EQ(field.type.GetName(), "f64");
952 EXPECT_EQ(field.metadata->GetValue()->GetValue<double>(), 2);
953 }
954 }
955 }
956
957 /**
958 * @tc.name: abc2program_annotations_test_function_annotation
959 * @tc.desc: check if annotated function has annotation
960 * @tc.type: FUNC
961 * @tc.require: IARNAU
962 */
963 HWTEST_F(Abc2ProgramAnnotationTest, abc2program_annotations_test_function_annotation, TestSize.Level1)
964 {
965 auto &annotations = a_function_->metadata->GetAnnotations();
__anoncd7fbe6d0202(const pandasm::AnnotationData &anno) 966 auto iter = std::find_if(annotations.begin(), annotations.end(), [](const pandasm::AnnotationData &anno) {
967 return anno.GetName() == "Anno";
968 });
969 EXPECT_NE(iter, annotations.end());
970 EXPECT_EQ(iter->GetName(), "Anno");
971 }
972
973 /**
974 * @tc.name: abc2program_annotations_test_function_annotation_fields
975 * @tc.desc: annotation of function fields comparison
976 * @tc.type: FUNC
977 * @tc.require: IARNAU
978 */
979 HWTEST_F(Abc2ProgramAnnotationTest, abc2program_annotations_test_function_annotation_fields, TestSize.Level1)
980 {
981 auto &annotations = b_function_->metadata->GetAnnotations();
__anoncd7fbe6d0302(const pandasm::AnnotationData &anno) 982 auto anno = std::find_if(annotations.begin(), annotations.end(), [](const pandasm::AnnotationData &anno) {
983 return anno.GetName() == "Anno";
984 });
985 EXPECT_NE(anno, annotations.end());
986 EXPECT_EQ(anno->GetName(), "Anno");
987 const auto &elements = anno->GetElements();
988 for (auto& elem : elements) {
989 EXPECT_TRUE(elem.GetName() == "a" || elem.GetName() == "b" || elem.GetName() == "c" || elem.GetName() == "d" ||
990 elem.GetName() == "e" || elem.GetName() == "f");
991 if (elem.GetName() == "a") {
992 EXPECT_EQ(elem.GetValue()->GetType(), pandasm::Value::Type::F64);
993 EXPECT_EQ(elem.GetValue()->GetAsScalar()->GetValue<double>(), 5);
994 }
995 if (elem.GetName() == "b") {
996 EXPECT_EQ(elem.GetValue()->GetType(), pandasm::Value::Type::LITERALARRAY);
997 auto &litarr = prog_->literalarray_table.at(elem.GetValue()->GetAsScalar()->GetValue<std::string>());
998 EXPECT_EQ(litarr.literals_.size(), 6);
999 }
1000 if (elem.GetName() == "c") {
1001 EXPECT_EQ(elem.GetValue()->GetType(), pandasm::Value::Type::STRING);
1002 EXPECT_EQ(elem.GetValue()->GetAsScalar()->GetValue<std::string>(), "def");
1003 }
1004 if (elem.GetName() == "d") {
1005 EXPECT_EQ(elem.GetValue()->GetType(), pandasm::Value::Type::U1);
1006 EXPECT_EQ(elem.GetValue()->GetAsScalar()->GetValue<bool>(), true);
1007 }
1008 if (elem.GetName() == "e") {
1009 EXPECT_EQ(elem.GetValue()->GetType(), pandasm::Value::Type::LITERALARRAY);
1010 auto &litarr = prog_->literalarray_table.at(elem.GetValue()->GetAsScalar()->GetValue<std::string>());
1011 EXPECT_EQ(litarr.literals_.size(), 4);
1012 }
1013 if (elem.GetName() == "f") {
1014 EXPECT_EQ(elem.GetValue()->GetType(), pandasm::Value::Type::F64);
1015 EXPECT_EQ(elem.GetValue()->GetAsScalar()->GetValue<double>(), 3);
1016 }
1017 }
1018 }
1019
1020 }; // panda::abc2program