14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_segmenter.h"
174514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_segments.h"
184514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_segment_iterator.h"
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
214514f5e3Sopenharmony_ci#include "ecmascript/js_segmenter.h"
224514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
254514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins;
264514f5e3Sopenharmony_cinamespace panda::test {
274514f5e3Sopenharmony_ciclass BuiltinsSegmenterTest : public BaseTestWithScope<true> {
284514f5e3Sopenharmony_ci};
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_cistatic JSTaggedValue JSSegmenterCreateWithLocaleTest(JSThread *thread, JSHandle<JSTaggedValue> &locale)
314514f5e3Sopenharmony_ci{
324514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
334514f5e3Sopenharmony_ci    JSHandle<JSFunction> newTarget(env->GetSegmenterFunction());
344514f5e3Sopenharmony_ci
354514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
364514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 6); // 6 means 1 call args
374514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
384514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
394514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue());
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
424514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsSegmenter::SegmenterConstructor(ecmaRuntimeCallInfo);
434514f5e3Sopenharmony_ci    EXPECT_TRUE(result.IsJSSegmenter());
444514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
454514f5e3Sopenharmony_ci    return result;
464514f5e3Sopenharmony_ci}
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_cistatic JSTaggedValue JSSegmenterCreateWithLocaleAndOptionsTest(JSThread *thread, JSHandle<JSTaggedValue> &locale,
494514f5e3Sopenharmony_ci                                                               JSHandle<JSTaggedValue> &granularity)
504514f5e3Sopenharmony_ci{
514514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
524514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
534514f5e3Sopenharmony_ci    JSHandle<JSFunction> newTarget(env->GetSegmenterFunction());
544514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> granularityKey = thread->GlobalConstants()->GetHandledGranularityString();
574514f5e3Sopenharmony_ci    JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
584514f5e3Sopenharmony_ci    JSObject::SetProperty(thread, optionsObj, granularityKey, granularity);
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
614514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 8); // 8 means 2 call args
624514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
634514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
644514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue());
654514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue());
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
684514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsSegmenter::SegmenterConstructor(ecmaRuntimeCallInfo);
694514f5e3Sopenharmony_ci    EXPECT_TRUE(result.IsJSSegmenter());
704514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
714514f5e3Sopenharmony_ci    return result;
724514f5e3Sopenharmony_ci}
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_cistatic JSTaggedValue JSSegmentsCreateTest(JSThread *thread, JSHandle<JSTaggedValue> &locale,
754514f5e3Sopenharmony_ci                                          JSHandle<JSTaggedValue> &granularity, JSHandle<JSTaggedValue> &stringValue)
764514f5e3Sopenharmony_ci{
774514f5e3Sopenharmony_ci    JSHandle<JSSegmenter> jsSegmenter =
784514f5e3Sopenharmony_ci        JSHandle<JSSegmenter>(thread, JSSegmenterCreateWithLocaleAndOptionsTest(thread, locale, granularity));
794514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
804514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);  // 6 means 1 call args
814514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
824514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(jsSegmenter.GetTaggedValue());
834514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, stringValue.GetTaggedValue());
844514f5e3Sopenharmony_ci
854514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
864514f5e3Sopenharmony_ci    JSTaggedValue segments = BuiltinsSegmenter::Segment(ecmaRuntimeCallInfo);
874514f5e3Sopenharmony_ci    EXPECT_TRUE(segments.IsJSSegments());
884514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
894514f5e3Sopenharmony_ci    return segments;
904514f5e3Sopenharmony_ci}
914514f5e3Sopenharmony_ci
924514f5e3Sopenharmony_ci// new  Intl.Segmenter ( [ locales [ , options ] ] )
934514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSegmenterTest, SegmenterConstructor)
944514f5e3Sopenharmony_ci{
954514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
964514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
974514f5e3Sopenharmony_ci    JSHandle<JSFunction> newTarget(env->GetSegmenterFunction());
984514f5e3Sopenharmony_ci
994514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> localesString(factory->NewFromASCII("en-US"));
1004514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
1014514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue(*newTarget), 6); // 6 means 1 call args
1024514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(newTarget.GetTaggedValue());
1034514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
1044514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, localesString.GetTaggedValue());
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1074514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsSegmenter::SegmenterConstructor(ecmaRuntimeCallInfo);
1084514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1094514f5e3Sopenharmony_ci
1104514f5e3Sopenharmony_ci    EXPECT_TRUE(result.IsJSSegmenter());
1114514f5e3Sopenharmony_ci}
1124514f5e3Sopenharmony_ci
1134514f5e3Sopenharmony_ci// Intl.Segmenter.prototype.segment ( string )
1144514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSegmenterTest, segment)
1154514f5e3Sopenharmony_ci{
1164514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1174514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locale(factory->NewFromASCII("zh-cn"));
1184514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("word"));
1194514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("这句话是中文"));
1204514f5e3Sopenharmony_ci    JSHandle<JSSegmenter> jsSegmenter =
1214514f5e3Sopenharmony_ci        JSHandle<JSSegmenter>(thread, JSSegmenterCreateWithLocaleAndOptionsTest(thread, locale, granularity));
1224514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
1234514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);  // 6 means 1 call args
1244514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1254514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(jsSegmenter.GetTaggedValue());
1264514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, stringValue.GetTaggedValue());
1274514f5e3Sopenharmony_ci
1284514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1294514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsSegmenter::Segment(ecmaRuntimeCallInfo);
1304514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1314514f5e3Sopenharmony_ci    EXPECT_TRUE(result.IsJSSegments());
1324514f5e3Sopenharmony_ci}
1334514f5e3Sopenharmony_ci
1344514f5e3Sopenharmony_ci// SupportedLocalesOf("lookup")
1354514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSegmenterTest, SupportedLocalesOf)
1364514f5e3Sopenharmony_ci{
1374514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1384514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1394514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> objFun = env->GetObjectFunction();
1404514f5e3Sopenharmony_ci
1414514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> localeMatcherKey = thread->GlobalConstants()->GetHandledLocaleMatcherString();
1424514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> localeMatcherValue(factory->NewFromASCII("lookup"));
1434514f5e3Sopenharmony_ci    JSHandle<JSObject> optionsObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(objFun), objFun);
1444514f5e3Sopenharmony_ci    JSObject::SetProperty(thread, optionsObj, localeMatcherKey, localeMatcherValue);
1454514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locale(factory->NewFromASCII("id-u-co-pinyin-de-ID"));
1464514f5e3Sopenharmony_ci
1474514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
1484514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8); // 8 means 2 call args
1494514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1504514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
1514514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, locale.GetTaggedValue());
1524514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(1, optionsObj.GetTaggedValue());
1534514f5e3Sopenharmony_ci
1544514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1554514f5e3Sopenharmony_ci    JSTaggedValue resultArr = BuiltinsSegmenter::SupportedLocalesOf(ecmaRuntimeCallInfo);
1564514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1574514f5e3Sopenharmony_ci
1584514f5e3Sopenharmony_ci    JSHandle<JSArray> resultHandle(thread, resultArr);
1594514f5e3Sopenharmony_ci    JSHandle<TaggedArray> elements(thread, resultHandle->GetElements());
1604514f5e3Sopenharmony_ci    EXPECT_EQ(elements->GetLength(), 1U);
1614514f5e3Sopenharmony_ci    JSHandle<EcmaString> handleEcmaStr(thread, elements->Get(0));
1624514f5e3Sopenharmony_ci    EXPECT_STREQ("id-u-co-pinyin-de-id", EcmaStringAccessor(handleEcmaStr).ToCString().c_str());
1634514f5e3Sopenharmony_ci}
1644514f5e3Sopenharmony_ci
1654514f5e3Sopenharmony_ci// Intl.Segmenter.prototype.resolvedOptions
1664514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSegmenterTest, ResolvedOptions)
1674514f5e3Sopenharmony_ci{
1684514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1694514f5e3Sopenharmony_ci    auto globalConst = thread->GlobalConstants();
1704514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locale(factory->NewFromASCII("de-DE"));
1714514f5e3Sopenharmony_ci    JSHandle<JSSegmenter> jsSegmenter =
1724514f5e3Sopenharmony_ci        JSHandle<JSSegmenter>(thread, JSSegmenterCreateWithLocaleTest(thread, locale));
1734514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
1744514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
1754514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(jsSegmenter.GetTaggedValue());
1764514f5e3Sopenharmony_ci
1774514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1784514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsSegmenter::ResolvedOptions(ecmaRuntimeCallInfo);
1794514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1804514f5e3Sopenharmony_ci
1814514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> resultObj =
1824514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue>(thread, JSTaggedValue(static_cast<JSTaggedType>(result.GetRawData())));
1834514f5e3Sopenharmony_ci    // judge whether the properties of the object are the same as those of jsdatetimeformat tag
1844514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> localeKey = globalConst->GetHandledLocaleString();
1854514f5e3Sopenharmony_ci    EXPECT_EQ(JSTaggedValue::SameValue(
1864514f5e3Sopenharmony_ci        JSObject::GetProperty(thread, resultObj, localeKey).GetValue(), locale), true);
1874514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> granularityKey = globalConst->GetHandledGranularityString();
1884514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> defaultGranularityValue(factory->NewFromASCII("grapheme"));
1894514f5e3Sopenharmony_ci    EXPECT_EQ(JSTaggedValue::SameValue(
1904514f5e3Sopenharmony_ci        JSObject::GetProperty(thread, resultObj, granularityKey).GetValue(), defaultGranularityValue), true);
1914514f5e3Sopenharmony_ci}
1924514f5e3Sopenharmony_ci
1934514f5e3Sopenharmony_civoid SegmentsPrototypeCommon(JSThread *thread, JSHandle<JSTaggedValue> &result,
1944514f5e3Sopenharmony_ci                             std::vector<JSHandle<JSTaggedValue>> &values)
1954514f5e3Sopenharmony_ci{
1964514f5e3Sopenharmony_ci    auto globalConst = thread->GlobalConstants();
1974514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> segmentKey = globalConst->GetHandledSegmentString();
1984514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> indexKey = globalConst->GetHandledIndexString();
1994514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> inputKey = globalConst->GetHandledInputString();
2004514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> isWordLikeKey = globalConst->GetHandledIsWordLikeString();
2014514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> segmentValue(JSObject::GetProperty(thread, result, segmentKey).GetValue());
2024514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> indexValue(JSObject::GetProperty(thread, result, indexKey).GetValue());
2034514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> inputValue(JSObject::GetProperty(thread, result, inputKey).GetValue());
2044514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> isWordLikeValue(JSObject::GetProperty(thread, result, isWordLikeKey).GetValue());
2054514f5e3Sopenharmony_ci    values.push_back(segmentValue);
2064514f5e3Sopenharmony_ci    values.push_back(indexValue);
2074514f5e3Sopenharmony_ci    values.push_back(inputValue);
2084514f5e3Sopenharmony_ci    values.push_back(isWordLikeValue);
2094514f5e3Sopenharmony_ci}
2104514f5e3Sopenharmony_ci
2114514f5e3Sopenharmony_ci// %SegmentsPrototype%.containing ( index )
2124514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSegmenterTest, SegmentsPrototypeContaining_001)
2134514f5e3Sopenharmony_ci{
2144514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2154514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locale(factory->NewFromASCII("zh-cn"));
2164514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("sentence"));
2174514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("这句话是中文。这句还是中文!"));
2184514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> segments(thread, JSSegmentsCreateTest(thread, locale, granularity, stringValue));
2194514f5e3Sopenharmony_ci
2204514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{ JSTaggedValue(static_cast<double>(3))};
2214514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
2224514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 6, segments.GetTaggedValue());  // 6 means 1 call args
2234514f5e3Sopenharmony_ci
2244514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
2254514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> result(thread, BuiltinsSegments::Containing(ecmaRuntimeCallInfo));
2264514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
2274514f5e3Sopenharmony_ci    EXPECT_TRUE(result->IsJSObject());
2284514f5e3Sopenharmony_ci    std::vector<JSHandle<JSTaggedValue>> outValues;
2294514f5e3Sopenharmony_ci    SegmentsPrototypeCommon(thread, result, outValues);
2304514f5e3Sopenharmony_ci
2314514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(outValues[0])).ToCString().c_str(),
2324514f5e3Sopenharmony_ci                 "这句话是中文。");
2334514f5e3Sopenharmony_ci    EXPECT_EQ(outValues[1]->GetRawData(), JSTaggedValue(0).GetRawData());   // 1:index value
2344514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(outValues[2])).ToCString().c_str(), // 2: input value
2354514f5e3Sopenharmony_ci                 "这句话是中文。这句还是中文!");
2364514f5e3Sopenharmony_ci    EXPECT_TRUE(outValues[3]->IsUndefined()); // 2: word link value
2374514f5e3Sopenharmony_ci}
2384514f5e3Sopenharmony_ci
2394514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSegmenterTest, SegmentsPrototypeContaining_002)
2404514f5e3Sopenharmony_ci{
2414514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2424514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locale(factory->NewFromASCII("fr"));
2434514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("word"));
2444514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("Que ma joie demeure"));
2454514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> segments(thread, JSSegmentsCreateTest(thread, locale, granularity, stringValue));
2464514f5e3Sopenharmony_ci
2474514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{ JSTaggedValue(static_cast<double>(10))};
2484514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
2494514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 6, segments.GetTaggedValue());  // 6 means 1 call args
2504514f5e3Sopenharmony_ci
2514514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
2524514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> result(thread, BuiltinsSegments::Containing(ecmaRuntimeCallInfo));
2534514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
2544514f5e3Sopenharmony_ci    EXPECT_TRUE(result->IsJSObject());
2554514f5e3Sopenharmony_ci
2564514f5e3Sopenharmony_ci    std::vector<JSHandle<JSTaggedValue>> outValues;
2574514f5e3Sopenharmony_ci    SegmentsPrototypeCommon(thread, result, outValues);
2584514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(outValues[0])).ToCString().c_str(),
2594514f5e3Sopenharmony_ci                 "joie");
2604514f5e3Sopenharmony_ci    EXPECT_EQ(outValues[1]->GetRawData(), JSTaggedValue(7).GetRawData());
2614514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(outValues[2])).ToCString().c_str(),
2624514f5e3Sopenharmony_ci                 "Que ma joie demeure");
2634514f5e3Sopenharmony_ci    EXPECT_EQ(outValues[3]->GetRawData(), JSTaggedValue::True().GetRawData());
2644514f5e3Sopenharmony_ci}
2654514f5e3Sopenharmony_ci
2664514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSegmenterTest, SegmentsPrototypeContaining_003)
2674514f5e3Sopenharmony_ci{
2684514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2694514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locale(factory->NewFromASCII("fr"));
2704514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("word"));
2714514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("Que ma joie demeure"));
2724514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> segments(thread, JSSegmentsCreateTest(thread, locale, granularity, stringValue));
2734514f5e3Sopenharmony_ci
2744514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{ JSTaggedValue(static_cast<double>(-10))};
2754514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
2764514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, args, 6, segments.GetTaggedValue());  // 6 means 1 call args
2774514f5e3Sopenharmony_ci
2784514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
2794514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> result(thread, BuiltinsSegments::Containing(ecmaRuntimeCallInfo));
2804514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
2814514f5e3Sopenharmony_ci    EXPECT_TRUE(result->IsUndefined());
2824514f5e3Sopenharmony_ci}
2834514f5e3Sopenharmony_ci
2844514f5e3Sopenharmony_ci// %SegmentsPrototype% [ @@iterator ] ( )
2854514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSegmenterTest, GetSegmentIterator)
2864514f5e3Sopenharmony_ci{
2874514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2884514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locale(factory->NewFromASCII("fr"));
2894514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("word"));
2904514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("Que ma joie demeure"));
2914514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> segments(thread, JSSegmentsCreateTest(thread, locale, granularity, stringValue));
2924514f5e3Sopenharmony_ci
2934514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
2944514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);  // 4 means 0 call args
2954514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
2964514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(segments.GetTaggedValue());
2974514f5e3Sopenharmony_ci
2984514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
2994514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsSegments::GetSegmentIterator(ecmaRuntimeCallInfo);
3004514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
3014514f5e3Sopenharmony_ci    EXPECT_TRUE(result.IsJSSegmentIterator());
3024514f5e3Sopenharmony_ci}
3034514f5e3Sopenharmony_ci
3044514f5e3Sopenharmony_ci// %SegmentIteratorPrototype%.next ( )
3054514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsSegmenterTest, SegmentIteratorNext)
3064514f5e3Sopenharmony_ci{
3074514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
3084514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> locale(factory->NewFromASCII("fr"));
3094514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> granularity(factory->NewFromASCII("sentence"));
3104514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> stringValue(factory->NewFromUtf8("Que ma joie demeure."));
3114514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> segments(thread, JSSegmentsCreateTest(thread, locale, granularity, stringValue));
3124514f5e3Sopenharmony_ci
3134514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo =
3144514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);  // 4 means 0 call args
3154514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
3164514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(segments.GetTaggedValue());
3174514f5e3Sopenharmony_ci
3184514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
3194514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> iterator(thread, BuiltinsSegments::GetSegmentIterator(ecmaRuntimeCallInfo));
3204514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
3214514f5e3Sopenharmony_ci    EXPECT_TRUE(iterator->IsJSSegmentIterator());
3224514f5e3Sopenharmony_ci
3234514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo1 =
3244514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);  // 4 means 0 call args
3254514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
3264514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetThis(iterator.GetTaggedValue());
3274514f5e3Sopenharmony_ci
3284514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo1);
3294514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> result1(thread, BuiltinsSegmentIterator::Next(ecmaRuntimeCallInfo1));
3304514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev1);
3314514f5e3Sopenharmony_ci    EXPECT_TRUE(result1->IsJSObject());
3324514f5e3Sopenharmony_ci    auto globalConst = thread->GlobalConstants();
3334514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> valueKey = globalConst->GetHandledValueString();
3344514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> doneKey = globalConst->GetHandledDoneString();
3354514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value1(JSObject::GetProperty(thread, result1, valueKey).GetValue());
3364514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> done1(JSObject::GetProperty(thread, result1, doneKey).GetValue());
3374514f5e3Sopenharmony_ci    EXPECT_TRUE(value1->IsJSObject());
3384514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> segmentKey = globalConst->GetHandledSegmentString();
3394514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> segmentValue(JSObject::GetProperty(thread, value1, segmentKey).GetValue());
3404514f5e3Sopenharmony_ci    EXPECT_STREQ(EcmaStringAccessor(JSHandle<EcmaString>::Cast(segmentValue)).ToCString().c_str(),
3414514f5e3Sopenharmony_ci                 "Que ma joie demeure.");
3424514f5e3Sopenharmony_ci    EXPECT_FALSE(done1->ToBoolean());
3434514f5e3Sopenharmony_ci
3444514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo2 =
3454514f5e3Sopenharmony_ci        TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);  // 4 means 0 call args
3464514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo2->SetFunction(JSTaggedValue::Undefined());
3474514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo2->SetThis(iterator.GetTaggedValue());
3484514f5e3Sopenharmony_ci
3494514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo2);
3504514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> result2(thread, BuiltinsSegmentIterator::Next(ecmaRuntimeCallInfo2));
3514514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev2);
3524514f5e3Sopenharmony_ci    EXPECT_TRUE(result2->IsJSObject());
3534514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value2(JSObject::GetProperty(thread, result2, valueKey).GetValue());
3544514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> done2(JSObject::GetProperty(thread, result2, doneKey).GetValue());
3554514f5e3Sopenharmony_ci    EXPECT_TRUE(value2->IsUndefined());
3564514f5e3Sopenharmony_ci    EXPECT_TRUE(done2->ToBoolean());
3574514f5e3Sopenharmony_ci}
3584514f5e3Sopenharmony_ci}
359