Skip to content

Commit

Permalink
Add more tests for dynFunction_parse.
Browse files Browse the repository at this point in the history
  • Loading branch information
PengZheng committed Jan 13, 2024
1 parent bfc57bd commit 1c370b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
7 changes: 6 additions & 1 deletion libs/dfi/gtest/src/dyn_function_ei_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ TEST_F(DynFunctionErrorInjectionTestSuite, ParseError) {
celix_ei_expect_ffi_prep_cif((void*)dynFunction_parse, 1, FFI_BAD_TYPEDEF);
rc = dynFunction_parseWithStr(EXAMPLE1_DESCRIPTOR, nullptr, &dynFunc);
EXPECT_NE(0, rc);
EXPECT_STREQ("Error initializing cif 1", celix_err_popLastError());
EXPECT_STREQ("Error ffi_prep_cif 1", celix_err_popLastError());

celix_ei_expect_calloc((void*)dynFunction_parse, 1, nullptr, 4);
rc = dynFunction_parseWithStr(EXAMPLE1_DESCRIPTOR, nullptr, &dynFunc);
EXPECT_NE(0, rc);
EXPECT_STREQ("Error allocating memory for ffi args", celix_err_popLastError());

celix_ei_expect_fmemopen((void*)dynFunction_parseWithStr, 0, nullptr);
rc = dynFunction_parseWithStr(EXAMPLE1_DESCRIPTOR, nullptr, &dynFunc);
Expand Down
18 changes: 10 additions & 8 deletions libs/dfi/src/dyn_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ int dynFunction_parse(FILE* descriptor, struct types_head* refTypes, dyn_functio
celix_err_pushf("Error parsing descriptor");
return status;
}
int rc = dynFunction_initCif(dynFunc);
if (rc != 0) {
return ERROR;
status = dynFunction_initCif(dynFunc);
if (status != OK) {
return status;
}

dyn_function_argument_type* arg = NULL;
Expand Down Expand Up @@ -161,15 +161,17 @@ enum dyn_function_argument_meta dynFunction_argumentMetaForIndex(const dyn_funct


static int dynFunction_initCif(dyn_function_type* dynFunc) {
int status = 0;

unsigned int nargs = 0;
dyn_function_argument_type* entry = NULL;
TAILQ_FOREACH(entry, &dynFunc->arguments, entries) {
nargs +=1;
}

dynFunc->ffiArguments = calloc(nargs, sizeof(ffi_type*));
if (dynFunc->ffiArguments == NULL) {
celix_err_push("Error allocating memory for ffi args");
return MEM_ERROR;
}

TAILQ_FOREACH(entry, &dynFunc->arguments, entries) {
dynFunc->ffiArguments[entry->index] = dynType_ffiType(entry->type);
Expand All @@ -180,11 +182,11 @@ static int dynFunction_initCif(dyn_function_type* dynFunc) {

int ffiResult = ffi_prep_cif(&dynFunc->cif, FFI_DEFAULT_ABI, nargs, returnType, args);
if (ffiResult != FFI_OK) {
celix_err_pushf("Error initializing cif %d", ffiResult);
status = 1;
celix_err_pushf("Error ffi_prep_cif %d", ffiResult);
return ERROR;
}

return status;
return OK;
}

void dynFunction_destroy(dyn_function_type* dynFunc) {
Expand Down

0 comments on commit 1c370b1

Please sign in to comment.