🧪 test(exdata): add test case
This commit is contained in:
@@ -10,7 +10,7 @@ typedef void exdata_free(int idx, void *ex_ptr, void *arg);
|
||||
struct exdata_schema;
|
||||
|
||||
struct exdata_schema *exdata_schema_new();
|
||||
void exdata_schema_free(struct exdata_schema *s);
|
||||
void exdata_schema_free(struct exdata_schema *schemas);
|
||||
|
||||
int exdata_schema_new_index(struct exdata_schema *schema, const char *name, exdata_free *free_func,void *free_arg);
|
||||
|
||||
@@ -18,12 +18,12 @@ int exdata_schema_get_idx_by_name(struct exdata_schema *schema, const char *name
|
||||
|
||||
struct exdata_runtime;
|
||||
|
||||
struct exdata_runtime *exdata_runtime_new(struct exdata_schema *h);
|
||||
void exdata_runtime_free(struct exdata_runtime *h);
|
||||
void exdata_runtime_reset(struct exdata_runtime *h);//call free_func, and set ex_ptr to NULL
|
||||
struct exdata_runtime *exdata_runtime_new(struct exdata_schema *schemas);
|
||||
void exdata_runtime_free(struct exdata_runtime *rt);
|
||||
void exdata_runtime_reset(struct exdata_runtime *rt);//call free_func, and set ex_ptr to NULL
|
||||
|
||||
int exdata_set(struct exdata_runtime *h, int idx, void *ex_ptr);
|
||||
void *exdata_get(struct exdata_runtime *h, int idx);
|
||||
int exdata_set(struct exdata_runtime *rt, int idx, void *ex_ptr);
|
||||
void *exdata_get(struct exdata_runtime *rt, int idx);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
add_library(exdata exdata.c)
|
||||
|
||||
# //TODO: Add test
|
||||
#add_subdirectory(test)
|
||||
add_subdirectory(test)
|
||||
@@ -136,7 +136,7 @@ void exdata_runtime_free(struct exdata_runtime *h)
|
||||
|
||||
int exdata_set(struct exdata_runtime *h, int idx, void *ex_ptr)
|
||||
{
|
||||
if(h==NULL || h->schema == NULL|| h->exdata_array == NULL)return -1;
|
||||
if(h==NULL || h->schema == NULL|| h->exdata_array == NULL || idx<0)return -1;
|
||||
unsigned int len=utarray_len(h->schema->exdata_meta_array);
|
||||
if(len < (unsigned int)idx)return -1;
|
||||
if((h->exdata_array+idx)->state == EXIT)return -1;
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
add_executable(gtest_module_manager
|
||||
module_manager_gtest_main.cpp
|
||||
add_executable(gtest_exdata
|
||||
gtest_exdata_main.cpp
|
||||
)
|
||||
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/infra/plugin_manager/)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/infra/tuple/)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/infra/exdata/)
|
||||
|
||||
target_link_libraries(
|
||||
gtest_module_manager
|
||||
module_manager
|
||||
dl
|
||||
"-rdynamic"
|
||||
gtest_exdata
|
||||
exdata
|
||||
gtest
|
||||
gmock
|
||||
)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(gtest_module_manager)
|
||||
gtest_discover_tests(gtest_exdata)
|
||||
135
infra/exdata/test/gtest_exdata_main.cpp
Normal file
135
infra/exdata/test/gtest_exdata_main.cpp
Normal file
@@ -0,0 +1,135 @@
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
||||
#include "exdata_internal.h"
|
||||
|
||||
/******************************************
|
||||
* TEST EXDATA SCHEMA *
|
||||
******************************************/
|
||||
void gtest_mock_free(int idx, void *ex_ptr, void *arg){};
|
||||
void gtest_mock_overwirte_free(int idx, void *ex_ptr, void *arg){};
|
||||
|
||||
TEST(exdata_schema, basic_new_index)
|
||||
{
|
||||
struct exdata_schema *schema=exdata_schema_new();
|
||||
int exdata_idx=exdata_schema_new_index(schema, "gtest", gtest_mock_free,
|
||||
NULL);
|
||||
EXPECT_GE(exdata_idx, 0);
|
||||
EXPECT_EQ(exdata_schema_get_idx_by_name(schema, "gtest"), exdata_idx);
|
||||
EXPECT_EQ(exdata_schema_get_idx_by_name(schema, "error"), -1);
|
||||
exdata_schema_free(schema);
|
||||
}
|
||||
|
||||
TEST(exdata_schema, exdata_new_index_overwrite) {
|
||||
struct exdata_schema *schema=exdata_schema_new();
|
||||
const char *exdata_name="PACKET_EXDATA";
|
||||
int exdata_idx=exdata_schema_new_index(schema,exdata_name, gtest_mock_free, NULL);
|
||||
EXPECT_GE(exdata_idx, 0);
|
||||
|
||||
int overwrite_idx=exdata_schema_new_index(schema,exdata_name, gtest_mock_overwirte_free, schema);
|
||||
EXPECT_GE(overwrite_idx, 0);
|
||||
EXPECT_EQ(overwrite_idx, exdata_idx);
|
||||
|
||||
{
|
||||
SCOPED_TRACE("White-box test, check stellar internal schema");
|
||||
struct exdata_meta *exdata_schema = (struct exdata_meta *)utarray_eltptr(
|
||||
schema->exdata_meta_array, (unsigned int)exdata_idx);
|
||||
EXPECT_EQ(exdata_schema->free_func, (void *)gtest_mock_overwirte_free);
|
||||
EXPECT_EQ(exdata_schema->free_arg, schema);
|
||||
EXPECT_EQ(exdata_schema->idx, exdata_idx);
|
||||
EXPECT_STREQ(exdata_schema->name, exdata_name);
|
||||
|
||||
int exdata_num = utarray_len(schema->exdata_meta_array);
|
||||
EXPECT_EQ(exdata_num, 1);
|
||||
}
|
||||
|
||||
EXPECT_EQ(exdata_schema_get_idx_by_name(schema, exdata_name), exdata_idx);
|
||||
EXPECT_EQ(exdata_schema_get_idx_by_name(schema, "error"), -1);
|
||||
|
||||
exdata_schema_free(schema);
|
||||
}
|
||||
|
||||
/******************************************
|
||||
* TEST EXDATA RUNTIME *
|
||||
******************************************/
|
||||
|
||||
struct gtest_exdata_runtime_env
|
||||
{
|
||||
int exdata_free_called;
|
||||
};
|
||||
|
||||
void gtest_exdata_runtime_free(int idx, void *ex_ptr, void *arg)
|
||||
{
|
||||
struct gtest_exdata_runtime_env *env=(struct gtest_exdata_runtime_env *)arg;
|
||||
env->exdata_free_called++;
|
||||
}
|
||||
|
||||
TEST(exdata_runtime, basic_set_get)
|
||||
{
|
||||
gtest_exdata_runtime_env env={};
|
||||
struct exdata_schema *schema=exdata_schema_new();
|
||||
int exdata_idx=exdata_schema_new_index(schema, "gtest", gtest_exdata_runtime_free,
|
||||
&env);
|
||||
struct exdata_runtime *exdata_rt=exdata_runtime_new(schema);
|
||||
|
||||
EXPECT_EQ(exdata_get(exdata_rt, exdata_idx), (void *)0);//default value null
|
||||
|
||||
EXPECT_EQ(exdata_set(exdata_rt, exdata_idx, (void *)1), 0);
|
||||
EXPECT_EQ(exdata_get(exdata_rt, exdata_idx), (void *)1);
|
||||
|
||||
EXPECT_EQ(exdata_set(exdata_rt, exdata_idx, (void *)2), 0);
|
||||
EXPECT_EQ(exdata_get(exdata_rt, exdata_idx), (void *)2);
|
||||
|
||||
exdata_runtime_reset(exdata_rt);//call free_func, and set ex_ptr to NULL
|
||||
EXPECT_EQ(exdata_get(exdata_rt, exdata_idx), (void *)0);
|
||||
|
||||
EXPECT_EQ(exdata_set(exdata_rt, exdata_idx, (void *)3), 0);
|
||||
EXPECT_EQ(exdata_get(exdata_rt, exdata_idx), (void *)3);
|
||||
|
||||
exdata_runtime_free(exdata_rt);
|
||||
exdata_schema_free(schema);
|
||||
|
||||
EXPECT_EQ(env.exdata_free_called, 2);//runtime_reset_cnt+runtime_free
|
||||
}
|
||||
|
||||
TEST(exdata_runtime, illegal_set_get)
|
||||
{
|
||||
gtest_exdata_runtime_env env={};
|
||||
struct exdata_schema *schema=exdata_schema_new();
|
||||
int exdata_idx=exdata_schema_new_index(schema, "gtest", gtest_exdata_runtime_free,
|
||||
&env);
|
||||
struct exdata_runtime *exdata_rt=exdata_runtime_new(schema);
|
||||
|
||||
EXPECT_EQ(exdata_set(exdata_rt, -1, (void *)1), -1);//illegal idx
|
||||
EXPECT_EQ(exdata_set(exdata_rt, exdata_idx+10, (void *)1), -1);//illegal idx
|
||||
|
||||
EXPECT_EQ(exdata_get(exdata_rt, -1), (void *)0);//illegal idx
|
||||
EXPECT_EQ(exdata_get(exdata_rt, exdata_idx+10), (void *)0);//illegal idx
|
||||
EXPECT_EQ(exdata_get(exdata_rt, exdata_idx), (void *)0);//default value null
|
||||
|
||||
EXPECT_EQ(exdata_set(exdata_rt, exdata_idx, (void *)1), 0);
|
||||
EXPECT_EQ(exdata_get(exdata_rt, exdata_idx), (void *)1);
|
||||
|
||||
exdata_runtime_reset(exdata_rt);//call free_func, and set ex_ptr to NULL
|
||||
|
||||
EXPECT_EQ(exdata_get(exdata_rt, exdata_idx), (void *)0);
|
||||
|
||||
exdata_runtime_free(exdata_rt);
|
||||
exdata_schema_free(schema);
|
||||
|
||||
EXPECT_EQ(env.exdata_free_called, 1);
|
||||
}
|
||||
|
||||
/**********************************************
|
||||
* GTEST MAIN *
|
||||
**********************************************/
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
int ret=0;
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
ret=RUN_ALL_TESTS();
|
||||
return ret;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user