!9022 添加单元测试demo

Merge pull request !9022 from jialChen/ets_runtime_branch
This commit is contained in:
openharmony_ci 2024-09-19 14:02:24 +00:00 committed by Gitee
commit dcac0f0700
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 157 additions and 29 deletions

View File

@ -138,9 +138,10 @@ group("ark_unittest") {
deps = []
# js unittest
deps += [
deps += [ # Register the executable block name of the unit test script here
"ecmascript/base/tests:host_unittest",
"ecmascript/builtins/tests:host_unittest",
"ecmascript/compiler/codegen/maple/test:host_unittest",
"ecmascript/containers/tests:host_unittest",
"ecmascript/dfx/cpu_profiler/tests:host_unittest",
"ecmascript/dfx/hprof/tests:host_unittest",

View File

@ -11,40 +11,57 @@
# See the License for the specific language governing permissions and
# limitations under the License.
configs = [ "${MAPLEALL_ROOT}:mapleallcompilecfg" ]
import("//arkcompiler/ets_runtime/js_runtime_config.gni")
import("//arkcompiler/ets_runtime/test/test_helper.gni")
cflags_cc -= [ "-Werror" ]
module_output_path = "arkcompiler/ets_runtime"
include_directories = [
"${MAPLE_ROOT}/tools/gtest_lib/include",
"${MAPLEALL_ROOT}/maple_util/include",
"${MAPLEALL_ROOT}/maple_driver/include",
]
src_mapleallUT = [
"cl_ut_test.cpp",
"triple_ut_test.cpp",
]
executable("mapleallUT") {
host_unittest_action("mapleallUT") {
rtti_compile_flag = false
configs = [ "${MAPLEALL_ROOT}:mapleallcompilecfg" ]
module_out_path = module_output_path
include_directories = [
"${MAPLEALL_ROOT}/maple_be/include/cg",
"${MAPLEALL_ROOT}/maple_be/include/litecg",
"${MAPLEALL_ROOT}/maple_be/include/ad",
"${MAPLEALL_ROOT}/maple_be/include/be",
"${MAPLEALL_ROOT}/maple_ir/include/",
"${MAPLEALL_ROOT}/maple_util/include/",
"${MAPLEALL_ROOT}/mempool/include/",
"${MAPLEALL_THIRD_PARTY_ROOT}/bounds_checking_function/include/",
"${MAPLEALL_ROOT}/maple_driver/include/",
"${MAPLEALL_ROOT}/maple_me/include/",
"${MAPLEALL_ROOT}/maple_phase/include/",
"${MAPLEALL_ROOT}/mpl2mpl/include/",
"${MAPLEALL_THIRD_PARTY_ROOT}/",
]
src_mapleallUT = [ "cgbb_test.cpp" ]
sources = src_mapleallUT
include_dirs = include_directories
output_dir = "${root_out_dir}/lib/${HOST_ARCH}"
deps = [
"${MAPLEALL_ROOT}/maple_driver:libmaple_driver",
"${MAPLEALL_ROOT}/maple_ir:libmplir",
"${MAPLEALL_ROOT}/maple_util:libcommandline",
"${MAPLEALL_ROOT}/maple_util:libmplutil",
deps_libcg = [ # Add Link Library
"${MAPLEALL_ROOT}/mempool:libmempool",
"${MAPLEALL_ROOT}/maple_phase:libmplphase",
"${MAPLEALL_ROOT}/mpl2mpl:libmpl2mpl",
"${MAPLEALL_THIRD_PARTY_ROOT}/bounds_checking_function:libsec_static",
"${MAPLEALL_ROOT}/maple_ir:libmplir",
"${MAPLEALL_ROOT}/maple_util:libmplutil",
"${MAPLEALL_ROOT}/maple_me:libmplme",
"${MAPLEALL_ROOT}/maple_be:libcg",
]
libs = [
"${MAPLE_ROOT}/tools/gtest_lib/lib/libgmock.a",
"${MAPLE_ROOT}/tools/gtest_lib/lib/libgmock_main.a",
"${MAPLE_ROOT}/tools/gtest_lib/lib/libgtest.a",
"${MAPLE_ROOT}/tools/gtest_lib/lib/libgtest_main.a",
"pthread",
]
deps = []
# If dynamic cast is used in the source code, set COMPILE_RTTI_ to true
rtti_compile_flag = true
deps += deps_libcg
libs = [ "pthread" ]
}
group("host_unittest") {
testonly = true
# deps file
deps = [ ":mapleallUTAction" ]
if (is_mac) {
deps -= [ ":mapleallUTAction" ]
}
}

View File

@ -0,0 +1,110 @@
/*
* Copyright (c) 2024 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 "gtest/gtest.h"
#include <iostream>
#include <unistd.h>
#include <climits>
#include "cgbb.h"
#include "mempool.h"
#include "cgfunc.h"
using namespace maplebe;
using namespace std;
maplebe::Insn CreateInsnObj(std::string poolName, maple::uint32 opc)
{
MemPoolCtrler memPoolCtrler;
MemPool memPool(memPoolCtrler, poolName);
maplebe::Insn Insn_obj(memPool, opc);
return Insn_obj;
}
maplebe::BB CreateBBObj(uint32 bbID, std::string poolName)
{
// BBID bbID, MapleAllocator &mallocator
MemPoolCtrler memPoolCtrler;
MemPool memPool(memPoolCtrler, poolName);
MapleAllocator mallocator(&memPool);
maplebe::BB BB_obj(bbID, mallocator);
return BB_obj;
}
TEST(InsertInsnBefore_FUNC, t01)
{
maplebe::BB test_bb = CreateBBObj(10, "BB_pool");
maplebe::Insn existing = CreateInsnObj("existing", 32);
maplebe::Insn newInsn = CreateInsnObj("newInsn", 3);
/*Test Function: Insn *BB::InsertInsnBefore(Insn &existing, Insn &newInsn)*/
test_bb.SetFirstInsn(&existing);
EXPECT_EQ(existing.GetPrev(), nullptr);
maplebe::Insn *ans = test_bb.InsertInsnBefore(existing, newInsn);
EXPECT_EQ(ans, &newInsn);
maplebe::Insn newInsn1 = CreateInsnObj("newInsn1", 3);
EXPECT_NE(existing.GetPrev(), nullptr);
ans = test_bb.InsertInsnBefore(existing, newInsn1);
EXPECT_EQ(ans, &newInsn1);
EXPECT_EQ(newInsn.GetNext(), &newInsn1);
}
TEST(InsertInsnAfter_FUNC, t02)
{
/*Test Function: Insn *BB::InsertInsnAfter(Insn &existing, Insn &newInsn)*/
maplebe::BB test_bb = CreateBBObj(2, "BB_pool");
maplebe::Insn existing = CreateInsnObj("existing", 32);
maplebe::Insn newInsn1 = CreateInsnObj("newInsn1", 1), newInsn2 = CreateInsnObj("newInsn2", 2);
maplebe::Insn *ans;
test_bb.SetFirstInsn(&existing);
ans = test_bb.InsertInsnAfter(existing, newInsn1);
EXPECT_EQ(ans, &newInsn1);
EXPECT_EQ(newInsn1.GetNext(), nullptr);
ans = test_bb.InsertInsnAfter(existing, newInsn2);
EXPECT_EQ(ans, &newInsn2);
EXPECT_EQ(newInsn2.GetNext(), &newInsn1);
}
TEST(ReplaceInsn_FUNC, t03)
{
/*Test Function: void BB::ReplaceInsn(Insn &insn, Insn &newInsn)*/
maplebe::BB test_bb = CreateBBObj(3, "BB_pool");
maplebe::Insn insn = CreateInsnObj("insn", 3);
maplebe::Insn newInsn = CreateInsnObj("newInsn", 3);
std::cout << "insn.IsAccessRefField() = " << insn.IsAccessRefField() << std::endl;
}
TEST(RemoveInsn_FUNC, t04)
{
/*Test Function: void BB::RemoveInsn(Insn &insn)*/
maplebe::BB test_bb = CreateBBObj(10, "BB_pool");
maplebe::Insn insn = CreateInsnObj("insn", 0);
maplebe::Insn insn1 = CreateInsnObj("insn1", 1);
maplebe::Insn insn2 = CreateInsnObj("insn2", 2);
test_bb.SetFirstInsn(&insn);
test_bb.RemoveInsn(insn);
EXPECT_EQ(test_bb.GetFirstInsn(), nullptr);
test_bb.SetFirstInsn(&insn);
test_bb.InsertInsnAfter(insn, insn1);
test_bb.InsertInsnAfter(insn1, insn2);
test_bb.RemoveInsn(insn);
EXPECT_EQ(test_bb.GetFirstInsn(), &insn1);
EXPECT_EQ((*(insn.GetNext())).GetPrev(), nullptr);
}