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/ecma_string.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/js_array.h"
20 #include "ecmascript/js_array_iterator.h"
21 #include "ecmascript/js_handle.h"
22 #include "ecmascript/js_iterator.h"
23 #include "ecmascript/js_object-inl.h"
24 #include "ecmascript/js_tagged_value.h"
25 #include "ecmascript/object_factory.h"
26 #include "ecmascript/tagged_array-inl.h"
27 #include "ecmascript/tests/test_helper.h"
28
29 using namespace panda;
30
31 using namespace panda::ecmascript;
32
33 namespace panda::test {
34 class JSIteratorTest : public testing::Test {
35 public:
SetUpTestCase()36 static void SetUpTestCase()
37 {
38 GTEST_LOG_(INFO) << "SetUpTestCase";
39 }
40
TearDownTestCase()41 static void TearDownTestCase()
42 {
43 GTEST_LOG_(INFO) << "TearDownCase";
44 }
45
SetUp()46 void SetUp() override
47 {
48 TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
49 }
50
TearDown()51 void TearDown() override
52 {
53 TestHelper::DestroyEcmaVMWithScope(instance, scope);
54 }
55
56 EcmaVM *instance {nullptr};
57 ecmascript::EcmaHandleScope *scope {nullptr};
58 JSThread *thread {nullptr};
59 };
60
HWTEST_F_L0(JSIteratorTest,GetIterator)61 HWTEST_F_L0(JSIteratorTest, GetIterator)
62 {
63 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
64 JSHandle<TaggedArray> data(factory->NewTaggedArray(2));
65 data->Set(thread, 0, JSTaggedValue(1));
66 data->Set(thread, 1, JSTaggedValue(1));
67 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, data));
68 EXPECT_TRUE(array->IsArray(thread));
69 JSHandle<JSArrayIterator> iter(JSIterator::GetIterator(thread, array));
70 EXPECT_TRUE(iter->IsJSArrayIterator());
71 EXPECT_TRUE(iter->GetIteratedArray().IsArray(thread));
72 }
73
HWTEST_F_L0(JSIteratorTest,IteratorNext)74 HWTEST_F_L0(JSIteratorTest, IteratorNext)
75 {
76 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
77 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
78
79 JSHandle<TaggedArray> data(factory->NewTaggedArray(1));
80 data->Set(thread, 0, JSTaggedValue(1));
81 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, data));
82 JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, array));
83 JSHandle<JSTaggedValue> result(JSIterator::IteratorNext(thread, iter));
84 JSHandle<JSTaggedValue> resultValue(JSObject::GetProperty(thread, result, valueStr).GetValue());
85 EXPECT_EQ(resultValue->GetInt(), 1);
86 }
87
HWTEST_F_L0(JSIteratorTest,IteratorComplete)88 HWTEST_F_L0(JSIteratorTest, IteratorComplete)
89 {
90 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
91 JSHandle<TaggedArray> data(factory->NewTaggedArray(2));
92 data->Set(thread, 0, JSTaggedValue(1));
93 data->Set(thread, 1, JSTaggedValue(1));
94 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, data));
95 JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, array));
96 JSHandle<JSTaggedValue> result1(JSIterator::IteratorNext(thread, iter));
97 EXPECT_EQ(false, JSIterator::IteratorComplete(thread, result1));
98 JSHandle<JSTaggedValue> result2(JSIterator::IteratorNext(thread, iter));
99 EXPECT_EQ(false, JSIterator::IteratorComplete(thread, result2));
100 JSHandle<JSTaggedValue> result3(JSIterator::IteratorNext(thread, iter));
101 EXPECT_EQ(true, JSIterator::IteratorComplete(thread, result3));
102 }
103
HWTEST_F_L0(JSIteratorTest,IteratorValue)104 HWTEST_F_L0(JSIteratorTest, IteratorValue)
105 {
106 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
107
108 JSHandle<TaggedArray> data(factory->NewTaggedArray(3));
109 data->Set(thread, 0, JSTaggedValue(1));
110 data->Set(thread, 1, JSTaggedValue(1));
111 data->Set(thread, 2, JSTaggedValue(1));
112 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, data));
113 JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, array));
114 JSHandle<JSTaggedValue> result(JSIterator::IteratorNext(thread, iter));
115 JSHandle<JSTaggedValue> resultValue(JSIterator::IteratorValue(thread, result));
116 EXPECT_EQ(resultValue->GetInt(), 1);
117 }
118
HWTEST_F_L0(JSIteratorTest,IteratorStep)119 HWTEST_F_L0(JSIteratorTest, IteratorStep)
120 {
121 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
122 JSHandle<TaggedArray> data(factory->NewTaggedArray(2));
123 data->Set(thread, 0, JSTaggedValue(1));
124 data->Set(thread, 1, JSTaggedValue(2));
125 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, data));
126 JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, array));
127 JSHandle<JSTaggedValue> result1(JSIterator::IteratorStep(thread, iter));
128 EXPECT_EQ(JSIterator::IteratorValue(thread, result1)->GetInt(), 1);
129 JSHandle<JSTaggedValue> result2(JSIterator::IteratorStep(thread, iter));
130 EXPECT_EQ(JSIterator::IteratorValue(thread, result2)->GetInt(), 2);
131 JSHandle<JSTaggedValue> result3(JSIterator::IteratorStep(thread, iter));
132 EXPECT_EQ(result3.GetTaggedValue(), JSTaggedValue::False());
133 }
134 } // namespace panda::test
135