• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         JSHandle<JSNativePointer> pointer = factory->NewJSNativePointer(newBitSetVector);
81         bitVector->SetNativePointer(thread, pointer);
82         return *bitVector;
83     }
84 };
85 
86 /**
87  * @tc.name: Next
88  * @tc.desc:
89  * @tc.type: FUNC
90  * @tc.require:
91  */
HWTEST_F_L0(JSAPIBitVectorIteratorTest,Next)92 HWTEST_F_L0(JSAPIBitVectorIteratorTest, Next)
93 {
94     JSHandle<JSAPIBitVector> bitVector(thread, CreateBitVector());
95     uint32_t elementsNum = 256;
96     for (uint32_t i = 0; i < elementsNum; i++) {
97         JSHandle<JSTaggedValue> value(thread, JSTaggedValue(1));
98         JSAPIBitVector::Push(thread, bitVector, value);
99     }
100     JSHandle<JSAPIBitVectorIterator> bitVectorIterator(thread, JSAPIBitVector::GetIteratorObj(thread, bitVector));
101     JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
102     uint32_t length = bitVector->GetLength();
103     for (uint32_t i = 0; i < length; i++) {
104         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
105         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
106         ecmaRuntimeCallInfo->SetThis(bitVectorIterator.GetTaggedValue());
107 
108         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
109         JSTaggedValue result = JSAPIBitVectorIterator::Next(ecmaRuntimeCallInfo);
110         TestHelper::TearDownFrame(thread, prev);
111 
112         JSHandle<JSObject> resultObj(thread, result);
113         if (i < elementsNum) {
114             EXPECT_EQ(bitVectorIterator->GetNextIndex(), i + 1U);
115             EXPECT_EQ((JSObject::GetProperty(thread, resultObj, valueStr).GetValue()).GetTaggedValue(),
116                 JSTaggedValue(1));
117         } else {
118             EXPECT_EQ(bitVectorIterator->GetIteratedBitVector(), JSTaggedValue::Undefined());
119             EXPECT_EQ((JSObject::GetProperty(thread, resultObj, valueStr).GetValue()).GetTaggedValue(),
120                 JSTaggedValue::Undefined());
121         }
122     }
123 
124     // test Next exception
125     {
126         auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
127         ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
128         ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
129 
130         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
131         JSTaggedValue result = JSAPIBitVectorIterator::Next(ecmaRuntimeCallInfo);
132         TestHelper::TearDownFrame(thread, prev);
133         EXPECT_EQ(result, JSTaggedValue::Exception());
134         EXPECT_EXCEPTION();
135     }
136 }
137 } // namespace panda::test
138