Add unit test suites (#3490)
This commit is contained in:
30
tests/unit/shared-utils/CMakeLists.txt
Normal file
30
tests/unit/shared-utils/CMakeLists.txt
Normal file
@ -0,0 +1,30 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
cmake_minimum_required(VERSION 2.9)
|
||||
|
||||
project (test-shared-utils)
|
||||
|
||||
add_definitions (-DRUN_ON_LINUX)
|
||||
|
||||
set (WAMR_BUILD_LIBC_WASI 0)
|
||||
set (WAMR_BUILD_APP_FRAMEWORK 0)
|
||||
|
||||
include (../unit_common.cmake)
|
||||
|
||||
include_directories (${CMAKE_CURRENT_SOURCE_DIR})
|
||||
|
||||
file (GLOB_RECURSE source_all ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)
|
||||
|
||||
set (UNIT_SOURCE ${source_all})
|
||||
|
||||
set (unit_test_sources
|
||||
${UNIT_SOURCE}
|
||||
${WAMR_RUNTIME_LIB_SOURCE}
|
||||
)
|
||||
|
||||
add_executable (shared_utils_test ${unit_test_sources})
|
||||
|
||||
target_link_libraries (shared_utils_test gtest_main)
|
||||
|
||||
gtest_discover_tests(shared_utils_test)
|
||||
38
tests/unit/shared-utils/bh_assert_test.cc
Normal file
38
tests/unit/shared-utils/bh_assert_test.cc
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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 "bh_assert.h"
|
||||
|
||||
class bh_assert_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// 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() {}
|
||||
|
||||
// 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() {}
|
||||
};
|
||||
|
||||
TEST_F(bh_assert_test_suite, bh_assert_internal)
|
||||
{
|
||||
bh_assert_internal(6, "file_name_test", 6, "expr_string_test");
|
||||
|
||||
// Test abnormal cases.
|
||||
EXPECT_DEATH(bh_assert_internal(0, "file_name_test", 1, "expr_string_test"),
|
||||
"");
|
||||
EXPECT_DEATH(bh_assert_internal(0, nullptr, 2, "expr_string_test"), "");
|
||||
EXPECT_DEATH(bh_assert_internal(0, "file_name_test", 3, nullptr), "");
|
||||
}
|
||||
96
tests/unit/shared-utils/bh_common_test.cc
Normal file
96
tests/unit/shared-utils/bh_common_test.cc
Normal file
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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 "bh_common.h"
|
||||
|
||||
class bh_common_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// 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() {}
|
||||
|
||||
// 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() {}
|
||||
|
||||
public:
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
#define STR_TEST "test"
|
||||
|
||||
TEST_F(bh_common_test_suite, wa_strdup)
|
||||
{
|
||||
EXPECT_EQ(nullptr, wa_strdup(nullptr));
|
||||
EXPECT_NE(nullptr, wa_strdup(STR_TEST));
|
||||
}
|
||||
|
||||
TEST_F(bh_common_test_suite, b_strcpy_s)
|
||||
{
|
||||
char dest[10] = { 0 };
|
||||
|
||||
EXPECT_EQ(0, b_strcpy_s(dest, sizeof(dest), STR_TEST));
|
||||
|
||||
// Test abnormal cases.
|
||||
EXPECT_EQ(-1, b_strcpy_s(nullptr, 0, nullptr));
|
||||
EXPECT_EQ(-1, b_strcpy_s(dest, sizeof(dest), nullptr));
|
||||
EXPECT_EQ(-1, b_strcpy_s(dest, 0, STR_TEST));
|
||||
}
|
||||
|
||||
TEST_F(bh_common_test_suite, b_strcat_s)
|
||||
{
|
||||
char dest[10] = { 0 };
|
||||
|
||||
EXPECT_EQ(0, b_strcat_s(dest, sizeof(dest), STR_TEST));
|
||||
|
||||
// Test abnormal cases.
|
||||
EXPECT_EQ(-1, b_strcat_s(nullptr, 0, nullptr));
|
||||
EXPECT_EQ(-1, b_strcat_s(dest, sizeof(dest), nullptr));
|
||||
EXPECT_EQ(-1, b_strcat_s(dest, 0, STR_TEST));
|
||||
}
|
||||
|
||||
TEST_F(bh_common_test_suite, bh_strdup)
|
||||
{
|
||||
EXPECT_NE(nullptr, bh_strdup(STR_TEST));
|
||||
EXPECT_EQ(nullptr, bh_strdup(nullptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_common_test_suite, b_memmove_s)
|
||||
{
|
||||
char dest[10] = { 0 };
|
||||
|
||||
EXPECT_EQ(0, b_memmove_s(dest, sizeof(dest), STR_TEST, sizeof(STR_TEST)));
|
||||
|
||||
// Test abnormal cases.
|
||||
EXPECT_EQ(0, b_memmove_s(dest, sizeof(dest), STR_TEST, 0));
|
||||
EXPECT_EQ(0, b_memmove_s(nullptr, sizeof(dest), STR_TEST, 0));
|
||||
|
||||
EXPECT_EQ(0, b_memmove_s(dest, sizeof(dest), nullptr, 0));
|
||||
EXPECT_EQ(-1, b_memmove_s(dest, sizeof(dest), STR_TEST, sizeof(dest) + 1));
|
||||
}
|
||||
|
||||
TEST_F(bh_common_test_suite, b_memcpy_s)
|
||||
{
|
||||
char dest[10] = { 0 };
|
||||
|
||||
EXPECT_EQ(0, b_memcpy_s(dest, sizeof(dest), STR_TEST, sizeof(STR_TEST)));
|
||||
|
||||
// Test abnormal cases.
|
||||
EXPECT_EQ(0, b_memcpy_s(dest, sizeof(dest), STR_TEST, 0));
|
||||
EXPECT_EQ(-1,
|
||||
b_memcpy_s(nullptr, sizeof(dest), STR_TEST, sizeof(STR_TEST)));
|
||||
EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), nullptr, sizeof(STR_TEST)));
|
||||
EXPECT_EQ(-1, b_memcpy_s(dest, sizeof(dest), STR_TEST, sizeof(dest) + 1));
|
||||
}
|
||||
362
tests/unit/shared-utils/bh_hashmap_test.cc
Normal file
362
tests/unit/shared-utils/bh_hashmap_test.cc
Normal file
@ -0,0 +1,362 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "bh_hashmap.h"
|
||||
#include "wasm.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
#include <future>
|
||||
|
||||
typedef struct HashMapElem {
|
||||
void *key;
|
||||
void *value;
|
||||
struct HashMapElem *next;
|
||||
} HashMapElem;
|
||||
|
||||
struct HashMap {
|
||||
/* size of element array */
|
||||
uint32 size;
|
||||
/* lock for elements */
|
||||
korp_mutex *lock;
|
||||
/* hash function of key */
|
||||
HashFunc hash_func;
|
||||
/* key equal function */
|
||||
KeyEqualFunc key_equal_func;
|
||||
KeyDestroyFunc key_destroy_func;
|
||||
ValueDestroyFunc value_destroy_func;
|
||||
HashMapElem *elements[1];
|
||||
};
|
||||
|
||||
int DESTROY_NUM = 0;
|
||||
char TRAVERSE_KEY[] = "key_1";
|
||||
char TRAVERSE_VAL[] = "val_1";
|
||||
int TRAVERSE_COMP_RES = 0;
|
||||
|
||||
class bh_hashmap_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// 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() {}
|
||||
|
||||
// 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() {}
|
||||
|
||||
public:
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(bh_hashmap_test_suite, bh_hash_map_create)
|
||||
{
|
||||
// Normally.
|
||||
EXPECT_NE((HashMap *)nullptr,
|
||||
bh_hash_map_create(32, true, (HashFunc)wasm_string_hash,
|
||||
(KeyEqualFunc)wasm_string_equal, nullptr,
|
||||
wasm_runtime_free));
|
||||
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ((HashMap *)nullptr,
|
||||
bh_hash_map_create(65537, true, (HashFunc)wasm_string_hash,
|
||||
(KeyEqualFunc)wasm_string_equal, nullptr,
|
||||
wasm_runtime_free));
|
||||
EXPECT_EQ((HashMap *)nullptr,
|
||||
bh_hash_map_create(65536, true, nullptr, nullptr, nullptr,
|
||||
wasm_runtime_free));
|
||||
EXPECT_EQ((HashMap *)nullptr,
|
||||
bh_hash_map_create(65536, true, (HashFunc)wasm_string_hash,
|
||||
nullptr, nullptr, wasm_runtime_free));
|
||||
}
|
||||
|
||||
TEST_F(bh_hashmap_test_suite, bh_hash_map_insert)
|
||||
{
|
||||
HashMap *test_hash_map = bh_hash_map_create(
|
||||
32, false, (HashFunc)wasm_string_hash, (KeyEqualFunc)wasm_string_equal,
|
||||
nullptr, wasm_runtime_free);
|
||||
int num = 0;
|
||||
void **p_old_key = nullptr;
|
||||
void **p_old_value = nullptr;
|
||||
|
||||
// Normally.
|
||||
EXPECT_EQ(true, bh_hash_map_insert(test_hash_map, (void *)"key_1",
|
||||
(void *)"val_1"));
|
||||
num++;
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ(false, bh_hash_map_insert(nullptr, nullptr, (void *)"val_2"));
|
||||
|
||||
// Execute fail: more than 32.
|
||||
for (; num <= 32; num++) {
|
||||
bh_hash_map_insert(test_hash_map, (void *)&num, (void *)"val");
|
||||
}
|
||||
EXPECT_EQ(false,
|
||||
bh_hash_map_insert(test_hash_map, (void *)&num, (void *)"val"));
|
||||
|
||||
// Remove one, insert one.
|
||||
bh_hash_map_remove(test_hash_map, (void *)"key_1", p_old_key, p_old_value);
|
||||
EXPECT_EQ(true, bh_hash_map_insert(test_hash_map, (void *)"key_1",
|
||||
(void *)"val_1"));
|
||||
}
|
||||
|
||||
TEST_F(bh_hashmap_test_suite, bh_hash_map_find)
|
||||
{
|
||||
HashMap *test_hash_map = bh_hash_map_create(
|
||||
32, false, (HashFunc)wasm_string_hash, (KeyEqualFunc)wasm_string_equal,
|
||||
nullptr, wasm_runtime_free);
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_1", (void *)"val_1");
|
||||
|
||||
// Normally. use_lock is false.
|
||||
EXPECT_NE((void *)nullptr,
|
||||
bh_hash_map_find(test_hash_map, (void *)"key_1"));
|
||||
|
||||
// Execute fail.
|
||||
EXPECT_EQ((void *)nullptr,
|
||||
bh_hash_map_find(test_hash_map, (void *)"KEY_1"));
|
||||
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ((void *)nullptr, bh_hash_map_find(nullptr, nullptr));
|
||||
EXPECT_EQ((void *)nullptr, bh_hash_map_find(test_hash_map, nullptr));
|
||||
|
||||
// Normally. use_lock is true.
|
||||
test_hash_map = bh_hash_map_create(32, true, (HashFunc)wasm_string_hash,
|
||||
(KeyEqualFunc)wasm_string_equal, nullptr,
|
||||
wasm_runtime_free);
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_1", (void *)"val_1");
|
||||
EXPECT_EQ((void *)nullptr,
|
||||
bh_hash_map_find(test_hash_map, (void *)"KEY_1"));
|
||||
}
|
||||
|
||||
TEST_F(bh_hashmap_test_suite, bh_hash_map_update)
|
||||
{
|
||||
char old_value[10] = { 0 };
|
||||
void **p_old_value = (void **)(&old_value);
|
||||
HashMap *test_hash_map = bh_hash_map_create(
|
||||
32, false, (HashFunc)wasm_string_hash, (KeyEqualFunc)wasm_string_equal,
|
||||
nullptr, wasm_runtime_free);
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_1", (void *)"val_1");
|
||||
|
||||
// test_hash_map->lock == nullptr. Normally.
|
||||
EXPECT_EQ(true, bh_hash_map_update(test_hash_map, (void *)"key_1",
|
||||
(void *)"val_2", p_old_value));
|
||||
// test_hash_map->lock == nullptr. Illegal parameters.
|
||||
EXPECT_EQ(false, bh_hash_map_update(nullptr, nullptr, (void *)"val_2",
|
||||
p_old_value));
|
||||
EXPECT_EQ(false, bh_hash_map_update(test_hash_map, nullptr, (void *)"val_2",
|
||||
p_old_value));
|
||||
EXPECT_EQ(false,
|
||||
bh_hash_map_update(nullptr, nullptr, (void *)"val_2", nullptr));
|
||||
|
||||
// test_hash_map->lock == nullptr. Update non-existent elements.
|
||||
EXPECT_EQ(false, bh_hash_map_update(test_hash_map, (void *)"key",
|
||||
(void *)"val", p_old_value));
|
||||
|
||||
test_hash_map = bh_hash_map_create(32, true, (HashFunc)wasm_string_hash,
|
||||
(KeyEqualFunc)wasm_string_equal, nullptr,
|
||||
wasm_runtime_free);
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_1", (void *)"val_1");
|
||||
|
||||
// test_hash_map->lock == no nullptr. Normally.
|
||||
EXPECT_EQ(true, bh_hash_map_update(test_hash_map, (void *)"key_1",
|
||||
(void *)"val_2", p_old_value));
|
||||
// test_hash_map->lock == no nullptr. Illegal parameters.
|
||||
EXPECT_EQ(false, bh_hash_map_update(nullptr, nullptr, (void *)"val_2",
|
||||
p_old_value));
|
||||
EXPECT_EQ(false, bh_hash_map_update(test_hash_map, nullptr, (void *)"val_2",
|
||||
p_old_value));
|
||||
}
|
||||
|
||||
void
|
||||
trav_callback_fun(void *key, void *value, void *user_data)
|
||||
{
|
||||
if (!strncmp(TRAVERSE_VAL, (const char *)value, 5)) {
|
||||
TRAVERSE_COMP_RES = 1;
|
||||
}
|
||||
else {
|
||||
TRAVERSE_COMP_RES = 0;
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(bh_hashmap_test_suite, bh_hash_map_traverse)
|
||||
{
|
||||
void **p_old_value = nullptr;
|
||||
HashMap *test_hash_map = bh_hash_map_create(
|
||||
32, false, (HashFunc)wasm_string_hash, (KeyEqualFunc)wasm_string_equal,
|
||||
nullptr, wasm_runtime_free);
|
||||
|
||||
// Normally: TRAVERSE_COMP_RES = 1.
|
||||
bh_hash_map_insert(test_hash_map, (void *)TRAVERSE_KEY,
|
||||
(void *)TRAVERSE_VAL);
|
||||
EXPECT_EQ(true,
|
||||
bh_hash_map_traverse(test_hash_map, trav_callback_fun, nullptr));
|
||||
EXPECT_EQ(1, TRAVERSE_COMP_RES);
|
||||
|
||||
// Normally: TRAVERSE_COMP_RES = 0.
|
||||
bh_hash_map_update(test_hash_map, (void *)TRAVERSE_KEY, (void *)"val",
|
||||
p_old_value);
|
||||
EXPECT_EQ(true,
|
||||
bh_hash_map_traverse(test_hash_map, trav_callback_fun, nullptr));
|
||||
EXPECT_EQ(0, TRAVERSE_COMP_RES);
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ(false, bh_hash_map_traverse(nullptr, trav_callback_fun, nullptr));
|
||||
EXPECT_EQ(false, bh_hash_map_traverse(test_hash_map, nullptr, nullptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_hashmap_test_suite, bh_hash_map_remove)
|
||||
{
|
||||
void **p_old_key = nullptr;
|
||||
void **p_old_value = nullptr;
|
||||
|
||||
HashMap *test_hash_map = bh_hash_map_create(
|
||||
32, false, (HashFunc)wasm_string_hash, (KeyEqualFunc)wasm_string_equal,
|
||||
nullptr, wasm_runtime_free);
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_1", (void *)"val_1");
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_2", (void *)"val_2");
|
||||
|
||||
// test_hash_map->lock == nullptr. Normally.
|
||||
EXPECT_EQ(true, bh_hash_map_remove(test_hash_map, (void *)"key_1",
|
||||
p_old_key, p_old_value));
|
||||
// test_hash_map->lock == nullptr. Remove non-existent elements.
|
||||
EXPECT_EQ(false, bh_hash_map_remove(test_hash_map, (void *)"key_1",
|
||||
p_old_key, p_old_value));
|
||||
// test_hash_map->lock == nullptr. Illegal parameters.
|
||||
EXPECT_EQ(false, bh_hash_map_remove(nullptr, (void *)"key_2", p_old_key,
|
||||
p_old_value));
|
||||
EXPECT_EQ(false, bh_hash_map_remove(test_hash_map, nullptr, p_old_key,
|
||||
p_old_value));
|
||||
|
||||
test_hash_map = bh_hash_map_create(32, true, (HashFunc)wasm_string_hash,
|
||||
(KeyEqualFunc)wasm_string_equal, nullptr,
|
||||
wasm_runtime_free);
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_1", (void *)"val_1");
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_2", (void *)"val_2");
|
||||
|
||||
// test_hash_map->lock == no nullptr. Normally.
|
||||
EXPECT_EQ(true, bh_hash_map_remove(test_hash_map, (void *)"key_1",
|
||||
p_old_key, p_old_value));
|
||||
// test_hash_map->lock == no nullptr. Illegal parameters.
|
||||
EXPECT_EQ(false, bh_hash_map_remove(nullptr, (void *)"key_2", p_old_key,
|
||||
p_old_value));
|
||||
}
|
||||
|
||||
TEST_F(bh_hashmap_test_suite, bh_hash_map_get_struct_size)
|
||||
{
|
||||
HashMap *test_hash_map = nullptr;
|
||||
uint32 size = 0;
|
||||
|
||||
// No lock.
|
||||
test_hash_map = bh_hash_map_create(32, false, (HashFunc)wasm_string_hash,
|
||||
(KeyEqualFunc)wasm_string_equal, nullptr,
|
||||
wasm_runtime_free);
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_1", (void *)"val_1");
|
||||
size = (size_t)(&((HashMap *)0)->elements)
|
||||
+ (uint32)sizeof(HashMapElem *) * test_hash_map->size;
|
||||
EXPECT_EQ(size, bh_hash_map_get_struct_size(test_hash_map));
|
||||
|
||||
// Has lock.
|
||||
test_hash_map = bh_hash_map_create(32, true, (HashFunc)wasm_string_hash,
|
||||
(KeyEqualFunc)wasm_string_equal, nullptr,
|
||||
wasm_runtime_free);
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_1", (void *)"val_1");
|
||||
size = (size_t)(&((HashMap *)0)->elements)
|
||||
+ (uint32)sizeof(HashMapElem *) * test_hash_map->size;
|
||||
size += (uint32)sizeof(korp_mutex);
|
||||
EXPECT_EQ(size, bh_hash_map_get_struct_size(test_hash_map));
|
||||
}
|
||||
|
||||
TEST_F(bh_hashmap_test_suite, bh_hash_map_get_elem_struct_size)
|
||||
{
|
||||
EXPECT_EQ((uint32)sizeof(HashMapElem), bh_hash_map_get_elem_struct_size());
|
||||
}
|
||||
|
||||
void
|
||||
destroy_func_test(void *key)
|
||||
{
|
||||
DESTROY_NUM++;
|
||||
}
|
||||
|
||||
TEST_F(bh_hashmap_test_suite, bh_hash_map_destroy)
|
||||
{
|
||||
HashMap *test_hash_map = bh_hash_map_create(
|
||||
32, true, (HashFunc)wasm_string_hash, (KeyEqualFunc)wasm_string_equal,
|
||||
destroy_func_test, wasm_runtime_free);
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_1", (void *)"val_1");
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_2", (void *)"val_2");
|
||||
|
||||
// test_hash_map->lock == no nullptr. Normally.
|
||||
EXPECT_EQ(true, bh_hash_map_destroy(test_hash_map));
|
||||
// key_destroy_func must be called 2 times.
|
||||
EXPECT_EQ(2, DESTROY_NUM);
|
||||
|
||||
test_hash_map = bh_hash_map_create(32, false, (HashFunc)wasm_string_hash,
|
||||
(KeyEqualFunc)wasm_string_equal,
|
||||
destroy_func_test, wasm_runtime_free);
|
||||
|
||||
// test_hash_map->lock == no nullptr. Illegal parameters.
|
||||
EXPECT_EQ(false, bh_hash_map_destroy(nullptr));
|
||||
// test_hash_map->lock == nullptr.
|
||||
EXPECT_EQ(true, bh_hash_map_destroy(test_hash_map));
|
||||
|
||||
// key_destroy_func and value_destroy_func is nullptr.
|
||||
test_hash_map =
|
||||
bh_hash_map_create(32, false, (HashFunc)wasm_string_hash,
|
||||
(KeyEqualFunc)wasm_string_equal, nullptr, nullptr);
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_1", (void *)"val_1");
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_2", (void *)"val_2");
|
||||
EXPECT_EQ(true, bh_hash_map_destroy(test_hash_map));
|
||||
}
|
||||
|
||||
// This fun allows inserting the same keys.
|
||||
bool
|
||||
string_equal_test(const char *s1, const char *s2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int COUNT_ELEM = 0;
|
||||
|
||||
void
|
||||
fun_count_elem(void *key, void *value, void *user_data)
|
||||
{
|
||||
COUNT_ELEM++;
|
||||
}
|
||||
|
||||
TEST_F(bh_hashmap_test_suite, bh_hashmap_thread_safety)
|
||||
{
|
||||
HashMap *test_hash_map = bh_hash_map_create(
|
||||
32, true, (HashFunc)wasm_string_hash, (KeyEqualFunc)string_equal_test,
|
||||
destroy_func_test, wasm_runtime_free);
|
||||
int32_t i = 0;
|
||||
std::vector<std::future<void>> threads;
|
||||
|
||||
// Creat 8 threads. In every thread, run the codes in brackets of
|
||||
// std::async.
|
||||
for (i = 0; i < 8; i++) {
|
||||
threads.push_back(std::async([&] {
|
||||
for (int j = 0; j < 25; j++) {
|
||||
bh_hash_map_insert(test_hash_map, (void *)"key_1",
|
||||
(void *)"val_1");
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
// Wait all 8 threads finished.
|
||||
for (auto &t : threads) {
|
||||
t.wait();
|
||||
}
|
||||
|
||||
// Count hash map elements.
|
||||
bh_hash_map_traverse(test_hash_map, fun_count_elem, nullptr);
|
||||
|
||||
EXPECT_EQ(200, COUNT_ELEM);
|
||||
EXPECT_EQ(true, bh_hash_map_destroy(test_hash_map));
|
||||
}
|
||||
137
tests/unit/shared-utils/bh_list_test.cc
Normal file
137
tests/unit/shared-utils/bh_list_test.cc
Normal file
@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
class bh_list_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// 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() {}
|
||||
|
||||
// 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() {}
|
||||
|
||||
public:
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(bh_list_test_suite, bh_list_init)
|
||||
{
|
||||
bh_list list_test;
|
||||
|
||||
// Normally.
|
||||
EXPECT_EQ(BH_LIST_SUCCESS, bh_list_init(&list_test));
|
||||
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ(BH_LIST_ERROR, bh_list_init(nullptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_list_test_suite, bh_list_insert)
|
||||
{
|
||||
bh_list list_test;
|
||||
bh_list_link elem_insert;
|
||||
|
||||
// Normally.
|
||||
bh_list_init(&list_test);
|
||||
EXPECT_EQ(BH_LIST_SUCCESS, bh_list_insert(&list_test, &elem_insert));
|
||||
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ(BH_LIST_ERROR, bh_list_insert(nullptr, nullptr));
|
||||
EXPECT_EQ(BH_LIST_ERROR, bh_list_insert(&list_test, nullptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_list_test_suite, bh_list_remove)
|
||||
{
|
||||
bh_list list_test;
|
||||
bh_list_link elem_insert_1;
|
||||
bh_list_link elem_insert_2;
|
||||
bh_list_link elem_insert_3;
|
||||
bh_list_link elem_insert_4;
|
||||
|
||||
// Normally.
|
||||
bh_list_init(&list_test);
|
||||
bh_list_insert(&list_test, &elem_insert_1);
|
||||
bh_list_insert(&list_test, &elem_insert_2);
|
||||
bh_list_insert(&list_test, &elem_insert_3);
|
||||
bh_list_insert(&list_test, &elem_insert_4);
|
||||
EXPECT_EQ(BH_LIST_SUCCESS, bh_list_remove(&list_test, &elem_insert_1));
|
||||
|
||||
// The elem specified by prameter is not in the list.
|
||||
EXPECT_EQ(BH_LIST_ERROR, bh_list_remove(&list_test, &elem_insert_1));
|
||||
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ(BH_LIST_ERROR, bh_list_remove(&list_test, nullptr));
|
||||
EXPECT_EQ(BH_LIST_ERROR, bh_list_remove(nullptr, nullptr));
|
||||
EXPECT_EQ(BH_LIST_ERROR, bh_list_remove(nullptr, &elem_insert_1));
|
||||
}
|
||||
|
||||
TEST_F(bh_list_test_suite, bh_list_length)
|
||||
{
|
||||
bh_list list_test;
|
||||
bh_list_link elem_insert_1;
|
||||
bh_list_link elem_insert_2;
|
||||
|
||||
bh_list_init(&list_test);
|
||||
|
||||
// The length is 0.
|
||||
EXPECT_EQ(0, bh_list_length(&list_test));
|
||||
|
||||
// The length is 2.
|
||||
bh_list_insert(&list_test, &elem_insert_1);
|
||||
bh_list_insert(&list_test, &elem_insert_2);
|
||||
EXPECT_EQ(2, bh_list_length(&list_test));
|
||||
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ(0, bh_list_length(nullptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_list_test_suite, bh_list_first_elem)
|
||||
{
|
||||
bh_list list_test;
|
||||
bh_list_link elem_insert_1;
|
||||
bh_list_link elem_insert_2;
|
||||
|
||||
bh_list_init(&list_test);
|
||||
|
||||
// There is no element in the list.
|
||||
EXPECT_EQ(nullptr, bh_list_first_elem(&list_test));
|
||||
|
||||
// There are 2 elements in the list.
|
||||
bh_list_insert(&list_test, &elem_insert_1);
|
||||
bh_list_insert(&list_test, &elem_insert_2);
|
||||
EXPECT_EQ(&elem_insert_2, bh_list_first_elem(&list_test));
|
||||
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ(nullptr, bh_list_first_elem(nullptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_list_test_suite, bh_list_elem_next)
|
||||
{
|
||||
bh_list list_test;
|
||||
bh_list_link elem_insert_1;
|
||||
bh_list_link elem_insert_2;
|
||||
|
||||
bh_list_init(&list_test);
|
||||
bh_list_insert(&list_test, &elem_insert_1);
|
||||
bh_list_insert(&list_test, &elem_insert_2);
|
||||
|
||||
// Normally.
|
||||
EXPECT_EQ(&elem_insert_1, bh_list_elem_next(&elem_insert_2));
|
||||
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ(nullptr, bh_list_elem_next(nullptr));
|
||||
}
|
||||
95
tests/unit/shared-utils/bh_log_test.cc
Normal file
95
tests/unit/shared-utils/bh_log_test.cc
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* 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 "bh_log.h"
|
||||
#include "stdio.h"
|
||||
|
||||
class bh_log_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// 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() {}
|
||||
|
||||
// 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() {}
|
||||
};
|
||||
|
||||
#define TEST_STR "This is a test."
|
||||
|
||||
TEST_F(bh_log_test_suite, bh_log_set_verbose_level)
|
||||
{
|
||||
bh_log_set_verbose_level(BH_LOG_LEVEL_DEBUG);
|
||||
}
|
||||
|
||||
TEST_F(bh_log_test_suite, bh_print_time)
|
||||
{
|
||||
std::string captured;
|
||||
|
||||
bh_log_set_verbose_level(BH_LOG_LEVEL_WARNING);
|
||||
bh_print_time(TEST_STR);
|
||||
|
||||
bh_log_set_verbose_level(BH_LOG_LEVEL_DEBUG);
|
||||
testing::internal::CaptureStdout();
|
||||
bh_print_time(TEST_STR);
|
||||
captured = testing::internal::GetCapturedStdout();
|
||||
EXPECT_EQ(0, strncmp(TEST_STR, captured.c_str(), strlen(TEST_STR)));
|
||||
|
||||
testing::internal::CaptureStdout();
|
||||
bh_print_time(TEST_STR);
|
||||
captured = testing::internal::GetCapturedStdout();
|
||||
EXPECT_EQ(0, strncmp(TEST_STR, captured.c_str(), strlen(TEST_STR)));
|
||||
}
|
||||
|
||||
TEST_F(bh_log_test_suite, bh_log)
|
||||
{
|
||||
std::string captured;
|
||||
|
||||
bh_log_set_verbose_level(BH_LOG_LEVEL_DEBUG);
|
||||
testing::internal::CaptureStdout();
|
||||
bh_log(BH_LOG_LEVEL_FATAL, __FILE__, __LINE__, TEST_STR);
|
||||
captured = testing::internal::GetCapturedStdout();
|
||||
EXPECT_PRED_FORMAT2(::testing::IsSubstring, TEST_STR, captured);
|
||||
|
||||
testing::internal::CaptureStdout();
|
||||
bh_log(BH_LOG_LEVEL_ERROR, __FILE__, __LINE__, TEST_STR);
|
||||
captured = testing::internal::GetCapturedStdout();
|
||||
EXPECT_PRED_FORMAT2(::testing::IsSubstring, TEST_STR, captured);
|
||||
|
||||
testing::internal::CaptureStdout();
|
||||
bh_log(BH_LOG_LEVEL_WARNING, __FILE__, __LINE__, TEST_STR);
|
||||
captured = testing::internal::GetCapturedStdout();
|
||||
EXPECT_PRED_FORMAT2(::testing::IsSubstring, TEST_STR, captured);
|
||||
|
||||
testing::internal::CaptureStdout();
|
||||
bh_log(BH_LOG_LEVEL_DEBUG, __FILE__, __LINE__, TEST_STR);
|
||||
captured = testing::internal::GetCapturedStdout();
|
||||
EXPECT_PRED_FORMAT2(::testing::IsSubstring, TEST_STR, captured);
|
||||
|
||||
// log_verbose_level == BH_LOG_LEVEL_DEBUG, so BH_LOG_LEVEL_VERBOSE is not
|
||||
// printed.
|
||||
testing::internal::CaptureStdout();
|
||||
bh_log(BH_LOG_LEVEL_VERBOSE, __FILE__, __LINE__, TEST_STR);
|
||||
captured = testing::internal::GetCapturedStdout();
|
||||
EXPECT_EQ(nullptr, strstr(captured.c_str(), TEST_STR));
|
||||
|
||||
// After set log_verbose_level = BH_LOG_LEVEL_VERBOSE, BH_LOG_LEVEL_VERBOSE
|
||||
// can be printed.
|
||||
bh_log_set_verbose_level(BH_LOG_LEVEL_VERBOSE);
|
||||
testing::internal::CaptureStdout();
|
||||
bh_log(BH_LOG_LEVEL_VERBOSE, __FILE__, __LINE__, TEST_STR);
|
||||
captured = testing::internal::GetCapturedStdout();
|
||||
EXPECT_PRED_FORMAT2(::testing::IsSubstring, TEST_STR, captured);
|
||||
}
|
||||
278
tests/unit/shared-utils/bh_queue_test.cc
Normal file
278
tests/unit/shared-utils/bh_queue_test.cc
Normal file
@ -0,0 +1,278 @@
|
||||
/*
|
||||
* 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 "bh_platform.h"
|
||||
|
||||
class bh_queue_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// 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() {}
|
||||
|
||||
// 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() {}
|
||||
|
||||
public:
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
typedef struct bh_queue_node {
|
||||
struct bh_queue_node *next;
|
||||
struct bh_queue_node *prev;
|
||||
unsigned short tag;
|
||||
unsigned int len;
|
||||
void *body;
|
||||
bh_msg_cleaner msg_cleaner;
|
||||
} bh_queue_node;
|
||||
|
||||
struct bh_queue {
|
||||
bh_queue_mutex queue_lock;
|
||||
bh_queue_cond queue_wait_cond;
|
||||
unsigned int cnt;
|
||||
unsigned int max;
|
||||
unsigned int drops;
|
||||
bh_queue_node *head;
|
||||
bh_queue_node *tail;
|
||||
|
||||
bool exit_loop_run;
|
||||
};
|
||||
|
||||
typedef enum LINK_MSG_TYPE {
|
||||
COAP_TCP_RAW = 0,
|
||||
COAP_UDP_RAW = 1,
|
||||
REQUEST_PACKET,
|
||||
RESPONSE_PACKET,
|
||||
INSTALL_WASM_APP,
|
||||
CBOR_GENERIC = 30,
|
||||
|
||||
LINK_MSG_TYPE_MAX = 50
|
||||
} LINK_MSG_TYPE;
|
||||
|
||||
typedef enum QUEUE_MSG_TYPE {
|
||||
COAP_PARSED = LINK_MSG_TYPE_MAX + 1,
|
||||
RESTFUL_REQUEST,
|
||||
RESTFUL_RESPONSE,
|
||||
TIMER_EVENT = 5,
|
||||
SENSOR_EVENT = 6,
|
||||
GPIO_INTERRUPT_EVENT = 7,
|
||||
BLE_EVENT = 8,
|
||||
JDWP_REQUEST = 9,
|
||||
WD_TIMEOUT = 10,
|
||||
BASE_EVENT_MAX = 100
|
||||
|
||||
} QUEUE_MSG_TYPE;
|
||||
|
||||
enum {
|
||||
WASM_Msg_Start = BASE_EVENT_MAX,
|
||||
TIMER_EVENT_WASM,
|
||||
SENSOR_EVENT_WASM,
|
||||
CONNECTION_EVENT_WASM,
|
||||
WIDGET_EVENT_WASM,
|
||||
WASM_Msg_End = WASM_Msg_Start + 100
|
||||
};
|
||||
|
||||
// If RES_CMP == 1, the function bh_queue_enter_loop_run run error.
|
||||
int RES_CMP = 0;
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_queue_create)
|
||||
{
|
||||
EXPECT_NE(nullptr, bh_queue_create());
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_queue_destroy)
|
||||
{
|
||||
bh_message_t msg_ptr;
|
||||
bh_queue *queue_ptr = bh_queue_create();
|
||||
|
||||
// Normally.
|
||||
msg_ptr = bh_new_msg(RESTFUL_REQUEST, nullptr, 0, nullptr);
|
||||
bh_post_msg2(queue_ptr, msg_ptr);
|
||||
bh_queue_destroy(queue_ptr);
|
||||
EXPECT_EQ(nullptr, queue_ptr->head);
|
||||
|
||||
// Illegal parameters.
|
||||
bh_queue_destroy(nullptr);
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_message_payload)
|
||||
{
|
||||
bh_message_t msg_ptr;
|
||||
|
||||
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
|
||||
sizeof("test_msg_body"), nullptr);
|
||||
EXPECT_EQ("test_msg_body", bh_message_payload(msg_ptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_message_payload_len)
|
||||
{
|
||||
bh_message_t msg_ptr;
|
||||
|
||||
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
|
||||
sizeof("test_msg_body"), nullptr);
|
||||
EXPECT_EQ(sizeof("test_msg_body"), bh_message_payload_len(msg_ptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_message_type)
|
||||
{
|
||||
bh_message_t msg_ptr;
|
||||
|
||||
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
|
||||
sizeof("test_msg_body"), nullptr);
|
||||
EXPECT_EQ(RESTFUL_REQUEST, bh_message_type(msg_ptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_new_msg)
|
||||
{
|
||||
EXPECT_NE(nullptr, bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
|
||||
sizeof("test_msg_body"), nullptr));
|
||||
}
|
||||
|
||||
void
|
||||
msg_cleaner_test(void *)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_free_msg)
|
||||
{
|
||||
bh_message_t msg_ptr;
|
||||
|
||||
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
|
||||
sizeof("test_msg_body"), (void *)msg_cleaner_test);
|
||||
bh_free_msg(msg_ptr);
|
||||
|
||||
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
|
||||
sizeof("test_msg_body"), nullptr);
|
||||
bh_free_msg(msg_ptr);
|
||||
|
||||
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)(uintptr_t)100, 0, nullptr);
|
||||
bh_free_msg(msg_ptr);
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_post_msg)
|
||||
{
|
||||
int i = 0;
|
||||
bh_queue *queue_ptr = bh_queue_create();
|
||||
bh_message_t msg_ptr;
|
||||
|
||||
EXPECT_EQ(true, bh_post_msg(queue_ptr, TIMER_EVENT_WASM, nullptr, 0));
|
||||
EXPECT_EQ(1, queue_ptr->cnt);
|
||||
|
||||
// queue_ptr->cnt >= queue_ptr->max.
|
||||
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
|
||||
sizeof("test_msg_body"), nullptr);
|
||||
for (i = 1; i <= 50; i++) {
|
||||
bh_post_msg2(queue_ptr, msg_ptr);
|
||||
}
|
||||
EXPECT_EQ(false, bh_post_msg(queue_ptr, TIMER_EVENT_WASM, nullptr, 0));
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_post_msg2)
|
||||
{
|
||||
bh_message_t msg_ptr;
|
||||
bh_queue *queue_ptr = bh_queue_create();
|
||||
|
||||
msg_ptr = bh_new_msg(RESTFUL_REQUEST, nullptr, 0, nullptr);
|
||||
EXPECT_EQ(true, bh_post_msg2(queue_ptr, msg_ptr));
|
||||
EXPECT_EQ(1, queue_ptr->cnt);
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_get_msg)
|
||||
{
|
||||
bh_message_t msg_ptr;
|
||||
bh_queue *queue_ptr = bh_queue_create();
|
||||
|
||||
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
|
||||
sizeof("test_msg_body"), nullptr);
|
||||
|
||||
// queue->cnt == 0, timeout_us == 0.
|
||||
EXPECT_EQ(nullptr, bh_get_msg(queue_ptr, 0));
|
||||
// queue->cnt == 0, timeout_us != 0.
|
||||
bh_get_msg(queue_ptr, 1);
|
||||
|
||||
bh_post_msg2(queue_ptr, msg_ptr);
|
||||
EXPECT_EQ(1, queue_ptr->cnt);
|
||||
bh_get_msg(queue_ptr, -1);
|
||||
EXPECT_EQ(0, queue_ptr->cnt);
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_queue_get_message_count)
|
||||
{
|
||||
int i = 0, j = 0;
|
||||
bh_message_t msg_ptr;
|
||||
bh_queue *queue_ptr = bh_queue_create();
|
||||
|
||||
// Normally.
|
||||
msg_ptr = bh_new_msg(RESTFUL_REQUEST, (void *)"test_msg_body",
|
||||
sizeof("test_msg_body"), nullptr);
|
||||
for (i = 1; i <= 20; i++) {
|
||||
bh_post_msg2(queue_ptr, msg_ptr);
|
||||
}
|
||||
i = i - 1;
|
||||
// The count of msg is less than queue_ptr->max.
|
||||
EXPECT_EQ(i, bh_queue_get_message_count(queue_ptr));
|
||||
|
||||
// The count of msg is more than queue_ptr->max.
|
||||
for (j = 1; j <= 60; j++) {
|
||||
bh_post_msg2(queue_ptr, msg_ptr);
|
||||
}
|
||||
j = j - 1;
|
||||
EXPECT_EQ(queue_ptr->max, bh_queue_get_message_count(queue_ptr));
|
||||
EXPECT_EQ(j + i - queue_ptr->max, queue_ptr->drops);
|
||||
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ(0, bh_queue_get_message_count(nullptr));
|
||||
}
|
||||
|
||||
void
|
||||
bh_queue_enter_loop_run_test_fun(void *message, void *arg)
|
||||
{
|
||||
static int count = 0;
|
||||
RES_CMP =
|
||||
strncmp("test_queue_loop", (char *)((bh_message_t)message)->body, 15);
|
||||
|
||||
count++;
|
||||
if (2 == count) {
|
||||
bh_queue_exit_loop_run((bh_queue *)arg);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_queue_enter_loop_run)
|
||||
{
|
||||
bh_queue *queue_ptr = bh_queue_create();
|
||||
bh_message_t msg_ptr1 =
|
||||
bh_new_msg(RESTFUL_REQUEST, (void *)"test_queue_loop",
|
||||
sizeof("test_queue_loop"), nullptr);
|
||||
bh_message_t msg_ptr2 =
|
||||
bh_new_msg(RESTFUL_REQUEST, (void *)"test_queue_loop",
|
||||
sizeof("test_queue_loop"), nullptr);
|
||||
|
||||
bh_post_msg2(queue_ptr, msg_ptr1);
|
||||
bh_post_msg2(queue_ptr, msg_ptr2);
|
||||
bh_queue_enter_loop_run(queue_ptr, bh_queue_enter_loop_run_test_fun,
|
||||
queue_ptr);
|
||||
EXPECT_EQ(0, RES_CMP);
|
||||
|
||||
// Illegal parameters.
|
||||
bh_queue_enter_loop_run(nullptr, bh_queue_enter_loop_run_test_fun,
|
||||
queue_ptr);
|
||||
}
|
||||
|
||||
TEST_F(bh_queue_test_suite, bh_queue_exit_loop_run)
|
||||
{
|
||||
// Illegal parameters.
|
||||
bh_queue_exit_loop_run(nullptr);
|
||||
}
|
||||
268
tests/unit/shared-utils/bh_vector_test.cc
Normal file
268
tests/unit/shared-utils/bh_vector_test.cc
Normal file
@ -0,0 +1,268 @@
|
||||
/*
|
||||
* 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 "bh_platform.h"
|
||||
|
||||
#include <future>
|
||||
|
||||
class bh_vector_test_suite : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// 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() {}
|
||||
|
||||
// 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() {}
|
||||
|
||||
public:
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
static inline void *
|
||||
malloc_internal(uint64 size);
|
||||
|
||||
TEST_F(bh_vector_test_suite, bh_vector_init)
|
||||
{
|
||||
Vector *vector_ptr = nullptr;
|
||||
|
||||
// Normally. use_lock is true.
|
||||
vector_ptr = (Vector *)wasm_runtime_malloc(sizeof(Vector));
|
||||
memset(vector_ptr, 0, sizeof(Vector));
|
||||
EXPECT_EQ(true, bh_vector_init(vector_ptr, 6, sizeof(Vector *), true));
|
||||
// use_lock is false.
|
||||
EXPECT_EQ(true, bh_vector_init(vector_ptr, 6, sizeof(Vector *), false));
|
||||
// init_length == 0.
|
||||
EXPECT_EQ(true, bh_vector_init(vector_ptr, 0, sizeof(Vector *), false));
|
||||
// size_elem > UINT32_MAX.
|
||||
EXPECT_EQ(true, bh_vector_init(vector_ptr, 6, UINT32_MAX + 1, false));
|
||||
// init_length > UINT32_MAX.
|
||||
EXPECT_EQ(true, bh_vector_init(vector_ptr, UINT32_MAX + 1, sizeof(Vector *),
|
||||
false));
|
||||
|
||||
// Illegal parameters.
|
||||
EXPECT_EQ(false, bh_vector_init(nullptr, 6, sizeof(Vector *), true));
|
||||
}
|
||||
|
||||
TEST_F(bh_vector_test_suite, bh_vector_set)
|
||||
{
|
||||
Vector *vector_ptr = nullptr;
|
||||
char elem_buf[] = "test_vector";
|
||||
|
||||
vector_ptr = (Vector *)wasm_runtime_malloc(sizeof(Vector));
|
||||
memset(vector_ptr, 0, sizeof(Vector));
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), true);
|
||||
bh_vector_append(vector_ptr, "test");
|
||||
|
||||
// Normally. use_lock is true.
|
||||
EXPECT_EQ(true, bh_vector_set(vector_ptr, 0, elem_buf));
|
||||
|
||||
// Illegal parameters: nullptr.
|
||||
EXPECT_EQ(false, bh_vector_set(nullptr, 0, nullptr));
|
||||
EXPECT_EQ(false, bh_vector_set(vector_ptr, 0, nullptr));
|
||||
// Illegal parameters: index >= vector->num_elems.
|
||||
EXPECT_EQ(false, bh_vector_set(vector_ptr, 1, elem_buf));
|
||||
|
||||
// Normally. use_lock is false.
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), false);
|
||||
bh_vector_append(vector_ptr, "test");
|
||||
EXPECT_EQ(true, bh_vector_set(vector_ptr, 0, elem_buf));
|
||||
}
|
||||
|
||||
TEST_F(bh_vector_test_suite, bh_vector_get)
|
||||
{
|
||||
Vector *vector_ptr = nullptr;
|
||||
char elem_buf[] = "test_vector";
|
||||
char get_elem[12] = { 0 };
|
||||
|
||||
vector_ptr = (Vector *)wasm_runtime_malloc(sizeof(Vector));
|
||||
memset(vector_ptr, 0, sizeof(Vector));
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), true);
|
||||
bh_vector_append(vector_ptr, elem_buf);
|
||||
|
||||
// Normally. use_lock is true.
|
||||
EXPECT_EQ(true, bh_vector_get(vector_ptr, 0, get_elem));
|
||||
EXPECT_EQ(0, strncmp(elem_buf, get_elem, 11));
|
||||
|
||||
// Illegal parameters: nullptr.
|
||||
EXPECT_EQ(false, bh_vector_get(nullptr, 0, nullptr));
|
||||
EXPECT_EQ(false, bh_vector_get(vector_ptr, 0, nullptr));
|
||||
// Illegal parameters: index >= vector->num_elems.
|
||||
EXPECT_EQ(false, bh_vector_get(vector_ptr, 1, get_elem));
|
||||
|
||||
// Normally. use_lock is false.
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), false);
|
||||
bh_vector_append(vector_ptr, elem_buf);
|
||||
EXPECT_EQ(true, bh_vector_get(vector_ptr, 0, get_elem));
|
||||
EXPECT_EQ(0, strncmp(elem_buf, get_elem, 11));
|
||||
}
|
||||
|
||||
TEST_F(bh_vector_test_suite, bh_vector_insert)
|
||||
{
|
||||
Vector *vector_ptr = nullptr;
|
||||
char elem_buf[] = "test_vector";
|
||||
char get_elem[12] = { 0 };
|
||||
|
||||
vector_ptr = (Vector *)wasm_runtime_malloc(sizeof(Vector));
|
||||
memset(vector_ptr, 0, sizeof(Vector));
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), true);
|
||||
bh_vector_append(vector_ptr, "test");
|
||||
bh_vector_append(vector_ptr, "test");
|
||||
bh_vector_append(vector_ptr, "test");
|
||||
bh_vector_append(vector_ptr, "test");
|
||||
|
||||
// Normally.
|
||||
EXPECT_EQ(true, bh_vector_insert(vector_ptr, 0, elem_buf));
|
||||
bh_vector_get(vector_ptr, 1, get_elem);
|
||||
EXPECT_EQ(0, strncmp(elem_buf, get_elem, 11));
|
||||
|
||||
EXPECT_EQ(true, bh_vector_insert(vector_ptr, 2, elem_buf));
|
||||
EXPECT_EQ(true, bh_vector_insert(vector_ptr, 5, elem_buf));
|
||||
|
||||
// Illegal parameters: nullptr.
|
||||
EXPECT_EQ(false, bh_vector_insert(nullptr, 0, nullptr));
|
||||
EXPECT_EQ(false, bh_vector_insert(vector_ptr, 0, nullptr));
|
||||
EXPECT_EQ(0, strncmp(elem_buf, get_elem, 0));
|
||||
// Illegal parameters: index >= vector->num_elems.
|
||||
EXPECT_EQ(false, bh_vector_insert(vector_ptr, 10, elem_buf));
|
||||
|
||||
// "if (!extend_vector(vector, vector->num_elems + 1))" == true.
|
||||
vector_ptr->num_elems = UINT32_MAX + 1;
|
||||
EXPECT_EQ(false, bh_vector_insert(vector_ptr, 2, elem_buf));
|
||||
|
||||
// use_lock is false.
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), false);
|
||||
bh_vector_append(vector_ptr, "test");
|
||||
EXPECT_EQ(true, bh_vector_insert(vector_ptr, 0, elem_buf));
|
||||
}
|
||||
|
||||
TEST_F(bh_vector_test_suite, bh_vector_append)
|
||||
{
|
||||
Vector *vector_ptr = nullptr;
|
||||
char elem_buf[] = "test_vector";
|
||||
char get_elem[12] = { 0 };
|
||||
|
||||
vector_ptr = (Vector *)wasm_runtime_malloc(sizeof(Vector));
|
||||
memset(vector_ptr, 0, sizeof(Vector));
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), true);
|
||||
|
||||
// Normally.
|
||||
EXPECT_EQ(true, bh_vector_append(vector_ptr, elem_buf));
|
||||
bh_vector_get(vector_ptr, 0, get_elem);
|
||||
EXPECT_EQ(0, strncmp(elem_buf, get_elem, 11));
|
||||
|
||||
// Illegal parameters: nullptr.
|
||||
EXPECT_EQ(false, bh_vector_append(nullptr, nullptr));
|
||||
EXPECT_EQ(false, bh_vector_append(vector_ptr, nullptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_vector_test_suite, bh_vector_remove)
|
||||
{
|
||||
Vector *vector_ptr = nullptr;
|
||||
char elem_buf[] = "test_vector";
|
||||
char old_elem[12] = { 0 };
|
||||
|
||||
vector_ptr = (Vector *)wasm_runtime_malloc(sizeof(Vector));
|
||||
memset(vector_ptr, 0, sizeof(Vector));
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), true);
|
||||
bh_vector_append(vector_ptr, elem_buf);
|
||||
bh_vector_append(vector_ptr, elem_buf);
|
||||
bh_vector_append(vector_ptr, elem_buf);
|
||||
bh_vector_append(vector_ptr, elem_buf);
|
||||
|
||||
// Normally.
|
||||
// Remove the first one.
|
||||
EXPECT_EQ(true, bh_vector_remove(vector_ptr, 0, old_elem));
|
||||
// Remove the middle one.
|
||||
EXPECT_EQ(true, bh_vector_remove(vector_ptr, 2, old_elem));
|
||||
// Remove the last one.
|
||||
EXPECT_EQ(true, bh_vector_remove(vector_ptr, 1, old_elem));
|
||||
|
||||
EXPECT_EQ(true, bh_vector_remove(vector_ptr, 0, nullptr));
|
||||
|
||||
// Illegal parameters: nullptr.
|
||||
EXPECT_EQ(false, bh_vector_remove(nullptr, 0, nullptr));
|
||||
EXPECT_EQ(false, bh_vector_remove(vector_ptr, 0, nullptr));
|
||||
// Illegal parameters: index >= vector->num_elems.
|
||||
EXPECT_EQ(false, bh_vector_remove(vector_ptr, 1, old_elem));
|
||||
|
||||
// use_lock is false.
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), false);
|
||||
bh_vector_append(vector_ptr, elem_buf);
|
||||
EXPECT_EQ(true, bh_vector_remove(vector_ptr, 0, old_elem));
|
||||
}
|
||||
|
||||
TEST_F(bh_vector_test_suite, bh_vector_size)
|
||||
{
|
||||
Vector *vector_ptr = nullptr;
|
||||
char elem_buf[] = "test_vector";
|
||||
|
||||
vector_ptr = (Vector *)wasm_runtime_malloc(sizeof(Vector));
|
||||
memset(vector_ptr, 0, sizeof(Vector));
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), true);
|
||||
bh_vector_append(vector_ptr, elem_buf);
|
||||
|
||||
EXPECT_EQ(1, bh_vector_size(vector_ptr));
|
||||
EXPECT_EQ(0, bh_vector_size(nullptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_vector_test_suite, bh_vector_destroy)
|
||||
{
|
||||
Vector *vector_ptr = nullptr;
|
||||
char elem_buf[] = "test_vector";
|
||||
|
||||
vector_ptr = (Vector *)wasm_runtime_malloc(sizeof(Vector));
|
||||
memset(vector_ptr, 0, sizeof(Vector));
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), true);
|
||||
bh_vector_append(vector_ptr, elem_buf);
|
||||
|
||||
// Normally.
|
||||
EXPECT_EQ(true, bh_vector_destroy(vector_ptr));
|
||||
// Illegal parameters: nullptr.
|
||||
EXPECT_EQ(false, bh_vector_destroy(nullptr));
|
||||
|
||||
// use_lock is false.
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem_buf), false);
|
||||
bh_vector_append(vector_ptr, elem_buf);
|
||||
EXPECT_EQ(true, bh_vector_destroy(vector_ptr));
|
||||
}
|
||||
|
||||
TEST_F(bh_vector_test_suite, bh_vector_thread_safety)
|
||||
{
|
||||
Vector *vector_ptr = nullptr;
|
||||
char elem;
|
||||
int32_t i = 0;
|
||||
std::vector<std::future<void>> threads;
|
||||
|
||||
vector_ptr = (Vector *)wasm_runtime_malloc(sizeof(Vector));
|
||||
memset(vector_ptr, 0, sizeof(Vector));
|
||||
bh_vector_init(vector_ptr, 6, sizeof(elem), true);
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
threads.push_back(std::async([&] {
|
||||
for (int j = 0; j < 25; j++) {
|
||||
bh_vector_append(vector_ptr, (void *)&elem);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
for (auto &t : threads) {
|
||||
t.wait();
|
||||
}
|
||||
|
||||
EXPECT_EQ(bh_vector_size(vector_ptr), 200);
|
||||
|
||||
// Normally.
|
||||
EXPECT_EQ(true, bh_vector_destroy(vector_ptr));
|
||||
}
|
||||
33
tests/unit/shared-utils/shared_utils_test.cc
Normal file
33
tests/unit/shared-utils/shared_utils_test.cc
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "test_helper.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
/* Test APIs under wamr/core/shared/utils */
|
||||
|
||||
class SharedUtilsTest : public testing::Test
|
||||
{
|
||||
protected:
|
||||
// You should make the members protected s.t. they can be
|
||||
// accessed from sub-classes.
|
||||
|
||||
// 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() {}
|
||||
|
||||
// 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() {}
|
||||
|
||||
public:
|
||||
WAMRRuntimeRAII<512 * 1024> runtime;
|
||||
};
|
||||
|
||||
TEST_F(SharedUtilsTest, bh_list) {}
|
||||
Reference in New Issue
Block a user