• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_buffer.h"
17 #include "ecmascript/containers/containers_private.h"
18 #include "ecmascript/containers/tests/containers_test_helper.h"
19 #include "ecmascript/ecma_runtime_call_info.h"
20 #include "ecmascript/ecma_string.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/js_api/js_api_buffer.h"
23 #include "ecmascript/js_array.h"
24 #include "ecmascript/js_handle.h"
25 #include "ecmascript/js_hclass.h"
26 #include "ecmascript/js_tagged_value-inl.h"
27 #include "ecmascript/js_tagged_value.h"
28 #include "ecmascript/js_thread.h"
29 #include "ecmascript/object_factory.h"
30 #include "ecmascript/tests/test_helper.h"
31 #include "gtest/gtest.h"
32 #include "macros.h"
33 
34 using namespace panda::ecmascript;
35 using namespace panda::ecmascript::containers;
36 namespace panda::test {
37 class ContainersBufferTest : public testing::Test {
38 public:
GetArgvCount(uint32_t setCount)39     static uint32_t GetArgvCount(uint32_t setCount)
40     {
41         return setCount * 2;  // 2 means the every arg cover 2 length
42     }
SetUpTestCase()43     static void SetUpTestCase()
44     {
45         GTEST_LOG_(INFO) << "SetUpTestCase";
46     }
TearDownTestCase()47     static void TearDownTestCase()
48     {
49         GTEST_LOG_(INFO) << "TearDownCase";
50     }
SetUp()51     void SetUp() override
52     {
53         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
54     }
TearDown()55     void TearDown() override
56     {
57         TestHelper::DestroyEcmaVMWithScope(instance, scope);
58     }
59     EcmaVM *instance {nullptr};
60     EcmaHandleScope *scope {nullptr};
61     JSThread *thread {nullptr};
62 
63 protected:
InitializeBufferConstructor()64     JSTaggedValue InitializeBufferConstructor()
65     {
66         ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
67         JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
68         JSHandle<JSTaggedValue> globalObject = env->GetJSGlobalObject();
69         JSHandle<JSTaggedValue> key(factory->NewFromASCII("ArkPrivate"));
70         JSHandle<JSTaggedValue> value =
71             JSObject::GetProperty(thread, JSHandle<JSTaggedValue>(globalObject), key).GetValue();
72         auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
73                                                                  ContainersBufferTest::GetArgvCount(3));
74         objCallInfo->SetFunction(JSTaggedValue::Undefined());
75         objCallInfo->SetThis(value.GetTaggedValue());
76         objCallInfo->SetCallArg(0, JSTaggedValue(static_cast<int>(ContainerTag::FastBuffer)));
77         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
78         JSTaggedValue result = ContainersPrivate::Load(objCallInfo);
79         TestHelper::TearDownFrame(thread, prev);
80         return result;
81     }
CreateJSAPIBuffer(uint32_t length=JSAPIFastBuffer::DEFAULT_CAPACITY_LENGTH)82     JSHandle<JSAPIFastBuffer> CreateJSAPIBuffer(uint32_t length = JSAPIFastBuffer::DEFAULT_CAPACITY_LENGTH)
83     {
84         JSHandle<JSFunction> newTarget(thread, InitializeBufferConstructor());
85         auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
86                                                                  ContainersBufferTest::GetArgvCount(4));
87         objCallInfo->SetFunction(newTarget.GetTaggedValue());
88         objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
89         objCallInfo->SetThis(JSTaggedValue::Undefined());
90         objCallInfo->SetCallArg(0, JSTaggedValue(length));
91         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
92         JSTaggedValue result = ContainersBuffer::BufferConstructor(objCallInfo);
93         TestHelper::TearDownFrame(thread, prev);
94         JSHandle<JSAPIFastBuffer> buffer(thread, result);
95         return buffer;
96     }
97 };
98 
HWTEST_F_L0(ContainersBufferTest,BufferConstructor)99 HWTEST_F_L0(ContainersBufferTest, BufferConstructor)
100 {
101     InitializeBufferConstructor();
102     JSHandle<JSFunction> newTarget(thread, InitializeBufferConstructor());
103     auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
104                                                              ContainersBufferTest::GetArgvCount(4));
105     objCallInfo->SetFunction(newTarget.GetTaggedValue());
106     objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
107     objCallInfo->SetThis(JSTaggedValue::Undefined());
108     objCallInfo->SetCallArg(0, JSTaggedValue(JSAPIFastBuffer::DEFAULT_CAPACITY_LENGTH));
109     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
110     JSTaggedValue result = ContainersBuffer::BufferConstructor(objCallInfo);
111     TestHelper::TearDownFrame(thread, prev);
112     ASSERT_TRUE(result.IsJSAPIBuffer());
113     JSHandle<JSAPIFastBuffer> buffer(thread, result);
114     JSTaggedValue resultProto = JSObject::GetPrototype(thread, JSHandle<JSObject>::Cast(buffer));
115     JSTaggedValue funcProto = newTarget->GetFunctionPrototype(thread);
116     ASSERT_EQ(resultProto, funcProto);
117     int length = buffer->GetLength();
118     ASSERT_EQ(length, JSAPIFastBuffer::DEFAULT_CAPACITY_LENGTH);
119     objCallInfo->SetNewTarget(JSTaggedValue::Undefined());
120     CONTAINERS_API_EXCEPTION_TEST(ContainersBuffer, BufferConstructor, objCallInfo);
121 }
122 
HWTEST_F_L0(ContainersBufferTest,Length_001)123 HWTEST_F_L0(ContainersBufferTest, Length_001)
124 {
125     JSHandle<JSAPIFastBuffer> buffer = CreateJSAPIBuffer();
126     auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
127                                                           ContainersBufferTest::GetArgvCount(2));
128     callInfo->SetFunction(JSTaggedValue::Undefined());
129     callInfo->SetThis(buffer.GetTaggedValue());
130     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
131     TestHelper::TearDownFrame(thread, prev);
132     EXPECT_EQ(buffer->GetLength(), JSAPIFastBuffer::DEFAULT_CAPACITY_LENGTH);
133 }
134 
HWTEST_F_L0(ContainersBufferTest,WriteInt32BEAndReadInt32BETest001)135 HWTEST_F_L0(ContainersBufferTest, WriteInt32BEAndReadInt32BETest001)
136 {
137     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
138     {
139         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
140                                                               ContainersBufferTest::GetArgvCount(4));
141         callInfo->SetFunction(JSTaggedValue::Undefined());
142         callInfo->SetThis(buf.GetTaggedValue());
143         callInfo->SetCallArg(0, JSTaggedValue(0x12345678));
144         callInfo->SetCallArg(1, JSTaggedValue(0));
145         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
146         ContainersBuffer::WriteInt32BE(callInfo);
147         TestHelper::TearDownFrame(thread, prev);
148     }
149     {
150         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
151                                                               ContainersBufferTest::GetArgvCount(3));
152         callInfo->SetFunction(JSTaggedValue::Undefined());
153         callInfo->SetThis(buf.GetTaggedValue());
154         callInfo->SetCallArg(0, JSTaggedValue(0));
155         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
156         JSTaggedValue res = ContainersBuffer::ReadInt32BE(callInfo);
157         TestHelper::TearDownFrame(thread, prev);
158         ASSERT_EQ(res.GetInt(), 0x12345678);
159     }
160 }
161 
HWTEST_F_L0(ContainersBufferTest,WriteInt32BEAndReadInt32BETest002)162 HWTEST_F_L0(ContainersBufferTest, WriteInt32BEAndReadInt32BETest002)
163 {
164     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
165     {
166         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
167                                                               ContainersBufferTest::GetArgvCount(4));
168         callInfo->SetFunction(JSTaggedValue::Undefined());
169         callInfo->SetThis(buf.GetTaggedValue());
170         callInfo->SetCallArg(0, JSTaggedValue(0x12345678));
171         callInfo->SetCallArg(1, JSTaggedValue(1));
172         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
173         ContainersBuffer::WriteInt32BE(callInfo);
174         TestHelper::TearDownFrame(thread, prev);
175     }
176     {
177         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
178                                                               ContainersBufferTest::GetArgvCount(3));
179         callInfo->SetFunction(JSTaggedValue::Undefined());
180         callInfo->SetThis(buf.GetTaggedValue());
181         callInfo->SetCallArg(0, JSTaggedValue(1));
182         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
183         JSTaggedValue res = ContainersBuffer::ReadInt32BE(callInfo);
184         TestHelper::TearDownFrame(thread, prev);
185         ASSERT_EQ(res.GetInt(), 0x12345678);
186     }
187 }
188 
HWTEST_F_L0(ContainersBufferTest,WriteInt16BEAndReadInt16BETest001)189 HWTEST_F_L0(ContainersBufferTest, WriteInt16BEAndReadInt16BETest001)
190 {
191     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
192     {
193         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
194                                                               ContainersBufferTest::GetArgvCount(4));
195         callInfo->SetFunction(JSTaggedValue::Undefined());
196         callInfo->SetThis(buf.GetTaggedValue());
197         callInfo->SetCallArg(0, JSTaggedValue(0x1234));
198         callInfo->SetCallArg(1, JSTaggedValue(0));
199         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
200         ContainersBuffer::WriteInt16BE(callInfo);
201         TestHelper::TearDownFrame(thread, prev);
202     }
203     {
204         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
205                                                               ContainersBufferTest::GetArgvCount(3));
206         callInfo->SetFunction(JSTaggedValue::Undefined());
207         callInfo->SetThis(buf.GetTaggedValue());
208         callInfo->SetCallArg(0, JSTaggedValue(0));
209         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
210         JSTaggedValue res = ContainersBuffer::ReadInt16BE(callInfo);
211         TestHelper::TearDownFrame(thread, prev);
212         ASSERT_EQ(res.GetInt(), 0x1234);
213     }
214 }
215 
HWTEST_F_L0(ContainersBufferTest,WriteInt16BEAndReadInt16BETest002)216 HWTEST_F_L0(ContainersBufferTest, WriteInt16BEAndReadInt16BETest002)
217 {
218     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
219     {
220         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
221                                                               ContainersBufferTest::GetArgvCount(4));
222         callInfo->SetFunction(JSTaggedValue::Undefined());
223         callInfo->SetThis(buf.GetTaggedValue());
224         callInfo->SetCallArg(0, JSTaggedValue(0x1234));
225         callInfo->SetCallArg(1, JSTaggedValue(1));
226         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
227         ContainersBuffer::WriteInt16LE(callInfo);
228         TestHelper::TearDownFrame(thread, prev);
229     }
230     {
231         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
232                                                               ContainersBufferTest::GetArgvCount(3));
233         callInfo->SetFunction(JSTaggedValue::Undefined());
234         callInfo->SetThis(buf.GetTaggedValue());
235         callInfo->SetCallArg(0, JSTaggedValue(1));
236         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
237         JSTaggedValue res = ContainersBuffer::ReadInt16BE(callInfo);
238         TestHelper::TearDownFrame(thread, prev);
239         ASSERT_EQ(res.GetInt(), 0x3412);
240     }
241 }
242 
HWTEST_F_L0(ContainersBufferTest,WriteInt32LEAndReadIntTest001)243 HWTEST_F_L0(ContainersBufferTest, WriteInt32LEAndReadIntTest001)
244 {
245     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
246     {
247         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
248                                                               ContainersBufferTest::GetArgvCount(4));
249         callInfo->SetFunction(JSTaggedValue::Undefined());
250         callInfo->SetThis(buf.GetTaggedValue());
251         callInfo->SetCallArg(0, JSTaggedValue(0x12345678));
252         callInfo->SetCallArg(1, JSTaggedValue(0));
253         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
254         ContainersBuffer::WriteInt32LE(callInfo);
255         TestHelper::TearDownFrame(thread, prev);
256     }
257     {
258         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
259                                                               ContainersBufferTest::GetArgvCount(3));
260         callInfo->SetFunction(JSTaggedValue::Undefined());
261         callInfo->SetThis(buf.GetTaggedValue());
262         callInfo->SetCallArg(0, JSTaggedValue(0));
263         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
264         JSTaggedValue res = ContainersBuffer::ReadInt32LE(callInfo);
265         TestHelper::TearDownFrame(thread, prev);
266         ASSERT_EQ(res.GetInt(), 0x12345678);
267     }
268     {
269         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
270                                                               ContainersBufferTest::GetArgvCount(3));
271         callInfo->SetFunction(JSTaggedValue::Undefined());
272         callInfo->SetThis(buf.GetTaggedValue());
273         callInfo->SetCallArg(0, JSTaggedValue(0));
274         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
275         JSTaggedValue res = ContainersBuffer::ReadInt32BE(callInfo);
276         TestHelper::TearDownFrame(thread, prev);
277         ASSERT_EQ(res.GetInt(), 0x78563412);
278     }
279 }
280 
HWTEST_F_L0(ContainersBufferTest,WriteInt32LEAndReadIntTest002)281 HWTEST_F_L0(ContainersBufferTest, WriteInt32LEAndReadIntTest002)
282 {
283     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
284     {
285         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
286                                                               ContainersBufferTest::GetArgvCount(4));
287         callInfo->SetFunction(JSTaggedValue::Undefined());
288         callInfo->SetThis(buf.GetTaggedValue());
289         callInfo->SetCallArg(0, JSTaggedValue(0x12345678));
290         callInfo->SetCallArg(1, JSTaggedValue(1));
291         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
292         ContainersBuffer::WriteInt32LE(callInfo);
293         TestHelper::TearDownFrame(thread, prev);
294     }
295     {
296         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
297                                                               ContainersBufferTest::GetArgvCount(3));
298         callInfo->SetFunction(JSTaggedValue::Undefined());
299         callInfo->SetThis(buf.GetTaggedValue());
300         callInfo->SetCallArg(0, JSTaggedValue(1));
301         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
302         JSTaggedValue res = ContainersBuffer::ReadInt32LE(callInfo);
303         TestHelper::TearDownFrame(thread, prev);
304         ASSERT_EQ(res.GetInt(), 0x12345678);
305     }
306     {
307         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
308                                                               ContainersBufferTest::GetArgvCount(3));
309         callInfo->SetFunction(JSTaggedValue::Undefined());
310         callInfo->SetThis(buf.GetTaggedValue());
311         callInfo->SetCallArg(0, JSTaggedValue(1));
312         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
313         JSTaggedValue res = ContainersBuffer::ReadInt32BE(callInfo);
314         TestHelper::TearDownFrame(thread, prev);
315         ASSERT_EQ(res.GetInt(), 0x78563412);
316     }
317 }
318 
HWTEST_F_L0(ContainersBufferTest,WriteIntLEAndReadIntTest001)319 HWTEST_F_L0(ContainersBufferTest, WriteIntLEAndReadIntTest001)
320 {
321     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
322     {
323         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
324                                                               ContainersBufferTest::GetArgvCount(5));
325         callInfo->SetFunction(JSTaggedValue::Undefined());
326         callInfo->SetThis(buf.GetTaggedValue());
327         callInfo->SetCallArg(0, JSTaggedValue(0x1234));
328         callInfo->SetCallArg(1, JSTaggedValue(1));
329         callInfo->SetCallArg(2, JSTaggedValue(2));
330         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
331         ContainersBuffer::WriteIntLE(callInfo);
332         TestHelper::TearDownFrame(thread, prev);
333     }
334     {
335         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
336                                                               ContainersBufferTest::GetArgvCount(4));
337         callInfo->SetFunction(JSTaggedValue::Undefined());
338         callInfo->SetThis(buf.GetTaggedValue());
339         callInfo->SetCallArg(0, JSTaggedValue(1));
340         callInfo->SetCallArg(1, JSTaggedValue(2));
341         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
342         JSTaggedValue res = ContainersBuffer::ReadIntLE(callInfo);
343         TestHelper::TearDownFrame(thread, prev);
344         ASSERT_EQ(res.GetInt(), 0x1234);
345     }
346     {
347         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
348                                                               ContainersBufferTest::GetArgvCount(4));
349         callInfo->SetFunction(JSTaggedValue::Undefined());
350         callInfo->SetThis(buf.GetTaggedValue());
351         callInfo->SetCallArg(0, JSTaggedValue(1));
352         callInfo->SetCallArg(1, JSTaggedValue(2));
353         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
354         JSTaggedValue res = ContainersBuffer::ReadIntBE(callInfo);
355         TestHelper::TearDownFrame(thread, prev);
356         ASSERT_EQ(res.GetInt(), 0x3412);
357     }
358 }
359 
HWTEST_F_L0(ContainersBufferTest,WriteIntLEAndReadIntTest002)360 HWTEST_F_L0(ContainersBufferTest, WriteIntLEAndReadIntTest002)
361 {
362     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
363     {
364         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
365                                                               ContainersBufferTest::GetArgvCount(5));
366         callInfo->SetFunction(JSTaggedValue::Undefined());
367         callInfo->SetThis(buf.GetTaggedValue());
368         callInfo->SetCallArg(0, JSTaggedValue(0x123456));
369         callInfo->SetCallArg(1, JSTaggedValue(1));
370         callInfo->SetCallArg(2, JSTaggedValue(3));
371         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
372         ContainersBuffer::WriteIntLE(callInfo);
373         TestHelper::TearDownFrame(thread, prev);
374     }
375     {
376         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
377                                                               ContainersBufferTest::GetArgvCount(4));
378         callInfo->SetFunction(JSTaggedValue::Undefined());
379         callInfo->SetThis(buf.GetTaggedValue());
380         callInfo->SetCallArg(0, JSTaggedValue(1));
381         callInfo->SetCallArg(1, JSTaggedValue(3));
382         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
383         JSTaggedValue res = ContainersBuffer::ReadIntLE(callInfo);
384         TestHelper::TearDownFrame(thread, prev);
385         ASSERT_EQ(res.GetInt(), 0x123456);
386     }
387     {
388         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
389                                                               ContainersBufferTest::GetArgvCount(4));
390         callInfo->SetFunction(JSTaggedValue::Undefined());
391         callInfo->SetThis(buf.GetTaggedValue());
392         callInfo->SetCallArg(0, JSTaggedValue(1));
393         callInfo->SetCallArg(1, JSTaggedValue(3));
394         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
395         JSTaggedValue res = ContainersBuffer::ReadIntBE(callInfo);
396         TestHelper::TearDownFrame(thread, prev);
397         ASSERT_EQ(res.GetInt(), 0x563412);
398     }
399 }
400 
HWTEST_F_L0(ContainersBufferTest,WriteIntAndReadIntTest003)401 HWTEST_F_L0(ContainersBufferTest, WriteIntAndReadIntTest003)
402 {
403     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
404     {
405         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
406                                                               ContainersBufferTest::GetArgvCount(5));
407         callInfo->SetFunction(JSTaggedValue::Undefined());
408         callInfo->SetThis(buf.GetTaggedValue());
409         callInfo->SetCallArg(0, JSTaggedValue(0x123456));
410         callInfo->SetCallArg(1, JSTaggedValue(1));
411         callInfo->SetCallArg(2, JSTaggedValue(3));
412         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
413         ContainersBuffer::WriteIntBE(callInfo);
414         TestHelper::TearDownFrame(thread, prev);
415     }
416     {
417         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
418                                                               ContainersBufferTest::GetArgvCount(4));
419         callInfo->SetFunction(JSTaggedValue::Undefined());
420         callInfo->SetThis(buf.GetTaggedValue());
421         callInfo->SetCallArg(0, JSTaggedValue(1));
422         callInfo->SetCallArg(1, JSTaggedValue(3));
423         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
424         JSTaggedValue res = ContainersBuffer::ReadIntBE(callInfo);
425         TestHelper::TearDownFrame(thread, prev);
426         ASSERT_EQ(res.GetInt(), 0x123456);
427     }
428     {
429         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
430                                                               ContainersBufferTest::GetArgvCount(4));
431         callInfo->SetFunction(JSTaggedValue::Undefined());
432         callInfo->SetThis(buf.GetTaggedValue());
433         callInfo->SetCallArg(0, JSTaggedValue(1));
434         callInfo->SetCallArg(1, JSTaggedValue(3));
435         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
436         JSTaggedValue res = ContainersBuffer::ReadIntLE(callInfo);
437         TestHelper::TearDownFrame(thread, prev);
438         ASSERT_EQ(res.GetInt(), 0x563412);
439     }
440 }
441 
HWTEST_F_L0(ContainersBufferTest,WriteIntAndReadIntTest004)442 HWTEST_F_L0(ContainersBufferTest, WriteIntAndReadIntTest004)
443 {
444     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
445     uint64_t value = 0x12345678ABCD;
446     {
447         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
448                                                               ContainersBufferTest::GetArgvCount(5));
449         callInfo->SetFunction(JSTaggedValue::Undefined());
450         callInfo->SetThis(buf.GetTaggedValue());
451         callInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(value)));
452         callInfo->SetCallArg(1, JSTaggedValue(0));
453         callInfo->SetCallArg(2, JSTaggedValue(6));
454         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
455         ContainersBuffer::WriteIntBE(callInfo);
456         TestHelper::TearDownFrame(thread, prev);
457     }
458     {
459         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
460                                                               ContainersBufferTest::GetArgvCount(4));
461         callInfo->SetFunction(JSTaggedValue::Undefined());
462         callInfo->SetThis(buf.GetTaggedValue());
463         callInfo->SetCallArg(0, JSTaggedValue(0));
464         callInfo->SetCallArg(1, JSTaggedValue(6));
465         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
466         JSTaggedValue res = ContainersBuffer::ReadIntBE(callInfo);
467         TestHelper::TearDownFrame(thread, prev);
468         ASSERT_EQ(static_cast<int64_t>(res.GetDouble()), value);
469     }
470 }
471 
HWTEST_F_L0(ContainersBufferTest,WriteIntAndReadIntTest005)472 HWTEST_F_L0(ContainersBufferTest, WriteIntAndReadIntTest005)
473 {
474     constexpr uint32_t BUFFER_SIZE = 10;
475     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(BUFFER_SIZE);
476     // 0x12345678ABCD : test value
477     uint64_t value = 0x12345678ABCD;
478     {
479         auto callInfo =
480             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
481                                                   ContainersBufferTest::GetArgvCount(5));  // 5 : five Args in callInfo
482         callInfo->SetFunction(JSTaggedValue::Undefined());
483         callInfo->SetThis(buf.GetTaggedValue());
484         // 0 : the first parameter
485         callInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(value)));
486         // 1 : the second parameter
487         callInfo->SetCallArg(1, JSTaggedValue(0));
488         // 2 : 6 : the third parameter; write 6 bytes
489         callInfo->SetCallArg(2, JSTaggedValue(6));
490         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
491         ContainersBuffer::WriteIntLE(callInfo);
492         TestHelper::TearDownFrame(thread, prev);
493     }
494     {
495         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
496                                                               ContainersBufferTest::GetArgvCount(4));
497         callInfo->SetFunction(JSTaggedValue::Undefined());
498         callInfo->SetThis(buf.GetTaggedValue());
499         // 0 : the first parameter
500         callInfo->SetCallArg(0, JSTaggedValue(0));
501         // 1 : 6 : the second parameter; read 6 bytes
502         callInfo->SetCallArg(1, JSTaggedValue(6));
503         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
504         JSTaggedValue res = ContainersBuffer::ReadIntLE(callInfo);
505         TestHelper::TearDownFrame(thread, prev);
506         ASSERT_EQ(static_cast<int64_t>(res.GetDouble()), value);
507     }
508 }
509 
HWTEST_F_L0(ContainersBufferTest,WriteIntAndReadIntTest006)510 HWTEST_F_L0(ContainersBufferTest, WriteIntAndReadIntTest006)
511 {
512     constexpr uint32_t BUFFER_SIZE = 10;
513     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(BUFFER_SIZE);
514     // 0x12345678ABCD : test value
515     uint64_t value = 0x12345678ABCD;
516     // -55338634693614 : test result
517     int64_t ret = -55338634693614;
518     {
519         auto callInfo =
520             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
521                                                   ContainersBufferTest::GetArgvCount(5));  // 5 : five Args in callInfo
522         callInfo->SetFunction(JSTaggedValue::Undefined());
523         callInfo->SetThis(buf.GetTaggedValue());
524         // 0 : the first parameter
525         callInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(value)));
526         // 1 : the second parameter
527         callInfo->SetCallArg(1, JSTaggedValue(0));
528         // 2 : 6 : the third parameter; write 6 bytes
529         callInfo->SetCallArg(2, JSTaggedValue(6));
530         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
531         ContainersBuffer::WriteIntBE(callInfo);
532         TestHelper::TearDownFrame(thread, prev);
533     }
534     {
535         auto callInfo =
536             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
537                                                   ContainersBufferTest::GetArgvCount(4));  // 4 : four Args in callInfo
538         callInfo->SetFunction(JSTaggedValue::Undefined());
539         callInfo->SetThis(buf.GetTaggedValue());
540         // 0 : the first parameter
541         callInfo->SetCallArg(0, JSTaggedValue(0));
542         // 1 : 6 : the second parameter; read 6 bytes
543         callInfo->SetCallArg(1, JSTaggedValue(6));
544         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
545         JSTaggedValue res = ContainersBuffer::ReadIntLE(callInfo);
546         TestHelper::TearDownFrame(thread, prev);
547         ASSERT_EQ(static_cast<int64_t>(res.GetDouble()), ret);
548     }
549 }
550 
HWTEST_F_L0(ContainersBufferTest,WriteIntAndReadIntTest007)551 HWTEST_F_L0(ContainersBufferTest, WriteIntAndReadIntTest007)
552 {
553     constexpr uint32_t BUFFER_SIZE = 10;
554     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(BUFFER_SIZE);
555     // 0x1234567890ab : test value
556     int64_t value = 0x1234567890ab;
557     // -0x546f87a9cbee : test result
558     int64_t ret = -0x546f87a9cbee;
559     {
560         auto callInfo =
561             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
562                                                   ContainersBufferTest::GetArgvCount(5));  // 5 : five Args in callInfo
563         callInfo->SetFunction(JSTaggedValue::Undefined());
564         callInfo->SetThis(buf.GetTaggedValue());
565         // 0 : the first parameter
566         callInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(value)));
567         // 1 : the second parameter
568         callInfo->SetCallArg(1, JSTaggedValue(0));
569         // 2 : 6 : the third parameter; write 6 bytes
570         callInfo->SetCallArg(2, JSTaggedValue(6));
571         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
572         ContainersBuffer::WriteIntBE(callInfo);
573         TestHelper::TearDownFrame(thread, prev);
574     }
575     {
576         auto callInfo =
577             TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
578                                                   ContainersBufferTest::GetArgvCount(4));  // 4 : four Args in callInfo
579         callInfo->SetFunction(JSTaggedValue::Undefined());
580         callInfo->SetThis(buf.GetTaggedValue());
581         // 0 : the first parameter
582         callInfo->SetCallArg(0, JSTaggedValue(0));
583         // 1 : 6 : the second parameter; read 6 bytes
584         callInfo->SetCallArg(1, JSTaggedValue(6));
585         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
586         JSTaggedValue res = ContainersBuffer::ReadIntLE(callInfo);
587         TestHelper::TearDownFrame(thread, prev);
588         ASSERT_EQ(static_cast<int64_t>(res.GetDouble()), ret);
589     }
590 }
591 
HWTEST_F_L0(ContainersBufferTest,WriteDoubleAndReadDoubleTest001)592 HWTEST_F_L0(ContainersBufferTest, WriteDoubleAndReadDoubleTest001)
593 {
594     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
595     double value = 112512.1919810;
596     {
597         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
598                                                               ContainersBufferTest::GetArgvCount(5));
599         callInfo->SetFunction(JSTaggedValue::Undefined());
600         callInfo->SetThis(buf.GetTaggedValue());
601         callInfo->SetCallArg(0, JSTaggedValue(value));
602         callInfo->SetCallArg(1, JSTaggedValue(0));
603         callInfo->SetCallArg(2, JSTaggedValue(6));
604         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
605         ContainersBuffer::WriteFloat64LE(callInfo);
606         TestHelper::TearDownFrame(thread, prev);
607     }
608     {
609         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
610                                                               ContainersBufferTest::GetArgvCount(4));
611         callInfo->SetFunction(JSTaggedValue::Undefined());
612         callInfo->SetThis(buf.GetTaggedValue());
613         callInfo->SetCallArg(0, JSTaggedValue(0));
614         callInfo->SetCallArg(1, JSTaggedValue(6));
615         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
616         JSTaggedValue res = ContainersBuffer::ReadFloat64LE(callInfo);
617         TestHelper::TearDownFrame(thread, prev);
618         ASSERT_EQ(res.GetNumber(), value);
619     }
620 }
621 
HWTEST_F_L0(ContainersBufferTest,WriteDoubleAndReadDoubleTest002)622 HWTEST_F_L0(ContainersBufferTest, WriteDoubleAndReadDoubleTest002)
623 {
624     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
625     double value = 112512.1919810;
626     {
627         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
628                                                               ContainersBufferTest::GetArgvCount(5));
629         callInfo->SetFunction(JSTaggedValue::Undefined());
630         callInfo->SetThis(buf.GetTaggedValue());
631         callInfo->SetCallArg(0, JSTaggedValue(value));
632         callInfo->SetCallArg(1, JSTaggedValue(0));
633         callInfo->SetCallArg(2, JSTaggedValue(6));
634         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
635         ContainersBuffer::WriteFloat64BE(callInfo);
636         TestHelper::TearDownFrame(thread, prev);
637     }
638     {
639         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
640                                                               ContainersBufferTest::GetArgvCount(4));
641         callInfo->SetFunction(JSTaggedValue::Undefined());
642         callInfo->SetThis(buf.GetTaggedValue());
643         callInfo->SetCallArg(0, JSTaggedValue(0));
644         callInfo->SetCallArg(1, JSTaggedValue(6));
645         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
646         JSTaggedValue res = ContainersBuffer::ReadFloat64BE(callInfo);
647         TestHelper::TearDownFrame(thread, prev);
648         ASSERT_EQ(res.GetNumber(), value);
649     }
650 }
651 
HWTEST_F_L0(ContainersBufferTest,WriteFloatAndReadFloatTest001)652 HWTEST_F_L0(ContainersBufferTest, WriteFloatAndReadFloatTest001)
653 {
654     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
655     double value = 123.45;
656     {
657         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
658                                                               ContainersBufferTest::GetArgvCount(4));
659         callInfo->SetFunction(JSTaggedValue::Undefined());
660         callInfo->SetThis(buf.GetTaggedValue());
661         callInfo->SetCallArg(0, JSTaggedValue(value));
662         callInfo->SetCallArg(1, JSTaggedValue(0));
663         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
664         ContainersBuffer::WriteFloat32LE(callInfo);
665         TestHelper::TearDownFrame(thread, prev);
666     }
667     {
668         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
669                                                               ContainersBufferTest::GetArgvCount(3));
670         callInfo->SetFunction(JSTaggedValue::Undefined());
671         callInfo->SetThis(buf.GetTaggedValue());
672         callInfo->SetCallArg(0, JSTaggedValue(0));
673         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
674         double res = ContainersBuffer::ReadFloat32LE(callInfo).GetDouble();
675         TestHelper::TearDownFrame(thread, prev);
676         ASSERT_TRUE(std::fabs(res - value) < 1e-4);
677     }
678 }
679 
HWTEST_F_L0(ContainersBufferTest,WriteFloatAndReadFloatTest002)680 HWTEST_F_L0(ContainersBufferTest, WriteFloatAndReadFloatTest002)
681 {
682     JSHandle<JSAPIFastBuffer> buf = CreateJSAPIBuffer(10);
683     double value = 123.45;
684     {
685         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
686                                                               ContainersBufferTest::GetArgvCount(4));
687         callInfo->SetFunction(JSTaggedValue::Undefined());
688         callInfo->SetThis(buf.GetTaggedValue());
689         callInfo->SetCallArg(0, JSTaggedValue(value));
690         callInfo->SetCallArg(1, JSTaggedValue(0));
691         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
692         ContainersBuffer::WriteFloat32BE(callInfo);
693         TestHelper::TearDownFrame(thread, prev);
694     }
695     {
696         auto callInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
697                                                               ContainersBufferTest::GetArgvCount(3));
698         callInfo->SetFunction(JSTaggedValue::Undefined());
699         callInfo->SetThis(buf.GetTaggedValue());
700         callInfo->SetCallArg(0, JSTaggedValue(0));
701         [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, callInfo);
702         double res = ContainersBuffer::ReadFloat32BE(callInfo).GetDouble();
703         TestHelper::TearDownFrame(thread, prev);
704         ASSERT_TRUE(std::fabs(res - value) < 1e-4);
705     }
706 }
707 
HWTEST_F_L0(ContainersBufferTest,CreateFromArrayTest001)708 HWTEST_F_L0(ContainersBufferTest, CreateFromArrayTest001)
709 {
710     JSHandle<JSTaggedValue> arr =
711         JSHandle<JSTaggedValue>(thread, JSArray::ArrayCreate(thread, JSTaggedNumber(10))->GetTaggedObject());
712     InitializeBufferConstructor();
713     JSHandle<JSFunction> newTarget(thread, InitializeBufferConstructor());
714     auto objCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(),
715                                                              ContainersBufferTest::GetArgvCount(4));
716     objCallInfo->SetFunction(newTarget.GetTaggedValue());
717     objCallInfo->SetNewTarget(newTarget.GetTaggedValue());
718     objCallInfo->SetThis(JSTaggedValue::Undefined());
719     objCallInfo->SetCallArg(0, arr.GetTaggedValue());
720     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, objCallInfo);
721     JSTaggedValue result = ContainersBuffer::BufferConstructor(objCallInfo);
722     TestHelper::TearDownFrame(thread, prev);
723     JSHandle<JSAPIFastBuffer> buffer(thread, result);
724     ASSERT_EQ(result, JSTaggedValue::Exception());
725 }
726 
727 };  // namespace panda::test
728