14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "builtin_test_util.h"
174514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_arraybuffer.h"
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h"
204514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
214514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
224514f5e3Sopenharmony_ci
234514f5e3Sopenharmony_ci#include "ecmascript/js_arraybuffer.h"
244514f5e3Sopenharmony_ci#include "ecmascript/js_handle.h"
254514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
264514f5e3Sopenharmony_ci
274514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
284514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins;
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_cinamespace panda::test {
314514f5e3Sopenharmony_ciclass BuiltinsArrayBufferTest : public BaseTestWithScope<false> {
324514f5e3Sopenharmony_ci};
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_ci// new ArrayBuffer(8)
354514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsArrayBufferTest, Constructor1)
364514f5e3Sopenharmony_ci{
374514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
384514f5e3Sopenharmony_ci    JSHandle<JSFunction> arrayBuffer(thread, env->GetArrayBufferFunction().GetTaggedValue());
394514f5e3Sopenharmony_ci    JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
404514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, arrayBuffer.GetTaggedValue(), 6);
414514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(arrayBuffer.GetTaggedValue());
424514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
434514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(8)));
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
464514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsArrayBuffer::ArrayBufferConstructor(ecmaRuntimeCallInfo);
474514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsECMAObject());
484514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
494514f5e3Sopenharmony_ci}
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci// (new ArrayBuffer(5)).byteLength
524514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsArrayBufferTest, byteLength1)
534514f5e3Sopenharmony_ci{
544514f5e3Sopenharmony_ci    JSTaggedValue tagged = BuiltTestUtil::CreateBuiltinsArrayBuffer(thread, 5);
554514f5e3Sopenharmony_ci    JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
564514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
574514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
584514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(arrBuf.GetTaggedValue());
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
614514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsArrayBuffer::GetByteLength(ecmaRuntimeCallInfo);
624514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(5).GetRawData());
634514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
644514f5e3Sopenharmony_ci}
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci// (new ArrayBuffer(10)).slice(1, 5).bytelength
674514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsArrayBufferTest, slice1)
684514f5e3Sopenharmony_ci{
694514f5e3Sopenharmony_ci    JSTaggedValue tagged = BuiltTestUtil::CreateBuiltinsArrayBuffer(thread, 10);
704514f5e3Sopenharmony_ci    JSHandle<JSArrayBuffer> arrBuf(thread, JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(tagged.GetRawData())));
714514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
724514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
734514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(arrBuf.GetTaggedValue());
744514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int32_t>(1)));
754514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(5)));
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
784514f5e3Sopenharmony_ci    JSTaggedValue result1 = BuiltinsArrayBuffer::Slice(ecmaRuntimeCallInfo);
794514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
804514f5e3Sopenharmony_ci    JSHandle<JSArrayBuffer> arrBuf1(thread,
814514f5e3Sopenharmony_ci                                     JSArrayBuffer::Cast(reinterpret_cast<TaggedObject *>(result1.GetRawData())));
824514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
834514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
844514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetThis(arrBuf1.GetTaggedValue());
854514f5e3Sopenharmony_ci    prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
864514f5e3Sopenharmony_ci    JSTaggedValue result2 = BuiltinsArrayBuffer::GetByteLength(ecmaRuntimeCallInfo1);
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ci    ASSERT_EQ(result2.GetRawData(), JSTaggedValue(4).GetRawData());
894514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
904514f5e3Sopenharmony_ci}
914514f5e3Sopenharmony_ci}  // namespace panda::test
92