• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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/builtins/builtins_string_iterator.h"
17 
18 #include "ecmascript/js_iterator.h"
19 #include "ecmascript/js_string_iterator.h"
20 #include "ecmascript/tests/test_helper.h"
21 
22 using namespace panda::ecmascript;
23 using namespace panda::ecmascript::builtins;
24 
25 namespace panda::test {
26 class BuiltinsStringIteratorTest : 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 
CreateBuiltinsJSStringIterator(JSThread * thread,const CString keyCStr)53 static JSTaggedValue CreateBuiltinsJSStringIterator(JSThread *thread, const CString keyCStr)
54 {
55     EcmaVM *ecmaVM = thread->GetEcmaVM();
56     ObjectFactory *factory = ecmaVM->GetFactory();
57 
58     JSHandle<EcmaString> string = factory->NewFromUtf8(&keyCStr[0]);
59     JSHandle<JSStringIterator> stringIterator = JSStringIterator::CreateStringIterator(thread, string);
60     EXPECT_TRUE(*stringIterator != nullptr);
61 
62     return stringIterator.GetTaggedValue();
63 }
64 
65 // Single char16_t Basic Multilingual plane character
HWTEST_F_L0(BuiltinsStringIteratorTest,Next_001)66 HWTEST_F_L0(BuiltinsStringIteratorTest, Next_001)
67 {
68     auto globalConst = thread->GlobalConstants();
69     CString string = "没有";
70     JSHandle<JSStringIterator> stringIterator =
71         JSHandle<JSStringIterator>(thread, CreateBuiltinsJSStringIterator(thread, string));
72     JSHandle<JSTaggedValue> valueStr = globalConst->GetHandledValueString();
73 
74     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
75     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
76     ecmaRuntimeCallInfo1->SetThis(stringIterator.GetTaggedValue());
77 
78     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
79     BuiltinsStringIterator::Next(ecmaRuntimeCallInfo1);
80     TestHelper::TearDownFrame(thread, prev);
81     EXPECT_EQ(stringIterator->GetStringIteratorNextIndex(), 1U);
82 
83     auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
84     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
85     ecmaRuntimeCallInfo2->SetThis(stringIterator.GetTaggedValue());
86 
87     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
88     BuiltinsStringIterator::Next(ecmaRuntimeCallInfo2);
89     TestHelper::TearDownFrame(thread, prev);
90     EXPECT_EQ(stringIterator->GetStringIteratorNextIndex(), 2U);
91 
92     auto ecmaRuntimeCallInfo3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
93     ecmaRuntimeCallInfo3->SetFunction(JSTaggedValue::Undefined());
94     ecmaRuntimeCallInfo3->SetThis(stringIterator.GetTaggedValue());
95 
96     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3);
97     JSTaggedValue result = BuiltinsStringIterator::Next(ecmaRuntimeCallInfo3);
98     TestHelper::TearDownFrame(thread, prev);
99 
100     JSHandle<JSTaggedValue> resultObj(thread, result);
101     EXPECT_TRUE(JSObject::GetProperty(thread, JSHandle<JSObject>(thread, result), valueStr).GetValue()->IsUndefined());
102 }
103 
104 // character with lead surrogate and trail surrogate character
HWTEST_F_L0(BuiltinsStringIteratorTest,Next_002)105 HWTEST_F_L0(BuiltinsStringIteratorTest, Next_002)
106 {
107     auto globalConst = thread->GlobalConstants();
108     CString string = "没��";
109     JSHandle<JSStringIterator> stringIterator =
110         JSHandle<JSStringIterator>(thread, CreateBuiltinsJSStringIterator(thread, string));
111     JSHandle<JSTaggedValue> valueStr = globalConst->GetHandledValueString();
112 
113     auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
114     ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
115     ecmaRuntimeCallInfo1->SetThis(stringIterator.GetTaggedValue());
116 
117     [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
118     BuiltinsStringIterator::Next(ecmaRuntimeCallInfo1);
119     TestHelper::TearDownFrame(thread, prev);
120     EXPECT_EQ(stringIterator->GetStringIteratorNextIndex(), 1U);
121 
122     auto ecmaRuntimeCallInfo2 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
123     ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
124     ecmaRuntimeCallInfo2->SetThis(stringIterator.GetTaggedValue());
125 
126     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
127     BuiltinsStringIterator::Next(ecmaRuntimeCallInfo2);
128     TestHelper::TearDownFrame(thread, prev);
129     EXPECT_EQ(stringIterator->GetStringIteratorNextIndex(), 3U);
130 
131     auto ecmaRuntimeCallInfo3 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
132     ecmaRuntimeCallInfo3->SetFunction(JSTaggedValue::Undefined());
133     ecmaRuntimeCallInfo3->SetThis(stringIterator.GetTaggedValue());
134 
135     prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo3);
136     JSTaggedValue result = BuiltinsStringIterator::Next(ecmaRuntimeCallInfo3);
137     TestHelper::TearDownFrame(thread, prev);
138 
139     JSHandle<JSTaggedValue> resultObj(thread, result);
140     EXPECT_TRUE(JSObject::GetProperty(thread, JSHandle<JSObject>(thread, result), valueStr).GetValue()->IsUndefined());
141 }
142 }  // namespace panda::test
143