static void tests() { dyn_function_type *dynFunction = NULL; int rc = 0; { int32_t (*func)(int32_t a, int32_t b, int32_t c) = NULL; rc = dynFunction_parseWithStr(EXAMPLE1_DESCRIPTOR, NULL, &dynFunction); CHECK_EQUAL(0, rc); rc = dynFunction_createClosure(dynFunction, example1_binding, NULL, (void(**)(void))&func); CHECK_EQUAL(0, rc); int32_t ret = func(2,3,4); CHECK_EQUAL(1, g_count); CHECK_EQUAL(9, ret); dynFunction_destroy(dynFunction); } { double (*func)(int32_t a, struct example2_arg2 b, int32_t c) = NULL; double (*func2)(int32_t a, struct example2_arg2 b, int32_t c) = NULL; dynFunction = NULL; rc = dynFunction_parseWithStr(EXAMPLE2_DESCRIPTOR, NULL, &dynFunction); CHECK_EQUAL(0, rc); rc = dynFunction_createClosure(dynFunction, example2_binding, NULL, (void(**)(void))&func); CHECK_EQUAL(0, rc); rc = dynFunction_getFnPointer(dynFunction, (void(**)(void))&func2); CHECK_EQUAL(0, rc); CHECK(func == func2); struct example2_arg2 b; b.val1 = 1.0; b.val2 = 1.5; b.val3 = 2.0; double ret = func(2,b,4); CHECK_EQUAL(2, g_count); CHECK_EQUAL(10.5, ret); dynFunction_destroy(dynFunction); } { struct example3_ret * (*func)(int32_t a, int32_t b, int32_t c) = NULL; dynFunction = NULL; rc = dynFunction_parseWithStr(EXAMPLE3_DESCRIPTOR, NULL, &dynFunction); CHECK_EQUAL(0, rc); rc = dynFunction_createClosure(dynFunction, example3_binding, NULL, (void(**)(void))&func); CHECK_EQUAL(0, rc); struct example3_ret *ret = func(2,8,4); CHECK_EQUAL(3, g_count); CHECK_EQUAL(14, ret->sum); dynFunction_destroy(dynFunction); free(ret); } }
static void test_example3(void) { dyn_function_type *dynFunc = NULL; void (*fp)(void) = (void(*)(void)) testExample3; int rc; rc = dynFunction_parseWithStr(EXAMPLE3_DESCRIPTOR, NULL, &dynFunc); CHECK_EQUAL(0, rc); double result = -1.0; double *input = &result; double a = 2.0; void *ptr = &a; void *args[3]; args[0] = &ptr; args[1] = &a; args[2] = &input; int rVal = 0; rc = dynFunction_call(dynFunc, fp, &rVal, args); CHECK_EQUAL(0, rc); CHECK_EQUAL(4.0, result); double *inMemResult = (double *)calloc(1, sizeof(double)); a = 2.0; ptr = &a; args[0] = &ptr; args[1] = &a; args[2] = &inMemResult; rVal = 0; rc = dynFunction_call(dynFunc, fp, &rVal, args); CHECK_EQUAL(0, rc); CHECK_EQUAL(4.0, result); free(inMemResult); dynFunction_destroy(dynFunc); }
void test_example2(void) { dyn_function_type *dynFunc = NULL; int rc; void (*fp)(void) = (void (*)(void)) example2; rc = dynFunction_parseWithStr(EXAMPLE2_DESCRIPTOR, NULL, &dynFunc); CHECK_EQUAL(0, rc); int32_t arg1 = 2; struct example2_arg arg2; arg2.val1 = 2; arg2.val2 = 3; arg2.val3 = 4.1; double arg3 = 8.1; double returnVal = 0; void *values[3]; values[0] = &arg1; values[1] = &arg2; values[2] = &arg3; rc = dynFunction_call(dynFunc, fp, &returnVal, values); CHECK_EQUAL(0, rc); CHECK_EQUAL(2.2, returnVal); dynFunction_destroy(dynFunc); }
void prepareTest(void) { dyn_function_type *dynFunc = NULL; int rc = dynFunction_parseWithStr("add(#am=handle;PDD#am=pre;*D)N", NULL, &dynFunc); CHECK_EQUAL(0, rc); char *result = NULL; void *handle = NULL; double arg1 = 1.0; double arg2 = 2.0; void *args[4]; args[0] = &handle; args[1] = &arg1; args[2] = &arg2; rc = jsonRpc_prepareInvokeRequest(dynFunc, "add", args, &result); CHECK_EQUAL(0, rc); //printf("result is %s\n", result); STRCMP_CONTAINS("\"add\"", result); STRCMP_CONTAINS("1.0", result); STRCMP_CONTAINS("2.0", result); free(result); dynFunction_destroy(dynFunc); }
void handleTestPre(void) { dyn_function_type *dynFunc = NULL; int rc = dynFunction_parseWithStr("add(#am=handle;PDD#am=pre;*D)N", NULL, &dynFunc); CHECK_EQUAL(0, rc); const char *reply = "{\"r\":2.2}"; double result = -1.0; double *out = &result; void *args[4]; args[3] = &out; rc = jsonRpc_handleReply(dynFunc, reply, args); CHECK_EQUAL(0, rc); //CHECK_EQUAL(2.2, result); dynFunction_destroy(dynFunc); }
void test_example1(void) { dyn_function_type *dynFunc = NULL; int rc; void (*fp)(void) = (void (*)(void)) example1; rc = dynFunction_parseWithStr(EXAMPLE1_DESCRIPTOR, NULL, &dynFunc); CHECK_EQUAL(0, rc); int32_t a = 2; int32_t b = 4; int32_t c = 8; void *values[3]; int32_t rVal = 0; values[0] = &a; values[1] = &b; values[2] = &c; rc = dynFunction_call(dynFunc, fp, &rVal, values); CHECK_EQUAL(0, rc); CHECK_EQUAL(1, rVal); dynFunction_destroy(dynFunc); }
static void test_access_functions(void) { dyn_function_type *dynFunc = NULL; int rc; rc = dynFunction_parseWithStr("add(D{DD a b}*D)V", NULL, &dynFunc); CHECK_EQUAL(0, rc); int nrOfArgs = dynFunction_nrOfArguments(dynFunc); CHECK_EQUAL(3, nrOfArgs); dyn_type *arg1 = dynFunction_argumentTypeForIndex(dynFunc, 1); CHECK(arg1 != NULL); CHECK_EQUAL('{', (char) dynType_descriptorType(arg1)); dyn_type *nonExist = dynFunction_argumentTypeForIndex(dynFunc, 10); CHECK(nonExist == NULL); dyn_type *returnType = dynFunction_returnType(dynFunc); CHECK_EQUAL('V', (char) dynType_descriptorType(returnType)); dynFunction_destroy(dynFunc); }
static void test_example4(void) { dyn_function_type *dynFunc = NULL; void (*fp)(void) = (void(*)(void)) example4Func; int rc; rc = dynFunction_parseWithStr(EXAMPLE4_DESCRIPTOR, NULL, &dynFunc); CHECK_EQUAL(0, rc); double buf[4]; buf[0] = 1.1; buf[1] = 2.2; struct tst_seq seq; seq.cap = 4; seq.len = 2; seq.buf = buf; void *args[1]; args[0] = &seq; rc = dynFunction_call(dynFunc, fp, NULL, args); CHECK_EQUAL(0, rc); dynFunction_destroy(dynFunc); }