• 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/base/atomic_helper.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/tests/test_helper.h"
19 
20 using namespace panda::ecmascript;
21 using namespace panda::ecmascript::base;
22 
23 namespace panda::test {
24 class AtomicHelperTest : public testing::Test {
25 public:
SetUpTestCase()26     static void SetUpTestCase()
27     {
28         GTEST_LOG_(INFO) << "SetUpTestCase";
29     }
30 
TearDownTestCase()31     static void TearDownTestCase()
32     {
33         GTEST_LOG_(INFO) << "TearDownCase";
34     }
35 
SetUp()36     void SetUp() override
37     {
38         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
39     }
40 
TearDown()41     void TearDown() override
42     {
43         TestHelper::DestroyEcmaVMWithScope(instance, scope);
44     }
45 
46     EcmaVM *instance {nullptr};
47     EcmaHandleScope *scope {nullptr};
48     JSThread *thread {nullptr};
49 };
50 
HWTEST_F_L0(AtomicHelperTest,ValidateIntegerTypedArray)51 HWTEST_F_L0(AtomicHelperTest, ValidateIntegerTypedArray)
52 {
53     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
54     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
55 
56     bool waitable = false;
57     uint32_t bufferSize = 10;
58     JSHandle<JSTaggedValue> func = env->GetInt8ArrayFunction();
59     JSHandle<JSTypedArray> array =
60         JSHandle<JSTypedArray>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(func), func));
61     JSHandle<JSArrayBuffer> buffer = factory->NewJSArrayBuffer(bufferSize);
62     JSHandle<JSTaggedValue> bufferVal = JSHandle<JSTaggedValue>::Cast(buffer);
63     array->SetViewedArrayBufferOrByteArray(thread, bufferVal);
64     JSHandle<JSTaggedValue> arrayVal = JSHandle<JSTaggedValue>::Cast(array);
65     JSHandle<JSTaggedValue> resultBuffer(thread, AtomicHelper::ValidateIntegerTypedArray(thread, arrayVal, waitable));
66     EXPECT_EQ(resultBuffer.GetTaggedValue(), buffer.GetTaggedValue());
67 }
68 
HWTEST_F_L0(AtomicHelperTest,ValidateAtomicAccess)69 HWTEST_F_L0(AtomicHelperTest, ValidateAtomicAccess)
70 {
71     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
72     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
73     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
74 
75     uint32_t bufferSize = 256;
76     uint32_t byteOffset = 7;
77     uint32_t arrayLength = 3;
78     JSHandle<JSTaggedValue> func = env->GetInt8ArrayFunction();
79     JSHandle<JSTypedArray> array =
80         JSHandle<JSTypedArray>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(func), func));
81     JSHandle<JSArrayBuffer> buffer = factory->NewJSArrayBuffer(bufferSize);
82     JSHandle<JSTaggedValue> bufferVal = JSHandle<JSTaggedValue>::Cast(buffer);
83     array->SetViewedArrayBufferOrByteArray(thread, bufferVal);
84     array->SetTypedArrayName(thread, globalConst->GetHandledInt8ArrayString()); // test int8 array
85     array->SetByteOffset(byteOffset);
86     array->SetArrayLength(arrayLength);
87     JSHandle<JSTaggedValue> arrayVal = JSHandle<JSTaggedValue>::Cast(array);
88     uint32_t index0 =
89         AtomicHelper::ValidateAtomicAccess(thread, arrayVal, JSHandle<JSTaggedValue>(thread, JSTaggedValue(0)));
90     uint32_t index1 =
91         AtomicHelper::ValidateAtomicAccess(thread, arrayVal, JSHandle<JSTaggedValue>(thread, JSTaggedValue(1)));
92     uint32_t index2 =
93         AtomicHelper::ValidateAtomicAccess(thread, arrayVal, JSHandle<JSTaggedValue>(thread, JSTaggedValue(2)));
94     EXPECT_EQ(index0, 0 * sizeof(int8_t) + byteOffset);
95     EXPECT_EQ(index1, 1 * sizeof(int8_t) + byteOffset);
96     EXPECT_EQ(index2, 2 * sizeof(int8_t) + byteOffset);
97 }
98 
HWTEST_F_L0(AtomicHelperTest,Atomic_Store_Load)99 HWTEST_F_L0(AtomicHelperTest, Atomic_Store_Load)
100 {
101     JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
102     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
103     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
104 
105     uint32_t bufferSize = 256;
106     uint32_t byteOffset = 7;
107     uint32_t arrayLength = 3;
108     JSHandle<JSTaggedValue> func = env->GetUint32ArrayFunction();
109     JSHandle<JSTypedArray> array =
110         JSHandle<JSTypedArray>::Cast(factory->NewJSObjectByConstructor(JSHandle<JSFunction>::Cast(func), func));
111     JSHandle<JSArrayBuffer> buffer = factory->NewJSArrayBuffer(bufferSize);
112     JSHandle<JSTaggedValue> bufferVal = JSHandle<JSTaggedValue>::Cast(buffer);
113     array->SetViewedArrayBufferOrByteArray(thread, bufferVal);
114     array->SetTypedArrayName(thread, globalConst->GetHandledUint32ArrayString()); // test uint32_t array
115     array->SetByteOffset(byteOffset);
116     array->SetArrayLength(arrayLength);
117     JSHandle<JSTaggedValue> arrayVal = JSHandle<JSTaggedValue>::Cast(array);
118     JSHandle<JSTaggedValue> index0(thread, JSTaggedValue(0));
119     JSHandle<JSTaggedValue> index1(thread, JSTaggedValue(1));
120     JSHandle<JSTaggedValue> index2(thread, JSTaggedValue(2));
121     JSHandle<JSTaggedValue> value0(thread, JSTaggedValue(-1)); // to uint32_t : 4294967295
122     JSHandle<JSTaggedValue> value1(thread, JSTaggedValue(1));
123 #ifdef PANDA_TARGET_MACOS
124     JSHandle<JSTaggedValue> value2(thread, static_cast<JSTaggedValue>(static_cast<uint32_t>(4294967295 + 1))); // to uint32_t : 0
125 #else
126     JSHandle<JSTaggedValue> value2(thread, JSTaggedValue(4294967295 + 1));
127 #endif
128     JSHandle<JSTaggedValue> bufferTag0(thread, AtomicHelper::AtomicStore(thread, arrayVal, index0, value0));
129     JSHandle<JSTaggedValue> bufferTag1(thread, AtomicHelper::AtomicStore(thread, arrayVal, index1, value1));
130     JSHandle<JSTaggedValue> bufferTag2(thread, AtomicHelper::AtomicStore(thread, arrayVal, index2, value2));
131     EXPECT_EQ(value0.GetTaggedValue().GetNumber(), -1);
132     EXPECT_EQ(value1.GetTaggedValue().GetNumber(), 1);
133     EXPECT_EQ(value2.GetTaggedValue().GetNumber(), 4294967296);
134 
135     JSHandle<JSTaggedValue> result0(thread, AtomicHelper::AtomicLoad(thread, arrayVal, index0));
136     JSHandle<JSTaggedValue> result1(thread, AtomicHelper::AtomicLoad(thread, arrayVal, index1));
137     JSHandle<JSTaggedValue> result2(thread, AtomicHelper::AtomicLoad(thread, arrayVal, index2));
138     EXPECT_EQ(result0.GetTaggedValue().GetNumber(), 4294967295); // 4294967295 : -1 to uint32_t
139     EXPECT_EQ(result1.GetTaggedValue().GetNumber(), 1);
140     EXPECT_EQ(result2.GetTaggedValue().GetNumber(), 0); // 0 : 4294967296 to uint32_t
141 }
142 }  // namespace panda::test
143