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 BaseTestWithScope<false> {
35 };
36
HWTEST_F_L0(JSIteratorTest,GetIterator)37 HWTEST_F_L0(JSIteratorTest, GetIterator)
38 {
39 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
40 JSHandle<TaggedArray> data(factory->NewTaggedArray(2));
41 data->Set(thread, 0, JSTaggedValue(1));
42 data->Set(thread, 1, JSTaggedValue(1));
43 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, data));
44 EXPECT_TRUE(array->IsArray(thread));
45 JSHandle<JSArrayIterator> iter(JSIterator::GetIterator(thread, array));
46 EXPECT_TRUE(iter->IsJSArrayIterator());
47 EXPECT_TRUE(iter->GetIteratedArray().IsArray(thread));
48 }
49
HWTEST_F_L0(JSIteratorTest,IteratorNext)50 HWTEST_F_L0(JSIteratorTest, IteratorNext)
51 {
52 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
53 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
54
55 JSHandle<TaggedArray> data(factory->NewTaggedArray(1));
56 data->Set(thread, 0, JSTaggedValue(1));
57 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, data));
58 JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, array));
59 JSHandle<JSTaggedValue> result(JSIterator::IteratorNext(thread, iter));
60 JSHandle<JSTaggedValue> resultValue(JSObject::GetProperty(thread, result, valueStr).GetValue());
61 EXPECT_EQ(resultValue->GetInt(), 1);
62 }
63
HWTEST_F_L0(JSIteratorTest,IteratorComplete)64 HWTEST_F_L0(JSIteratorTest, IteratorComplete)
65 {
66 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
67 JSHandle<TaggedArray> data(factory->NewTaggedArray(2));
68 data->Set(thread, 0, JSTaggedValue(1));
69 data->Set(thread, 1, JSTaggedValue(1));
70 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, data));
71 JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, array));
72 JSHandle<JSTaggedValue> result1(JSIterator::IteratorNext(thread, iter));
73 EXPECT_EQ(false, JSIterator::IteratorComplete(thread, result1));
74 JSHandle<JSTaggedValue> result2(JSIterator::IteratorNext(thread, iter));
75 EXPECT_EQ(false, JSIterator::IteratorComplete(thread, result2));
76 JSHandle<JSTaggedValue> result3(JSIterator::IteratorNext(thread, iter));
77 EXPECT_EQ(true, JSIterator::IteratorComplete(thread, result3));
78 }
79
HWTEST_F_L0(JSIteratorTest,IteratorValue)80 HWTEST_F_L0(JSIteratorTest, IteratorValue)
81 {
82 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
83
84 JSHandle<TaggedArray> data(factory->NewTaggedArray(3));
85 data->Set(thread, 0, JSTaggedValue(1));
86 data->Set(thread, 1, JSTaggedValue(1));
87 data->Set(thread, 2, JSTaggedValue(1));
88 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, data));
89 JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, array));
90 JSHandle<JSTaggedValue> result(JSIterator::IteratorNext(thread, iter));
91 JSHandle<JSTaggedValue> resultValue(JSIterator::IteratorValue(thread, result));
92 EXPECT_EQ(resultValue->GetInt(), 1);
93 }
94
HWTEST_F_L0(JSIteratorTest,IteratorStep)95 HWTEST_F_L0(JSIteratorTest, IteratorStep)
96 {
97 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
98 JSHandle<TaggedArray> data(factory->NewTaggedArray(2));
99 data->Set(thread, 0, JSTaggedValue(1));
100 data->Set(thread, 1, JSTaggedValue(2));
101 JSHandle<JSTaggedValue> array(JSArray::CreateArrayFromList(thread, data));
102 JSHandle<JSTaggedValue> iter(JSIterator::GetIterator(thread, array));
103 JSHandle<JSTaggedValue> result1(JSIterator::IteratorStep(thread, iter));
104 EXPECT_EQ(JSIterator::IteratorValue(thread, result1)->GetInt(), 1);
105 JSHandle<JSTaggedValue> result2(JSIterator::IteratorStep(thread, iter));
106 EXPECT_EQ(JSIterator::IteratorValue(thread, result2)->GetInt(), 2);
107 JSHandle<JSTaggedValue> result3(JSIterator::IteratorStep(thread, iter));
108 EXPECT_EQ(result3.GetTaggedValue(), JSTaggedValue::False());
109 }
110 } // namespace panda::test
111