• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ecmascript/symbol_table.h"
17 #include "ecmascript/ecma_string.h"
18 #include "ecmascript/js_symbol.h"
19 #include "ecmascript/object_factory.h"
20 #include "ecmascript/js_tagged_value.h"
21 #include "ecmascript/tagged_array-inl.h"
22 #include "ecmascript/tests/test_helper.h"
23 
24 using namespace panda::ecmascript;
25 
26 namespace panda::test {
27 class SymbolTableTest : public BaseTestWithScope<false> {
28 };
29 
HWTEST_F_L0(SymbolTableTest,GetKeyIndex)30 HWTEST_F_L0(SymbolTableTest, GetKeyIndex)
31 {
32     int entry = 0;
33     EXPECT_EQ(SymbolTable::GetKeyIndex(entry), 3);
34 }
35 
HWTEST_F_L0(SymbolTableTest,GetValueIndex)36 HWTEST_F_L0(SymbolTableTest, GetValueIndex)
37 {
38     int entry = 0;
39     EXPECT_EQ(SymbolTable::GetValueIndex(entry), 4);
40 }
41 
HWTEST_F_L0(SymbolTableTest,GetEntryIndex)42 HWTEST_F_L0(SymbolTableTest, GetEntryIndex)
43 {
44     int entry = 0;
45     EXPECT_EQ(SymbolTable::GetEntryIndex(entry), 3);
46 }
47 
HWTEST_F_L0(SymbolTableTest,GetEntrySize)48 HWTEST_F_L0(SymbolTableTest, GetEntrySize)
49 {
50     EXPECT_EQ(SymbolTable::GetEntrySize(), 2);
51 }
52 
53 /*
54  * Feature: SymbolTableTest
55  * Function: IsMatch
56  * SubFunction: StringsAreEqual
57  * FunctionPoints: Is Match
58  * CaseDescription: Judge whether two string variables are equal. If they are equal,
59  *                  it returns true, otherwise it returns false.
60  */
HWTEST_F_L0(SymbolTableTest,IsMatch)61 HWTEST_F_L0(SymbolTableTest, IsMatch)
62 {
63     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
64 
65     JSHandle<EcmaString> symbolTableString = factory->NewFromASCII("name");
66     JSTaggedValue symbolTableOther = symbolTableString.GetTaggedValue();
67 
68     JSTaggedValue symbolTableName1 = JSTaggedValue::Hole();
69     EXPECT_EQ(SymbolTable::IsMatch(thread, symbolTableName1, symbolTableOther), false);
70 
71     JSTaggedValue symbolTableName2 = JSTaggedValue::Undefined();
72     EXPECT_EQ(SymbolTable::IsMatch(thread, symbolTableName2, symbolTableOther), false);
73 
74     JSTaggedValue symbolTableName3 = symbolTableString.GetTaggedValue();
75     EXPECT_EQ(SymbolTable::IsMatch(thread, symbolTableName3, symbolTableOther), true);
76 }
77 
78 /*
79  * Feature: SymbolTableTest
80  * Function: Hash_Utf8
81  * SubFunction: GetHashCode
82  * FunctionPoints: Hash
83  * CaseDescription: The hash code is obtained by passing in a character string array or an uint8_t
84  *                  type array through a specific calculation formula.
85  */
HWTEST_F_L0(SymbolTableTest,Hash_Utf8)86 HWTEST_F_L0(SymbolTableTest, Hash_Utf8)
87 {
88     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
89     uint8_t utf8ArrayName1[4] = {1, 2, 3}; // The last element is "\0"
90     uint32_t utf8ArrayNameLen1 = sizeof(utf8ArrayName1) - 1;
91     JSHandle<EcmaString> nameStringUtf8Obj1 = factory->NewFromUtf8(utf8ArrayName1, utf8ArrayNameLen1);
92     EXPECT_EQ(SymbolTable::Hash(thread,
93         nameStringUtf8Obj1.GetTaggedValue()), 1026U); // 1026 = (1 << 5 - 1 + 2) << 5 - 2 + 3
94 
95     uint8_t utf8ArrayName2[] = "key";
96     uint32_t utf8ArrayNameLen2 = sizeof(utf8ArrayName2) - 1;
97     JSHandle<EcmaString> nameStringUtf8Obj2 = factory->NewFromUtf8(utf8ArrayName2, utf8ArrayNameLen2);
98     EXPECT_EQ(SymbolTable::Hash(thread, nameStringUtf8Obj2.GetTaggedValue()), 106079U);
99 }
100 
101 /*
102  * Feature: SymbolTableTest
103  * Function: Hash_Utf16
104  * SubFunction: GetHashCode
105  * FunctionPoints: Hash
106  * CaseDescription: The hash code is obtained by passing in a character string array or an uint16_t
107  *                  type array through a specific calculation formula.
108  */
HWTEST_F_L0(SymbolTableTest,Hash_Utf16)109 HWTEST_F_L0(SymbolTableTest, Hash_Utf16)
110 {
111     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
112 
113     uint16_t utf16ArrayName1[] = {1, 2, 3};
114     uint32_t utf16ArrayNameLen1 = sizeof(utf16ArrayName1) / sizeof(utf16ArrayName1[0]);
115     JSHandle<EcmaString> nameStringUtf16Obj1 = factory->NewFromUtf16(utf16ArrayName1, utf16ArrayNameLen1);
116     EXPECT_EQ(SymbolTable::Hash(thread,
117         nameStringUtf16Obj1.GetTaggedValue()), 1026U); // 1026 = (1 << 5 - 1 + 2) << 5 - 2 + 3
118 
119     uint16_t utf16ArrayName2[] = {0, 1, 2};
120     uint32_t utf16ArrayNameLen2 = sizeof(utf16ArrayName2) / sizeof(utf16ArrayName2[0]);
121     JSHandle<EcmaString> nameStringUtf16Obj2 = factory->NewFromUtf16(utf16ArrayName2, utf16ArrayNameLen2);
122     EXPECT_EQ(SymbolTable::Hash(thread,
123         nameStringUtf16Obj2.GetTaggedValue()), 33U); // 33 = (0 << 5 - 0 + 1) << 5 - 1 + 2
124 }
125 
126 /*
127  * Feature: SymbolTableTest
128  * Function: Create
129  * SubFunction: *Value
130  * FunctionPoints: Create
131  * CaseDescription: A pointer variable of symboltable type is obtained by changing the function,
132  *                  If it is created successfully, the pointer variable is not equal to null,
133  *                  The prerequisite for creation is that compressedstringsenabled must be true.
134  */
HWTEST_F_L0(SymbolTableTest,Create)135 HWTEST_F_L0(SymbolTableTest, Create)
136 {
137     int numberOfElements = SymbolTable::DEFAULT_ELEMENTS_NUMBER;
138 
139     JSHandle<SymbolTable> symbolTable = SymbolTable::Create(thread, numberOfElements);
140     EXPECT_TRUE(*symbolTable != nullptr);
141 }
142 
143 /*
144  * Feature: SymbolTableTest
145  * Function: ContainsKey
146  * SubFunction: Getkey
147  * FunctionPoints: Contains Key
148  * CaseDescription: Judge whether the key value can be found in the key value in your own created symbol
149  *                  table.If you do not have a key value, you will return false.
150  */
HWTEST_F_L0(SymbolTableTest,ContainsKey)151 HWTEST_F_L0(SymbolTableTest, ContainsKey)
152 {
153     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
154     JSHandle<EcmaString> symbolTableStringKey1 = factory->NewFromASCII("key");
155     JSHandle<EcmaString> symbolTableStringKey2 = factory->NewFromASCII("key1");
156     JSHandle<EcmaString> symbolTableStringKey3 = factory->NewFromASCII("value");
157 
158     int numberOfElements = 2;
159     JSHandle<SymbolTable> symbolTable = SymbolTable::Create(thread, numberOfElements);
160     EXPECT_EQ(symbolTable->ContainsKey(thread, symbolTableStringKey1.GetTaggedValue()), false);
161 
162     symbolTable->SetKey(thread, 1, JSTaggedValue::Hole());
163     EXPECT_EQ(symbolTable->ContainsKey(thread, symbolTableStringKey1.GetTaggedValue()), false);
164 
165     symbolTable->SetKey(thread, 1, JSTaggedValue::Undefined());
166     EXPECT_EQ(symbolTable->ContainsKey(thread, symbolTableStringKey1.GetTaggedValue()), false);
167 
168     symbolTable->SetKey(thread, 1, symbolTableStringKey1.GetTaggedValue());
169     EXPECT_EQ(symbolTable->ContainsKey(thread, symbolTableStringKey1.GetTaggedValue()), true);
170 
171     // the key value has numbers
172     symbolTable->SetKey(thread, 1, symbolTableStringKey2.GetTaggedValue());
173     EXPECT_EQ(symbolTable->ContainsKey(thread, symbolTableStringKey2.GetTaggedValue()), false);
174 
175     symbolTable->SetKey(thread, 1, symbolTableStringKey3.GetTaggedValue());
176     EXPECT_EQ(symbolTable->ContainsKey(thread, symbolTableStringKey3.GetTaggedValue()), true);
177 }
178 
179 /*
180  * Feature: SymbolTableTest
181  * Function: GetSymbol
182  * SubFunction: GetValue
183  * FunctionPoints: Get Symbol
184  * CaseDescription: This function obtains the value in the key of symbol table pointer variable created
185  *                  by the create function. If the pointer variable has no value set, it returns false.
186  */
HWTEST_F_L0(SymbolTableTest,GetSymbol)187 HWTEST_F_L0(SymbolTableTest, GetSymbol)
188 {
189     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
190     int numberOfElements = 2;
191 
192     JSHandle<EcmaString> symbolTableStringKey = factory->NewFromASCII("key");
193     JSHandle<SymbolTable> symbolTable = SymbolTable::Create(thread, numberOfElements);
194 
195     symbolTable->SetKey(thread, 1, symbolTableStringKey.GetTaggedValue());
196     EXPECT_EQ(symbolTable->GetSymbol(thread, symbolTableStringKey.GetTaggedValue()), JSTaggedValue::Undefined());
197 
198     symbolTable->SetValue(thread, 0, JSTaggedValue(1));
199     EXPECT_EQ(symbolTable->GetSymbol(thread, symbolTableStringKey.GetTaggedValue()), JSTaggedValue::Undefined());
200 
201     symbolTable->SetValue(thread, 1, JSTaggedValue(1));
202     EXPECT_EQ(symbolTable->GetSymbol(thread, symbolTableStringKey.GetTaggedValue()).GetInt(), 1);
203 }
204 
205 /*
206  * Feature: SymbolTableTest
207  * Function: FindSymbol
208  * SubFunction: GetKey
209  * FunctionPoints: Find Symbol
210  * CaseDescription: This function compares the key value in the symboltable pointer variable created by the create
211  *                  function with the description value in the jssymbol type variable. If they are equal, it indicates
212  *                  that the find symbol is successful, and the return value is the key value. Before creating the
213  *                  symboltable pointer, perform the NewFromASCII operation to obtain the array length.
214  */
HWTEST_F_L0(SymbolTableTest,FindSymbol)215 HWTEST_F_L0(SymbolTableTest, FindSymbol)
216 {
217     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
218     JSHandle<JSTaggedValue> symbolTableStringKey1(factory->NewFromASCII("key"));
219     JSHandle<JSTaggedValue> symbolTableStringKey2(factory->NewFromASCII("key1"));
220 
221     int numberOfElements = 2;
222     JSHandle<JSSymbol> handleSymbol = factory->NewJSSymbol();
223     JSHandle<SymbolTable> symbolTable = SymbolTable::Create(thread, numberOfElements);
224 
225     JSTaggedValue resultValue1 = symbolTable->FindSymbol(thread, handleSymbol.GetTaggedValue());
226     EXPECT_EQ(JSTaggedValue::SameValue(thread, resultValue1, JSTaggedValue::Undefined()), true);
227 
228     handleSymbol->SetDescription(thread, symbolTableStringKey1.GetTaggedValue());
229     JSTaggedValue resultValue2 = symbolTable->FindSymbol(thread, handleSymbol.GetTaggedValue());
230     EXPECT_EQ(JSTaggedValue::SameValue(thread, resultValue2, JSTaggedValue::Undefined()), true);
231 
232     symbolTable->SetKey(thread, 1, symbolTableStringKey1.GetTaggedValue());
233     JSTaggedValue resultValue3 = symbolTable->FindSymbol(thread, handleSymbol.GetTaggedValue());
234     EXPECT_EQ(resultValue3.GetRawData() == symbolTableStringKey1.GetTaggedValue().GetRawData(), true);
235 
236     symbolTable->SetKey(thread, 1, symbolTableStringKey2.GetTaggedValue());
237     JSTaggedValue resultValue4 = symbolTable->FindSymbol(thread, handleSymbol.GetTaggedValue());
238     EXPECT_EQ(JSTaggedValue::SameValue(thread, resultValue4, JSTaggedValue::Undefined()), true);
239 }
240 }  // namespace panda::test
241