Add unit test suites (#3490)
This commit is contained in:
56
tests/unit/running-modes/CMakeLists.txt
Normal file
56
tests/unit/running-modes/CMakeLists.txt
Normal file
@ -0,0 +1,56 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
|
||||
project(test-running-modes)
|
||||
|
||||
# Compile wasm modules
|
||||
add_subdirectory(wasm-apps)
|
||||
|
||||
add_definitions(-DRUN_ON_LINUX)
|
||||
|
||||
set(WAMR_BUILD_LIBC_WASI 1)
|
||||
set(WAMR_BUILD_APP_FRAMEWORK 0)
|
||||
set(WAMR_BUILD_JIT 1)
|
||||
set(WAMR_BUILD_FAST_JIT 1)
|
||||
|
||||
# if only load this CMake other than load it as subdirectory
|
||||
include(../unit_common.cmake)
|
||||
|
||||
set(LLVM_SRC_ROOT "${WAMR_ROOT_DIR}/core/deps/llvm")
|
||||
|
||||
if (NOT EXISTS "${LLVM_SRC_ROOT}/build")
|
||||
message(FATAL_ERROR "Cannot find LLVM dir: ${LLVM_SRC_ROOT}/build")
|
||||
endif ()
|
||||
|
||||
set(CMAKE_PREFIX_PATH "${LLVM_SRC_ROOT}/build;${CMAKE_PREFIX_PATH}")
|
||||
find_package(LLVM REQUIRED CONFIG)
|
||||
include_directories(${LLVM_INCLUDE_DIRS})
|
||||
add_definitions(${LLVM_DEFINITIONS})
|
||||
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
|
||||
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
|
||||
|
||||
include(${IWASM_DIR}/compilation/iwasm_compl.cmake)
|
||||
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
file(GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)
|
||||
|
||||
set(UNIT_SOURCE ${source_all})
|
||||
|
||||
aux_source_directory(. SRC_LIST)
|
||||
|
||||
set(unit_test_sources
|
||||
${UNIT_SOURCE}
|
||||
${WAMR_RUNTIME_LIB_SOURCE}
|
||||
${UNCOMMON_SHARED_SOURCE}
|
||||
${SRC_LIST}
|
||||
)
|
||||
|
||||
# Now simply link against gtest or gtest_main as needed. Eg
|
||||
add_executable(wasm_running_modes_test ${unit_test_sources})
|
||||
|
||||
target_link_libraries(wasm_running_modes_test ${LLVM_AVAILABLE_LIBS} gtest_main)
|
||||
|
||||
gtest_discover_tests(wasm_running_modes_test)
|
||||
49
tests/unit/running-modes/wasm-apps/CMakeLists.txt
Normal file
49
tests/unit/running-modes/wasm-apps/CMakeLists.txt
Normal file
@ -0,0 +1,49 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
project(wasm-apps)
|
||||
|
||||
set(WAMR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../..)
|
||||
|
||||
set(CMAKE_SYSTEM_PROCESSOR wasm32)
|
||||
set(CMAKE_SYSROOT ${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot)
|
||||
|
||||
if (NOT DEFINED WASI_SDK_DIR)
|
||||
set(WASI_SDK_DIR "/opt/wasi-sdk")
|
||||
endif ()
|
||||
|
||||
set(CMAKE_C_FLAGS "-nostdlib -pthread -Qunused-arguments")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -z stack-size=8192 -nostdlib")
|
||||
set(CMAKE_C_COMPILER_TARGET "wasm32")
|
||||
set(CMAKE_C_COMPILER "${WASI_SDK_DIR}/bin/clang")
|
||||
|
||||
set(DEFINED_SYMBOLS
|
||||
"${WAMR_ROOT_DIR}/wamr-sdk/app/libc-builtin-sysroot/share/defined-symbols.txt")
|
||||
|
||||
set(CMAKE_EXE_LINKER_FLAGS
|
||||
"-Wl,--no-entry \
|
||||
-Wl,--initial-memory=65536 \
|
||||
-Wl,--export-all \
|
||||
-Wl,--allow-undefined"
|
||||
)
|
||||
|
||||
add_executable(mytest.wasm mytest.c)
|
||||
target_link_libraries(mytest.wasm)
|
||||
|
||||
add_executable(hello.wasm hello.c)
|
||||
target_link_libraries(hello.wasm)
|
||||
|
||||
add_custom_command(TARGET hello.wasm POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_BINARY_DIR}/hello.wasm
|
||||
${CMAKE_CURRENT_BINARY_DIR}/../
|
||||
COMMENT "Copy hello.wasm to the same directory of google test"
|
||||
)
|
||||
|
||||
add_custom_command(TARGET mytest.wasm POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${CMAKE_CURRENT_BINARY_DIR}/mytest.wasm
|
||||
${CMAKE_CURRENT_BINARY_DIR}/../
|
||||
COMMENT "Copy mytest.wasm to the same directory of google test"
|
||||
)
|
||||
31
tests/unit/running-modes/wasm-apps/hello.c
Normal file
31
tests/unit/running-modes/wasm-apps/hello.c
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
double
|
||||
foo(double d)
|
||||
{
|
||||
return d / 3.0;
|
||||
}
|
||||
|
||||
double
|
||||
maybe_min(double d, double e)
|
||||
{
|
||||
return d < e ? d : e;
|
||||
}
|
||||
|
||||
double
|
||||
factor(double a, double b, double c)
|
||||
{
|
||||
return (a * c) + (b * c);
|
||||
}
|
||||
|
||||
int
|
||||
echo(int a)
|
||||
{
|
||||
double b = foo(14.5);
|
||||
double c = maybe_min(12.2, 15.4);
|
||||
double d = factor(a, b, c);
|
||||
return 2 * a;
|
||||
}
|
||||
53
tests/unit/running-modes/wasm-apps/mytest.c
Normal file
53
tests/unit/running-modes/wasm-apps/mytest.c
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int
|
||||
recursive(int a)
|
||||
{
|
||||
if (a > 0) {
|
||||
return recursive(a - 1) + 1;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
testFunction(int *input, int length)
|
||||
{
|
||||
int sum = 0;
|
||||
for (int i = 0; i < length; ++i) {
|
||||
sum += input[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
|
||||
int arr[5] = { 1, 2, 3, 4, 5 };
|
||||
testFunction(arr, recursive(5));
|
||||
|
||||
char *buf;
|
||||
|
||||
printf("Hello world!\n");
|
||||
|
||||
buf = malloc(1024);
|
||||
if (!buf) {
|
||||
printf("malloc buf failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("buf ptr: %p\n", buf);
|
||||
|
||||
snprintf(buf, 1024, "%s", "1234\n");
|
||||
printf("buf: %s", buf);
|
||||
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
308
tests/unit/running-modes/wasm_running_modes_test.cc
Normal file
308
tests/unit/running-modes/wasm_running_modes_test.cc
Normal file
@ -0,0 +1,308 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "platform_common.h"
|
||||
#include "wasm_runtime_common.h"
|
||||
#include "bh_read_file.h"
|
||||
#include "wasm_runtime.h"
|
||||
#include "bh_platform.h"
|
||||
#include "wasm_export.h"
|
||||
#include "aot_runtime.h"
|
||||
|
||||
namespace {
|
||||
|
||||
std::string CWD;
|
||||
std::string TEST_WASM1 = "/hello.wasm";
|
||||
std::string TEST_WASM2 = "/mytest.wasm";
|
||||
char *WASM_FILE_1;
|
||||
char *WASM_FILE_2;
|
||||
std::vector<RunningMode> running_mode_supportted = { Mode_Interp,
|
||||
#if WASM_ENABLE_FAST_JIT != 0
|
||||
Mode_Fast_JIT,
|
||||
#endif
|
||||
#if WASM_ENABLE_JIT != 0
|
||||
Mode_LLVM_JIT,
|
||||
#endif
|
||||
#if WASM_ENABLE_JIT != 0 && WASM_ENABLE_FAST_JIT != 0 \
|
||||
&& WASM_ENABLE_LAZY_JIT != 0
|
||||
Mode_Multi_Tier_JIT
|
||||
#endif
|
||||
};
|
||||
|
||||
// To use a test fixture and Value Parameterized Tests,
|
||||
// derive a class from testing::TestWithParam.
|
||||
class wasm_running_modes_test_suite : public testing::TestWithParam<RunningMode>
|
||||
{
|
||||
private:
|
||||
std::string get_binary_path()
|
||||
{
|
||||
char cwd[1024];
|
||||
memset(cwd, 0, 1024);
|
||||
|
||||
if (readlink("/proc/self/exe", cwd, 1024) <= 0) {
|
||||
}
|
||||
|
||||
char *path_end = strrchr(cwd, '/');
|
||||
if (path_end != NULL) {
|
||||
*path_end = '\0';
|
||||
}
|
||||
|
||||
return std::string(cwd);
|
||||
}
|
||||
|
||||
bool load_wasm_file(const char *wasm_file)
|
||||
{
|
||||
const char *file;
|
||||
unsigned char *wasm_file_buf;
|
||||
uint32 wasm_file_size;
|
||||
|
||||
file = wasm_file;
|
||||
|
||||
wasm_file_buf =
|
||||
(unsigned char *)bh_read_file_to_buffer(file, &wasm_file_size);
|
||||
if (!wasm_file_buf)
|
||||
goto fail;
|
||||
|
||||
if (!(module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
printf("Load wasm module failed. error: %s\n", error_buf);
|
||||
goto fail;
|
||||
}
|
||||
return true;
|
||||
|
||||
fail:
|
||||
if (!module)
|
||||
wasm_runtime_unload(module);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool init_exec_env()
|
||||
{
|
||||
if (!(module_inst =
|
||||
wasm_runtime_instantiate(module, stack_size, heap_size,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
printf("Instantiate wasm module failed. error: %s\n", error_buf);
|
||||
goto fail;
|
||||
}
|
||||
if (!(exec_env =
|
||||
wasm_runtime_create_exec_env(module_inst, stack_size))) {
|
||||
printf("Create wasm execution environment failed.\n");
|
||||
goto fail;
|
||||
}
|
||||
return true;
|
||||
|
||||
fail:
|
||||
if (exec_env)
|
||||
wasm_runtime_destroy_exec_env(exec_env);
|
||||
if (module_inst)
|
||||
wasm_runtime_unload(module);
|
||||
return false;
|
||||
}
|
||||
|
||||
void destory_exec_env()
|
||||
{
|
||||
wasm_runtime_destroy_exec_env(exec_env);
|
||||
wasm_runtime_deinstantiate(module_inst);
|
||||
wasm_runtime_unload(module);
|
||||
}
|
||||
|
||||
protected:
|
||||
void run_wasm_basic(
|
||||
char *filename, bool in_default_running_mode,
|
||||
RunningMode running_mode = static_cast<RunningMode>(Mode_Default))
|
||||
{
|
||||
bool ret;
|
||||
uint32_t wasm_argv[2];
|
||||
ret = load_wasm_file(filename);
|
||||
ASSERT_TRUE(ret);
|
||||
ret = init_exec_env();
|
||||
ASSERT_TRUE(ret);
|
||||
|
||||
if (!in_default_running_mode) {
|
||||
ret = wasm_runtime_set_running_mode(module_inst, running_mode);
|
||||
ASSERT_TRUE(ret);
|
||||
ASSERT_EQ(running_mode, wasm_runtime_get_running_mode(module_inst));
|
||||
}
|
||||
|
||||
wasm_function_inst_t echo2xback_func =
|
||||
wasm_runtime_lookup_function(module_inst, "echo");
|
||||
ASSERT_TRUE(echo2xback_func != NULL);
|
||||
|
||||
wasm_argv[0] = 5;
|
||||
ret = wasm_runtime_call_wasm(exec_env, echo2xback_func, 1, wasm_argv);
|
||||
ASSERT_TRUE(ret);
|
||||
ASSERT_EQ(10, wasm_argv[0]);
|
||||
|
||||
destory_exec_env();
|
||||
}
|
||||
|
||||
void run_wasm_complex(char *filename1, char *filename2,
|
||||
RunningMode default_running_mode,
|
||||
RunningMode running_mode)
|
||||
{
|
||||
bool ret;
|
||||
uint32_t wasm_argv[2];
|
||||
|
||||
/* run wasm file 1 in default running mode */
|
||||
wasm_runtime_set_default_running_mode(default_running_mode);
|
||||
ret = load_wasm_file(filename1);
|
||||
ASSERT_TRUE(ret);
|
||||
ret = init_exec_env();
|
||||
ASSERT_TRUE(ret);
|
||||
|
||||
uint8_t *buffer, *buffer2;
|
||||
wasm_function_inst_t echo2xback_func, main;
|
||||
|
||||
ASSERT_EQ(default_running_mode,
|
||||
wasm_runtime_get_running_mode(module_inst));
|
||||
echo2xback_func = wasm_runtime_lookup_function(module_inst, "echo");
|
||||
ASSERT_TRUE(echo2xback_func != NULL);
|
||||
wasm_argv[0] = 5;
|
||||
ret = wasm_runtime_call_wasm(exec_env, echo2xback_func, 1, wasm_argv);
|
||||
ASSERT_TRUE(ret);
|
||||
ASSERT_EQ(10, wasm_argv[0]);
|
||||
|
||||
destory_exec_env();
|
||||
|
||||
/* run wasm file 2 in running_mode */
|
||||
ret = load_wasm_file(filename2);
|
||||
ASSERT_TRUE(ret);
|
||||
ret = init_exec_env();
|
||||
ASSERT_TRUE(ret);
|
||||
|
||||
ret = wasm_runtime_set_running_mode(module_inst, running_mode);
|
||||
ASSERT_TRUE(ret);
|
||||
ASSERT_EQ(running_mode, wasm_runtime_get_running_mode(module_inst));
|
||||
main = wasm_runtime_lookup_function(module_inst, "__main_argc_argv");
|
||||
ASSERT_TRUE(main != NULL);
|
||||
ret = wasm_runtime_call_wasm(exec_env, main, 2, wasm_argv);
|
||||
ASSERT_TRUE(ret);
|
||||
|
||||
destory_exec_env();
|
||||
}
|
||||
|
||||
public:
|
||||
// If your test fixture defines SetUpTestSuite() or TearDownTestSuite()
|
||||
// they must be declared public rather than protected in order to use
|
||||
// TEST_P.
|
||||
|
||||
// virtual void SetUp() will be called before each test is run. You
|
||||
// should define it if you need to initialize the varaibles.
|
||||
// Otherwise, this can be skipped.
|
||||
virtual void SetUp()
|
||||
{
|
||||
CWD = get_binary_path();
|
||||
WASM_FILE_1 = strdup((CWD + TEST_WASM1).c_str());
|
||||
WASM_FILE_2 = strdup((CWD + TEST_WASM2).c_str());
|
||||
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
init_args.mem_alloc_type = Alloc_With_Pool;
|
||||
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
|
||||
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
|
||||
|
||||
ASSERT_EQ(wasm_runtime_full_init(&init_args), true);
|
||||
|
||||
cleanup = true;
|
||||
}
|
||||
|
||||
static void SetUpTestCase() {}
|
||||
|
||||
// virtual void TearDown() will be called after each test is run.
|
||||
// You should define it if there is cleanup work to do. Otherwise,
|
||||
// you don't have to provide it.
|
||||
//
|
||||
virtual void TearDown()
|
||||
{
|
||||
if (cleanup) {
|
||||
wasm_runtime_destroy();
|
||||
cleanup = false;
|
||||
}
|
||||
free(WASM_FILE_1);
|
||||
free(WASM_FILE_2);
|
||||
}
|
||||
|
||||
static void TearDownTestCase() {}
|
||||
|
||||
std::string CWD;
|
||||
RuntimeInitArgs init_args;
|
||||
wasm_module_t module = NULL;
|
||||
wasm_module_inst_t module_inst = NULL;
|
||||
wasm_exec_env_t exec_env = NULL;
|
||||
char error_buf[128];
|
||||
char global_heap_buf[512 * 1024];
|
||||
uint32_t stack_size = 8092, heap_size = 8092;
|
||||
bool cleanup = true;
|
||||
};
|
||||
|
||||
TEST_F(wasm_running_modes_test_suite, wasm_runtime_is_running_mode_supported)
|
||||
{
|
||||
// normal situation
|
||||
ASSERT_EQ(true, wasm_runtime_is_running_mode_supported(
|
||||
static_cast<RunningMode>(Mode_Default)));
|
||||
for (auto running_mode : running_mode_supportted) {
|
||||
ASSERT_EQ(true, wasm_runtime_is_running_mode_supported(running_mode));
|
||||
}
|
||||
|
||||
// abnormal situation
|
||||
ASSERT_EQ(false, wasm_runtime_is_running_mode_supported(
|
||||
static_cast<RunningMode>(-1)));
|
||||
ASSERT_EQ(false, wasm_runtime_is_running_mode_supported(
|
||||
static_cast<RunningMode>(5)));
|
||||
ASSERT_EQ(false, wasm_runtime_is_running_mode_supported(
|
||||
static_cast<RunningMode>(0xFF)));
|
||||
}
|
||||
|
||||
TEST_F(wasm_running_modes_test_suite, wasm_runtime_set_default_running_mode)
|
||||
{
|
||||
// normal situation: only set up
|
||||
ASSERT_EQ(true, wasm_runtime_set_default_running_mode(
|
||||
static_cast<RunningMode>(Mode_Default)));
|
||||
for (auto running_mode : running_mode_supportted) {
|
||||
ASSERT_EQ(true, wasm_runtime_set_default_running_mode(running_mode));
|
||||
}
|
||||
|
||||
// abnormal situation
|
||||
ASSERT_EQ(false, wasm_runtime_set_default_running_mode(
|
||||
static_cast<RunningMode>(-1)));
|
||||
ASSERT_EQ(false, wasm_runtime_set_default_running_mode(
|
||||
static_cast<RunningMode>(5)));
|
||||
ASSERT_EQ(false, wasm_runtime_set_default_running_mode(
|
||||
static_cast<RunningMode>(0xFF)));
|
||||
}
|
||||
|
||||
TEST_P(wasm_running_modes_test_suite,
|
||||
wasm_runtime_set_default_running_mode_basic)
|
||||
{
|
||||
RunningMode running_mode = GetParam();
|
||||
ASSERT_EQ(true, wasm_runtime_set_default_running_mode(running_mode));
|
||||
run_wasm_basic(WASM_FILE_1, true);
|
||||
}
|
||||
|
||||
TEST_P(wasm_running_modes_test_suite,
|
||||
wasm_runtime_set_and_get_running_mode_basic)
|
||||
{
|
||||
RunningMode running_mode = GetParam();
|
||||
run_wasm_basic(WASM_FILE_1, false, running_mode);
|
||||
}
|
||||
|
||||
TEST_P(wasm_running_modes_test_suite,
|
||||
wasm_runtime_set_and_get_running_mode_complex)
|
||||
{
|
||||
RunningMode default_running_mode = GetParam();
|
||||
for (auto running_mode : running_mode_supportted) {
|
||||
run_wasm_complex(WASM_FILE_1, WASM_FILE_2, default_running_mode,
|
||||
running_mode);
|
||||
}
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(RunningMode, wasm_running_modes_test_suite,
|
||||
testing::ValuesIn(running_mode_supportted));
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user