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_vm.h"
18 #include "ecmascript/ecma_runtime_call_info.h"
19 #include "ecmascript/js_tagged_value.h"
20 #include "ecmascript/js_api/js_api_bitvector.h"
21 #include "ecmascript/js_api/js_api_bitvector_iterator.h"
22 #include "ecmascript/global_env.h"
23 #include "ecmascript/object_factory.h"
24 #include "ecmascript/tests/test_helper.h"
25
26 using namespace panda;
27 using namespace panda::ecmascript;
28
29 namespace panda::test {
30 class JSAPIBitVectorIteratorTest : public testing::Test {
31 public:
SetUpTestCase()32 static void SetUpTestCase()
33 {
34 GTEST_LOG_(INFO) << "SetUpTestCase";
35 }
36
TearDownTestCase()37 static void TearDownTestCase()
38 {
39 GTEST_LOG_(INFO) << "TearDownCase";
40 }
41
SetUp()42 void SetUp() override
43 {
44 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
45 }
46
TearDown()47 void TearDown() override
48 {
49 TestHelper::DestroyEcmaVMWithScope(instance, scope);
50 }
51
52 EcmaVM *instance {nullptr};
53 EcmaHandleScope *scope {nullptr};
54 JSThread *thread {nullptr};
55 protected:
CreateBitVector()56 JSAPIBitVector *CreateBitVector()
57 {
58 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
59 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
60
61 JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
62 JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
63 JSHandle<JSTaggedValue> value =
64 JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
65
66 auto objCallInfo =
67 TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
68 objCallInfo->SetFunction(JSTaggedValue::Undefined());
69 objCallInfo->SetThis(value.GetTaggedValue());
70 objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(containers::ContainerTag::BitVector)));
71
72 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
73 JSTaggedValue result = containers::ContainersPrivate::Load(objCallInfo);
74 TestHelper::TearDownFrame(thread, prev);
75
76 JSHandle<JSTaggedValue> constructor(thread, result);
77 JSHandle<JSAPIBitVector> bitVector(
78 factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
79 auto *newBitSetVector = new std::vector<std::bitset<JSAPIBitVector::BIT_SET_LENGTH>>();
80 auto deleter = []([[maybe_unused]] void *env, void *pointer, [[maybe_unused]] void *data) {
81 if (pointer == nullptr) {
82 return;
83 }
84 delete reinterpret_cast<std::vector<std::bitset<JSAPIBitVector::BIT_SET_LENGTH>> *>(pointer);
85 };
86 JSHandle<JSNativePointer> pointer = factory->NewSJSNativePointer(newBitSetVector, deleter, newBitSetVector);
87 bitVector->SetNativePointer(thread, pointer);
88 return *bitVector;
89 }
90 };
91
92 /**
93 * @tc.name: Next
94 * @tc.desc:
95 * @tc.type: FUNC
96 * @tc.require:
97 */
HWTEST_F_L0(JSAPIBitVectorIteratorTest,Next)98 HWTEST_F_L0(JSAPIBitVectorIteratorTest, Next)
99 {
100 JSHandle<JSAPIBitVector> bitVector(thread, CreateBitVector());
101 uint32_t elementsNum = 256;
102 for (uint32_t i = 0; i < elementsNum; i++) {
103 JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
104 JSAPIBitVector::Push(thread, bitVector, value);
105 }
106 JSHandle<JSAPIBitVectorIterator> bitVectorIterator(thread, JSAPIBitVector::GetIteratorObj(thread, bitVector));
107 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
108 uint32_t length = bitVector->GetLength();
109 for (uint32_t i = 0; i < length; i++) {
110 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
111 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
112 ecmaRuntimeCallInfo->SetThis(bitVectorIterator.GetTaggedValue());
113
114 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
115 JSTaggedValue result = JSAPIBitVectorIterator::Next(ecmaRuntimeCallInfo);
116 TestHelper::TearDownFrame(thread, prev);
117
118 JSHandle<JSObject> resultObj(thread, result);
119 if (i < elementsNum) {
120 EXPECT_EQ(bitVectorIterator->GetNextIndex(), i + 1U);
121 EXPECT_EQ((JSObject::GetProperty(thread, resultObj, valueStr).GetValue()).GetTaggedValue(),
122 JSTaggedValue(1));
123 } else {
124 EXPECT_EQ(bitVectorIterator->GetIteratedBitVector(thread), JSTaggedValue::Undefined());
125 EXPECT_EQ((JSObject::GetProperty(thread, resultObj, valueStr).GetValue()).GetTaggedValue(),
126 JSTaggedValue::Undefined());
127 }
128 }
129
130 // test Next exception
131 {
132 auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
133 ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
134 ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
135
136 [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
137 JSTaggedValue result = JSAPIBitVectorIterator::Next(ecmaRuntimeCallInfo);
138 TestHelper::TearDownFrame(thread, prev);
139 EXPECT_EQ(result, JSTaggedValue::Exception());
140 EXPECT_EXCEPTION();
141 }
142 }
143 } // namespace panda::test
144