]> gerrit.simantics Code Review - simantics/fmil.git/blobdiff - org.simantics.fmil.core/native/FMILibrary/Test/FMI1/fmi1_logger_test.c
Add FMILibrary-2.0.3 to org.simantics.fmil.core\native.
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / Test / FMI1 / fmi1_logger_test.c
diff --git a/org.simantics.fmil.core/native/FMILibrary/Test/FMI1/fmi1_logger_test.c b/org.simantics.fmil.core/native/FMILibrary/Test/FMI1/fmi1_logger_test.c
new file mode 100644 (file)
index 0000000..69964ff
--- /dev/null
@@ -0,0 +1,220 @@
+/*\r
+    Copyright (C) 2012 Modelon AB\r
+\r
+    This program is free software: you can redistribute it and/or modify\r
+    it under the terms of the BSD style license.\r
+\r
+    This program is distributed in the hope that it will be useful,\r
+    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+    FMILIB_License.txt file for more details.\r
+\r
+    You should have received a copy of the FMILIB_License.txt file\r
+    along with this program. If not, contact Modelon AB <http://www.modelon.com>.\r
+*/\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <stdarg.h>\r
+#include <assert.h>\r
+\r
+#include "config_test.h"\r
+#include <fmilib.h>\r
+#include <fmu_dummy/fmu1_model_defines.h>\r
+#include <JM/jm_portability.h>\r
+\r
+FILE* logFile;\r
+\r
+void logger(jm_callbacks* c, jm_string module, jm_log_level_enu_t log_level, jm_string message) {\r
+       char buf[10000];\r
+       const char* loadingstr = "Loading '";\r
+       /* need to replace platform specific message with a standard one */\r
+       if( (log_level == jm_log_level_info) && \r
+               (strcmp(module, "FMILIB") == 0) &&\r
+               (strncmp(message, "Loading '", strlen(loadingstr)) == 0)\r
+               )\r
+       {\r
+               jm_snprintf(buf, 10000, "[INFO][FMILIB] Loading '-----' binary with '------' platform types\n");\r
+       }\r
+       else\r
+               jm_snprintf(buf, 10000, "[%s][%s] %s\n", jm_log_level_to_string(log_level), module, message);\r
+       printf("%s", buf);\r
+       fprintf(logFile, "%s", buf);\r
+}\r
+\r
+void do_exit(int code)\r
+{\r
+       /* getchar();*/\r
+       exit(code);\r
+}\r
+          \r
+int test_logger(fmi1_import_t* fmu)\r
+{      \r
+       fmi1_status_t fmistatus; \r
+       jm_status_enu_t jmstatus;       \r
+\r
+       jmstatus = fmi1_import_instantiate_model(fmu, "LoggerTesting");\r
+       if (jmstatus == jm_status_error) {\r
+               printf("fmi1_import_instantiate_model failed\n");\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }\r
+\r
+       /* Logger message to print: */\r
+       {\r
+               fmi1_value_reference_t* vr;\r
+               size_t n;\r
+               size_t k;\r
+               const char* str[] = {\r
+                       "###### Reals ######",\r
+                       "OK HIGHT = #r0#", /* HIGHT */\r
+                       "OK HIGHT_SPEED = #r1#", /* HIGHT_SPEED */\r
+                       "OK GRAVITY = #r2#", /* GRAVITY */\r
+                       "OK BOUNCE_COF = #r3#", /* BOUNCE_COF */\r
+                       "Bad reference #r0",\r
+                       "Bad reference #r-0",\r
+                       "Bad reference #r-1",\r
+                       "Bad reference #r100#",\r
+                       "###### Integers ######",\r
+                       "OK LOGGER_TEST_INTEGER = #i0#", /* LOGGER_TEST_INTEGER */                      \r
+                       "Bad reference #i0",\r
+                       "Bad reference #i-0",\r
+                       "Bad reference #i-1",\r
+                       "Bad reference #i100#",\r
+                       "###### Booleans ######",\r
+                       "OK LOGGER_TEST_BOOLEAN = #b0#", /* LOGGER_TEST_BOOLEAN */      \r
+                       "Bad reference #b0",\r
+                       "Bad reference #b-0",\r
+                       "Bad reference #b-1",\r
+                       "Bad reference #b100#",\r
+                       "###### Strings ######",\r
+                       "OK LOGGER_TEST = #s0#", /* LOGGER_TEST */\r
+                       "Bad reference #s0",\r
+                       "Bad reference #s-0",\r
+                       "Bad reference #s-1",\r
+                       "Bad reference #s100#"\r
+               };\r
+\r
+               n = sizeof(str)/sizeof(*str);   \r
+               vr = calloc(n, sizeof(fmi1_value_reference_t));\r
+               for (k = 0; k < n; k++) {\r
+                       vr[k] = VAR_S_LOGGER_TEST;\r
+               }\r
+\r
+               fmistatus = fmi1_import_set_string(fmu, vr, n, str);\r
+        if(fmistatus != fmi1_status_ok) {\r
+            abort();\r
+        }              \r
+\r
+               { /* Print a really big message */\r
+\r
+#define MESSAGE_SIZE_TO_EXPAND_AND_PRINT 3000 /* Using fixed size since the log message is printed to a file and compared */\r
+#if JM_MAX_ERROR_MESSAGE_SIZE + 200 > MESSAGE_SIZE_TO_EXPAND_AND_PRINT\r
+#error This test triggers the logger function to allocate more memory than the default size JM_MAX_ERROR_MESSAGE_SIZE. If you change JM_MAX_ERROR_MESSAGE_SIZE, please update this test.\r
+#endif\r
+                       char longmessage[MESSAGE_SIZE_TO_EXPAND_AND_PRINT];\r
+                       const char* str[1];\r
+\r
+                       str[0] = (const char*)&longmessage;\r
+                       {
+                               fmi1_value_reference_t vr = VAR_S_LOGGER_TEST;
+                               int k;
+                               char repmsg[] = "#r0# "; /* HIGHT */
+                               for (k = 0; k < sizeof(longmessage)/sizeof(*longmessage) - 1; k++) {
+                                       longmessage[k] = repmsg[k%(sizeof(repmsg)/sizeof(*repmsg) - 1)];                                        
+                               }
+                               longmessage[k] = '\0';
+                               
+                               fmistatus = fmi1_import_set_string(fmu, &vr, 1, str);\r
+                               if(fmistatus != fmi1_status_ok) {\r
+                                       abort();\r
+                               }
+                       }\r
+               }\r
+\r
+               free(vr);\r
+       }\r
+\r
+\r
+       fmi1_import_free_model_instance(fmu);\r
+       return 0;\r
+}\r
+\r
+int main(int argc, char *argv[])\r
+{\r
+       fmi1_callback_functions_t callBackFunctions;\r
+       const char* FMUPath;\r
+       const char* tmpPath;\r
+       jm_callbacks callbacks;\r
+       fmi_import_context_t* context;\r
+       fmi_version_enu_t version;\r
+       jm_status_enu_t status;\r
+       int register_active_fmu;\r
+       fmi1_import_t* fmu;     \r
+       char* outfile;\r
+\r
+       if(argc < 4) {\r
+               printf("Usage: %s <fmu_file> <temporary_dir> <output_file>\n", argv[0]);\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }\r
+\r
+       FMUPath = argv[1];\r
+       tmpPath = argv[2];\r
+       outfile = argv[3];\r
+\r
+       logFile = fopen(outfile, "wb");\r
+\r
+       if(!logFile) {\r
+               printf("Could not open output file %s\n", outfile);\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }\r
+\r
+       callbacks.malloc = malloc;\r
+    callbacks.calloc = calloc;\r
+    callbacks.realloc = realloc;\r
+    callbacks.free = free;\r
+    callbacks.logger = logger;\r
+       callbacks.log_level = jm_log_level_info;\r
+    callbacks.context = 0;\r
+\r
+       callBackFunctions.logger = fmi1_log_forwarding;\r
+       callBackFunctions.allocateMemory = calloc;\r
+       callBackFunctions.freeMemory = free;\r
+\r
+#ifdef FMILIB_GENERATE_BUILD_STAMP\r
+       printf("Library build stamp:\n%s\n", fmilib_get_build_stamp());\r
+#endif\r
+\r
+       context = fmi_import_allocate_context(&callbacks);\r
+\r
+       version = fmi_import_get_fmi_version(context, FMUPath, tmpPath);\r
+\r
+       if(version != fmi_version_1_enu) {\r
+               printf("Only version 1.0 is supported so far\n");\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }\r
+\r
+       fmu = fmi1_import_parse_xml(context, tmpPath);\r
+\r
+       if(!fmu) {\r
+               printf("Error parsing XML, exiting\n");\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }       \r
+\r
+       register_active_fmu = 1; /* Must be used to use our logger. (jm standard prints to strerr which does not generate out put test file)  */\r
+       status = fmi1_import_create_dllfmu(fmu, callBackFunctions, register_active_fmu);\r
+       if (status == jm_status_error) {\r
+               printf("Could not create the DLL loading mechanism(C-API test).\n");\r
+               do_exit(CTEST_RETURN_FAIL);\r
+       }\r
+       \r
+       test_logger(fmu);\r
+\r
+       fmi1_import_destroy_dllfmu(fmu);\r
+\r
+       fmi1_import_free(fmu);\r
+       fmi_import_free_context(context);\r
+       fclose(logFile);\r
+       return 0;\r
+}\r
+\r
+\r