1/* 2 * Copyright (c) 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 "ecmascript/containers/containers_private.h" 17#include "ecmascript/ecma_string.h" 18#include "ecmascript/ecma_vm.h" 19#include "ecmascript/global_env.h" 20#include "ecmascript/js_function.h" 21#include "ecmascript/js_handle.h" 22#include "ecmascript/js_iterator.h" 23#include "ecmascript/js_api/js_api_hashset.h" 24#include "ecmascript/js_api/js_api_hashset_iterator.h" 25#include "ecmascript/js_object-inl.h" 26#include "ecmascript/js_tagged_value.h" 27#include "ecmascript/object_factory.h" 28#include "ecmascript/tests/ecma_test_common.h" 29 30using namespace panda; 31using namespace panda::ecmascript; 32 33namespace panda::test { 34class JSAPIHashSetTest : public BaseTestWithScope<false> { 35protected: 36 JSAPIHashSet *CreateHashSet() 37 { 38 return EcmaContainerCommon::CreateHashSet(thread); 39 } 40 41 void Update(JSHandle<JSAPIHashSet>& hashSet, JSMutableHandle<JSTaggedValue> value, std::string& myValue, 42 uint32_t numbers) 43 { 44 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 45 for (uint32_t i = 0; i < numbers; i++) { 46 std::string iValue = myValue + std::to_string(i); 47 value.Update(factory->NewFromStdString(iValue).GetTaggedValue()); 48 JSAPIHashSet::Add(thread, hashSet, value); 49 } 50 EXPECT_EQ(hashSet->GetSize(), numbers); 51 } 52}; 53 54HWTEST_F_L0(JSAPIHashSetTest, HashSetCreate) 55{ 56 JSAPIHashSet *set = CreateHashSet(); 57 EXPECT_TRUE(set != nullptr); 58} 59 60HWTEST_F_L0(JSAPIHashSetTest, HashSetAddAndHas) 61{ 62 constexpr uint32_t NODE_NUMBERS = 8; 63 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined()); 64 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet()); 65 std::string myValue("myvalue"); 66 Update(hashSet, value, myValue, NODE_NUMBERS); 67 68 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 69 for (uint32_t i = 0; i < NODE_NUMBERS; i++) { 70 std::string iValue = myValue + std::to_string(i); 71 value.Update(factory->NewFromStdString(iValue).GetTaggedValue()); 72 73 // test has 74 JSTaggedValue bHas = hashSet->Has(thread, value.GetTaggedValue()); 75 EXPECT_EQ(bHas, JSTaggedValue::True()); 76 } 77 78 // test add exception 79 JSHandle<JSTaggedValue> hole(thread, JSTaggedValue::Hole()); 80 JSAPIHashSet::Add(thread, hashSet, hole); 81 EXPECT_EXCEPTION(); 82 83 // test Has exception 84 JSTaggedValue exceptionHas = hashSet->Has(thread, JSTaggedValue::Hole()); 85 EXPECT_EQ(exceptionHas, JSTaggedValue::Exception()); 86 EXPECT_EXCEPTION(); 87} 88 89HWTEST_F_L0(JSAPIHashSetTest, HashSetRemoveAndHas) 90{ 91 constexpr uint32_t NODE_NUMBERS = 8; 92 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 93 // test JSAPIHashSet 94 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet()); 95 std::string myValue("myvalue"); 96 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined()); 97 Update(hashSet, value, myValue, NODE_NUMBERS); 98 99 for (uint32_t i = 0; i < NODE_NUMBERS / 2; i++) { 100 std::string iValue = myValue + std::to_string(i); 101 value.Update(factory->NewFromStdString(iValue).GetTaggedValue()); 102 [[maybe_unused]] JSTaggedValue rvalue = JSAPIHashSet::Remove(thread, hashSet, value.GetTaggedValue()); 103 } 104 EXPECT_EQ(hashSet->GetSize(), NODE_NUMBERS / 2); 105 106 for (uint32_t i = 0; i < NODE_NUMBERS / 2; i++) { 107 std::string iValue = myValue + std::to_string(i); 108 value.Update(factory->NewFromStdString(iValue).GetTaggedValue()); 109 110 // test has 111 JSTaggedValue has = hashSet->Has(thread, value.GetTaggedValue()); 112 EXPECT_EQ(has, JSTaggedValue::False()); 113 } 114 115 for (uint32_t i = NODE_NUMBERS / 2; i < NODE_NUMBERS; i++) { 116 std::string iValue = myValue + std::to_string(i); 117 value.Update(factory->NewFromStdString(iValue).GetTaggedValue()); 118 119 // test has 120 JSTaggedValue has = hashSet->Has(thread, value.GetTaggedValue()); 121 EXPECT_EQ(has, JSTaggedValue::True()); 122 } 123 124 // test Remove exception 125 JSTaggedValue exceptionValue = JSAPIHashSet::Remove(thread, hashSet, JSTaggedValue::Hole()); 126 EXPECT_EQ(exceptionValue, JSTaggedValue::Exception()); 127 EXPECT_EXCEPTION(); 128} 129 130HWTEST_F_L0(JSAPIHashSetTest, JSAPIHashSetRemoveRBTreeTest) 131{ 132 std::vector<int> hashCollisionVector = {1224, 1285, 1463, 4307, 5135, 5903, 6603, 6780, 8416, 9401, 9740}; 133 uint32_t NODE_NUMBERS = static_cast<uint32_t>(hashCollisionVector.size()); 134 constexpr uint32_t REMOVE_NUMBERS = 4; 135 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet()); 136 137 // test Remove empty 138 JSTaggedValue emptyValue = JSAPIHashSet::Remove(thread, hashSet, JSTaggedValue(0)); 139 EXPECT_EQ(emptyValue, JSTaggedValue::False()); 140 141 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined()); 142 143 for (size_t i = 0; i < hashCollisionVector.size(); i++) { 144 value.Update(JSTaggedValue(hashCollisionVector[i])); 145 JSAPIHashSet::Add(thread, hashSet, value); 146 } 147 148 // test Remove non-existent value 149 JSTaggedValue nonExistentValue = JSAPIHashSet::Remove(thread, hashSet, JSTaggedValue(0)); 150 EXPECT_EQ(nonExistentValue, JSTaggedValue::False()); 151 152 // test Remove RBTree 153 for (uint32_t i = 0; i < REMOVE_NUMBERS; i++) { 154 value.Update(JSTaggedValue(hashCollisionVector[i])); 155 JSAPIHashSet::Remove(thread, hashSet, value.GetTaggedValue()); 156 } 157 EXPECT_EQ(hashSet->GetSize(), NODE_NUMBERS - REMOVE_NUMBERS); 158 159 for (uint32_t i = 0; i < REMOVE_NUMBERS; i++) { 160 value.Update(JSTaggedValue(hashCollisionVector[i])); 161 JSTaggedValue has = hashSet->Has(thread, value.GetTaggedValue()); 162 EXPECT_EQ(has, JSTaggedValue::False()); 163 } 164 165 for (uint32_t i = REMOVE_NUMBERS; i < NODE_NUMBERS; i++) { 166 value.Update(JSTaggedValue(hashCollisionVector[i])); 167 JSTaggedValue has = hashSet->Has(thread, value.GetTaggedValue()); 168 EXPECT_EQ(has, JSTaggedValue::True()); 169 } 170} 171 172HWTEST_F_L0(JSAPIHashSetTest, HashSetClearAddHas) 173{ 174 constexpr uint32_t NODE_NUMBERS = 8; 175 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined()); 176 std::string myValue("myvalue"); 177 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet()); 178 Update(hashSet, value, myValue, NODE_NUMBERS); 179 180 hashSet->Clear(thread); 181 JSTaggedValue isEmpty = hashSet->IsEmpty(); 182 EXPECT_EQ(isEmpty, JSTaggedValue::True()); 183} 184 185HWTEST_F_L0(JSAPIHashSetTest, JSAPIHashSetIterator) 186{ 187 constexpr uint32_t NODE_NUMBERS = 8; 188 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 189 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet()); 190 191 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined()); 192 for (uint32_t i = 0; i < NODE_NUMBERS; i++) { 193 value.Update(JSTaggedValue(i)); 194 JSAPIHashSet::Add(thread, hashSet, value); 195 } 196 197 // test value 198 JSHandle<JSTaggedValue> valueIter(factory->NewJSAPIHashSetIterator(hashSet, IterationKind::VALUE)); 199 JSMutableHandle<JSTaggedValue> valueIterResult(thread, JSTaggedValue::Undefined()); 200 for (uint32_t i = 0; i < NODE_NUMBERS; i++) { 201 valueIterResult.Update(JSIterator::IteratorStep(thread, valueIter).GetTaggedValue()); 202 JSHandle<JSTaggedValue> tmpIterValue = JSIterator::IteratorValue(thread, valueIterResult); 203 JSTaggedValue iterValueFlag = hashSet->Has(thread, tmpIterValue.GetTaggedValue()); 204 EXPECT_EQ(JSTaggedValue::True(), iterValueFlag); 205 } 206 // test end 207 valueIterResult.Update(JSIterator::IteratorStep(thread, valueIter).GetTaggedValue()); 208 EXPECT_EQ(JSTaggedValue::False(), valueIterResult.GetTaggedValue()); 209 210 // test key and value 211 JSHandle<JSTaggedValue> indexKey(thread, JSTaggedValue(0)); 212 JSHandle<JSTaggedValue> elementKey(thread, JSTaggedValue(1)); 213 JSHandle<JSTaggedValue> iter(factory->NewJSAPIHashSetIterator(hashSet, IterationKind::KEY_AND_VALUE)); 214 JSMutableHandle<JSTaggedValue> iterResult(thread, JSTaggedValue::Undefined()); 215 JSMutableHandle<JSTaggedValue> result(thread, JSTaggedValue::Undefined()); 216 for (uint32_t i = 0; i < NODE_NUMBERS; i++) { 217 iterResult.Update(JSIterator::IteratorStep(thread, iter).GetTaggedValue()); 218 result.Update(JSIterator::IteratorValue(thread, iterResult).GetTaggedValue()); 219 EXPECT_EQ(JSTaggedValue(i), JSObject::GetProperty(thread, result, indexKey).GetValue().GetTaggedValue()); 220 JSHandle<JSTaggedValue> tmpValue = JSObject::GetProperty(thread, result, elementKey).GetValue(); 221 JSTaggedValue iterValueFlag = hashSet->Has(thread, tmpValue.GetTaggedValue()); 222 EXPECT_EQ(JSTaggedValue::True(), iterValueFlag); 223 } 224} 225 226HWTEST_F_L0(JSAPIHashSetTest, JSAPIHashSetIteratorRBTreeTest) 227{ 228 constexpr uint32_t NODE_NUMBERS = 11; 229 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); 230 JSHandle<JSAPIHashSet> hashSet(thread, CreateHashSet()); 231 JSMutableHandle<JSTaggedValue> value(thread, JSTaggedValue::Undefined()); 232 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString(); 233 std::vector<int> hashCollisionVector = {1224, 1285, 1463, 4307, 5135, 5903, 6603, 6780, 8416, 9401, 9740}; 234 235 for (size_t i = 0; i < hashCollisionVector.size(); i++) { 236 value.Update(JSTaggedValue(hashCollisionVector[i])); 237 JSAPIHashSet::Add(thread, hashSet, value); 238 } 239 240 JSHandle<JSAPIHashSetIterator> hashsetIterator = factory->NewJSAPIHashSetIterator(hashSet, IterationKind::VALUE); 241 for (uint32_t i = 0; i < NODE_NUMBERS; i++) { 242 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4); 243 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined()); 244 ecmaRuntimeCallInfo->SetThis(hashsetIterator.GetTaggedValue()); 245 246 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo); 247 JSTaggedValue result = JSAPIHashSetIterator::Next(ecmaRuntimeCallInfo); 248 TestHelper::TearDownFrame(thread, prev); 249 250 JSHandle<JSObject> resultObj(thread, result); 251 if (i <= NODE_NUMBERS - 1U) { 252 EXPECT_TRUE(JSObject::GetProperty(thread, resultObj, valueStr).GetValue()->IsInt()); 253 } 254 } 255} 256} // namespace panda::test 257