test codecheck

Signed-off-by: lifansheng <lifansheng1@huawei.com>
This commit is contained in:
lifansheng
2021-09-14 22:04:03 +08:00
parent 252a21b3d8
commit bfc72e54ea
7 changed files with 589 additions and 1 deletions
+59
View File
@@ -0,0 +1,59 @@
# 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/ohos.gni")
import("//build/ohos/ace/ace.gni")
base_output_path = get_label_info(":js_lrubuffer", "target_out_dir")
js_lrubuffer_obj_path = base_output_path + "/lrubuffer.o"
gen_js_obj("js_lrubuffer") {
input = "//base/compileruntime/js_util_module/lrubuffer/js_lrubuffer.js"
output = js_lrubuffer_obj_path
}
ohos_shared_library("lrubuffer") {
include_dirs = [
"//foundation/ace/napi",
"//foundation/ace/napi/native_engine",
"//third_party/icu/icu4c/source/common",
"//third_party/node/src",
"//foundation/ace/napi/interfaces/kits",
"//base/compileruntime/js_util_module/lrubuffer",
]
sources = [ "native_module_lrubuffer.cpp" ]
deps = [
":js_lrubuffer",
"//base/compileruntime/js_util_module/lrubuffer/:js_lrubuffer",
"//foundation/ace/napi/:ace_napi",
"//foundation/ace/napi/:ace_napi_quickjs",
"//third_party/icu/icu4c:static_icuuc",
"//utils/native/base:utils",
]
if (is_standard_system) {
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
} else {
external_deps = [ "hilog:libhilog" ]
}
subsystem_name = "ccruntime"
part_name = "jsapi_util"
relative_install_dir = "module"
}
group("lrubuffer_packages") {
deps = [ ":lrubuffer" ]
}
+189
View File
@@ -0,0 +1,189 @@
/*
* 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.
*/
'use strict';
class LruBuffer {
constructor(capacity) {
this.maxSize = 64;
this.putCount = 0;
this.createCount = 0;
this.evictionCount = 0;
this.hitCount = 0;
this.missCount = 0;
if (capacity !== undefined) {
if (capacity <= 0) {
throw new Error('data error');
}
this.maxSize = capacity;
}
this.cache = new Map();
}
updateCapacity(newCapacity) {
if (newCapacity <= 0) {
throw new Error('data error');
}
else if (this.cache.size > newCapacity) {
this.changeCapacity(newCapacity);
}
this.maxSize = newCapacity;
}
get(key) {
if (key === null) {
throw new Error('key search failed');
}
let value;
if (this.cache.has(key)) {
value = this.cache.get(key);
this.hitCount++;
this.cache.delete(key);
this.cache.set(key, value);
return value;
}
this.missCount++;
let createValue = this.createDefault(key);
if (createValue === undefined) {
return undefined;
}
else {
value = this.put(key, createValue);
this.createCount++;
if (value !== null) {
this.put(key, value);
this.afterRemoval(false, key, createValue, value);
return value;
}
return createValue;
}
}
put(key, value) {
if (key === null || value === null) {
throw new Error('key or value search failed');
}
let former;
this.putCount++;
if (this.cache.has(key)) {
former = this.cache.get(key);
this.cache.delete(key);
this.afterRemoval(false, key, former, null);
}
else if (this.cache.size >= this.maxSize) {
this.cache.delete(this.cache.keys().next().value);
this.evictionCount++;
}
this.cache.set(key, value);
return former;
}
getCreatCount() {
return this.createCount;
}
getMissCount() {
return this.missCount;
}
getRemovalCount() {
return this.evictionCount;
}
getMatchCount() {
return this.hitCount;
}
getPutCount() {
return this.putCount;
}
capacity() {
return this.maxSize;
}
size() {
return this.cache.size;
}
clear() {
this.cache.clear();
this.afterRemoval(false, this.cache.keys(), this.cache.values(), null);
}
isEmpty() {
let temp = false;
if (this.cache.size === 0) {
temp = true;
}
return temp;
}
contains(key) {
let flag = false;
if (this.cache.has(key)) {
flag = true;
let value;
value = this.cache.get(key);
this.cache.delete(key);
this.cache.set(key, value);
}
return flag;
}
remove(key) {
if (key === null) {
throw new Error('key search failed');
}
else if (this.cache.has(key)) {
let former;
former = this.cache.get(key);
this.cache.delete(key);
if (key !== null) {
this.afterRemoval(false, key, former, null);
return former;
}
}
return undefined;
}
toString() {
let peek = 0;
let hitRate = 0;
peek = this.hitCount + this.missCount;
if (peek !== 0) {
hitRate = 100 * this.hitCount / peek;
}
else {
hitRate = 0;
}
let str = '';
str = 'Lrubuffer[ maxSize = ' + this.maxSize + ', hits = ' + this.hitCount + ', misses = ' + this.missCount
+ ', hitRate = ' + hitRate + '% ]';
return str;
}
values() {
let arr = [];
for (let value of this.cache.values()) {
arr.push(value);
}
return arr;
}
keys() {
let arr = [];
for (let key of this.cache.keys()) {
arr.push(key);
}
return arr;
}
afterRemoval(isEvict, key, value, newValue) {
}
createDefault(key) {
return undefined;
}
changeCapacity(newCapacity) {
while (this.cache.size > newCapacity) {
this.cache.delete(this.cache.keys().next().value);
this.evictionCount++;
this.afterRemoval(true, this.cache.keys(), this.cache.values(), null);
}
}
}
export default {
LruBuffer: LruBuffer,
};
+65
View File
@@ -0,0 +1,65 @@
/*
* 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 "napi/native_api.h"
#include "napi/native_node_api.h"
#include "utils/log.h"
extern const char _binary_js_lrubuffer_js_start[];
extern const char _binary_js_lrubuffer_js_end[];
static napi_value LruBufferInit(napi_env env, napi_value exports)
{
HILOG_INFO("LXY -------module----- LruBufferInit start");
const char* lruBufferClassName = "lrubuffer";
napi_value lruBufferClass = nullptr;
NAPI_CALL(env, napi_define_class(env, lruBufferClassName, strlen(lruBufferClassName), nullptr,
nullptr, 0, nullptr,
&lruBufferClass));
static napi_property_descriptor desc[] = {
DECLARE_NAPI_PROPERTY("lrubuffer", lruBufferClass),
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
HILOG_INFO("LXY -------module----- LruBufferInit end ");
return exports;
}
// lrubuffer module define
static napi_module lrubufferModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = LruBufferInit,
.nm_modname = "lrubuffer",
.nm_priv = ((void*)0),
.reserved = { 0 },
};
extern "C" __attribute__ ((constructor)) void RegisterModule()
{
napi_module_register(&lrubufferModule);
}
// lrubuffer JS register
extern "C"
__attribute__((visibility("default"))) void NAPI_lrubuffer_GetJSCode(const char** buf, int* buflen)
{
if (buf != nullptr) {
*buf = _binary_js_lrubuffer_js_start;
}
if (buflen != nullptr) {
*buflen = _binary_js_lrubuffer_js_end - _binary_js_lrubuffer_js_start;
}
}
+5 -1
View File
@@ -7,10 +7,14 @@
"phone"
],
"module_list": [
"//base/compileruntime/js_util_module/util:util_packages"
"//base/compileruntime/js_util_module/util:util_packages",
"//base/compileruntime/js_util_module/scope:scope_packages",
"//base/compileruntime/js_util_module/lrubuffer:lrubuffer_packages"
],
"inner_kits": [
],
"test_list": [
]
}
}
}
Executable
+59
View File
@@ -0,0 +1,59 @@
# 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/ohos.gni")
import("//build/ohos/ace/ace.gni")
base_output_path = get_label_info(":scope_js", "target_out_dir")
scope_js_obj_path = base_output_path + "/scope.o"
gen_js_obj("scope_js") {
input = "//base/compileruntime/js_util_module/scope/scope_js.js"
output = scope_js_obj_path
}
ohos_shared_library("scope") {
include_dirs = [
"//foundation/ace/napi",
"//foundation/ace/napi/native_engine",
"//third_party/icu/icu4c/source/common",
"//third_party/node/src",
"//foundation/ace/napi/interfaces/kits",
"//base/compileruntime/js_util_module/scope",
]
sources = [ "native_module_scope.cpp" ]
deps = [
":scope_js",
"//base/compileruntime/js_util_module/scope/:scope_js",
"//foundation/ace/napi/:ace_napi",
"//foundation/ace/napi/:ace_napi_quickjs",
"//third_party/icu/icu4c:static_icuuc",
"//utils/native/base:utils",
]
if (is_standard_system) {
external_deps = [ "hiviewdfx_hilog_native:libhilog" ]
} else {
external_deps = [ "hilog:libhilog" ]
}
subsystem_name = "ccruntime"
part_name = "jsapi_util"
relative_install_dir = "module"
}
group("scope_packages") {
deps = [ ":scope" ]
}
+65
View File
@@ -0,0 +1,65 @@
/*
* 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 "napi/native_api.h"
#include "napi/native_node_api.h"
#include "utils/log.h"
extern const char _binary_scope_js_js_start[];
extern const char _binary_scope_js_js_end[];
static napi_value ScopeInit(napi_env env, napi_value exports)
{
const char* ClassName = "scope";
napi_value scopeClass = nullptr;
NAPI_CALL(env, napi_define_class(env, ClassName, strlen(ClassName), nullptr,
nullptr, 0, nullptr,
&scopeClass));
static napi_property_descriptor desc[] = {
DECLARE_NAPI_PROPERTY("scope", scopeClass)
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
return exports;
}
// Scope module define
static napi_module scopeModule = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = nullptr,
.nm_register_func = ScopeInit,
.nm_modname = "scope",
.nm_priv = ((void*)0),
.reserved = {0},
};
// Scope module register
extern "C"
__attribute__((constructor)) void RegisterModule()
{
napi_module_register(&scopeModule);
}
// Scope JS register
extern "C"
__attribute__((visibility("default"))) void NAPI_scope_GetJSCode(const char** buf, int* buflen)
{
if (buf != nullptr) {
*buf = _binary_scope_js_js_start;
}
if (buflen != nullptr) {
*buflen = _binary_scope_js_js_end - _binary_scope_js_js_start;
}
}
+147
View File
@@ -0,0 +1,147 @@
/*
* 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.
*/
class Scope {
constructor(lowerObj, upperObj) {
this.lowerObj = lowerObj;
this.upperObj = upperObj;
this.checkNull(lowerObj, 'lower limit not be null');
this.checkNull(upperObj, 'upper limit not be null');
if (lowerObj.compareTo(upperObj)) {
throw new Error('lower limit must be less than or equal to upper limit');
}
this._lowerLimit = lowerObj;
this._upperLimit = upperObj;
}
getLower() {
return this._lowerLimit;
}
getUpper() {
return this._upperLimit;
}
contains(x) {
let resLower;
let resUpper;
this.checkNull(x, 'value must not be null');
if (x instanceof Scope) {
resLower = x._lowerLimit.compareTo(this._lowerLimit);
resUpper = this._upperLimit.compareTo(x._upperLimit);
} else {
resLower = x.compareTo(this._lowerLimit);
resUpper = this._upperLimit.compareTo(x);
}
return resLower && resUpper;
}
clamp(value) {
this.checkNull(value, 'value must not be null');
if (!value.compareTo(this._lowerLimit)) {
return this._lowerLimit;
} else if (value.compareTo(this._upperLimit)) {
return this._upperLimit;
} else {
return value;
}
}
intersect(x, y) {
let reLower;
let reUpper;
let mLower;
let mUpper;
if (y) {
this.checkNull(x, 'lower limit must not be null');
this.checkNull(y, 'upper limit must not be null');
reLower = this._lowerLimit.compareTo(x);
reUpper = y.compareTo(this._upperLimit);
if (reLower && reUpper) {
return this;
} else {
mLower = reLower ? this._lowerLimit : x;
mUpper = reUpper ? this._upperLimit : y;
return new Scope(mLower, mUpper);
}
} else {
this.checkNull(x, 'scope must not be null');
reLower = this._lowerLimit.compareTo(x._lowerLimit);
reUpper = x._upperLimit.compareTo(this._upperLimit);
if (!reLower && !reUpper) {
return x;
} else if (reLower && reUpper) {
return this;
} else {
mLower = reLower ? this._lowerLimit : x._lowerLimit;
mUpper = reUpper ? this._upperLimit : x._upperLimit;
return new Scope(mLower, mUpper);
}
}
}
expand(x, y) {
let reLower;
let reUpper;
let mLower;
let mUpper;
if (!y) {
this.checkNull(x, 'value must not be null');
if (!(x instanceof Scope)) {
this.checkNull(x, 'value must not be null');
return this.expand(x, x);
}
let reLower = x._lowerLimit.compareTo(this._lowerLimit);
let reUpper = this._upperLimit.compareTo(x._upperLimit);
if (reLower && reUpper) {
return this;
} else if (!reLower && !reUpper) {
return x;
} else {
let mLower = reLower ? this._lowerLimit : x._lowerLimit;
let mUpper = reUpper ? this._upperLimit : x._upperLimit;
return new Scope(mLower, mUpper);
}
}
else {
this.checkNull(x, 'lower limit must not be null');
this.checkNull(y, 'upper limit must not be null');
let reLower = x.compareTo(this._lowerLimit);
let reUpper = this._upperLimit.compareTo(y);
if (reLower && reUpper) {
return this;
}
let mLower = reLower ? this._lowerLimit : x;
let mUpper = reUpper ? this._upperLimit : y;
return new Scope(mLower, mUpper);
}
}
toString() {
let strLower = this._lowerLimit.toString();
let strUpper = this._upperLimit.toString();
return `[${strLower}, ${strUpper}]`;
}
checkNull(o, str) {
if (o == null) {
throw new Error(str);
}
}
}
export default {
Scope: Scope,
};