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 <gtest/gtest.h> 17 #include <iostream> 18 #include <string> 19 20 #include "define.h" 21 #include "lexer.h" 22 23 using namespace panda::pandasm; 24 using namespace testing::ext; 25 26 namespace panda::pandasm { 27 class LexerTest : public testing::Test { 28 }; 29 30 /** 31 * @tc.name: lexer_test_001 32 * @tc.desc: Verify the TokenTypeWhat function. 33 * @tc.type: FUNC 34 * @tc.require: issueNumber 35 */ 36 HWTEST_F(LexerTest, lexer_test_001, TestSize.Level1) 37 { 38 Lexer l; 39 std::string s = "mov v1, v2"; 40 Tokens tok = l.TokenizeString(s); 41 EXPECT_EQ(TokenTypeWhat(tok.first[0].type), "OPERATION") << "OPERATION expected"; 42 EXPECT_EQ(TokenTypeWhat(tok.first[1].type), "ID") << "ID expected"; 43 EXPECT_EQ(TokenTypeWhat(tok.first[2].type), "DEL_COMMA") << "DEL_COMMA expected"; 44 EXPECT_EQ(TokenTypeWhat(tok.first[3].type), "ID") << "ID expected"; 45 EXPECT_EQ(tok.second.err, Error::ErrorType::ERR_NONE) << "ERR_NONE expected"; 46 } 47 48 /** 49 * @tc.name: lexer_test_002 50 * @tc.desc: Verify the TokenTypeWhat function. 51 * @tc.type: FUNC 52 * @tc.require: issueNumber 53 */ 54 HWTEST_F(LexerTest, lexer_test_002, TestSize.Level1) 55 { 56 Lexer l; 57 std::string s = "ldai 1"; 58 Tokens tok = l.TokenizeString(s); 59 EXPECT_EQ(TokenTypeWhat(tok.first[0].type), "OPERATION") << "OPERATION expected"; 60 EXPECT_EQ(TokenTypeWhat(tok.first[1].type), "ID") << "ID expected"; 61 EXPECT_EQ(tok.second.err, Error::ErrorType::ERR_NONE) << "ERR_NONE expected"; 62 } 63 64 /** 65 * @tc.name: lexer_test_003 66 * @tc.desc: Verify the TokenTypeWhat function. 67 * @tc.type: FUNC 68 * @tc.require: issueNumber 69 */ 70 HWTEST_F(LexerTest, lexer_test_003, TestSize.Level1) 71 { 72 Lexer l; 73 std::string s = "movi\nlda v2 v10 mov v2"; 74 Tokens tok = l.TokenizeString(s); 75 EXPECT_EQ(TokenTypeWhat(tok.first[0].type), "ID") << "ID expected"; 76 EXPECT_EQ(TokenTypeWhat(tok.first[1].type), "OPERATION") << "OPERATION expected"; 77 EXPECT_EQ(TokenTypeWhat(tok.first[2].type), "ID") << "ID expected"; 78 EXPECT_EQ(TokenTypeWhat(tok.first[3].type), "ID") << "ID expected"; 79 EXPECT_EQ(TokenTypeWhat(tok.first[4].type), "OPERATION") << "OPERATION expected"; 80 EXPECT_EQ(TokenTypeWhat(tok.first[5].type), "ID") << "ID expected"; 81 EXPECT_EQ(tok.second.err, Error::ErrorType::ERR_NONE) << "ERR_NONE expected"; 82 } 83 84 /** 85 * @tc.name: lexer_test_004 86 * @tc.desc: Verify the TokenTypeWhat function. 87 * @tc.type: FUNC 88 * @tc.require: issueNumber 89 */ 90 HWTEST_F(LexerTest, lexer_test_004, TestSize.Level1) 91 { 92 Lexer l; 93 std::string s = "jmp Iasdfsadkfjhasifhsaiuhdacoisjdaociewhasdasdfkjasdfhjksadhfkhsakdfjhksajhdkfjhskhdfkjahhjdskaj"; 94 Tokens tok = l.TokenizeString(s); 95 EXPECT_EQ(TokenTypeWhat(tok.first[0].type), "OPERATION") << "OPERATION expected"; 96 EXPECT_EQ(TokenTypeWhat(tok.first[1].type), "ID") << "ID expected"; 97 EXPECT_EQ(tok.second.err, Error::ErrorType::ERR_NONE) << "ERR_NONE expected"; 98 } 99 100 /** 101 * @tc.name: lexer_test_005 102 * @tc.desc: Verify the TokenTypeWhat function. 103 * @tc.type: FUNC 104 * @tc.require: issueNumber 105 */ 106 HWTEST_F(LexerTest, lexer_test_005, TestSize.Level1) 107 { 108 Lexer l; 109 std::string s = "call.short 1111, 1"; 110 Tokens tok = l.TokenizeString(s); 111 EXPECT_EQ(TokenTypeWhat(tok.first[0].type), "ID") << "ID expected"; 112 EXPECT_EQ(TokenTypeWhat(tok.first[1].type), "ID") << "ID expected"; 113 EXPECT_EQ(TokenTypeWhat(tok.first[2].type), "DEL_COMMA") << "DEL_COMMA expected"; 114 EXPECT_EQ(TokenTypeWhat(tok.first[3].type), "ID") << "ID expected"; 115 EXPECT_EQ(tok.second.err, Error::ErrorType::ERR_NONE) << "ERR_NONE expected"; 116 } 117 118 /** 119 * @tc.name: lexer_test_006 120 * @tc.desc: Verify the TokenTypeWhat function. 121 * @tc.type: FUNC 122 * @tc.require: issueNumber 123 */ 124 HWTEST_F(LexerTest, lexer_test_006, TestSize.Level1) 125 { 126 Lexer l; 127 std::string s = "jle v1 met"; 128 Tokens tok = l.TokenizeString(s); 129 EXPECT_EQ(TokenTypeWhat(tok.first[0].type), "ID") << "ID expected"; 130 EXPECT_EQ(TokenTypeWhat(tok.first[1].type), "ID") << "ID expected"; 131 EXPECT_EQ(TokenTypeWhat(tok.first[2].type), "ID") << "ID expected"; 132 EXPECT_EQ(tok.second.err, Error::ErrorType::ERR_NONE) << "ERR_NONE expected"; 133 } 134 135 /** 136 * @tc.name: lexer_test_007 137 * @tc.desc: Verify the TokenTypeWhat function. 138 * @tc.type: FUNC 139 * @tc.require: issueNumber 140 */ 141 HWTEST_F(LexerTest, lexer_test_007, TestSize.Level1) 142 { 143 Lexer l; 144 std::string s = "label:"; 145 Tokens tok = l.TokenizeString(s); 146 EXPECT_EQ(TokenTypeWhat(tok.first[0].type), "ID") << "ID expected"; 147 EXPECT_EQ(tok.second.err, Error::ErrorType::ERR_NONE) << "ERR_NONE expected"; 148 } 149 150 /** 151 * @tc.name: lexer_test_008 152 * @tc.desc: Verify the TokenTypeWhat function. 153 * @tc.type: FUNC 154 * @tc.require: issueNumber 155 */ 156 HWTEST_F(LexerTest, lexer_test_008, TestSize.Level1) 157 { 158 Lexer l; 159 std::string s = ","; 160 Tokens tok = l.TokenizeString(s); 161 EXPECT_EQ(TokenTypeWhat(tok.first[0].type), "DEL_COMMA") << "DEL_COMMA expected"; 162 EXPECT_EQ(tok.second.err, Error::ErrorType::ERR_NONE) << "ERR_NONE expected"; 163 } 164 165 /** 166 * @tc.name: lexer_test_009 167 * @tc.desc: Verify the TokenTypeWhat function. 168 * @tc.type: FUNC 169 * @tc.require: issueNumber 170 */ 171 HWTEST_F(LexerTest, lexer_test_009, TestSize.Level1) 172 { 173 Lexer l; 174 std::string s = ",:{}()<>="; 175 Tokens tok = l.TokenizeString(s); 176 EXPECT_EQ(TokenTypeWhat(tok.first[0].type), "DEL_COMMA") << "DEL_COMMA expected"; 177 EXPECT_EQ(TokenTypeWhat(tok.first[1].type), "DEL_COLON") << "DEL_COMMA expected"; 178 EXPECT_EQ(TokenTypeWhat(tok.first[2].type), "DEL_BRACE_L") << "DEL_COMMA expected"; 179 EXPECT_EQ(TokenTypeWhat(tok.first[3].type), "DEL_BRACE_R") << "DEL_COMMA expected"; 180 EXPECT_EQ(TokenTypeWhat(tok.first[4].type), "DEL_BRACKET_L") << "DEL_BRACKET_L expected"; 181 EXPECT_EQ(TokenTypeWhat(tok.first[5].type), "DEL_BRACKET_R") << "DEL_BRACKET_R expected"; 182 EXPECT_EQ(TokenTypeWhat(tok.first[6].type), "DEL_LT") << "DEL_LT expected"; 183 EXPECT_EQ(TokenTypeWhat(tok.first[7].type), "DEL_GT") << "DEL_GT expected"; 184 EXPECT_EQ(TokenTypeWhat(tok.first[8].type), "DEL_EQ") << "DEL_EQ expected"; 185 EXPECT_EQ(tok.second.err, Error::ErrorType::ERR_NONE) << "ERR_NONE expected"; 186 } 187 188 /** 189 * @tc.name: lexer_test_010 190 * @tc.desc: Verify the TokenTypeWhat function. 191 * @tc.type: FUNC 192 * @tc.require: issueNumber 193 */ 194 HWTEST_F(LexerTest, lexer_test_010, TestSize.Level1) 195 { 196 Lexer l; 197 std::string s = 198 "i64.to.f32 alsdhashdjskhfka " 199 "shdkfhkasdhfkhsakdhfkshkfhskahlfkjsdfkjadskhfkshadkhfsdakhfksahdkfaksdfkhaskldhkfashdlfkjhasdkjfhklasjhdfklhsa" 200 "fhska"; 201 Tokens tok = l.TokenizeString(s); 202 EXPECT_EQ(tok.second.err, Error::ErrorType::ERR_NONE) << "ERR_NONE expected"; 203 } 204 205 /** 206 * @tc.name: lexer_test_011 207 * @tc.desc: Verify the TokenTypeWhat function. 208 * @tc.type: FUNC 209 * @tc.require: issueNumber 210 */ 211 HWTEST_F(LexerTest, lexer_test_011, TestSize.Level1) 212 { 213 Lexer l; 214 std::string s = ".function asd(u32){}"; 215 Tokens tok = l.TokenizeString(s); 216 217 EXPECT_EQ(TokenTypeWhat(tok.first[0].type), "KEYWORD") << "KEYWORD expected"; 218 EXPECT_EQ(TokenTypeWhat(tok.first[1].type), "ID") << "ID expected"; 219 EXPECT_EQ(tok.second.err, Error::ErrorType::ERR_NONE) << "ERR_NONE expected"; 220 } 221 222 /** 223 * @tc.name: lexer_test_012 224 * @tc.desc: Verify the TokenizeString function. 225 * @tc.type: FUNC 226 * @tc.require: issueNumber 227 */ 228 HWTEST_F(LexerTest, lexer_test_012, TestSize.Level1) 229 { 230 { 231 Lexer l; 232 std::string s = "\"123"; 233 Tokens tok = l.TokenizeString(s); 234 235 Error e = tok.second; 236 237 EXPECT_EQ(e.err, Error::ErrorType::ERR_STRING_MISSING_TERMINATING_CHARACTER); 238 } 239 240 { 241 Lexer l; 242 std::string s = "\"123\\\""; 243 Tokens tok = l.TokenizeString(s); 244 245 Error e = tok.second; 246 247 EXPECT_EQ(e.err, Error::ErrorType::ERR_STRING_MISSING_TERMINATING_CHARACTER); 248 } 249 250 { 251 Lexer l; 252 std::string s = "\" a b \\ c d \""; 253 Tokens tok = l.TokenizeString(s); 254 255 Error e = tok.second; 256 257 EXPECT_EQ(e.err, Error::ErrorType::ERR_NONE); 258 EXPECT_EQ(tok.first.size(), 1U); 259 EXPECT_EQ(tok.first[0].type, Token::Type::ID_STRING); 260 EXPECT_EQ(tok.first[0].bound_left, 0U); 261 EXPECT_EQ(tok.first[0].bound_right, s.length()); 262 } 263 264 { 265 Lexer l; 266 std::string s = "\"abcd\"1234"; 267 Tokens tok = l.TokenizeString(s); 268 269 Error e = tok.second; 270 271 EXPECT_EQ(e.err, Error::ErrorType::ERR_NONE); 272 EXPECT_EQ(tok.first.size(), 2U); 273 EXPECT_EQ(tok.first[0].type, Token::Type::ID_STRING); 274 EXPECT_EQ(tok.first[0].bound_left, 0U); 275 EXPECT_EQ(tok.first[0].bound_right, s.find('1')); 276 } 277 } 278 279 /** 280 * @tc.name: lexer_test_013 281 * @tc.desc: Verify the TokenizeString function. 282 * @tc.type: FUNC 283 * @tc.require: issueNumber 284 */ 285 HWTEST_F(LexerTest, lexer_test_013, TestSize.Level1) 286 { 287 Lexer l; 288 std::string s = "i32[]"; 289 Tokens tok = l.TokenizeString(s); 290 291 Error e = tok.second; 292 EXPECT_EQ(e.err, Error::ErrorType::ERR_NONE); 293 EXPECT_EQ(tok.first.size(), 3U); 294 EXPECT_EQ(tok.first[0].type, Token::Type::ID); 295 EXPECT_EQ(tok.first[1].type, Token::Type::DEL_SQUARE_BRACKET_L); 296 EXPECT_EQ(tok.first[2].type, Token::Type::DEL_SQUARE_BRACKET_R); 297 } 298 }