14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device 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_string.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/base/builtins_base.h"
194514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_regexp.h"
204514f5e3Sopenharmony_ci#include "ecmascript/ecma_runtime_call_info.h"
214514f5e3Sopenharmony_ci#include "ecmascript/ecma_string.h"
224514f5e3Sopenharmony_ci#include "ecmascript/ecma_vm.h"
234514f5e3Sopenharmony_ci#include "ecmascript/global_env.h"
244514f5e3Sopenharmony_ci#include "ecmascript/js_array.h"
254514f5e3Sopenharmony_ci#include "ecmascript/js_object-inl.h"
264514f5e3Sopenharmony_ci#include "ecmascript/js_primitive_ref.h"
274514f5e3Sopenharmony_ci#include "ecmascript/js_regexp.h"
284514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
294514f5e3Sopenharmony_ci#include "ecmascript/object_factory.h"
304514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
314514f5e3Sopenharmony_ci
324514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
334514f5e3Sopenharmony_ciusing namespace panda::ecmascript::builtins;
344514f5e3Sopenharmony_ci
354514f5e3Sopenharmony_cinamespace panda::test {
364514f5e3Sopenharmony_ciclass BuiltinsStringTest : public BaseTestWithScope<true> {
374514f5e3Sopenharmony_ci};
384514f5e3Sopenharmony_ci
394514f5e3Sopenharmony_ciJSTaggedValue CreateBuiltinsStringRegExpObjByPatternAndFlags(JSThread *thread, const JSHandle<EcmaString> &pattern,
404514f5e3Sopenharmony_ci                                                             const JSHandle<EcmaString> &flags)
414514f5e3Sopenharmony_ci{
424514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
434514f5e3Sopenharmony_ci    JSHandle<JSFunction> regexp(env->GetRegExpFunction());
444514f5e3Sopenharmony_ci    JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_ci    // 8 : test case
474514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, regexp.GetTaggedValue(), 8);
484514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(regexp.GetTaggedValue());
494514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
504514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, pattern.GetTaggedValue());
514514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(1, flags.GetTaggedValue());
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
544514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsRegExp::RegExpConstructor(ecmaRuntimeCallInfo);
554514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
564514f5e3Sopenharmony_ci    return result;
574514f5e3Sopenharmony_ci}
584514f5e3Sopenharmony_ci
594514f5e3Sopenharmony_cienum class AlgorithmType {
604514f5e3Sopenharmony_ci    FROM_CHAR_CODE,
614514f5e3Sopenharmony_ci    FROM_CODE_POINT,
624514f5e3Sopenharmony_ci    CHAR_AT,
634514f5e3Sopenharmony_ci    CHAR_CODE_AT,
644514f5e3Sopenharmony_ci    CODE_POINT_AT,
654514f5e3Sopenharmony_ci    CONCAT,
664514f5e3Sopenharmony_ci    INDEX_OF,
674514f5e3Sopenharmony_ci    LAST_INDEX_OF,
684514f5e3Sopenharmony_ci    INCLUDES,
694514f5e3Sopenharmony_ci    START_WITH,
704514f5e3Sopenharmony_ci    ENDS_WITH,
714514f5e3Sopenharmony_ci    TO_STRING,
724514f5e3Sopenharmony_ci    VALUE_OF,
734514f5e3Sopenharmony_ci    REPLACE,
744514f5e3Sopenharmony_ci    SPLIT,
754514f5e3Sopenharmony_ci};
764514f5e3Sopenharmony_ci
774514f5e3Sopenharmony_ciJSTaggedValue StringAlgorithmOther(EcmaRuntimeCallInfo*ecmaRuntimeCallInfos, AlgorithmType type)
784514f5e3Sopenharmony_ci{
794514f5e3Sopenharmony_ci    switch (type) {
804514f5e3Sopenharmony_ci        case AlgorithmType::START_WITH:
814514f5e3Sopenharmony_ci            return BuiltinsString::StartsWith(ecmaRuntimeCallInfos);
824514f5e3Sopenharmony_ci        case AlgorithmType::ENDS_WITH:
834514f5e3Sopenharmony_ci            return BuiltinsString::EndsWith(ecmaRuntimeCallInfos);
844514f5e3Sopenharmony_ci        case AlgorithmType::TO_STRING:
854514f5e3Sopenharmony_ci            return BuiltinsString::ToString(ecmaRuntimeCallInfos);
864514f5e3Sopenharmony_ci        case AlgorithmType::VALUE_OF:
874514f5e3Sopenharmony_ci            return BuiltinsString::ValueOf(ecmaRuntimeCallInfos);
884514f5e3Sopenharmony_ci        case AlgorithmType::REPLACE:
894514f5e3Sopenharmony_ci            return BuiltinsString::Replace(ecmaRuntimeCallInfos);
904514f5e3Sopenharmony_ci        case AlgorithmType::SPLIT:
914514f5e3Sopenharmony_ci            return BuiltinsString::Split(ecmaRuntimeCallInfos);
924514f5e3Sopenharmony_ci        default:
934514f5e3Sopenharmony_ci            return JSTaggedValue::Undefined();
944514f5e3Sopenharmony_ci    }
954514f5e3Sopenharmony_ci}
964514f5e3Sopenharmony_ci
974514f5e3Sopenharmony_ciJSTaggedValue StringAlgorithm(JSThread *thread, JSTaggedValue thisArg, std::vector<JSTaggedValue>& args,
984514f5e3Sopenharmony_ci    uint32_t argLen = 8, AlgorithmType type = AlgorithmType::FROM_CHAR_CODE)
994514f5e3Sopenharmony_ci{
1004514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfos = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), argLen);
1014514f5e3Sopenharmony_ci    ecmaRuntimeCallInfos->SetFunction(JSTaggedValue::Undefined());
1024514f5e3Sopenharmony_ci    ecmaRuntimeCallInfos->SetThis(thisArg);
1034514f5e3Sopenharmony_ci    for (size_t i = 0; i < args.size(); i++) {
1044514f5e3Sopenharmony_ci        ecmaRuntimeCallInfos->SetCallArg(i, args[i]);
1054514f5e3Sopenharmony_ci    }
1064514f5e3Sopenharmony_ci    auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfos);
1074514f5e3Sopenharmony_ci    JSTaggedValue result;
1084514f5e3Sopenharmony_ci    switch (type) {
1094514f5e3Sopenharmony_ci        case AlgorithmType::FROM_CHAR_CODE:
1104514f5e3Sopenharmony_ci            result = BuiltinsString::FromCharCode(ecmaRuntimeCallInfos);
1114514f5e3Sopenharmony_ci            break;
1124514f5e3Sopenharmony_ci        case AlgorithmType::FROM_CODE_POINT:
1134514f5e3Sopenharmony_ci            result = BuiltinsString::FromCodePoint(ecmaRuntimeCallInfos);
1144514f5e3Sopenharmony_ci            break;
1154514f5e3Sopenharmony_ci        case AlgorithmType::CHAR_AT:
1164514f5e3Sopenharmony_ci            result = BuiltinsString::CharAt(ecmaRuntimeCallInfos);
1174514f5e3Sopenharmony_ci            break;
1184514f5e3Sopenharmony_ci        case AlgorithmType::CHAR_CODE_AT:
1194514f5e3Sopenharmony_ci            result = BuiltinsString::CharCodeAt(ecmaRuntimeCallInfos);
1204514f5e3Sopenharmony_ci            break;
1214514f5e3Sopenharmony_ci        case AlgorithmType::CODE_POINT_AT:
1224514f5e3Sopenharmony_ci            result = BuiltinsString::CodePointAt(ecmaRuntimeCallInfos);
1234514f5e3Sopenharmony_ci            break;
1244514f5e3Sopenharmony_ci        case AlgorithmType::CONCAT:
1254514f5e3Sopenharmony_ci            result = BuiltinsString::Concat(ecmaRuntimeCallInfos);
1264514f5e3Sopenharmony_ci            break;
1274514f5e3Sopenharmony_ci        case AlgorithmType::INDEX_OF:
1284514f5e3Sopenharmony_ci            result = BuiltinsString::IndexOf(ecmaRuntimeCallInfos);
1294514f5e3Sopenharmony_ci            break;
1304514f5e3Sopenharmony_ci        case AlgorithmType::LAST_INDEX_OF:
1314514f5e3Sopenharmony_ci            result = BuiltinsString::LastIndexOf(ecmaRuntimeCallInfos);
1324514f5e3Sopenharmony_ci            break;
1334514f5e3Sopenharmony_ci        case AlgorithmType::INCLUDES:
1344514f5e3Sopenharmony_ci            result = BuiltinsString::Includes(ecmaRuntimeCallInfos);
1354514f5e3Sopenharmony_ci            break;
1364514f5e3Sopenharmony_ci        default:
1374514f5e3Sopenharmony_ci            result = StringAlgorithmOther(ecmaRuntimeCallInfos, type);
1384514f5e3Sopenharmony_ci            break;
1394514f5e3Sopenharmony_ci    }
1404514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
1414514f5e3Sopenharmony_ci    return result;
1424514f5e3Sopenharmony_ci}
1434514f5e3Sopenharmony_ci
1444514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, StringConstructor1)
1454514f5e3Sopenharmony_ci{
1464514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
1474514f5e3Sopenharmony_ci
1484514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1494514f5e3Sopenharmony_ci    JSHandle<JSFunction> string(env->GetStringFunction());
1504514f5e3Sopenharmony_ci    JSHandle<JSObject> globalObject(thread, env->GetGlobalObject());
1514514f5e3Sopenharmony_ci    JSHandle<EcmaString> string2 = factory->NewFromASCII("ABC");
1524514f5e3Sopenharmony_ci
1534514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, string.GetTaggedValue(), 6);
1544514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(string.GetTaggedValue());
1554514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(globalObject.GetTaggedValue());
1564514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, string2.GetTaggedValue());
1574514f5e3Sopenharmony_ci
1584514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
1594514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::StringConstructor(ecmaRuntimeCallInfo);
1604514f5e3Sopenharmony_ci    JSTaggedValue value(static_cast<JSTaggedType>(result.GetRawData()));
1614514f5e3Sopenharmony_ci    ASSERT_TRUE(value.IsECMAObject());
1624514f5e3Sopenharmony_ci    JSHandle<JSPrimitiveRef> ref(thread, JSPrimitiveRef::Cast(value.GetTaggedObject()));
1634514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("ABC");
1644514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance,
1654514f5e3Sopenharmony_ci        JSHandle<EcmaString>(thread, EcmaString::Cast(ref->GetValue())), test), 0);
1664514f5e3Sopenharmony_ci}
1674514f5e3Sopenharmony_ci
1684514f5e3Sopenharmony_ci// String.fromCharCode(65, 66, 67)
1694514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, fromCharCode1)
1704514f5e3Sopenharmony_ci{
1714514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1724514f5e3Sopenharmony_ci    const double arg1 = 65;
1734514f5e3Sopenharmony_ci    const double arg2 = 66;
1744514f5e3Sopenharmony_ci    const double arg3 = 67;
1754514f5e3Sopenharmony_ci
1764514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{JSTaggedValue(arg1), JSTaggedValue(arg2), JSTaggedValue(arg3)};
1774514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, JSTaggedValue::Undefined(), args, 10, AlgorithmType::FROM_CHAR_CODE);
1784514f5e3Sopenharmony_ci
1794514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
1804514f5e3Sopenharmony_ci    JSTaggedValue value(static_cast<JSTaggedType>(result.GetRawData()));
1814514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> valueHandle(thread, JSTaggedValue(value.GetTaggedObject()));
1824514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("ABC");
1834514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, JSHandle<EcmaString>::Cast(valueHandle), test), 0);
1844514f5e3Sopenharmony_ci}
1854514f5e3Sopenharmony_ci
1864514f5e3Sopenharmony_ci// String.fromCodePoint(65, 66, 67)
1874514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, fromCodePoint1)
1884514f5e3Sopenharmony_ci{
1894514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
1904514f5e3Sopenharmony_ci    const double arg1 = 65;
1914514f5e3Sopenharmony_ci    const double arg2 = 66;
1924514f5e3Sopenharmony_ci    const double arg3 = 67;
1934514f5e3Sopenharmony_ci
1944514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{JSTaggedValue(arg1), JSTaggedValue(arg2), JSTaggedValue(arg3)};
1954514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, JSTaggedValue::Undefined(), args, 10, AlgorithmType::FROM_CODE_POINT);
1964514f5e3Sopenharmony_ci
1974514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
1984514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
1994514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("ABC");
2004514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
2014514f5e3Sopenharmony_ci}
2024514f5e3Sopenharmony_ci
2034514f5e3Sopenharmony_ci// "abcabcabc".charAt(5)
2044514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, charAt1)
2054514f5e3Sopenharmony_ci{
2064514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2074514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisVal = factory->NewFromASCII("abcabcabc");
2084514f5e3Sopenharmony_ci
2094514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{JSTaggedValue(static_cast<double>(5))};
2104514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisVal.GetTaggedValue(), args, 6, AlgorithmType::CHAR_AT);
2114514f5e3Sopenharmony_ci
2124514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
2134514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
2144514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("c");
2154514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
2164514f5e3Sopenharmony_ci}
2174514f5e3Sopenharmony_ci
2184514f5e3Sopenharmony_ci// "一二三四".charAt(2)
2194514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, charAt2)
2204514f5e3Sopenharmony_ci{
2214514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2224514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisVal = factory->NewFromUtf8("一二三四");
2234514f5e3Sopenharmony_ci
2244514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{JSTaggedValue(static_cast<double>(2))};
2254514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisVal.GetTaggedValue(), args, 6, AlgorithmType::CHAR_AT);
2264514f5e3Sopenharmony_ci
2274514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
2284514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
2294514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromUtf8("三");
2304514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
2314514f5e3Sopenharmony_ci}
2324514f5e3Sopenharmony_ci
2334514f5e3Sopenharmony_ci// "abcabcabc".charAt(-1)
2344514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, charAt3)
2354514f5e3Sopenharmony_ci{
2364514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2374514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisVal = factory->NewFromASCII("abcabcabc");
2384514f5e3Sopenharmony_ci
2394514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{JSTaggedValue(static_cast<double>(-1))};
2404514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisVal.GetTaggedValue(), args, 6, AlgorithmType::CHAR_AT);
2414514f5e3Sopenharmony_ci
2424514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
2434514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
2444514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->GetEmptyString();
2454514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
2464514f5e3Sopenharmony_ci}
2474514f5e3Sopenharmony_ci
2484514f5e3Sopenharmony_ci// "ABC".charCodeAt(0)
2494514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, charCodeAt1)
2504514f5e3Sopenharmony_ci{
2514514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2524514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisVal = factory->NewFromASCII("ABC");
2534514f5e3Sopenharmony_ci
2544514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{JSTaggedValue(static_cast<double>(0))};
2554514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisVal.GetTaggedValue(), args, 6, AlgorithmType::CHAR_CODE_AT);
2564514f5e3Sopenharmony_ci
2574514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(65).GetRawData());
2584514f5e3Sopenharmony_ci}
2594514f5e3Sopenharmony_ci
2604514f5e3Sopenharmony_ci// "ABC".charCodeAt(-1)
2614514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, charCodeAt2)
2624514f5e3Sopenharmony_ci{
2634514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2644514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisVal = factory->NewFromASCII("ABC");
2654514f5e3Sopenharmony_ci
2664514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{JSTaggedValue(static_cast<double>(-1))};
2674514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisVal.GetTaggedValue(), args, 6, AlgorithmType::CHAR_CODE_AT);
2684514f5e3Sopenharmony_ci
2694514f5e3Sopenharmony_ci    JSTaggedValue test = BuiltinsString::GetTaggedDouble(base::NAN_VALUE);
2704514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), test.GetRawData());
2714514f5e3Sopenharmony_ci}
2724514f5e3Sopenharmony_ci
2734514f5e3Sopenharmony_ci// "ABC".codePointAt(1)
2744514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, codePointAt1)
2754514f5e3Sopenharmony_ci{
2764514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2774514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisVal = factory->NewFromASCII("ABC");
2784514f5e3Sopenharmony_ci
2794514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{JSTaggedValue(static_cast<double>(1))};
2804514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisVal.GetTaggedValue(), args, 6, AlgorithmType::CODE_POINT_AT);
2814514f5e3Sopenharmony_ci
2824514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(66).GetRawData());
2834514f5e3Sopenharmony_ci}
2844514f5e3Sopenharmony_ci
2854514f5e3Sopenharmony_ci// 'a'.concat('b', 'c', 'd')
2864514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, concat1)
2874514f5e3Sopenharmony_ci{
2884514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
2894514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("a");
2904514f5e3Sopenharmony_ci    JSHandle<EcmaString> val1 = factory->NewFromASCII("b");
2914514f5e3Sopenharmony_ci    JSHandle<EcmaString> val2 = factory->NewFromASCII("c");
2924514f5e3Sopenharmony_ci    JSHandle<EcmaString> val3 = factory->NewFromASCII("d");
2934514f5e3Sopenharmony_ci
2944514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val1.GetTaggedValue(), val2.GetTaggedValue(), val3.GetTaggedValue()};
2954514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 10, AlgorithmType::CONCAT);
2964514f5e3Sopenharmony_ci
2974514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
2984514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
2994514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("abcd");
3004514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
3014514f5e3Sopenharmony_ci}
3024514f5e3Sopenharmony_ci
3034514f5e3Sopenharmony_ci// "abcabcabc".indexof('b')
3044514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, indexof1)
3054514f5e3Sopenharmony_ci{
3064514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
3074514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
3084514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("b");
3094514f5e3Sopenharmony_ci
3104514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue()};
3114514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 6, AlgorithmType::INDEX_OF);
3124514f5e3Sopenharmony_ci
3134514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(1).GetRawData());
3144514f5e3Sopenharmony_ci}
3154514f5e3Sopenharmony_ci
3164514f5e3Sopenharmony_ci// "abcabcabc".indexof('b', 2)
3174514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, indexof2)
3184514f5e3Sopenharmony_ci{
3194514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
3204514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
3214514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("b");
3224514f5e3Sopenharmony_ci
3234514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue(), JSTaggedValue(static_cast<double>(2))};
3244514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::INDEX_OF);
3254514f5e3Sopenharmony_ci
3264514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(4).GetRawData());
3274514f5e3Sopenharmony_ci}
3284514f5e3Sopenharmony_ci
3294514f5e3Sopenharmony_ci// "abcabcabc".indexof('d')
3304514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, indexof3)
3314514f5e3Sopenharmony_ci{
3324514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
3334514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
3344514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("d");
3354514f5e3Sopenharmony_ci
3364514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue()};
3374514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 6, AlgorithmType::INDEX_OF);
3384514f5e3Sopenharmony_ci
3394514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(-1).GetRawData());
3404514f5e3Sopenharmony_ci}
3414514f5e3Sopenharmony_ci
3424514f5e3Sopenharmony_ci// "abcabcabc".lastIndexOf('b')
3434514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, lastIndexOf1)
3444514f5e3Sopenharmony_ci{
3454514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
3464514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
3474514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("b");
3484514f5e3Sopenharmony_ci
3494514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue()};
3504514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 6, AlgorithmType::LAST_INDEX_OF);
3514514f5e3Sopenharmony_ci
3524514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(7).GetRawData());
3534514f5e3Sopenharmony_ci}
3544514f5e3Sopenharmony_ci// "abcabcabc".lastIndexOf('b', 2)
3554514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, lastIndexOf2)
3564514f5e3Sopenharmony_ci{
3574514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
3584514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
3594514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("b");
3604514f5e3Sopenharmony_ci
3614514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue(), JSTaggedValue(static_cast<double>(2))};
3624514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::LAST_INDEX_OF);
3634514f5e3Sopenharmony_ci
3644514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(1).GetRawData());
3654514f5e3Sopenharmony_ci}
3664514f5e3Sopenharmony_ci
3674514f5e3Sopenharmony_ci// "abcabcabc".lastIndexOf('d')
3684514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, lastIndexOf3)
3694514f5e3Sopenharmony_ci{
3704514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
3714514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
3724514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("d");
3734514f5e3Sopenharmony_ci
3744514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue()};
3754514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 6, AlgorithmType::LAST_INDEX_OF);
3764514f5e3Sopenharmony_ci
3774514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(-1).GetRawData());
3784514f5e3Sopenharmony_ci}
3794514f5e3Sopenharmony_ci
3804514f5e3Sopenharmony_ci// "abcabcabc".includes('b')
3814514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, Includes2)
3824514f5e3Sopenharmony_ci{
3834514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
3844514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
3854514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("b");
3864514f5e3Sopenharmony_ci
3874514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue()};
3884514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 6, AlgorithmType::INCLUDES);
3894514f5e3Sopenharmony_ci
3904514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
3914514f5e3Sopenharmony_ci}
3924514f5e3Sopenharmony_ci
3934514f5e3Sopenharmony_ci// "abccccccc".includes('b',2)
3944514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, Includes3)
3954514f5e3Sopenharmony_ci{
3964514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
3974514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abccccccc");
3984514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("b");
3994514f5e3Sopenharmony_ci
4004514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue(), JSTaggedValue(static_cast<double>(2))};
4014514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::INCLUDES);
4024514f5e3Sopenharmony_ci
4034514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData());
4044514f5e3Sopenharmony_ci}
4054514f5e3Sopenharmony_ci
4064514f5e3Sopenharmony_ci// "一二三四".includes('二')
4074514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, Includes4)
4084514f5e3Sopenharmony_ci{
4094514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
4104514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromUtf8("一二三四");
4114514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromUtf8("二");
4124514f5e3Sopenharmony_ci
4134514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue()};
4144514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 6, AlgorithmType::INCLUDES);
4154514f5e3Sopenharmony_ci
4164514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
4174514f5e3Sopenharmony_ci}
4184514f5e3Sopenharmony_ci
4194514f5e3Sopenharmony_ci// "To be, or not to be, that is the question.".startsWith('To be')
4204514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, startsWith1)
4214514f5e3Sopenharmony_ci{
4224514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
4234514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
4244514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("To be");
4254514f5e3Sopenharmony_ci
4264514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue()};
4274514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 6, AlgorithmType::START_WITH);
4284514f5e3Sopenharmony_ci
4294514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
4304514f5e3Sopenharmony_ci}
4314514f5e3Sopenharmony_ci
4324514f5e3Sopenharmony_ci// "To be, or not to be, that is the question.".startsWith('not to be')
4334514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, startsWith2)
4344514f5e3Sopenharmony_ci{
4354514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
4364514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
4374514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("not to be");
4384514f5e3Sopenharmony_ci
4394514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue()};
4404514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 6, AlgorithmType::START_WITH);
4414514f5e3Sopenharmony_ci
4424514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData());
4434514f5e3Sopenharmony_ci}
4444514f5e3Sopenharmony_ci
4454514f5e3Sopenharmony_ci// "To be, or not to be, that is the question.".startsWith('not to be', 10)
4464514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, startsWith3)
4474514f5e3Sopenharmony_ci{
4484514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
4494514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
4504514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("not to be");
4514514f5e3Sopenharmony_ci
4524514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue(), JSTaggedValue(static_cast<double>(10))};
4534514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::START_WITH);
4544514f5e3Sopenharmony_ci
4554514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
4564514f5e3Sopenharmony_ci}
4574514f5e3Sopenharmony_ci
4584514f5e3Sopenharmony_ci// "To be, or not to be, that is the question.".endsWith('question.')
4594514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, endsWith1)
4604514f5e3Sopenharmony_ci{
4614514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
4624514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
4634514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("question.");
4644514f5e3Sopenharmony_ci
4654514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue()};
4664514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 6, AlgorithmType::ENDS_WITH);
4674514f5e3Sopenharmony_ci
4684514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
4694514f5e3Sopenharmony_ci}
4704514f5e3Sopenharmony_ci
4714514f5e3Sopenharmony_ci// "To be, or not to be, that is the question.".endsWith('to be')
4724514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, endsWith2)
4734514f5e3Sopenharmony_ci{
4744514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
4754514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
4764514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("to be");
4774514f5e3Sopenharmony_ci
4784514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue()};
4794514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 6, AlgorithmType::ENDS_WITH);
4804514f5e3Sopenharmony_ci
4814514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue::False().GetRawData());
4824514f5e3Sopenharmony_ci}
4834514f5e3Sopenharmony_ci
4844514f5e3Sopenharmony_ci// "To be, or not to be, that is the question.".endsWith('to be', 19)
4854514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, endsWith3)
4864514f5e3Sopenharmony_ci{
4874514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
4884514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("To be, or not to be, that is the question.");
4894514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("to be");
4904514f5e3Sopenharmony_ci
4914514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{val.GetTaggedValue(), JSTaggedValue(static_cast<double>(19))};
4924514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::ENDS_WITH);
4934514f5e3Sopenharmony_ci
4944514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue::True().GetRawData());
4954514f5e3Sopenharmony_ci}
4964514f5e3Sopenharmony_ci
4974514f5e3Sopenharmony_ci// "有ABC".toLocaleLowerCase()
4984514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, toLocaleLowerCase2)
4994514f5e3Sopenharmony_ci{
5004514f5e3Sopenharmony_ci    ASSERT_NE(thread, nullptr);
5014514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
5024514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromUtf8("有ABC");
5034514f5e3Sopenharmony_ci
5044514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
5054514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
5064514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
5074514f5e3Sopenharmony_ci
5084514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
5094514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::ToLocaleLowerCase(ecmaRuntimeCallInfo);
5104514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
5114514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
5124514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromUtf8("有abc");
5134514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
5144514f5e3Sopenharmony_ci}
5154514f5e3Sopenharmony_ci
5164514f5e3Sopenharmony_ci// "ABC".toLowerCase()
5174514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, toLowerCase1)
5184514f5e3Sopenharmony_ci{
5194514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
5204514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("ABC");
5214514f5e3Sopenharmony_ci
5224514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
5234514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
5244514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
5254514f5e3Sopenharmony_ci
5264514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
5274514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::ToLowerCase(ecmaRuntimeCallInfo);
5284514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
5294514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
5304514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("abc");
5314514f5e3Sopenharmony_ci    ASSERT_TRUE(JSTaggedValue::SameValue(resultHandle.GetTaggedValue(), test.GetTaggedValue()));
5324514f5e3Sopenharmony_ci}
5334514f5e3Sopenharmony_ci
5344514f5e3Sopenharmony_ci// "abc".toUpperCase()
5354514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, toUpperCase1)
5364514f5e3Sopenharmony_ci{
5374514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
5384514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abc");
5394514f5e3Sopenharmony_ci
5404514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
5414514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
5424514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
5434514f5e3Sopenharmony_ci
5444514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
5454514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::ToUpperCase(ecmaRuntimeCallInfo);
5464514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
5474514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
5484514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("ABC");
5494514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
5504514f5e3Sopenharmony_ci}
5514514f5e3Sopenharmony_ci
5524514f5e3Sopenharmony_ci// "abc".localecompare('b')
5534514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, localecompare1)
5544514f5e3Sopenharmony_ci{
5554514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
5564514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abc");
5574514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("b");
5584514f5e3Sopenharmony_ci
5594514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
5604514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
5614514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
5624514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
5634514f5e3Sopenharmony_ci
5644514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
5654514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::LocaleCompare(ecmaRuntimeCallInfo);
5664514f5e3Sopenharmony_ci
5674514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(-1).GetRawData());
5684514f5e3Sopenharmony_ci}
5694514f5e3Sopenharmony_ci
5704514f5e3Sopenharmony_ci// "abc".localecompare('abc')
5714514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, localecompare2)
5724514f5e3Sopenharmony_ci{
5734514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
5744514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abc");
5754514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("abc");
5764514f5e3Sopenharmony_ci
5774514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
5784514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
5794514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
5804514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
5814514f5e3Sopenharmony_ci
5824514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
5834514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::LocaleCompare(ecmaRuntimeCallInfo);
5844514f5e3Sopenharmony_ci
5854514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(0).GetRawData());
5864514f5e3Sopenharmony_ci}
5874514f5e3Sopenharmony_ci
5884514f5e3Sopenharmony_ci// "abc".localecompare('aa')
5894514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, localecompare3)
5904514f5e3Sopenharmony_ci{
5914514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
5924514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abc");
5934514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("aa");
5944514f5e3Sopenharmony_ci
5954514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
5964514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
5974514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
5984514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
5994514f5e3Sopenharmony_ci
6004514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
6014514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::LocaleCompare(ecmaRuntimeCallInfo);
6024514f5e3Sopenharmony_ci
6034514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(1).GetRawData());
6044514f5e3Sopenharmony_ci}
6054514f5e3Sopenharmony_ci
6064514f5e3Sopenharmony_ci// "你好".localecompare('辅助')
6074514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, localecompare4)
6084514f5e3Sopenharmony_ci{
6094514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
6104514f5e3Sopenharmony_ci    std::string referenceStr = "你好";
6114514f5e3Sopenharmony_ci    std::string compareStr = "辅助";
6124514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromStdString(referenceStr);
6134514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromStdString(compareStr);
6144514f5e3Sopenharmony_ci    JSHandle<EcmaString> locale = factory->NewFromASCII("zh-Hans");
6154514f5e3Sopenharmony_ci
6164514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
6174514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
6184514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
6194514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
6204514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(1, locale.GetTaggedValue());
6214514f5e3Sopenharmony_ci
6224514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
6234514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::LocaleCompare(ecmaRuntimeCallInfo);
6244514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev);
6254514f5e3Sopenharmony_ci
6264514f5e3Sopenharmony_ci    ASSERT_GT(result.GetRawData(), JSTaggedValue(0).GetRawData());
6274514f5e3Sopenharmony_ci}
6284514f5e3Sopenharmony_ci
6294514f5e3Sopenharmony_ci// Test localeCompare when locales changed
6304514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, localecompare5)
6314514f5e3Sopenharmony_ci{
6324514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
6334514f5e3Sopenharmony_ci    std::string referenceStr = "ä";
6344514f5e3Sopenharmony_ci    std::string compareStr = "z";
6354514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromStdString(referenceStr);
6364514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromStdString(compareStr);
6374514f5e3Sopenharmony_ci    JSHandle<EcmaString> locale = factory->NewFromASCII("de");
6384514f5e3Sopenharmony_ci
6394514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
6404514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
6414514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
6424514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
6434514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(1, locale.GetTaggedValue());
6444514f5e3Sopenharmony_ci
6454514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev1 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
6464514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::LocaleCompare(ecmaRuntimeCallInfo);
6474514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev1);
6484514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), JSTaggedValue(-1).GetRawData());
6494514f5e3Sopenharmony_ci
6504514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo1 = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
6514514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetFunction(JSTaggedValue::Undefined());
6524514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetThis(thisStr.GetTaggedValue());
6534514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetCallArg(0, val.GetTaggedValue());
6544514f5e3Sopenharmony_ci    // change locale
6554514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo1->SetCallArg(1, JSTaggedValue::Undefined());
6564514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev2 = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
6574514f5e3Sopenharmony_ci    JSTaggedValue result1 = BuiltinsString::LocaleCompare(ecmaRuntimeCallInfo1);
6584514f5e3Sopenharmony_ci    TestHelper::TearDownFrame(thread, prev2);
6594514f5e3Sopenharmony_ci    ASSERT_EQ(result1.GetRawData(), JSTaggedValue(-1).GetRawData());
6604514f5e3Sopenharmony_ci}
6614514f5e3Sopenharmony_ci
6624514f5e3Sopenharmony_ci// "abc".normalize('NFC')
6634514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, normalize1)
6644514f5e3Sopenharmony_ci{
6654514f5e3Sopenharmony_ci    ASSERT_NE(thread, nullptr);
6664514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
6674514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abc");
6684514f5e3Sopenharmony_ci    JSHandle<EcmaString> val = factory->NewFromASCII("NFC");
6694514f5e3Sopenharmony_ci
6704514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
6714514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
6724514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
6734514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, val.GetTaggedValue());
6744514f5e3Sopenharmony_ci
6754514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
6764514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::Normalize(ecmaRuntimeCallInfo);
6774514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
6784514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
6794514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("abc");
6804514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
6814514f5e3Sopenharmony_ci}
6824514f5e3Sopenharmony_ci
6834514f5e3Sopenharmony_ci// "abc".repeat(5)
6844514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, repeat1)
6854514f5e3Sopenharmony_ci{
6864514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
6874514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abc");
6884514f5e3Sopenharmony_ci
6894514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
6904514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
6914514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
6924514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(5)));
6934514f5e3Sopenharmony_ci
6944514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
6954514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::Repeat(ecmaRuntimeCallInfo);
6964514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
6974514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
6984514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("abcabcabcabcabc");
6994514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
7004514f5e3Sopenharmony_ci}
7014514f5e3Sopenharmony_ci
7024514f5e3Sopenharmony_ci// 'The morning is upon us.'.slice(4, -2)
7034514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, slice1)
7044514f5e3Sopenharmony_ci{
7054514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
7064514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("The morning is upon us.");
7074514f5e3Sopenharmony_ci
7084514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
7094514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
7104514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
7114514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(4)));
7124514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<double>(-2)));
7134514f5e3Sopenharmony_ci
7144514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
7154514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::Slice(ecmaRuntimeCallInfo);
7164514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
7174514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
7184514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("morning is upon u");
7194514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
7204514f5e3Sopenharmony_ci}
7214514f5e3Sopenharmony_ci
7224514f5e3Sopenharmony_ci// 'The morning is upon us.'.slice(12)
7234514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, slice2)
7244514f5e3Sopenharmony_ci{
7254514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
7264514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("The morning is upon us.");
7274514f5e3Sopenharmony_ci
7284514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
7294514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
7304514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
7314514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(12)));
7324514f5e3Sopenharmony_ci
7334514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
7344514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::Slice(ecmaRuntimeCallInfo);
7354514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
7364514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
7374514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("is upon us.");
7384514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
7394514f5e3Sopenharmony_ci}
7404514f5e3Sopenharmony_ci
7414514f5e3Sopenharmony_ci// 'Mozilla'.substring(3, -3)
7424514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, substring1)
7434514f5e3Sopenharmony_ci{
7444514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
7454514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("Mozilla");
7464514f5e3Sopenharmony_ci
7474514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
7484514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
7494514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
7504514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(3)));
7514514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<double>(-3)));
7524514f5e3Sopenharmony_ci
7534514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
7544514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::Substring(ecmaRuntimeCallInfo);
7554514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
7564514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
7574514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("Moz");
7584514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
7594514f5e3Sopenharmony_ci}
7604514f5e3Sopenharmony_ci
7614514f5e3Sopenharmony_ci// 'Mozilla'.substring(7, 4)
7624514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, substring2)
7634514f5e3Sopenharmony_ci{
7644514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
7654514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("Mozilla");
7664514f5e3Sopenharmony_ci
7674514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 8);
7684514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
7694514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
7704514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(7)));
7714514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<double>(4)));
7724514f5e3Sopenharmony_ci
7734514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
7744514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::Substring(ecmaRuntimeCallInfo);
7754514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
7764514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
7774514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("lla");
7784514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
7794514f5e3Sopenharmony_ci}
7804514f5e3Sopenharmony_ci
7814514f5e3Sopenharmony_ci// "   Hello world!   ".trim()
7824514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, trim1)
7834514f5e3Sopenharmony_ci{
7844514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
7854514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("   Hello world!   ");
7864514f5e3Sopenharmony_ci
7874514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
7884514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
7894514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisStr.GetTaggedValue());
7904514f5e3Sopenharmony_ci
7914514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
7924514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::Trim(ecmaRuntimeCallInfo);
7934514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
7944514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
7954514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("Hello world!");
7964514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
7974514f5e3Sopenharmony_ci}
7984514f5e3Sopenharmony_ci
7994514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, trim2)
8004514f5e3Sopenharmony_ci{
8014514f5e3Sopenharmony_ci    auto ecmaVM = thread->GetEcmaVM();
8024514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
8034514f5e3Sopenharmony_ci
8044514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
8054514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("   Hello world!   ");
8064514f5e3Sopenharmony_ci    JSHandle<JSFunction> stringObject(env->GetStringFunction());
8074514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value(thread, JSTaggedValue(thisStr.GetTaggedValue().GetTaggedObject()));
8084514f5e3Sopenharmony_ci    JSHandle<JSPrimitiveRef> str = factory->NewJSPrimitiveRef(stringObject, value);
8094514f5e3Sopenharmony_ci
8104514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
8114514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
8124514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(str.GetTaggedValue());
8134514f5e3Sopenharmony_ci
8144514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
8154514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::Trim(ecmaRuntimeCallInfo);
8164514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
8174514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
8184514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("Hello world!");
8194514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
8204514f5e3Sopenharmony_ci}
8214514f5e3Sopenharmony_ci
8224514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, trimRight)
8234514f5e3Sopenharmony_ci{
8244514f5e3Sopenharmony_ci    auto ecmaVM = thread->GetEcmaVM();
8254514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
8264514f5e3Sopenharmony_ci
8274514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
8284514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("      ");
8294514f5e3Sopenharmony_ci    JSHandle<JSFunction> stringObject(env->GetStringFunction());
8304514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value(thread, JSTaggedValue(thisStr.GetTaggedValue().GetTaggedObject()));
8314514f5e3Sopenharmony_ci    JSHandle<JSPrimitiveRef> str = factory->NewJSPrimitiveRef(stringObject, value);
8324514f5e3Sopenharmony_ci
8334514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 4);
8344514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
8354514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(str.GetTaggedValue());
8364514f5e3Sopenharmony_ci
8374514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
8384514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::TrimRight(ecmaRuntimeCallInfo);
8394514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
8404514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
8414514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("");
8424514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
8434514f5e3Sopenharmony_ci}
8444514f5e3Sopenharmony_ci
8454514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, ToString)
8464514f5e3Sopenharmony_ci{
8474514f5e3Sopenharmony_ci    auto ecmaVM = thread->GetEcmaVM();
8484514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
8494514f5e3Sopenharmony_ci
8504514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
8514514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
8524514f5e3Sopenharmony_ci    JSHandle<JSFunction> stringObject(env->GetStringFunction());
8534514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value(thread, JSTaggedValue(thisStr.GetTaggedValue().GetTaggedObject()));
8544514f5e3Sopenharmony_ci    JSHandle<JSPrimitiveRef> str = factory->NewJSPrimitiveRef(stringObject, value);
8554514f5e3Sopenharmony_ci
8564514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{};
8574514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, str.GetTaggedValue(), args, 4, AlgorithmType::TO_STRING);
8584514f5e3Sopenharmony_ci
8594514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
8604514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
8614514f5e3Sopenharmony_ci    JSTaggedValue test = JSTaggedValue(*thisStr);
8624514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), test.GetRawData());
8634514f5e3Sopenharmony_ci}
8644514f5e3Sopenharmony_ci
8654514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, ValueOf)
8664514f5e3Sopenharmony_ci{
8674514f5e3Sopenharmony_ci    auto ecmaVM = thread->GetEcmaVM();
8684514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> env = ecmaVM->GetGlobalEnv();
8694514f5e3Sopenharmony_ci
8704514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
8714514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("abcabcabc");
8724514f5e3Sopenharmony_ci    JSHandle<JSFunction> stringObject(env->GetStringFunction());
8734514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value(thread, JSTaggedValue(thisStr.GetTaggedValue().GetTaggedObject()));
8744514f5e3Sopenharmony_ci    JSHandle<JSPrimitiveRef> str = factory->NewJSPrimitiveRef(stringObject, value);
8754514f5e3Sopenharmony_ci
8764514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{};
8774514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, str.GetTaggedValue(), args, 4, AlgorithmType::VALUE_OF);
8784514f5e3Sopenharmony_ci
8794514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
8804514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
8814514f5e3Sopenharmony_ci    JSTaggedValue test = JSTaggedValue(*thisStr);
8824514f5e3Sopenharmony_ci    ASSERT_EQ(result.GetRawData(), test.GetRawData());
8834514f5e3Sopenharmony_ci}
8844514f5e3Sopenharmony_ci
8854514f5e3Sopenharmony_cistatic inline JSFunction *BuiltinsStringTestCreate(JSThread *thread)
8864514f5e3Sopenharmony_ci{
8874514f5e3Sopenharmony_ci    EcmaVM *ecmaVM = thread->GetEcmaVM();
8884514f5e3Sopenharmony_ci    JSHandle<GlobalEnv> globalEnv = ecmaVM->GetGlobalEnv();
8894514f5e3Sopenharmony_ci    return globalEnv->GetObjectFunction().GetObject<JSFunction>();
8904514f5e3Sopenharmony_ci}
8914514f5e3Sopenharmony_ci
8924514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, Raw)
8934514f5e3Sopenharmony_ci{
8944514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
8954514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> foo(factory->NewFromASCII("foo"));
8964514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> bar(factory->NewFromASCII("bar"));
8974514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> baz(factory->NewFromASCII("baz"));
8984514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> rawArray = JSHandle<JSTaggedValue>::Cast(JSArray::ArrayCreate(thread, JSTaggedNumber(0)));
8994514f5e3Sopenharmony_ci    JSHandle<JSObject> obj(rawArray);
9004514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key0(thread, JSTaggedValue(0));
9014514f5e3Sopenharmony_ci    PropertyDescriptor desc0(thread, foo);
9024514f5e3Sopenharmony_ci    JSArray::DefineOwnProperty(thread, obj, key0, desc0);
9034514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key1(thread, JSTaggedValue(1));
9044514f5e3Sopenharmony_ci    PropertyDescriptor desc1(thread, bar);
9054514f5e3Sopenharmony_ci    JSArray::DefineOwnProperty(thread, obj, key1, desc1);
9064514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> key2(thread, JSTaggedValue(2));
9074514f5e3Sopenharmony_ci    PropertyDescriptor desc2(thread, baz);
9084514f5e3Sopenharmony_ci    JSArray::DefineOwnProperty(thread, obj, key2, desc2);
9094514f5e3Sopenharmony_ci
9104514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> constructor(thread, BuiltinsStringTestCreate(thread));
9114514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> templateString(
9124514f5e3Sopenharmony_ci        factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
9134514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> rawKey(factory->NewFromASCII("raw"));
9144514f5e3Sopenharmony_ci    JSObject::SetProperty(thread, templateString, rawKey, rawArray);
9154514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromASCII("foo5barJavaScriptbaz");
9164514f5e3Sopenharmony_ci
9174514f5e3Sopenharmony_ci    JSHandle<EcmaString> javascript = factory->NewFromASCII("JavaScript");
9184514f5e3Sopenharmony_ci
9194514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 10);
9204514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
9214514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(JSTaggedValue::Undefined());
9224514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(templateString.GetObject<EcmaString>()));
9234514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(1, JSTaggedValue(static_cast<int32_t>(5)));
9244514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(2, javascript.GetTaggedValue());
9254514f5e3Sopenharmony_ci
9264514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
9274514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::Raw(ecmaRuntimeCallInfo);
9284514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
9294514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(reinterpret_cast<EcmaString *>(result.GetRawData()), *test));
9304514f5e3Sopenharmony_ci}
9314514f5e3Sopenharmony_ci
9324514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, Replace)
9334514f5e3Sopenharmony_ci{
9344514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
9354514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("Twas the night before Xmas...");
9364514f5e3Sopenharmony_ci    JSHandle<EcmaString> searchStr = factory->NewFromASCII("Xmas");
9374514f5e3Sopenharmony_ci    JSHandle<EcmaString> replaceStr = factory->NewFromASCII("Christmas");
9384514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected = factory->NewFromASCII("Twas the night before Christmas...");
9394514f5e3Sopenharmony_ci
9404514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{searchStr.GetTaggedValue(), replaceStr.GetTaggedValue()};
9414514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::REPLACE);
9424514f5e3Sopenharmony_ci
9434514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
9444514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(reinterpret_cast<EcmaString *>(result.GetRawData()), *expected));
9454514f5e3Sopenharmony_ci
9464514f5e3Sopenharmony_ci    JSHandle<EcmaString> replaceStr1 = factory->NewFromASCII("abc$$");
9474514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected1 = factory->NewFromASCII("Twas the night before abc$...");
9484514f5e3Sopenharmony_ci    args[0] = searchStr.GetTaggedValue();
9494514f5e3Sopenharmony_ci    args[1] = replaceStr1.GetTaggedValue();
9504514f5e3Sopenharmony_ci    auto result1 = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::REPLACE);
9514514f5e3Sopenharmony_ci
9524514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultString1(thread, result1);
9534514f5e3Sopenharmony_ci    ASSERT_TRUE(result1.IsString());
9544514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString1, *expected1));
9554514f5e3Sopenharmony_ci
9564514f5e3Sopenharmony_ci    JSHandle<EcmaString> replaceStr2 = factory->NewFromASCII("abc$$dd");
9574514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected2 = factory->NewFromASCII("Twas the night before abc$dd...");
9584514f5e3Sopenharmony_ci    args[0] = searchStr.GetTaggedValue();
9594514f5e3Sopenharmony_ci    args[1] = replaceStr2.GetTaggedValue();
9604514f5e3Sopenharmony_ci    auto result2 = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::REPLACE);
9614514f5e3Sopenharmony_ci
9624514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultString2(thread, result2);
9634514f5e3Sopenharmony_ci    ASSERT_TRUE(result2.IsString());
9644514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString2, *expected2));
9654514f5e3Sopenharmony_ci
9664514f5e3Sopenharmony_ci    JSHandle<EcmaString> replaceStr3 = factory->NewFromASCII("abc$&dd");
9674514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected3 = factory->NewFromASCII("Twas the night before abcXmasdd...");
9684514f5e3Sopenharmony_ci    args[0] = searchStr.GetTaggedValue();
9694514f5e3Sopenharmony_ci    args[1] = replaceStr3.GetTaggedValue();
9704514f5e3Sopenharmony_ci    auto result3 = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::REPLACE);
9714514f5e3Sopenharmony_ci
9724514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultString3(thread, result3);
9734514f5e3Sopenharmony_ci    ASSERT_TRUE(result3.IsString());
9744514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString3, *expected3));
9754514f5e3Sopenharmony_ci
9764514f5e3Sopenharmony_ci    JSHandle<EcmaString> replaceStr4 = factory->NewFromASCII("abc$`dd");
9774514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected4 =
9784514f5e3Sopenharmony_ci        factory->NewFromASCII("Twas the night before abcTwas the night before dd...");
9794514f5e3Sopenharmony_ci    args[0] = searchStr.GetTaggedValue();
9804514f5e3Sopenharmony_ci    args[1] = replaceStr4.GetTaggedValue();
9814514f5e3Sopenharmony_ci    auto result4 = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::REPLACE);
9824514f5e3Sopenharmony_ci
9834514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultString4(thread, result4);
9844514f5e3Sopenharmony_ci    ASSERT_TRUE(result4.IsString());
9854514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString4, *expected4));
9864514f5e3Sopenharmony_ci}
9874514f5e3Sopenharmony_ci
9884514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, Replace2)
9894514f5e3Sopenharmony_ci{
9904514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
9914514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("Twas the night before Xmas...");
9924514f5e3Sopenharmony_ci    JSHandle<EcmaString> searchStr = factory->NewFromASCII("Xmas");
9934514f5e3Sopenharmony_ci    JSHandle<EcmaString> replaceStr = factory->NewFromASCII("abc$\'dd");
9944514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected = factory->NewFromASCII("Twas the night before abc...dd...");
9954514f5e3Sopenharmony_ci
9964514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{searchStr.GetTaggedValue(), replaceStr.GetTaggedValue()};
9974514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::REPLACE);
9984514f5e3Sopenharmony_ci
9994514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
10004514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(reinterpret_cast<EcmaString *>(result.GetRawData()), *expected));
10014514f5e3Sopenharmony_ci
10024514f5e3Sopenharmony_ci    JSHandle<EcmaString> replaceStr2 = factory->NewFromASCII("abc$`dd$\'$ff");
10034514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected2 =
10044514f5e3Sopenharmony_ci        factory->NewFromASCII("Twas the night before abcTwas the night before dd...$ff...");
10054514f5e3Sopenharmony_ci    args[0] = searchStr.GetTaggedValue();
10064514f5e3Sopenharmony_ci    args[1] = replaceStr2.GetTaggedValue();
10074514f5e3Sopenharmony_ci    auto result2 = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::REPLACE);
10084514f5e3Sopenharmony_ci
10094514f5e3Sopenharmony_ci
10104514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultString2(thread, result2);
10114514f5e3Sopenharmony_ci    ASSERT_TRUE(result2.IsString());
10124514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString2, *expected2));
10134514f5e3Sopenharmony_ci
10144514f5e3Sopenharmony_ci    JSHandle<EcmaString> replaceStr3 = factory->NewFromASCII("abc$`dd$\'$");
10154514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected3 =
10164514f5e3Sopenharmony_ci        factory->NewFromASCII("Twas the night before abcTwas the night before dd...$...");
10174514f5e3Sopenharmony_ci    args[0] = searchStr.GetTaggedValue();
10184514f5e3Sopenharmony_ci    args[1] = replaceStr3.GetTaggedValue();
10194514f5e3Sopenharmony_ci    auto result3 = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::REPLACE);
10204514f5e3Sopenharmony_ci
10214514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultString3(thread, result3);
10224514f5e3Sopenharmony_ci    ASSERT_TRUE(result3.IsString());
10234514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString3, *expected3));
10244514f5e3Sopenharmony_ci
10254514f5e3Sopenharmony_ci    JSHandle<EcmaString> replaceStr4 = factory->NewFromASCII("abc$`dd$$");
10264514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected4 =
10274514f5e3Sopenharmony_ci        factory->NewFromASCII("Twas the night before abcTwas the night before dd$...");
10284514f5e3Sopenharmony_ci    args[0] = searchStr.GetTaggedValue();
10294514f5e3Sopenharmony_ci    args[1] = replaceStr4.GetTaggedValue();
10304514f5e3Sopenharmony_ci    auto result4 = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::REPLACE);
10314514f5e3Sopenharmony_ci
10324514f5e3Sopenharmony_ci    ASSERT_TRUE(result4.IsString());
10334514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultString4(thread, result4);
10344514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*resultString4, *expected4));
10354514f5e3Sopenharmony_ci}
10364514f5e3Sopenharmony_ci
10374514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, Replace3)
10384514f5e3Sopenharmony_ci{
10394514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
10404514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("Twas the night before Xmas...");
10414514f5e3Sopenharmony_ci    JSHandle<EcmaString> searchStr = factory->NewFromASCII("Xmas");
10424514f5e3Sopenharmony_ci    JSHandle<EcmaString> replaceStr = factory->NewFromASCII("$&a $` $\' $2 $01 $$1 $21 $32 a");
10434514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected = factory->NewFromASCII(
10444514f5e3Sopenharmony_ci        "Twas the night before Xmasa Twas the night before  ... $2 $01 $1 $21 $32 a...");
10454514f5e3Sopenharmony_ci
10464514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{searchStr.GetTaggedValue(), replaceStr.GetTaggedValue()};
10474514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::REPLACE);
10484514f5e3Sopenharmony_ci
10494514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
10504514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(reinterpret_cast<EcmaString *>(result.GetRawData()), *expected));
10514514f5e3Sopenharmony_ci}
10524514f5e3Sopenharmony_ci
10534514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, Replace4)
10544514f5e3Sopenharmony_ci{
10554514f5e3Sopenharmony_ci    // invoke RegExpConstructor method
10564514f5e3Sopenharmony_ci    JSHandle<EcmaString> pattern1 =
10574514f5e3Sopenharmony_ci        thread->GetEcmaVM()->GetFactory()->NewFromASCII("quick\\s(brown).+?(jumps)");
10584514f5e3Sopenharmony_ci    JSHandle<EcmaString> flags1 = thread->GetEcmaVM()->GetFactory()->NewFromASCII("iug");
10594514f5e3Sopenharmony_ci    JSTaggedValue result1 = CreateBuiltinsStringRegExpObjByPatternAndFlags(thread, pattern1, flags1);
10604514f5e3Sopenharmony_ci    JSHandle<JSRegExp> searchStr(thread, reinterpret_cast<JSRegExp *>(result1.GetRawData()));
10614514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected = thread->GetEcmaVM()->GetFactory()->NewFromASCII(
10624514f5e3Sopenharmony_ci        "The Quick Brown Fox Jumpsa The   Over The Lazy Dog Jumps Brown $1 Jumps1 $32 a Over The Lazy Dog");
10634514f5e3Sopenharmony_ci
10644514f5e3Sopenharmony_ci    // make ecma_runtime_call_info2
10654514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr =
10664514f5e3Sopenharmony_ci        thread->GetEcmaVM()->GetFactory()->NewFromASCII("The Quick Brown Fox Jumps Over The Lazy Dog");
10674514f5e3Sopenharmony_ci    JSHandle<EcmaString> replaceStr =
10684514f5e3Sopenharmony_ci        thread->GetEcmaVM()->GetFactory()->NewFromASCII("$&a $` $\' $2 $01 $$1 $21 $32 a");
10694514f5e3Sopenharmony_ci
10704514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{searchStr.GetTaggedValue(), replaceStr.GetTaggedValue()};
10714514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::REPLACE);
10724514f5e3Sopenharmony_ci
10734514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
10744514f5e3Sopenharmony_ci    ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(reinterpret_cast<EcmaString *>(result.GetRawData()), *expected));
10754514f5e3Sopenharmony_ci}
10764514f5e3Sopenharmony_ci
10774514f5e3Sopenharmony_civoid SplitCommon(JSThread *thread, std::vector<JSHandle<EcmaString>> expecteds, JSHandle<JSArray> &resultArray)
10784514f5e3Sopenharmony_ci{
10794514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> resultObj(resultArray);
10804514f5e3Sopenharmony_ci    for (size_t i = 0; i < expecteds.size(); i++) {
10814514f5e3Sopenharmony_ci        JSHandle<EcmaString> str(
10824514f5e3Sopenharmony_ci            JSObject::GetProperty(thread, resultObj,
10834514f5e3Sopenharmony_ci                                  JSHandle<JSTaggedValue>(thread, JSTaggedValue(static_cast<int>(i))))
10844514f5e3Sopenharmony_ci                .GetValue());
10854514f5e3Sopenharmony_ci        ASSERT_TRUE(EcmaStringAccessor::StringsAreEqual(*str, *expecteds[i]));
10864514f5e3Sopenharmony_ci    }
10874514f5e3Sopenharmony_ci}
10884514f5e3Sopenharmony_ci
10894514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, Split)
10904514f5e3Sopenharmony_ci{
10914514f5e3Sopenharmony_ci    // invoke RegExpConstructor method
10924514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
10934514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("Hello World. How are you doing?");
10944514f5e3Sopenharmony_ci    JSHandle<EcmaString> separatorStr = factory->NewFromASCII(" ");
10954514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> limit(thread, JSTaggedValue(3));
10964514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected1 = factory->NewFromASCII("Hello");
10974514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected2 = factory->NewFromASCII("World.");
10984514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected3 = factory->NewFromASCII("How");
10994514f5e3Sopenharmony_ci
11004514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{separatorStr.GetTaggedValue(), JSTaggedValue(static_cast<int32_t>(3))};
11014514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::SPLIT);
11024514f5e3Sopenharmony_ci
11034514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsECMAObject());
11044514f5e3Sopenharmony_ci    JSHandle<JSArray> resultArray(thread, reinterpret_cast<JSArray *>(result.GetRawData()));
11054514f5e3Sopenharmony_ci    ASSERT_TRUE(resultArray->IsJSArray());
11064514f5e3Sopenharmony_ci    std::vector<JSHandle<EcmaString>> expecteds{expected1, expected2, expected3};
11074514f5e3Sopenharmony_ci    SplitCommon(thread, expecteds, resultArray);
11084514f5e3Sopenharmony_ci}
11094514f5e3Sopenharmony_ci
11104514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, Split2)
11114514f5e3Sopenharmony_ci{
11124514f5e3Sopenharmony_ci    // invoke RegExpConstructor method
11134514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
11144514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisStr = factory->NewFromASCII("a-b-c");
11154514f5e3Sopenharmony_ci    JSHandle<EcmaString> pattern1 = factory->NewFromASCII("-");
11164514f5e3Sopenharmony_ci    JSHandle<EcmaString> flags1 = factory->NewFromASCII("iug");
11174514f5e3Sopenharmony_ci    JSTaggedValue result1 = CreateBuiltinsStringRegExpObjByPatternAndFlags(thread, pattern1, flags1);
11184514f5e3Sopenharmony_ci    JSHandle<JSRegExp> separatorObj(thread, result1);
11194514f5e3Sopenharmony_ci
11204514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> limit(thread, JSTaggedValue(3));
11214514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected1 = factory->NewFromASCII("a");
11224514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected2 = factory->NewFromASCII("b");
11234514f5e3Sopenharmony_ci    JSHandle<EcmaString> expected3 = factory->NewFromASCII("c");
11244514f5e3Sopenharmony_ci
11254514f5e3Sopenharmony_ci    std::vector<JSTaggedValue> args{separatorObj.GetTaggedValue(), JSTaggedValue(static_cast<int32_t>(3))};
11264514f5e3Sopenharmony_ci    auto result = StringAlgorithm(thread, thisStr.GetTaggedValue(), args, 8, AlgorithmType::SPLIT);
11274514f5e3Sopenharmony_ci
11284514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsECMAObject());
11294514f5e3Sopenharmony_ci    JSHandle<JSArray> resultArray(thread, result);
11304514f5e3Sopenharmony_ci    ASSERT_TRUE(resultArray->IsJSArray());
11314514f5e3Sopenharmony_ci    std::vector<JSHandle<EcmaString>> expecteds{expected1, expected2, expected3};
11324514f5e3Sopenharmony_ci    SplitCommon(thread, expecteds, resultArray);
11334514f5e3Sopenharmony_ci}
11344514f5e3Sopenharmony_ci
11354514f5e3Sopenharmony_ci// "一二三四".at(3)
11364514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, at1)
11374514f5e3Sopenharmony_ci{
11384514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
11394514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisVal = factory->NewFromUtf8("一二三四");
11404514f5e3Sopenharmony_ci
11414514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
11424514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
11434514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisVal.GetTaggedValue());
11444514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(3)));
11454514f5e3Sopenharmony_ci
11464514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
11474514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::At(ecmaRuntimeCallInfo);
11484514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
11494514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
11504514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromUtf8("四");
11514514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
11524514f5e3Sopenharmony_ci}
11534514f5e3Sopenharmony_ci
11544514f5e3Sopenharmony_ci// "一二三四".at(-2)
11554514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, at2)
11564514f5e3Sopenharmony_ci{
11574514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
11584514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisVal = factory->NewFromUtf8("一二三四");
11594514f5e3Sopenharmony_ci
11604514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
11614514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
11624514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisVal.GetTaggedValue());
11634514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(-2)));
11644514f5e3Sopenharmony_ci
11654514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
11664514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::At(ecmaRuntimeCallInfo);
11674514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsString());
11684514f5e3Sopenharmony_ci    JSHandle<EcmaString> resultHandle(thread, reinterpret_cast<EcmaString *>(result.GetRawData()));
11694514f5e3Sopenharmony_ci    JSHandle<EcmaString> test = factory->NewFromUtf8("三");
11704514f5e3Sopenharmony_ci    ASSERT_EQ(EcmaStringAccessor::Compare(instance, resultHandle, test), 0);
11714514f5e3Sopenharmony_ci}
11724514f5e3Sopenharmony_ci
11734514f5e3Sopenharmony_ci// "一二三四".at(-5)
11744514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, at3)
11754514f5e3Sopenharmony_ci{
11764514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
11774514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisVal = factory->NewFromUtf8("一二三四");
11784514f5e3Sopenharmony_ci
11794514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
11804514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
11814514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisVal.GetTaggedValue());
11824514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(-5)));
11834514f5e3Sopenharmony_ci
11844514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
11854514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::At(ecmaRuntimeCallInfo);
11864514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsUndefined());
11874514f5e3Sopenharmony_ci}
11884514f5e3Sopenharmony_ci
11894514f5e3Sopenharmony_ci// "abcabcabc".at(9)
11904514f5e3Sopenharmony_ciHWTEST_F_L0(BuiltinsStringTest, at4)
11914514f5e3Sopenharmony_ci{
11924514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
11934514f5e3Sopenharmony_ci    JSHandle<EcmaString> thisVal = factory->NewFromASCII("abcabcabc");
11944514f5e3Sopenharmony_ci
11954514f5e3Sopenharmony_ci    auto ecmaRuntimeCallInfo = TestHelper::CreateEcmaRuntimeCallInfo(thread, JSTaggedValue::Undefined(), 6);
11964514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetFunction(JSTaggedValue::Undefined());
11974514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetThis(thisVal.GetTaggedValue());
11984514f5e3Sopenharmony_ci    ecmaRuntimeCallInfo->SetCallArg(0, JSTaggedValue(static_cast<double>(9)));
11994514f5e3Sopenharmony_ci
12004514f5e3Sopenharmony_ci    [[maybe_unused]] auto prev = TestHelper::SetupFrame(thread, ecmaRuntimeCallInfo);
12014514f5e3Sopenharmony_ci    JSTaggedValue result = BuiltinsString::At(ecmaRuntimeCallInfo);
12024514f5e3Sopenharmony_ci    ASSERT_TRUE(result.IsUndefined());
12034514f5e3Sopenharmony_ci}
12044514f5e3Sopenharmony_ci}  // namespace panda::test
1205