From 090f85130b1abe7b44e25d940fd20f9053d5efdf Mon Sep 17 00:00:00 2001 From: lifansheng Date: Thu, 16 Sep 2021 09:43:02 +0800 Subject: [PATCH 1/5] add host_test Signed-off-by: lifansheng --- test/unittest/BUILD.gn | 63 +++ test/unittest/test.h | 33 ++ test/unittest/test_quickjs.cpp | 60 +++ test/unittest/test_util.cpp | 690 +++++++++++++++++++++++++++++++++ 4 files changed, 846 insertions(+) create mode 100755 test/unittest/BUILD.gn create mode 100755 test/unittest/test.h create mode 100755 test/unittest/test_quickjs.cpp create mode 100755 test/unittest/test_util.cpp diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn new file mode 100755 index 0000000..3434bb1 --- /dev/null +++ b/test/unittest/BUILD.gn @@ -0,0 +1,63 @@ +# 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 new file mode 100755 index 0000000..af07722 --- /dev/null +++ b/test/unittest/test.h @@ -0,0 +1,33 @@ +/* + * 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 new file mode 100755 index 0000000..d5d4c0b --- /dev/null +++ b/test/unittest/test_quickjs.cpp @@ -0,0 +1,60 @@ +/* + * 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 new file mode 100755 index 0000000..14a1618 --- /dev/null +++ b/test/unittest/test_util.cpp @@ -0,0 +1,690 @@ +/* + * 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; + } +} -- Gitee From 41e3cd85bc5878076899e70517b4bd07d93d6642 Mon Sep 17 00:00:00 2001 From: lifansheng Date: Thu, 16 Sep 2021 16:01:48 +0800 Subject: [PATCH 2/5] modify gn Signed-off-by: lifansheng --- test/unittest/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 3434bb1..e136af4 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -14,7 +14,7 @@ import("//build/test.gni") if (is_standard_system) { - module_output_path = "compileruntime/js_util_module" + module_output_path = "ace_engine_standard/napi" } ohos_unittest("test_util_unittest") { -- Gitee From 3515d2d3527600cd54d0c46fe7a85e6fa5f10b20 Mon Sep 17 00:00:00 2001 From: lifansheng Date: Thu, 16 Sep 2021 19:21:50 +0800 Subject: [PATCH 3/5] modify codecheck Signed-off-by: lifansheng --- lrubuffer/js_lrubuffer.js | 3 +++ ohos.build | 1 + test/unittest/test_util.cpp | 1 - 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lrubuffer/js_lrubuffer.js b/lrubuffer/js_lrubuffer.js index a287e39..1f25bb5 100755 --- a/lrubuffer/js_lrubuffer.js +++ b/lrubuffer/js_lrubuffer.js @@ -121,10 +121,13 @@ class LruBuffer { if (this.cache.has(key)) { flag = true; let value; + this.hitCount++; value = this.cache.get(key); this.cache.delete(key); this.cache.set(key, value); + return flag; } + this.missCount++; return flag; } remove(key) { diff --git a/ohos.build b/ohos.build index f53f42b..b49feec 100755 --- a/ohos.build +++ b/ohos.build @@ -14,6 +14,7 @@ "inner_kits": [ ], "test_list": [ + "//base/compileruntime/js_util_module/test/unittest:unittest" ] } } diff --git a/test/unittest/test_util.cpp b/test/unittest/test_util.cpp index 14a1618..e0b3b17 100755 --- a/test/unittest/test_util.cpp +++ b/test/unittest/test_util.cpp @@ -23,7 +23,6 @@ #include "utils/log.h" #include "js_textdecoder.h" #include "js_textencoder.h" -// #include "unicode/unistr.h" #define ASSERT_CHECK_CALL(call) \ { \ -- Gitee From 77dce077c5c56307a62c62d6c57dbbc3a30a0d78 Mon Sep 17 00:00:00 2001 From: lifansheng Date: Fri, 17 Sep 2021 16:10:15 +0800 Subject: [PATCH 4/5] modify lrubuffer Signed-off-by: lifansheng --- lrubuffer/js_lrubuffer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lrubuffer/js_lrubuffer.js b/lrubuffer/js_lrubuffer.js index 1f25bb5..71a4752 100755 --- a/lrubuffer/js_lrubuffer.js +++ b/lrubuffer/js_lrubuffer.js @@ -138,7 +138,7 @@ class LruBuffer { let former; former = this.cache.get(key); this.cache.delete(key); - if (key !== null) { + if (former !== null) { this.afterRemoval(false, key, former, null); return former; } -- Gitee From b5b91e6970b1d59c45eb0309edc9ac544f86dadc Mon Sep 17 00:00:00 2001 From: lifansheng Date: Fri, 17 Sep 2021 16:13:34 +0800 Subject: [PATCH 5/5] modify build.gn Signed-off-by: lifansheng --- test/unittest/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index e136af4..e7277ab 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -14,7 +14,7 @@ import("//build/test.gni") if (is_standard_system) { - module_output_path = "ace_engine_standard/napi" + module_output_path = "jsapi_util/util" } ohos_unittest("test_util_unittest") { -- Gitee