CELIX-237: Added dyn_interface on top of dyn_function / dyn_type -> simplified descrriptor translator
Project: http://git-wip-us.apache.org/repos/asf/celix/repo Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/eb9ec11e Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/eb9ec11e Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/eb9ec11e Branch: refs/heads/feature/CELIX-237_rsa-ffi Commit: eb9ec11eac8bb0c9a1a31552fb83e3c38138684e Parents: d26eb2a Author: Pepijn Noltes <[email protected]> Authored: Wed Jul 8 16:20:05 2015 +0200 Committer: Pepijn Noltes <[email protected]> Committed: Wed Jul 8 16:20:05 2015 +0200 ---------------------------------------------------------------------- .../dynamic_function_interface/CMakeLists.txt | 1 + .../avro_descriptor_translator.c | 199 ++++++++++--------- .../descriptor_translator.h | 22 +- .../dynamic_function_interface/dfi_log_util.h | 2 - .../dynamic_function_interface/dyn_function.c | 8 +- .../dynamic_function_interface/dyn_function.h | 4 +- .../dynamic_function_interface/dyn_interface.c | 47 +++++ .../dynamic_function_interface/dyn_interface.h | 46 +++++ .../tst/avro_descriptor_translator_tests.cpp | 142 +++++++++---- .../tst/dyn_closure_tests.cpp | 6 - 10 files changed, 317 insertions(+), 160 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/celix/blob/eb9ec11e/remote_services/dynamic_function_interface/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/CMakeLists.txt b/remote_services/dynamic_function_interface/CMakeLists.txt index 06720dd..0c8cccb 100644 --- a/remote_services/dynamic_function_interface/CMakeLists.txt +++ b/remote_services/dynamic_function_interface/CMakeLists.txt @@ -21,6 +21,7 @@ endif() add_library(dfi dyn_type.c dyn_function.c + dyn_interface.c json_serializer.c avro_descriptor_translator.c ${MEMSTREAM_SOURCES} http://git-wip-us.apache.org/repos/asf/celix/blob/eb9ec11e/remote_services/dynamic_function_interface/avro_descriptor_translator.c ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/avro_descriptor_translator.c b/remote_services/dynamic_function_interface/avro_descriptor_translator.c index 06fad8b..cc7b3e6 100644 --- a/remote_services/dynamic_function_interface/avro_descriptor_translator.c +++ b/remote_services/dynamic_function_interface/avro_descriptor_translator.c @@ -15,27 +15,38 @@ #include "fmemopen.h" #endif +DFI_SETUP_LOG(descriptorTranslator) + static const int OK = 0; static const int MEM_ERROR = 1; static const int PARSE_ERROR = 2; static const int INV_ARG_ERROR = 2; -static int descriptorTranslator_createMethodDescriptor(interface_descriptor_type *desc, json_t *schema, const char *name, int index, json_t *message); +static int descriptorTranslator_createMethodInfo(dyn_interface_type *intf, json_t *schema, const char *name, int index, json_t *message); +static int descriptorTranslator_parseType(dyn_interface_type *intf, json_t *type); static int descriptorTranslator_parseMessage(json_t *schema, const char *name, json_t *message, bool asJavaSignature, char **descriptor); -static int descriptorTranslator_parseArgument(FILE *stream, json_t *schema, json_t *type); -static int descriptorTranslator_parseType(FILE *stream, json_t *schema, const char *typeName); +static int descriptorTranslator_parseArgument(FILE *stream, json_t *type); -int descriptorTranslator_create(const char *schemaStr, interface_descriptor_type **out) { +int descriptorTranslator_translate(const char *schemaStr, dyn_interface_type **out) { + LOG_DEBUG("translating descriptor for schema '%s'\n", schemaStr); int status = OK; - interface_descriptor_type *desc = calloc(1, sizeof(*desc)); - if (desc != NULL) { - TAILQ_INIT(&desc->methodDescriptors); + dyn_interface_type *intf = NULL; + status = dynInterface_create("TODO", &intf); + if (status == 0) { json_error_t error; json_t *schema = json_loads(schemaStr, JSON_DECODE_ANY, &error); if (schema != NULL) { - //TODO + json_t *types = json_object_get(schema, "types"); + if (types != NULL) { + json_t *type = NULL; + int index = 0; + json_array_foreach(types, index, type) { + descriptorTranslator_parseType(intf, type); + } + + } json_t *messages = json_object_get(schema, "messages"); if (messages != NULL) { const char *name; @@ -43,7 +54,7 @@ int descriptorTranslator_create(const char *schemaStr, interface_descriptor_type int rc = 0; int index = 0; json_object_foreach(messages, name, message) { - rc = descriptorTranslator_createMethodDescriptor(desc, schema, name, index++, message); + rc = descriptorTranslator_createMethodInfo(intf, schema, name, index++, message); if (rc != OK) { break; } @@ -59,41 +70,27 @@ int descriptorTranslator_create(const char *schemaStr, interface_descriptor_type } - if (status == 0) { - *out = desc; - } else if (status == MEM_ERROR) { - printf("AVRO_DESCRIPTOR_TRANSLATOR: error cannot allocate memory\n"); - descriptorTranslator_destroy(desc); - } else { - descriptorTranslator_destroy(desc); - } - return status; -} - -int descriptorTranslator_destroy(interface_descriptor_type *desc) { - int status = OK; - if (desc != NULL) { - //TODO free existing members - free(desc); + if (status == OK) { + *out = intf; } else { - status = INV_ARG_ERROR; + dynInterface_destroy(intf); } return status; } -static int descriptorTranslator_createMethodDescriptor(interface_descriptor_type *desc, json_t *schema, const char *name, int index, json_t *message) { +static int descriptorTranslator_createMethodInfo(dyn_interface_type *intf, json_t *schema, const char *name, int index, json_t *message) { int status = OK; - method_descriptor_type *mDesc = calloc(1, sizeof(*mDesc)); - if (mDesc != NULL) { - mDesc->identifier = index; - status = descriptorTranslator_parseMessage(schema, name, message, false, &mDesc->descriptor); + method_info_type *mInfo = calloc(1, sizeof(*mInfo)); + if (mInfo != NULL) { + mInfo->identifier = index; + status = descriptorTranslator_parseMessage(schema, name, message, false, &mInfo->descriptor); if (status == OK) { - mDesc->name = strdup(name); - if (mDesc->name == NULL) { + mInfo->name = strdup(name); + if (mInfo->name == NULL) { status = MEM_ERROR; } else { - status = descriptorTranslator_parseMessage(schema, name, message, true, &mDesc->strIdentifier); + status = descriptorTranslator_parseMessage(schema, name, message, true, &mInfo->strIdentifier); } } } else { @@ -101,19 +98,19 @@ static int descriptorTranslator_createMethodDescriptor(interface_descriptor_type } if (status == OK) { - TAILQ_INSERT_TAIL(&desc->methodDescriptors, mDesc, entries); + TAILQ_INSERT_TAIL(&intf->methodInfos, mInfo, entries); } else { - if (mDesc != NULL) { - if (mDesc->name != NULL) { - free(mDesc->name); + if (mInfo != NULL) { + if (mInfo->name != NULL) { + free(mInfo->name); } - if (mDesc->descriptor != NULL) { - free(mDesc->descriptor); + if (mInfo->descriptor != NULL) { + free(mInfo->descriptor); } - if (mDesc->strIdentifier != NULL) { - free(mDesc->strIdentifier); + if (mInfo->strIdentifier != NULL) { + free(mInfo->strIdentifier); } - free(mDesc); + free(mInfo); } } @@ -144,7 +141,7 @@ static int descriptorTranslator_parseMessage(json_t *schema, const char *name, j //json_t *name = json_object_get(arg, "name"); json_t *type = json_object_get(arg, "type"); if (type != NULL) { - status = descriptorTranslator_parseArgument(memStream, schema, type); + status = descriptorTranslator_parseArgument(memStream, type); } else { printf("expected type for request argument %zu for message %s\n", index, name); status = PARSE_ERROR; @@ -165,7 +162,7 @@ static int descriptorTranslator_parseMessage(json_t *schema, const char *name, j } else { fputc('*', memStream); //output parameter } - status = descriptorTranslator_parseArgument(memStream, schema, response); + status = descriptorTranslator_parseArgument(memStream, response); } if (!asJavaSignature) { @@ -197,7 +194,7 @@ static const char * const PRIMITIVE_DOUBLE = "double"; static const char * const PRIMITIVE_NULL = "null"; static const char * const PRIMITIVE_BYTES = "bytes"; -static int descriptorTranslator_parseArgument(FILE *stream, json_t *schema, json_t *type) { +static int descriptorTranslator_parseArgument(FILE *stream, json_t *type) { int status = OK; if (json_is_string(type)) { const char *typeStr = json_string_value(type); @@ -219,7 +216,7 @@ static int descriptorTranslator_parseArgument(FILE *stream, json_t *schema, json } else if (strcmp(typeStr, PRIMITIVE_BYTES) == 0) { t = 'B'; } else { - status = descriptorTranslator_parseType(stream, schema, typeStr); + fprintf(stream, "L%s;", typeStr); } if (t != '\0') { fputc(t, stream); @@ -230,7 +227,7 @@ static int descriptorTranslator_parseArgument(FILE *stream, json_t *schema, json if (strcmp("array", json_string_value(subType)) == 0) { //array fputc('[', stream); - descriptorTranslator_parseArgument(stream, schema, items); + descriptorTranslator_parseArgument(stream, items); } else { printf("sub type %s not supported\n", json_string_value(subType)); status = PARSE_ERROR; @@ -239,60 +236,82 @@ static int descriptorTranslator_parseArgument(FILE *stream, json_t *schema, json return status; } - -static int descriptorTranslator_parseType(FILE *stream, json_t *schema, const char *typeName) { +static int descriptorTranslator_parseType(dyn_interface_type *intf, json_t *type) { int status = OK; + const char *name = json_string_value(json_object_get(type, "name")); + type_info_type *tInfo = NULL; - json_t *types = json_object_get(schema, "types"); - json_t *type = NULL; - if (json_is_array(types)) { - json_t *el; - int index; - json_array_foreach(types, index, el) { - const char *name = json_string_value(json_object_get(el, "name")); - if (strcmp(typeName, name) == 0) { - type = el; - break; - } - } + char *ptr = NULL; + size_t ptrSize; + FILE *stream = open_memstream(&ptr, &ptrSize); - if (el != NULL) { - fputc('{', stream); - json_t *fields = json_object_get(type, "fields"); - if (json_is_array(fields)) { - json_t *field; + if (stream != NULL) { + fputc('{', stream); + json_t *fields = json_object_get(type, "fields"); + if (json_is_array(fields)) { + int index = 0; + json_t *field = NULL; + json_array_foreach(fields, index, field) { + json_t *type = json_object_get(field, "type"); + status = descriptorTranslator_parseArgument(stream, type); + if (status != OK) { + break; + } + } + if (status == OK) { json_array_foreach(fields, index, field) { - json_t *type = json_object_get(field, "type"); - status = descriptorTranslator_parseArgument(stream, schema, type); - if (status != OK) { + const char *fieldName = json_string_value(json_object_get(field, "name")); + if (fieldName != NULL) { + fputc(' ', stream); + fwrite(fieldName, 1, strlen(fieldName), stream); + } else { + status = PARSE_ERROR; + printf("Expected name for field\n"); break; } } - if (status == OK) { - json_array_foreach(fields, index, field) { - const char *fieldName = json_string_value(json_object_get(field, "name")); - if (fieldName != NULL) { - fputc(' ', stream); - fwrite(fieldName, 1, strlen(fieldName), stream); - } else { - status = PARSE_ERROR; - printf("Expected name for field\n"); - break; - } - } - } - } else { - status = PARSE_ERROR; - printf("Expected array type"); } - fputc('}', stream); } else { status = PARSE_ERROR; - printf("cannot find type with name %s\n", typeName); + printf("Expected array type"); } + fputc('}', stream); + fclose(stream); } else { - status = PARSE_ERROR; - printf("Expected array type\n"); + status = MEM_ERROR; + LOG_ERROR("Error creating memory stream"); + } + + if (status == OK) { + tInfo = calloc(1, sizeof(*tInfo)); + if (tInfo != NULL) { + tInfo->name = strdup(name); + if (tInfo->name != NULL) { + tInfo->descriptor = ptr; + } else { + status = MEM_ERROR; + LOG_ERROR("Error allocating memory for type info name"); + } + } else { + status = MEM_ERROR; + LOG_ERROR("Error allocating memory for type_info"); + } + } + + if (status != 0 ) { + if (tInfo != NULL) { + if (tInfo->name != NULL) { + free(tInfo->name); + } + free(tInfo); + } + if (ptr != NULL) { + free(ptr); + } + } + + if (status == OK) { + TAILQ_INSERT_TAIL(&intf->typeInfos, tInfo, entries); } return status; http://git-wip-us.apache.org/repos/asf/celix/blob/eb9ec11e/remote_services/dynamic_function_interface/descriptor_translator.h ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/descriptor_translator.h b/remote_services/dynamic_function_interface/descriptor_translator.h index b75c8d7..6473d0d 100644 --- a/remote_services/dynamic_function_interface/descriptor_translator.h +++ b/remote_services/dynamic_function_interface/descriptor_translator.h @@ -4,25 +4,15 @@ #ifndef __DESCRIPTOR_TRANSLATOR_H_ #define __DESCRIPTOR_TRANSLATOR_H_ +#include <stdio.h> #include <sys/queue.h> -typedef struct _interface_descriptor_type interface_descriptor_type; +#include "dfi_log_util.h" +#include "dyn_interface.h" -struct _interface_descriptor_type { - TAILQ_HEAD(, _method_descriptor_type) methodDescriptors; -}; +//logging +DFI_SETUP_LOG_HEADER(descriptorTranslator); -typedef struct _method_descriptor_type method_descriptor_type; - -struct _method_descriptor_type { - int identifier; - char *strIdentifier; - char *descriptor; - char *name; - TAILQ_ENTRY(_method_descriptor_type) entries; -}; - -int descriptorTranslator_create(const char *schemaStr, interface_descriptor_type **out); -int descriptorTranslator_destroy(interface_descriptor_type *desc); +int descriptorTranslator_translate(const char *schemaStr, dyn_interface_type **out); #endif http://git-wip-us.apache.org/repos/asf/celix/blob/eb9ec11e/remote_services/dynamic_function_interface/dfi_log_util.h ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dfi_log_util.h b/remote_services/dynamic_function_interface/dfi_log_util.h index 85208ff..80fecfb 100644 --- a/remote_services/dynamic_function_interface/dfi_log_util.h +++ b/remote_services/dynamic_function_interface/dfi_log_util.h @@ -4,8 +4,6 @@ #ifndef _DFI_LOG_UTIL_H_ #define _DFI_LOG_UTIL_H_ -#define DFI_TEST(cmp) printf("%s", cmp); - typedef void (*logf_ft)(void *handle, int level, const char *file, int line, const char *format, ...); #define DFI_SETUP_LOG_HEADER(cmp) \ http://git-wip-us.apache.org/repos/asf/celix/blob/eb9ec11e/remote_services/dynamic_function_interface/dyn_function.c ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_function.c b/remote_services/dynamic_function_interface/dyn_function.c index 9d0de7d..96804e7 100644 --- a/remote_services/dynamic_function_interface/dyn_function.c +++ b/remote_services/dynamic_function_interface/dyn_function.c @@ -120,8 +120,7 @@ static int dynFunction_initCif(ffi_cif *cif, dyn_type *arguments, dyn_type *retu return result; } -int dynFunction_destroy(dyn_function_type *dynFunc) { - int result = 0; +void dynFunction_destroy(dyn_function_type *dynFunc) { if (dynFunc != NULL) { if (dynFunc->arguments != NULL) { dynType_destroy(dynFunc->arguments); @@ -131,7 +130,6 @@ int dynFunction_destroy(dyn_function_type *dynFunc) { } free(dynFunc); } - return result; } int dynFunction_call(dyn_function_type *dynFunc, void *returnValue, void **argValues) { @@ -186,8 +184,7 @@ int dynClosure_getFnPointer(dyn_closure_type *dynClosure, void (**fn)(void)) { } -int dynClosure_destroy(dyn_closure_type *dynClosure) { - int result = 0; +void dynClosure_destroy(dyn_closure_type *dynClosure) { if (dynClosure != NULL) { if (dynClosure->arguments != NULL) { dynType_destroy(dynClosure->arguments); @@ -200,5 +197,4 @@ int dynClosure_destroy(dyn_closure_type *dynClosure) { } free(dynClosure); } - return result; } http://git-wip-us.apache.org/repos/asf/celix/blob/eb9ec11e/remote_services/dynamic_function_interface/dyn_function.h ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_function.h b/remote_services/dynamic_function_interface/dyn_function.h index 5f7be35..4d3536e 100644 --- a/remote_services/dynamic_function_interface/dyn_function.h +++ b/remote_services/dynamic_function_interface/dyn_function.h @@ -20,11 +20,11 @@ typedef struct _dyn_closure_type dyn_closure_type; DFI_SETUP_LOG_HEADER(dynFunction); int dynFunction_create(const char *descriptor, dyn_type_list_type *typeReferences, void (*fn)(void), dyn_function_type **dynFunc); -int dynFunction_destroy(dyn_function_type *dynFunc); +void dynFunction_destroy(dyn_function_type *dynFunc); int dynFunction_call(dyn_function_type *dynFunc, void *returnValue, void **argValues); int dynClosure_create(const char *descriptor, dyn_type_list_type *typeReferences, void (*bind)(void *, void **, void*), void *userData, dyn_closure_type **out); int dynClosure_getFnPointer(dyn_closure_type *dynClosure, void(**fn)(void)); -int dynClosure_destroy(dyn_closure_type *dynClosure); +void dynClosure_destroy(dyn_closure_type *dynClosure); #endif http://git-wip-us.apache.org/repos/asf/celix/blob/eb9ec11e/remote_services/dynamic_function_interface/dyn_interface.c ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_interface.c b/remote_services/dynamic_function_interface/dyn_interface.c new file mode 100644 index 0000000..bb0b525 --- /dev/null +++ b/remote_services/dynamic_function_interface/dyn_interface.c @@ -0,0 +1,47 @@ +/** + * Licensed under Apache License v2. See LICENSE for more information. + */ +#include "dyn_interface.h" + +#include <stdlib.h> +#include <string.h> + +DFI_SETUP_LOG(dynInterface) + +int dynInterface_create(const char *name, dyn_interface_type **out) { + int status = 0; + dyn_interface_type *inft = calloc(1, sizeof(*inft)); + if (inft != NULL) { + inft->name = strdup(name); + } + if (inft == NULL || inft->name == NULL) { + status = 1; + LOG_ERROR("Error allocating memory for dynamic interface\n"); + } + + if (status == 0) { + TAILQ_INIT(&inft->typeInfos); + TAILQ_INIT(&inft->methodInfos); + } + *out = inft; + return status; +} + +void dynInterface_destroy(dyn_interface_type *intf) { + if (intf != NULL) { + if (intf->name != NULL) { + free(intf->name); + } + type_info_type *tInfo = NULL; + TAILQ_FOREACH(tInfo, &intf->typeInfos, entries) { + LOG_WARNING("TODO"); + //TODO add destroy func for type_info + } + method_info_type *mInfo = NULL; + TAILQ_FOREACH(mInfo, &intf->typeInfos, entries) { + LOG_WARNING("TODO"); + //TODO add destroy func for method + } + } +} + http://git-wip-us.apache.org/repos/asf/celix/blob/eb9ec11e/remote_services/dynamic_function_interface/dyn_interface.h ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_interface.h b/remote_services/dynamic_function_interface/dyn_interface.h new file mode 100644 index 0000000..5d0b427 --- /dev/null +++ b/remote_services/dynamic_function_interface/dyn_interface.h @@ -0,0 +1,46 @@ +/* + * Licensed under Apache License v2. See LICENSE for more information. + */ +#ifndef __DYN_INTERFACE_H_ +#define __DYN_INTERFACE_H_ + +#include "dyn_type.h" +#include "dyn_function.h" +#include "dfi_log_util.h" + +DFI_SETUP_LOG_HEADER(dynInterface); + +typedef struct _dyn_interface_type dyn_interface_type; + +struct _dyn_interface_type { + char *name; + TAILQ_HEAD(, _type_info_type) typeInfos; + TAILQ_HEAD(, _method_info_type) methodInfos; +}; + +typedef struct _method_info_type method_info_type; +struct _method_info_type { + int identifier; + char *strIdentifier; + char *descriptor; + char *name; + + dyn_function_type *dynFunc; + dyn_closure_type *dynClosure; + + TAILQ_ENTRY(_method_info_type) entries; +}; + +typedef struct _type_info_type type_info_type; +struct _type_info_type { + char *name; + char *descriptor; + TAILQ_ENTRY(_type_info_type) entries; +}; + + +int dynInterface_create(const char *name, dyn_interface_type **out); +void dynInterface_destroy(dyn_interface_type *intf); + + +#endif http://git-wip-us.apache.org/repos/asf/celix/blob/eb9ec11e/remote_services/dynamic_function_interface/tst/avro_descriptor_translator_tests.cpp ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/tst/avro_descriptor_translator_tests.cpp b/remote_services/dynamic_function_interface/tst/avro_descriptor_translator_tests.cpp index 32ba044..87fd76a 100644 --- a/remote_services/dynamic_function_interface/tst/avro_descriptor_translator_tests.cpp +++ b/remote_services/dynamic_function_interface/tst/avro_descriptor_translator_tests.cpp @@ -17,56 +17,122 @@ extern "C" { #include "fmemopen.h" #endif -void test1(void) { - //TODO split up - size_t size = 0; - char *ptr = NULL; - - //first argument void *handle, last argument output pointer for result and return int with status for exception handling - //sum(DD)D -> sum(PDD*D)N - //sub(DD)D -> sub(PDD*D)N - //sqrt(D)D -> sqrt(PD*D)N - FILE *schema = fopen("schemas/simple.avpr", "r"); - FILE *stream = open_memstream(&ptr, &size); - - int c = fgetc(schema); - while (c != EOF ) { - fputc(c, stream); - c = fgetc(schema); + static void stdLog(void *handle, int level, const char *file, int line, const char *msg, ...) { + va_list ap; + const char *levels[5] = {"NIL", "ERROR", "WARNING", "INFO", "DEBUG"}; + fprintf(stderr, "%s: FILE:%s, LINE:%i, MSG:",levels[level], file, line); + va_start(ap, msg); + vfprintf(stderr, msg, ap); + fprintf(stderr, "\n"); } - fclose(schema); - fclose(stream); - - assert(schema != NULL && stream != NULL); - interface_descriptor_type *ift= NULL; - int status = descriptorTranslator_create(ptr, &ift); - CHECK_EQUAL(0, status); - - int count = 0; - method_descriptor_type *mDesc = NULL; - TAILQ_FOREACH(mDesc, &ift->methodDescriptors, entries) { - count +=1; + + + static char *readSchema(const char *file) { + size_t size = 0; + char *ptr = NULL; + + FILE *schema = fopen(file, "r"); + FILE *stream = open_memstream(&ptr, &size); + + assert(schema != NULL); + assert(stream != NULL); + + int c = fgetc(schema); + while (c != EOF ) { + fputc(c, stream); + c = fgetc(schema); + } + fclose(schema); + fclose(stream); + + assert(ptr != NULL); + return ptr; + } + + static dyn_interface_type *createInterfaceInfo(const char *schemaFile) { + char *schema = readSchema(schemaFile); + dyn_interface_type *ift= NULL; + + int status = descriptorTranslator_translate(schema, &ift); + CHECK_EQUAL(0, status); + + free(schema); + return ift; + } + + static int countMethodInfos(dyn_interface_type *info) { + int count = 0; + method_info_type *mInfo = NULL; + TAILQ_FOREACH(mInfo, &info->methodInfos, entries) { + count +=1; + } + return count; } - CHECK_EQUAL(3, count); - - TAILQ_FOREACH(mDesc, &ift->methodDescriptors, entries) { - if (strcmp("sum", mDesc->name) == 0) { - STRCMP_EQUAL("sum(PDD*D)N", mDesc->descriptor); - } else if (strcmp("add", mDesc->name) == 0) { - STRCMP_EQUAL("add(PDD*D)N", mDesc->descriptor); - } else if (strcmp("sqrt", mDesc->name) == 0) { - STRCMP_EQUAL("sqrt(PD*D)N", mDesc->descriptor); + + static int countTypeInfos(dyn_interface_type *info) { + int count = 0; + type_info_type *tInfo = NULL; + TAILQ_FOREACH(tInfo, &info->typeInfos, entries) { + count +=1; } + return count; + } + + static void test1(void) { + //first argument void *handle, last argument output pointer for result and return int with status for exception handling + //sum(DD)D -> sum(PDD*D)N + //sub(DD)D -> sub(PDD*D)N + //sqrt(D)D -> sqrt(PD*D)N + + dyn_interface_type *ift = createInterfaceInfo("schemas/simple.avpr"); + + int count = countMethodInfos(ift); + CHECK_EQUAL(3, count); + + count = countTypeInfos(ift); + CHECK_EQUAL(0, count); + + method_info_type *mInfo = NULL; + TAILQ_FOREACH(mInfo, &ift->methodInfos, entries) { + if (strcmp("sum", mInfo->name) == 0) { + STRCMP_EQUAL("sum(PDD*D)N", mInfo->descriptor); + } else if (strcmp("add", mInfo->name) == 0) { + STRCMP_EQUAL("add(PDD*D)N", mInfo->descriptor); + } else if (strcmp("sqrt", mInfo->name) == 0) { + STRCMP_EQUAL("sqrt(PD*D)N", mInfo->descriptor); + } + } + } + + static void test2(void) { + dyn_interface_type *ift = createInterfaceInfo("schemas/complex.avpr"); + + int count = countMethodInfos(ift); + CHECK_EQUAL(1, count); + + method_info_type *mInfo = TAILQ_FIRST(&ift->methodInfos); + STRCMP_EQUAL("stats(P[D*LStatResult;)N", mInfo->descriptor); + + count = countTypeInfos(ift); + CHECK_EQUAL(1, count); + + type_info_type *tInfo = TAILQ_FIRST(&ift->typeInfos); + STRCMP_EQUAL("StatResult", tInfo->name); + STRCMP_EQUAL("{DDD[D sum min max input}", tInfo->descriptor); } -} } TEST_GROUP(AvroDescTranslatorTest) { void setup() { + descriptorTranslator_logSetup(stdLog, NULL, 4); } }; TEST(AvroDescTranslatorTest, Test1) { test1(); } + +TEST(AvroDescTranslatorTest, Test2) { + test2(); +} http://git-wip-us.apache.org/repos/asf/celix/blob/eb9ec11e/remote_services/dynamic_function_interface/tst/dyn_closure_tests.cpp ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/tst/dyn_closure_tests.cpp b/remote_services/dynamic_function_interface/tst/dyn_closure_tests.cpp index a017d91..94808d9 100644 --- a/remote_services/dynamic_function_interface/tst/dyn_closure_tests.cpp +++ b/remote_services/dynamic_function_interface/tst/dyn_closure_tests.cpp @@ -27,7 +27,6 @@ static void stdLog(void *handle, int level, const char *file, int line, const ch #define EXAMPLE1_DESCRIPTOR "example(III)I" static void example1_binding(void *userData, void* args[], void *out) { - printf("example1 closure called\n"); int32_t a = *((int32_t *)args[0]); int32_t b = *((int32_t *)args[1]); int32_t c = *((int32_t *)args[2]); @@ -43,7 +42,6 @@ struct example2_arg2 { double val3; }; void example2_binding(void *userData, void* args[], void *out) { - printf("example2 closure called\n"); int32_t a = *((int32_t *)args[0]); struct example2_arg2 b = *((struct example2_arg2 *)args[1]); int32_t c = *((int32_t *)args[2]); @@ -61,7 +59,6 @@ struct example3_ret { }; static void example3_binding(void *userData, void* args[], void *out) { - printf("example closure called\n"); int32_t a = *((int32_t *)args[0]); int32_t b = *((int32_t *)args[1]); int32_t c = *((int32_t *)args[2]); @@ -88,7 +85,6 @@ static void tests() { int rc = dynClosure_getFnPointer(dynClosure, (void(**)(void))&func); CHECK_EQUAL(0, rc); int32_t ret = func(2,3,4); - //printf("Return value for example1 is %i\n", ret); CHECK_EQUAL(1, g_count); CHECK_EQUAL(9, ret); dynClosure_destroy(dynClosure); @@ -106,7 +102,6 @@ static void tests() { b.val2 = 1.5; b.val3 = 2.0; double ret = func(2,b,4); - //printf("Return value for example2 is %f\n", ret); CHECK_EQUAL(2, g_count); CHECK_EQUAL(10.5, ret); dynClosure_destroy(dynClosure); @@ -120,7 +115,6 @@ static void tests() { rc = dynClosure_getFnPointer(dynClosure, (void(**)(void))&func); CHECK_EQUAL(0, rc); struct example3_ret *ret = func(2,8,4); - //printf("Return value for example3 is {sum:%i, max:%i, min:%i}\n", ret->sum, ret->max, ret->min); CHECK_EQUAL(3, g_count); CHECK_EQUAL(14, ret->sum); dynClosure_destroy(dynClosure);
