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/byte_array.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/tests/test_helper.h"
20 #include "ecmascript/js_dataview.h"
21
22
23 using namespace panda::ecmascript;
24
25 namespace panda::test {
26 class ByteArrayTest : public BaseTestWithScope<false> {
27 };
28
29 /**
30 * @tc.name: ComputeSize / GetData
31 * @tc.desc: Compute the bytesize of bytearray in memory and get the pointer of Onheap data.
32 * @tc.type: FUNC
33 * @tc.require:
34 */
HWTEST_F_L0(ByteArrayTest,ComputeSizeAndGetData)35 HWTEST_F_L0(ByteArrayTest, ComputeSizeAndGetData)
36 {
37 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
38 JSHandle<ByteArray> byteArray = factory->NewByteArray(5, 8);
39 EXPECT_EQ(ByteArray::ComputeSize(8, 0), 16U);
40 EXPECT_EQ(ByteArray::ComputeSize(16, 4), 80U);
41 uintptr_t dataPointer = reinterpret_cast<uintptr_t>(byteArray.GetTaggedValue().GetTaggedObject()) + 32;
42 EXPECT_EQ(byteArray->GetData(2), reinterpret_cast<void *>(dataPointer));
43 }
44
45 /**
46 * @tc.name: Set / Get
47 * @tc.desc: Set data to bytearray and get data from bytearray.
48 * @tc.type: FUNC
49 * @tc.require:
50 */
HWTEST_F_L0(ByteArrayTest,SetAndGet)51 HWTEST_F_L0(ByteArrayTest, SetAndGet)
52 {
53 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
54 uint32_t value = 4294967295;
55 JSTaggedType val = JSTaggedValue(value).GetRawData();
56 JSHandle<ByteArray> byteArray = factory->NewByteArray(3, sizeof(value));
57 byteArray->Set(thread, 1, DataViewType::UINT32, val, 2);
58 EXPECT_EQ(byteArray->Get(thread, 1, DataViewType::UINT32, 2), JSTaggedValue(value));
59 }
60 } // namespace panda::ecmascript