diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn deleted file mode 100755 index 3434bb1bc1b010c868838c7160e0d45d2034a53e..0000000000000000000000000000000000000000 --- a/test/unittest/BUILD.gn +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//build/test.gni") - -if (is_standard_system) { - module_output_path = "compileruntime/js_util_module" -} - -ohos_unittest("test_util_unittest") { - module_out_path = module_output_path - - include_dirs = [ - "//base/compileruntime/js_util_module/util", - "//foundation/ace/napi", - "//foundation/ace/napi/interfaces/kits", - "//foundation/ace/napi/native_engine", - "//foundation/ace/napi/native_engine/impl/quickjs", - "//third_party/icu/icu4c/source/common", - "//third_party/googletest/include", - "//third_party/node/src", - "//utils/native/base/include", - ] - - cflags = [ "-g3" ] - - sources = [ - "test_quickjs.cpp", - "test_util.cpp", - ] - - deps = [ - "//base/compileruntime/js_util_module/util:util_packages", - "//foundation/ace/napi/:ace_napi", - "//foundation/ace/napi/:ace_napi_quickjs", - "//third_party/googletest:gtest", - "//third_party/googletest:gtest_main", - "//third_party/icu/icu4c:static_icuuc", - "//third_party/libuv:uv_static", - "//third_party/quickjs:qjs", - "//utils/native/base:utils", - "//utils/native/base:utilsecurec", - ] - - if (is_standard_system) { - external_deps = [ "hiviewdfx_hilog_native:libhilog" ] - } -} - -group("unittest") { - testonly = true - deps = [ ":test_util_unittest" ] -} diff --git a/test/unittest/test.h b/test/unittest/test.h deleted file mode 100755 index af0772277b1847dd5c06d254738beb8d2a02596c..0000000000000000000000000000000000000000 --- a/test/unittest/test.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H -#define FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H - -#include "native_engine.h" - -#include "gtest/gtest.h" - -class NativeEngineTest : public testing::Test { -public: - NativeEngineTest(); - virtual ~NativeEngineTest(); - void SetUp() override {} - void TearDown() override {} -protected: - NativeEngine* engine_; -}; - -#endif /* FOUNDATION_ACE_NAPI_TEST_UNITTEST_TEST_H */ \ No newline at end of file diff --git a/test/unittest/test_quickjs.cpp b/test/unittest/test_quickjs.cpp deleted file mode 100755 index d5d4c0ba68b1fb8ff7f56548ec52c7cfb497e74d..0000000000000000000000000000000000000000 --- a/test/unittest/test_quickjs.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "test.h" - -#include "quickjs_native_engine.h" - -static NativeEngine* g_nativeEngine = nullptr; - -NativeEngineTest::NativeEngineTest() -{ - engine_ = g_nativeEngine; -} - -NativeEngineTest::~NativeEngineTest() {} - -int main(int argc, char** argv) -{ - testing::GTEST_FLAG(output) = "xml:./"; - testing::InitGoogleTest(&argc, argv); - - JSRuntime* rt = JS_NewRuntime(); - if (rt == nullptr) { - return 0; - } - - JSContext* ctx = JS_NewContext(rt); - if (ctx == nullptr) { - return 0; - } - - js_std_add_helpers(ctx, 0, nullptr); - - g_nativeEngine = new QuickJSNativeEngine(rt, ctx, nullptr); - - int ret = RUN_ALL_TESTS(); - - g_nativeEngine->Loop(LOOP_DEFAULT); - - delete g_nativeEngine; - g_nativeEngine = nullptr; - - js_std_free_handlers(rt); - JS_FreeContext(ctx); - JS_FreeRuntime(rt); - - return ret; -} diff --git a/test/unittest/test_util.cpp b/test/unittest/test_util.cpp deleted file mode 100755 index 14a1618d7fa0c74e7d6d662dfb65d420b9567bc5..0000000000000000000000000000000000000000 --- a/test/unittest/test_util.cpp +++ /dev/null @@ -1,690 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "test.h" - -#include -#include "napi/native_api.h" -#include "napi/native_node_api.h" - -#include "securec.h" -#include "utils/log.h" -#include "js_textdecoder.h" -#include "js_textencoder.h" -// #include "unicode/unistr.h" - -#define ASSERT_CHECK_CALL(call) \ - { \ - ASSERT_EQ(call, napi_ok); \ - } - -#define ASSERT_CHECK_VALUE_TYPE(env, value, type) \ - { \ - napi_valuetype valueType = napi_undefined; \ - ASSERT_TRUE(value != nullptr); \ - ASSERT_CHECK_CALL(napi_typeof(env, value, &valueType)); \ - ASSERT_EQ(valueType, type); \ - } - - -/* @tc.name: getEncodingTest001 - * @tc.desc: Test acquire encoding mode. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, getEncodingTest001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("getEncodingTest001 start"); - napi_env env = (napi_env)engine_; - - OHOS::Util::TextEncoder textEncoder(env); - napi_value result = textEncoder.GetEncoding(); - - std::string tmpTestStr = "utf-8"; - - char *buffer = nullptr; - size_t bufferSize = 0; - napi_get_value_string_utf8(env, result, buffer, -1, &bufferSize); - if (bufferSize > 0) { - buffer = new char[bufferSize + 1]; - napi_get_value_string_utf8(env, result, buffer, bufferSize + 1, &bufferSize); - } - - ASSERT_STREQ(buffer, tmpTestStr.c_str()); - if (buffer != nullptr) { - delete []buffer; - buffer = nullptr; - } -} - -/** - * @tc.name: textEncodeTest001 - * @tc.desc: Test encode src. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, textEncodeTest001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("getEncodingTest001 start"); - napi_env env = (napi_env)engine_; - OHOS::Util::TextEncoder textEncoder(env); - - std::string input = "abc123"; - napi_value src = nullptr; - napi_create_string_utf8(env, input.c_str(), input.size(), &src); - napi_value result = textEncoder.Encode(src); - - char excepted[7] = {0x61, 0x62, 0x63, 0x31, 0x32, 0x33, 0}; - - napi_typedarray_type type; - size_t srcLength = 0; - void* srcData = nullptr; - napi_value srcBuffer = nullptr; - size_t byteOffset = 0; - - napi_get_typedarray_info( - env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); - - ASSERT_EQ(srcLength, 6); - char* res = (char*)srcData; - - res[srcLength] = 0; - ASSERT_STREQ(res, excepted); -} - -/** - * @tc.name: textEncodeTest001 - * @tc.desc: Test encode src. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, textEncodeTest002, testing::ext::TestSize.Level0) -{ - HILOG_INFO("getEncodingTest001 start"); - napi_env env = (napi_env)engine_; - OHOS::Util::TextEncoder textEncoder(env); - - std::string input = ""; - napi_value src = nullptr; - napi_create_string_utf8(env, input.c_str(), input.size(), &src); - napi_value result = textEncoder.Encode(src); - - napi_typedarray_type type; - size_t srcLength = 0; - void* srcData = nullptr; - napi_value srcBuffer = nullptr; - size_t byteOffset = 0; - - napi_get_typedarray_info( - env, result, &type, &srcLength, &srcData, &srcBuffer, &byteOffset); - - ASSERT_STREQ((char*)srcData, nullptr); -} - -/** - * @tc.name: textEncodeIntoTest001 - * @tc.desc: Test returns a dictionary object indicating the progress of the encoding - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, textEncodeIntoTest001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("textEncodeIntoTest001 start"); - napi_env env = (napi_env)engine_; - OHOS::Util::TextEncoder textEncoder(env); - - std::string input = "abc123"; - napi_value src = nullptr; - napi_create_string_utf8(env, input.c_str(), input.size(), &src); - - napi_value arrayBuffer = nullptr; - void* arrayBufferPtr = nullptr; - size_t arrayBufferSize = 20; - napi_create_arraybuffer(env, arrayBufferSize, &arrayBufferPtr, &arrayBuffer); - - napi_value dest = nullptr; - napi_create_typedarray(env, napi_int8_array, arrayBufferSize, arrayBuffer, 0, &dest); - - napi_value result = textEncoder.EncodeInto(src, dest); - - napi_value read = nullptr; - napi_get_named_property(env, result, "read", &read); - - uint32_t resRead = 0; - - napi_get_value_uint32(env, read, &resRead); - - napi_value written = nullptr; - napi_get_named_property(env, result, "written", &written); - - uint32_t resWritten = 0; - napi_get_value_uint32(env, read, &resWritten); - - ASSERT_EQ(resRead, (uint32_t)6); - ASSERT_EQ(resWritten, (uint32_t)6); -} - - -/** - * @tc.name: GetEncoding001 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, GetEncoding001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("TextDecoder::getEncodingTest001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = -1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - napi_value testString = textDecoder.GetEncoding(); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - std::string tmpTestStr = "utf-8"; - size_t strLength = 0; - char* buffer = nullptr; - if (bufferSize > 0) { - buffer = new char[bufferSize + 1]{ 0 }; - napi_get_value_string_utf8(env, testString, buffer, bufferSize + 1, &strLength); - } - ASSERT_STREQ(tmpTestStr.c_str(), buffer); - ASSERT_EQ(tmpTestStr.length(), strLength); - if (buffer != nullptr) { - delete []buffer; - buffer = nullptr; - } -} - -/** - * @tc.name: GetFatal001 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, GetFatal001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("TextDecoder::GetFatal001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 1; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - napi_value naVal = textDecoder.GetFatal(); - bool result = false; - napi_get_value_bool(env, naVal, &result); - ASSERT_TRUE(result); -} - -/** - * @tc.name: GetFatal002 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, GetFatal002, testing::ext::TestSize.Level0) -{ - HILOG_INFO("TextDecoder::GetFatal002 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = 1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - napi_value naVal = textDecoder.GetFatal(); - bool result = false; - napi_get_value_bool(env, naVal, &result); - ASSERT_FALSE(result); -} - -/** - * @tc.name: GetIgnoreBOM001 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, GetIgnoreBOM001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("TextDecoder::GetIgnoreBOM001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = 1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - napi_value naVal = textDecoder.GetIgnoreBOM(); - bool result = false; - napi_get_value_bool(env, naVal, &result); - ASSERT_TRUE(result); -} - -/** - * @tc.name: decoderUtf8001 utf-8 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf8001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf8001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = -1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = false; - size_t byteLength = 3; - void* data = nullptr; - napi_value resultBuff; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[3] = {0x61, 0x62, 0x63}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - size_t length = 0; - char* ch = nullptr; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - std::string tempStr = "abc"; - ASSERT_STREQ(tempStr.c_str(), ch); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf8002 utf-8 - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf8002, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf8002 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-8"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = true; - size_t byteLength = 3; - void* data = nullptr; - napi_value resultBuff; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[3] = {0x61, 0x62, 0x63}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - size_t length = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - char* ch = nullptr; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - - std::string tempStr = "abc"; - ASSERT_STREQ(tempStr.c_str(), ch); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16le001 utf-16le - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16le001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16le001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16le"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = false; - size_t byteLength = 6; - void* data = nullptr; - napi_value resultBuff; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[6] = {0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - size_t length = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - char* ch = nullptr; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - - std::string tempStr = "abc"; - ASSERT_STREQ(tempStr.c_str(), ch); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16le002 utf-16le - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16le002, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16le002 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16le"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = true; - size_t byteLength = 6; - void* data = nullptr; - napi_value resultBuff; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[6] = {0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - char* ch = nullptr; - size_t length = 0; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - std::string tempStr = "abc"; - ASSERT_STREQ(tempStr.c_str(), ch); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16le003 utf-16le - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16le003, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16le003 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16le"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = true; - size_t byteLength = 8; - void* data = nullptr; - napi_value resultBuff; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[8] = {0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - char* ch = nullptr; - size_t length = 0; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - std::string tempStr01(ch); - std::u16string tempU16str02 = - std::wstring_convert, char16_t> {}.from_bytes(tempStr01); - ASSERT_EQ(0xFEFF, (int)tempU16str02[0]); - ASSERT_EQ(0x61, (int)tempU16str02[1]); - ASSERT_EQ(0x62, (int)tempU16str02[2]); - ASSERT_EQ(0x63, (int)tempU16str02[3]); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16le004 utf-16le - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16le004, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16le004 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = -1; - int ignoreBOM = -1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16le"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = false; - size_t byteLength = 8; - void* data = nullptr; - napi_value resultBuff; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[8] = {0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2; - - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - char* ch = nullptr; - size_t length = 0; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - std::string tempStr01(ch); - std::u16string tempU16str02 = - std::wstring_convert, char16_t> {}.from_bytes(tempStr01); - ASSERT_EQ(0xFEFF, (int)tempU16str02[0]); - ASSERT_EQ(0x61, (int)tempU16str02[1]); - ASSERT_EQ(0x62, (int)tempU16str02[2]); - ASSERT_EQ(0x63, (int)tempU16str02[3]); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16be001 utf-16be - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16be001, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16be001 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16be"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = false; - size_t byteLength = 6; - void* data = nullptr; - napi_value resultBuff; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[6] = {0x00, 0x61, 0x00, 0x62, 0x00, 0x63}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - size_t length = 0; - char* ch = nullptr; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - std::string tempStr = "abc"; - ASSERT_STREQ(tempStr.c_str(), ch); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16be002 utf-16be - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16be002, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16be002 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 0; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16be"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = false; - size_t byteLength = 8; - void* data = nullptr; - napi_value resultBuff; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[8] = {0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - size_t length = 0; - char* ch = nullptr; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - std::string tempStr01(ch); - std::u16string tempU16str02 = - std::wstring_convert, char16_t> {}.from_bytes(tempStr01); - ASSERT_EQ(0xFEFF, (int)tempU16str02[0]); - ASSERT_EQ(0x61, (int)tempU16str02[1]); - ASSERT_EQ(0x62, (int)tempU16str02[2]); - ASSERT_EQ(0x63, (int)tempU16str02[3]); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -} - -/** - * @tc.name: decoderUtf16be003 utf-16be - * @tc.desc: Test date type. - * @tc.type: FUNC - */ -HWTEST_F(NativeEngineTest, decoderUtf16be003, testing::ext::TestSize.Level0) -{ - HILOG_INFO("decoderUtf16be003 start"); - napi_env env = (napi_env)engine_; - std::vector inputVec; - int fatal = 0; - int ignoreBOM = 1; - inputVec.push_back(fatal); - inputVec.push_back(ignoreBOM); - std::string str = "utf-16be"; - OHOS::Util::TextDecoder textDecoder(env, str, inputVec); - bool iflag = true; - size_t byteLength = 8; - void* data = nullptr; - napi_value resultBuff; - napi_create_arraybuffer(env, byteLength, &data, &resultBuff); - unsigned char arr[8] = {0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63}; - int ret = memcpy_s(data, sizeof(arr), reinterpret_cast(arr), sizeof(arr)); - ASSERT_EQ(0, ret); - napi_value result2; - napi_create_typedarray(env, napi_int8_array, byteLength, resultBuff, 0, &result2); - napi_value testString = textDecoder.Decode(result2, iflag); - size_t bufferSize = 0; - napi_get_value_string_utf8(env, testString, nullptr, 0, &bufferSize); - size_t length = 0; - char* ch = nullptr; - if (bufferSize > 0) { - ch = new char[bufferSize + 1](); - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - } - napi_get_value_string_utf8(env, testString, ch, bufferSize + 1, &length); - std::string tempStr01(ch); - std::u16string tempU16str02 = - std::wstring_convert, char16_t> {}.from_bytes(tempStr01); - ASSERT_EQ(0xFEFF, (int)tempU16str02[0]); - ASSERT_EQ(0x61, (int)tempU16str02[1]); - ASSERT_EQ(0x62, (int)tempU16str02[2]); - ASSERT_EQ(0x63, (int)tempU16str02[3]); - if (ch != nullptr) { - delete []ch; - ch = nullptr; - } -}