Skip to content

Commit

Permalink
Merge pull request #22 from filip-stenstrom/variable_no_type
Browse files Browse the repository at this point in the history
Check variability!=continuous for non-real variables
  • Loading branch information
filip-stenstrom authored Aug 10, 2020
2 parents 0edf0b0 + 045d3c6 commit b3d3834
Show file tree
Hide file tree
Showing 15 changed files with 246 additions and 21 deletions.
6 changes: 6 additions & 0 deletions Config.cmake/test_fmi1.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ set(XML_CS_PATH ${FMU_DUMMY_FOLDER}/modelDescription_cs.xml)
set(XML_CS_TC_PATH ${FMU_DUMMY_FOLDER}/modelDescription_cs_tc.xml)
set(XML_MF_PATH ${FMU_DUMMY_FOLDER}/modelDescription_malformed.xml)
set(TYPE_DEFINITIONS_MODEL_DESC_DIR ${RTTESTDIR}/FMI1/parser_test_xmls/type_definitions)
set(VARIABLE_BAD_TYPE_VARIABILITY_MODEL_DESC_DIR ${RTTESTDIR}/FMI1/parser_test_xmls/variable_bad_type_variability)

set(SHARED_LIBRARY_ME_PATH ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}fmu1_dll_me${CMAKE_SHARED_LIBRARY_SUFFIX})
set(SHARED_LIBRARY_CS_PATH ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}fmu1_dll_cs${CMAKE_SHARED_LIBRARY_SUFFIX})
Expand All @@ -65,6 +66,9 @@ compress_fmu("${TEST_OUTPUT_FOLDER}" "${FMU_DUMMY_ME_MODEL_IDENTIFIER}" "me" "fm
compress_fmu("${TEST_OUTPUT_FOLDER}" "${FMU_DUMMY_CS_MODEL_IDENTIFIER}" "cs" "fmu1_dll_cs" "${XML_CS_PATH}" "${SHARED_LIBRARY_CS_PATH}")
compress_fmu("${TEST_OUTPUT_FOLDER}" "${FMU_DUMMY_CS_MODEL_IDENTIFIER}" "cs_tc" "fmu1_dll_cs" "${XML_CS_TC_PATH}" "${SHARED_LIBRARY_CS_PATH}")

add_executable(fmi1_variable_bad_type_variability_test ${RTTESTDIR}/FMI1/fmi1_variable_bad_type_variability_test.c)
target_link_libraries(fmi1_variable_bad_type_variability_test ${FMILIBFORTEST})

add_executable (fmi1_import_default_experiment_test ${RTTESTDIR}/FMI1/fmi1_import_default_experiment_test.c)
target_link_libraries (fmi1_import_default_experiment_test ${FMILIBFORTEST} )
add_executable(fmi1_type_definitions_test ${RTTESTDIR}/FMI1/fmi1_import_type_definitions_test.c)
Expand All @@ -89,6 +93,7 @@ to_native_c_path("${TEST_OUTPUT_FOLDER}/${FMU_DUMMY_CS_MODEL_IDENTIFIER}_cs_tc.f
# set(FMU_TEMPFOLDER ${TEST_OUTPUT_FOLDER}/tempfolder)
to_native_c_path(${TEST_OUTPUT_FOLDER}/tempfolder FMU_TEMPFOLDER)

add_test(ctest_fmi1_variable_bad_type_variability_test fmi1_variable_bad_type_variability_test ${VARIABLE_BAD_TYPE_VARIABILITY_MODEL_DESC_DIR})
add_test(ctest_fmi1_xml_parsing_test fmi1_import_default_experiment_test ${RTTESTDIR}/FMI1/parser_test_xmls/default_experiment/)
add_test(ctest_fmi1_xml_parsing_test fmi1_xml_parsing_test ${RTTESTDIR}/FMI1/parser_test_xmls/)
add_test(ctest_fmi1_type_definitions_test fmi1_type_definitions_test ${TYPE_DEFINITIONS_MODEL_DESC_DIR})
Expand Down Expand Up @@ -142,6 +147,7 @@ if(FMILIB_BUILD_BEFORE_TESTS)
ctest_fmi1_logger_test_run
ctest_fmi1_xml_parsing_test
ctest_fmi1_type_definitions_test
ctest_fmi1_variable_bad_type_variability_test
PROPERTIES DEPENDS ctest_build_all)
endif()

Expand Down
9 changes: 9 additions & 0 deletions Config.cmake/test_fmi2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ set(VARIABLE_NO_TYPE_MODEL_DESC_DIR
${RTTESTDIR}/FMI2/parser_test_xmls/variable_no_type)
set(VARIABLE_BAD_VARIABILITY_CAUSALITY_MODEL_DESC_DIR
${RTTESTDIR}/FMI2/parser_test_xmls/variable_bad_variability_causality)
set(VARIABLE_BAD_TYPE_VARIABILITY_MODEL_DESC_DIR
${RTTESTDIR}/FMI2/parser_test_xmls/variable_bad_type_variability)
set(TYPE_DEFINITIONS_MODEL_DESC_DIR
${RTTESTDIR}/FMI2/parser_test_xmls/type_definitions)

Expand Down Expand Up @@ -93,6 +95,9 @@ target_link_libraries(fmi2_variable_no_type_test ${FMILIBFORTEST})
add_executable(fmi2_variable_bad_variability_causality_test
${RTTESTDIR}/FMI2/fmi2_variable_bad_variability_causality_test.c)
target_link_libraries(fmi2_variable_bad_variability_causality_test ${FMILIBFORTEST})
add_executable(fmi2_variable_bad_type_variability_test
${RTTESTDIR}/FMI2/fmi2_variable_bad_type_variability_test.c)
target_link_libraries(fmi2_variable_bad_type_variability_test ${FMILIBFORTEST})
add_executable(fmi2_enum_test ${RTTESTDIR}/FMI2/fmi2_enum_test.c)
target_link_libraries(fmi2_enum_test ${FMILIBFORTEST})

Expand Down Expand Up @@ -128,6 +133,9 @@ add_test(ctest_fmi2_variable_no_type_test
add_test(ctest_fmi2_variable_bad_variability_causality_test
fmi2_variable_bad_variability_causality_test
${VARIABLE_BAD_VARIABILITY_CAUSALITY_MODEL_DESC_DIR})
add_test(ctest_fmi2_variable_bad_type_variability_test
fmi2_variable_bad_type_variability_test
${VARIABLE_BAD_TYPE_VARIABILITY_MODEL_DESC_DIR})
add_test(ctest_fmi2_type_definitions_test
fmi2_type_definitions_test
${TYPE_DEFINITIONS_MODEL_DESC_DIR})
Expand All @@ -149,5 +157,6 @@ if(FMILIB_BUILD_BEFORE_TESTS)
ctest_fmi2_type_definitions_test
ctest_fmi2_enum_test
ctest_fmi2_variable_bad_variability_causality_test
ctest_fmi2_variable_bad_type_variability_test
PROPERTIES DEPENDS ctest_build_all)
endif()
51 changes: 51 additions & 0 deletions Test/FMI1/fmi1_variable_bad_type_variability_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "fmilib.h"
#include "fmil_test.h"
#include "config_test.h"

/* global used by logger - make sure to reset between different XML parsings */
int g_n_logger_found_err_msg = 0;

static void log_default(jm_string module, jm_log_level_enu_t log_level, jm_string message)
{
printf("module = %s, log level = %d: %s\n", module, log_level, message);
}

static void logger_verify(jm_callbacks* c, jm_string module, jm_log_level_enu_t log_level, jm_string message) {
const char* msg_exp = "Only Real variables can have variability='continuous'";
if (!strncmp(msg_exp, message, strlen(msg_exp))) {
g_n_logger_found_err_msg++;
}
log_default(module, log_level, message);
}

int main(int argc, char **argv)
{
fmi1_import_t *xml;
jm_callbacks * cb = jm_get_default_callbacks();
fmi_import_context_t *ctx;

if (argc != 2) {
printf("Usage: %s <variable_bad_causality_variability_dir>\n", argv[0]);
return CTEST_RETURN_FAIL;
}

cb->logger = logger_verify;
ctx = fmi_import_allocate_context(cb);
if (ctx == NULL) {
printf("Failed to allocate memory");
return CTEST_RETURN_FAIL;
}

xml = fmi1_import_parse_xml(ctx, argv[1]);
fmi_import_free_context(ctx);
if (xml == NULL) {
return CTEST_RETURN_FAIL;
}

if (g_n_logger_found_err_msg != 4) {
printf("incorrect number of variables found with invalid variability\n");
return CTEST_RETURN_FAIL;
}

return CTEST_RETURN_SUCCESS;
}
6 changes: 3 additions & 3 deletions Test/FMI1/fmu_dummy/modelDescription_cs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
<ScalarVariable name="BOUNCE_COF" valueReference="3" description="Bouncing coefficient">
<Real start="0.5" fixed="true"/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST" valueReference="0" description="The logger will print the value of this variable when it is set.">
<ScalarVariable name="LOGGER_TEST" valueReference="0" variability='discrete' description="The logger will print the value of this variable when it is set.">
<String/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" description="This is only used to test logger replace function #i0#">
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" variability='discrete' description="This is only used to test logger replace function #i0#">
<Integer/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" description="This is only used to test logger replace function #b0#">
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" variability='discrete' description="This is only used to test logger replace function #b0#">
<Boolean/>
</ScalarVariable>
</ModelVariables>
Expand Down
6 changes: 3 additions & 3 deletions Test/FMI1/fmu_dummy/modelDescription_cs_tc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
<ScalarVariable name="BOUNCE_COF" valueReference="3" description="Bouncing coefficient">
<Real start="0.5" fixed="true"/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST" valueReference="0" description="The logger will print the value of this variable when it is set.">
<ScalarVariable name="LOGGER_TEST" valueReference="0" variability='discrete' description="The logger will print the value of this variable when it is set.">
<String/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" description="This is only used to test logger replace function #i0#">
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" variability='discrete' description="This is only used to test logger replace function #i0#">
<Integer/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" description="This is only used to test logger replace function #b0#">
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" variability='discrete' description="This is only used to test logger replace function #b0#">
<Boolean/>
</ScalarVariable>
<ScalarVariable name="INTEGER" valueReference="1">
Expand Down
6 changes: 3 additions & 3 deletions Test/FMI1/fmu_dummy/modelDescription_malformed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
<ScalarVariable name="BOUNCE_COF" valueReference="3" description="Bouncing coefficient">
<Real start="0.5" fixed="true"/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST" valueReference="0" description="The logger will print the value of this variable when it is set.">
<ScalarVariable name="LOGGER_TEST" valueReference="0" variability='discrete' description="The logger will print the value of this variable when it is set.">
<String/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" description="This is only used to test logger replace function #i0#">
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" variability='discrete' description="This is only used to test logger replace function #i0#">
<Integer/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" description="This is only used to test logger replace function #b0#">
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" variability='discrete' description="This is only used to test logger replace function #b0#">
<Boolean/>
</ScalarVariable>
<ScalarVariable name="BAD_INPUT" valueReference="4" causality="input" description="Input without start value, the logger should warn about this">
Expand Down
6 changes: 3 additions & 3 deletions Test/FMI1/fmu_dummy/modelDescription_me.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
<ScalarVariable name="BOUNCE_COF" valueReference="3" description="Bouncing coefficient">
<Real start="0.5" fixed="true"/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST" valueReference="0" description="The logger will print the value of this variable when it is set.">
<ScalarVariable name="LOGGER_TEST" valueReference="0" variability='discrete' description="The logger will print the value of this variable when it is set.">
<String/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" description="This is only used to test logger replace function #i0#">
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" variability='discrete' description="This is only used to test logger replace function #i0#">
<Integer/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" description="This is only used to test logger replace function #b0#">
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" variability='discrete' description="This is only used to test logger replace function #b0#">
<Boolean/>
</ScalarVariable>
</ModelVariables>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<fmiModelDescription
fmiVersion="1.0"
modelName="modelName"
modelIdentifier="modelIdentifier"
guid="d7d98edb-d924-4104-87db-227bb677fc95"
numberOfContinuousStates="0"
numberOfEventIndicators="0"
>

<TypeDefinitions>
<Type name="MyEnum">
<EnumerationType quantity="TypeQuantity">
<Item name="item1"/>
</EnumerationType>
</Type>
</TypeDefinitions>

<ModelVariables>
<!-- non-Reals can't have variability='continuous' (default value) -->
<ScalarVariable name="var0" valueReference="0">
<Boolean/>
</ScalarVariable>
<ScalarVariable name="var1" valueReference="1">
<Integer/>
</ScalarVariable>
<ScalarVariable name="var2" valueReference="2">
<Enumeration declaredType="MyEnum"/>
</ScalarVariable>
<ScalarVariable name="var3" valueReference="3">
<String/>
</ScalarVariable>

<ScalarVariable name="var4" valueReference="4"> <!-- OK, should not give error -->
<Real/>
</ScalarVariable>
</ModelVariables>

</fmiModelDescription>
51 changes: 51 additions & 0 deletions Test/FMI2/fmi2_variable_bad_type_variability_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "fmilib.h"
#include "fmil_test.h"
#include "config_test.h"

/* global used by logger - make sure to reset between different XML parsings */
int g_n_logger_found_err_msg = 0;

static void log_default(jm_string module, jm_log_level_enu_t log_level, jm_string message)
{
printf("module = %s, log level = %d: %s\n", module, log_level, message);
}

static void logger_verify(jm_callbacks* c, jm_string module, jm_log_level_enu_t log_level, jm_string message) {
const char* msg_exp = "Only Real variables can have variability='continuous'";
if (!strncmp(msg_exp, message, strlen(msg_exp))) {
g_n_logger_found_err_msg++;
}
log_default(module, log_level, message);
}

int main(int argc, char **argv)
{
fmi2_import_t *xml;
jm_callbacks * cb = jm_get_default_callbacks();
fmi_import_context_t *ctx;

if (argc != 2) {
printf("Usage: %s <variable_bad_causality_variability_dir>\n", argv[0]);
return CTEST_RETURN_FAIL;
}

cb->logger = logger_verify;
ctx = fmi_import_allocate_context(cb);
if (ctx == NULL) {
printf("Failed to allocate memory");
return CTEST_RETURN_FAIL;
}

xml = fmi2_import_parse_xml(ctx, argv[1], NULL);
fmi_import_free_context(ctx);
if (xml == NULL) {
return CTEST_RETURN_FAIL;
}

if (g_n_logger_found_err_msg != 4) {
printf("incorrect number of variables found with invalid variability\n");
return CTEST_RETURN_FAIL;
}

return CTEST_RETURN_SUCCESS;
}
6 changes: 3 additions & 3 deletions Test/FMI2/fmu_dummy/modelDescription_cs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
<ScalarVariable name="BOUNCE_COF" valueReference="3" initial="exact" description="Bouncing coefficient">
<Real start="0.5" />
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST" valueReference="0" description="The logger will print the value of this variable when it is set.">
<ScalarVariable name="LOGGER_TEST" valueReference="0" variability="discrete" description="The logger will print the value of this variable when it is set.">
<String/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" description="This is only used to test logger replace function #i0#">
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" variability="discrete" description="This is only used to test logger replace function #i0#">
<Integer/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" description="This is only used to test logger replace function #b0#">
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" variability="discrete" description="This is only used to test logger replace function #b0#">
<Boolean/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_DERIVATIVE" valueReference="10" description="This is to test that the derivative attribute may appear on any Real variables.">
Expand Down
6 changes: 3 additions & 3 deletions Test/FMI2/fmu_dummy/modelDescription_malformed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
<ScalarVariable name="BAD_INPUT" valueReference="4" causality="input" description="Input without start value, the logger should warn about this">
<Real/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST" valueReference="0" description="The logger will print the value of this variable when it is set.">
<ScalarVariable name="LOGGER_TEST" valueReference="0" variability="discrete" description="The logger will print the value of this variable when it is set.">
<String/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" description="This is only used to test logger replace function #i0#">
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" variability="discrete" description="This is only used to test logger replace function #i0#">
<Integer/>
</ScalarVariable>
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" description="This is only used to test logger replace function #b0#">
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" variability="discrete" description="This is only used to test logger replace function #b0#">
<Boolean/>
</ScalarVariable>
</ModelVariables>
Expand Down
6 changes: 3 additions & 3 deletions Test/FMI2/fmu_dummy/modelDescription_me.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@
<Real start="0.5" />
</ScalarVariable>
<!-- 8 -->
<ScalarVariable name="LOGGER_TEST" valueReference="0" description="The logger will print the value of this variable when it is set.">
<ScalarVariable name="LOGGER_TEST" valueReference="0" variability="discrete" description="The logger will print the value of this variable when it is set.">
<String/>
</ScalarVariable>
<!-- 9 -->
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" description="This is only used to test logger replace function #i0#">
<ScalarVariable name="LOGGER_TEST_INTEGER" valueReference="0" variability="discrete" description="This is only used to test logger replace function #i0#">
<Integer/>
</ScalarVariable>
<!-- 10 -->
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" description="This is only used to test logger replace function #b0#">
<ScalarVariable name="LOGGER_TEST_BOOLEAN" valueReference="0" variability="discrete" description="This is only used to test logger replace function #b0#">
<Boolean/>
</ScalarVariable>
<!-- 11 -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<fmiModelDescription
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
fmiVersion="2.0"
modelName="modelName"
guid="d7d98edb-d924-4104-87db-227bb677fc95">

<CoSimulation modelIdentifier="modelIdentifier" />

<TypeDefinitions>
<SimpleType name="MyEnum">
<Enumeration quantity="TypeQuantity">
<Item name="item1" value="1"/>
</Enumeration>
</SimpleType>
</TypeDefinitions>

<ModelVariables>
<!-- non-Reals can't have variability='continuous' (default value) -->
<ScalarVariable name="var0" valueReference="0">
<Boolean/>
</ScalarVariable>
<ScalarVariable name="var1" valueReference="1">
<Integer/>
</ScalarVariable>
<ScalarVariable name="var2" valueReference="2">
<Enumeration declaredType="MyEnum"/>
</ScalarVariable>
<ScalarVariable name="var3" valueReference="3">
<String/>
</ScalarVariable>

<ScalarVariable name="var4" valueReference="4"> <!-- OK, should not give error -->
<Real/>
</ScalarVariable>
</ModelVariables>

<ModelStructure />

</fmiModelDescription>
Loading

0 comments on commit b3d3834

Please sign in to comment.