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 testing::Test {
27 public:
SetUpTestCase()28 static void SetUpTestCase()
29 {
30 GTEST_LOG_(INFO) << "SetUpTestCase";
31 }
32
TearDownTestCase()33 static void TearDownTestCase()
34 {
35 GTEST_LOG_(INFO) << "TearDownCase";
36 }
37
SetUp()38 void SetUp() override
39 {
40 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
41 }
42
TearDown()43 void TearDown() override
44 {
45 TestHelper::DestroyEcmaVMWithScope(instance, scope);
46 }
47
48 EcmaVM *instance {nullptr};
49 EcmaHandleScope *scope {nullptr};
50 JSThread *thread {nullptr};
51 };
52
53 /**
54 * @tc.name: ComputeSize / GetData
55 * @tc.desc: Compute the bytesize of bytearray in memory and get the pointer of Onheap data.
56 * @tc.type: FUNC
57 * @tc.require:
58 */
HWTEST_F_L0(ByteArrayTest,ComputeSizeAndGetData)59 HWTEST_F_L0(ByteArrayTest, ComputeSizeAndGetData)
60 {
61 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
62 JSHandle<ByteArray> byteArray = factory->NewByteArray(5, 8);
63 EXPECT_EQ(ByteArray::ComputeSize(8, 0), 16U);
64 EXPECT_EQ(ByteArray::ComputeSize(16, 4), 80U);
65 uintptr_t dataPointer = reinterpret_cast<uintptr_t>(byteArray.GetTaggedValue().GetTaggedObject()) + 32;
66 EXPECT_EQ(byteArray->GetData(2), reinterpret_cast<void *>(dataPointer));
67 }
68
69 /**
70 * @tc.name: Set / Get
71 * @tc.desc: Set data to bytearray and get data from bytearray.
72 * @tc.type: FUNC
73 * @tc.require:
74 */
HWTEST_F_L0(ByteArrayTest,SetAndGet)75 HWTEST_F_L0(ByteArrayTest, SetAndGet)
76 {
77 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
78 uint32_t value = 4294967295;
79 JSTaggedType val = JSTaggedValue(value).GetRawData();
80 JSHandle<ByteArray> byteArray = factory->NewByteArray(3, sizeof(value));
81 byteArray->Set(thread, 1, DataViewType::UINT32, val, 2);
82 EXPECT_EQ(byteArray->Get(thread, 1, DataViewType::UINT32, 2), JSTaggedValue(value));
83 }
84 } // namespace panda::ecmascript