1 /**
2 * Copyright (c) 2021-2022 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 <gtest/gtest.h>
17
18 #include "runtime/include/runtime.h"
19 #include "runtime/include/runtime_options.h"
20 #include "plugins/ets/runtime/types/ets_method_signature.h"
21
22 namespace panda::ets::test {
23
24 // NOTE(a.urakov): move initialization to a common internal objects testing base class
25 class EtsMethodSignatureTest : public testing::Test {
26 public:
EtsMethodSignatureTest()27 EtsMethodSignatureTest()
28 {
29 RuntimeOptions options;
30 options.SetShouldLoadBootPandaFiles(false);
31 options.SetShouldInitializeIntrinsics(false);
32 options.SetCompilerEnableJit(false);
33 options.SetGcType("epsilon");
34 options.SetLoadRuntimes({"ets"});
35
36 Runtime::Create(options);
37 }
38
~EtsMethodSignatureTest()39 ~EtsMethodSignatureTest() override
40 {
41 Runtime::Destroy();
42 }
43
44 NO_COPY_SEMANTIC(EtsMethodSignatureTest);
45 NO_MOVE_SEMANTIC(EtsMethodSignatureTest);
46 };
47
TEST_F(EtsMethodSignatureTest,MethodSignature)48 TEST_F(EtsMethodSignatureTest, MethodSignature)
49 {
50 EtsMethodSignature minimal(":V");
51 EXPECT_EQ(minimal.GetProto(), Method::Proto(
52 Method::Proto::ShortyVector {
53 panda_file::Type {panda_file::Type::TypeId::VOID},
54 },
55 Method::Proto::RefTypeVector {}));
56 EtsMethodSignature minimalint("I:V");
57 EXPECT_EQ(minimalint.GetProto(), Method::Proto(
58 Method::Proto::ShortyVector {
59 panda_file::Type {panda_file::Type::TypeId::VOID},
60 panda_file::Type {panda_file::Type::TypeId::I32},
61 },
62 Method::Proto::RefTypeVector {}));
63
64 EtsMethodSignature simple("BCSIJFD:Z");
65 EXPECT_EQ(simple.GetProto(), Method::Proto(
66 Method::Proto::ShortyVector {
67 panda_file::Type {panda_file::Type::TypeId::U1},
68 panda_file::Type {panda_file::Type::TypeId::I8},
69 panda_file::Type {panda_file::Type::TypeId::U16},
70 panda_file::Type {panda_file::Type::TypeId::I16},
71 panda_file::Type {panda_file::Type::TypeId::I32},
72 panda_file::Type {panda_file::Type::TypeId::I64},
73 panda_file::Type {panda_file::Type::TypeId::F32},
74 panda_file::Type {panda_file::Type::TypeId::F64},
75 },
76 Method::Proto::RefTypeVector {}));
77 EtsMethodSignature arrays("SB[J[Lstd/core/String;I:LT;");
78 EXPECT_EQ(arrays.GetProto(), Method::Proto(
79 Method::Proto::ShortyVector {
80 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
81 panda_file::Type {panda_file::Type::TypeId::I16},
82 panda_file::Type {panda_file::Type::TypeId::I8},
83 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
84 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
85 panda_file::Type {panda_file::Type::TypeId::I32},
86 },
87 Method::Proto::RefTypeVector {
88 std::string_view {"LT;"},
89 std::string_view {"[J"},
90 std::string_view {"[Lstd/core/String;"},
91 }));
92
93 EtsMethodSignature multiarr("[[[LT;:[[I");
94 EXPECT_EQ(multiarr.GetProto(), Method::Proto(
95 Method::Proto::ShortyVector {
96 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
97 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
98 },
99 Method::Proto::RefTypeVector {
100 std::string_view {"[[I"},
101 std::string_view {"[[[LT;"},
102 }));
103 EtsMethodSignature big("J[BI[CI[IZ:I");
104 EXPECT_EQ(big.GetProto(), Method::Proto(
105 Method::Proto::ShortyVector {
106 panda_file::Type {panda_file::Type::TypeId::I32},
107 panda_file::Type {panda_file::Type::TypeId::I64},
108 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
109 panda_file::Type {panda_file::Type::TypeId::I32},
110 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
111 panda_file::Type {panda_file::Type::TypeId::I32},
112 panda_file::Type {panda_file::Type::TypeId::REFERENCE},
113 panda_file::Type {panda_file::Type::TypeId::U1},
114 },
115 Method::Proto::RefTypeVector {
116 std::string_view {"[B"},
117 std::string_view {"[C"},
118 std::string_view {"[I"},
119 }));
120 }
121
TEST_F(EtsMethodSignatureTest,InvalidMethodSignature)122 TEST_F(EtsMethodSignatureTest, InvalidMethodSignature)
123 {
124 EtsMethodSignature invalid1("");
125 EXPECT_FALSE(invalid1.IsValid());
126
127 EtsMethodSignature invalid2(":");
128 EXPECT_FALSE(invalid2.IsValid());
129
130 EtsMethodSignature invalid3(":L");
131 EXPECT_FALSE(invalid3.IsValid());
132
133 EtsMethodSignature invalid4("[]");
134 EXPECT_FALSE(invalid4.IsValid());
135
136 EtsMethodSignature invalid5("IL:J");
137 EXPECT_FALSE(invalid5.IsValid());
138
139 EtsMethodSignature invalid6("[:S");
140 EXPECT_FALSE(invalid6.IsValid());
141
142 EtsMethodSignature invalid7("V:Lg;");
143 EXPECT_FALSE(invalid7.IsValid());
144
145 EtsMethodSignature invalid8("L;:[");
146 EXPECT_FALSE(invalid8.IsValid());
147
148 EtsMethodSignature invalid9("::");
149 EXPECT_FALSE(invalid9.IsValid());
150
151 EtsMethodSignature invalid10("I:I:Z");
152 EXPECT_FALSE(invalid10.IsValid());
153 }
154
155 } // namespace panda::ets::test