Ignore:
Timestamp:
Oct 10, 2019 2:41:07 PM (2 months ago)
Author:
Christian Andersson
Message:

Recommitted changeset:13700 to trunk with two bug fixes. Related to ticket:5837

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/Python/src/tests_jmodelica/general/test_extfunctions.py

    r13703 r13719  
    2222
    2323import nose
     24import fnmatch
    2425
    2526from pymodelica import compile_fmu
    2627from pymodelica.common.core import get_platform_dir, create_temp_dir
    27 from pyfmi import load_fmu
     28from pyfmi import load_fmu, FMUModelME2
    2829from pyfmi.fmi import FMUException
    2930from tests_jmodelica import testattr, get_files_path
     
    3334path_to_mofiles = os.path.join(get_files_path(), 'Modelica')
    3435
     36class TestEvaluator:
     37    @classmethod
     38    def setUpClass(cls):
     39        """
     40        Sets up the test class.
     41        """
     42        cls.fpath = path(path_to_mofiles, "ExtFunctionTests.mo")
     43   
     44    @testattr(stddist_base = True)
     45    def test_builtin_substring(self):
     46        cpath = "ExtFunctionTests.Evaluator_Substring"
     47        fmu_name = compile_fmu(cpath, self.fpath, compiler_options={"external_constant_evaluation_dynamic":True}, compiler_log_level="d:log.txt")
     48       
     49        nbr_of_evaluator_calls = 0
     50        with open("log.txt") as f:
     51            res = fnmatch.filter(f, "Succesfully connected external function '*' to the evaluator*")
     52            nbr_of_evaluator_calls = len(res)
     53       
     54        assert nbr_of_evaluator_calls == 4, "Wrong number of external function calls, check log."
     55       
     56        model = load_fmu(fmu_name)
     57       
     58        assert model.get("sub")[0] == "Y", model.get("sub")
     59        assert model.get("string2")[0] == "line", model.get("string2")
     60        assert model.get("len") == 16, model.get("len")
     61        assert model.get("start") == 4, model.get("start")
     62        assert not model.get("not_equal"), model.get("not_equal")
     63        assert model.get("equal"), model.get("equal")
     64   
     65    @testattr(stddist_base = True)
     66    def test_add(self):
     67        cpath = "ExtFunctionTests.Evaluator_Add"
     68        fmu_name = compile_fmu(cpath, self.fpath, compiler_options={"external_constant_evaluation_dynamic":True}, version=2.0, compiler_log_level="d:log.txt")
     69       
     70        nbr_of_evaluator_calls = 0
     71        with open("log.txt") as f:
     72            res = fnmatch.filter(f, "Succesfully connected external function '*' to the evaluator*")
     73            nbr_of_evaluator_calls = len(res)
     74       
     75        assert nbr_of_evaluator_calls == 2, "Wrong number of external function calls, check log."
     76       
     77        model = FMUModelME2(fmu_name, _connect_dll=False)
     78       
     79        assert model.get_variable_start("c") == 3, model.get_variable_start("c")
     80        assert model.get_variable_start("d") == 3, model.get_variable_start("d")
     81   
     82    @testattr(stddist_base = True)
     83    def test_unsupported_signature(self):
     84        cpath = "ExtFunctionTests.Evaluator_Multiple_Add"
     85        fmu_name = compile_fmu(cpath, self.fpath, compiler_options={"external_constant_evaluation_dynamic":True}, compiler_log_level="d:log.txt")
     86       
     87        matches = 0
     88        with open("log.txt") as f:
     89            res = fnmatch.filter(f, "*is not supported. Disabling use of the evaluator*")
     90            matches = len(res)
     91       
     92        assert matches == 1, "Does not seem to disabling the evaluator"
     93
     94    @testattr(stddist_base = True)
     95    def test_unknown_shared_library(self):
     96        cpath = "ExtFunctionTests.Evaluator_Unknown_Shared"
     97        try:
     98            fmu_name = compile_fmu(cpath, self.fpath, compiler_options={"external_constant_evaluation_dynamic":True}, compiler_log_level="d:log_unknown_shared.txt")
     99        except: #The compilation will fail, the interesting parts will though still be contained in the log file
     100            pass
     101       
     102        matches = 0
     103        with open("log_unknown_shared.txt") as f:
     104            res = fnmatch.filter(f, "Could not find a shared library containing*")
     105            matches = len(res)
     106       
     107        assert matches == 1, "Seems to have found an unknown shared library"
     108   
     109    @testattr(stddist_base = True)
     110    def test_double_return(self):
     111        cpath = "ExtFunctionTests.Evaluator_Double"
     112        fmu_name = compile_fmu(cpath, self.fpath, compiler_options={"external_constant_evaluation_dynamic":True}, version=2.0, compiler_log_level="d:log.txt")
     113       
     114        nbr_of_evaluator_calls = 0
     115        with open("log.txt") as f:
     116            res = fnmatch.filter(f, "Succesfully connected external function '*' to the evaluator*")
     117            nbr_of_evaluator_calls = len(res)
     118       
     119        assert nbr_of_evaluator_calls == 2, "Wrong number of external function calls, check log."
     120       
     121        model = FMUModelME2(fmu_name, _connect_dll=False)
     122       
     123        assert model.get_variable_start("c") == 3.0, model.get_variable_start("c")
     124        assert model.get_variable_start("d") == 9.0, model.get_variable_start("d")
     125   
     126    @testattr(stddist_base = True)
     127    def test_integer_return(self):
     128        cpath = "ExtFunctionTests.Evaluator_Integer"
     129        fmu_name = compile_fmu(cpath, self.fpath, compiler_options={"external_constant_evaluation_dynamic":True}, version=2.0, compiler_log_level="d:log.txt")
     130       
     131        nbr_of_evaluator_calls = 0
     132        with open("log.txt") as f:
     133            res = fnmatch.filter(f, "Succesfully connected external function '*' to the evaluator*")
     134            nbr_of_evaluator_calls = len(res)
     135       
     136        assert nbr_of_evaluator_calls == 1, "Wrong number of external function calls, check log."
     137       
     138        model = FMUModelME2(fmu_name, _connect_dll=False)
     139       
     140        assert model.get_variable_start("c") == 3.0, model.get_variable_start("c")
     141   
     142    @testattr(stddist_base = True)
     143    def test_record(self):
     144        cpath = "ExtFunctionTests.Evaluator_Record"
     145        fmu_name = compile_fmu(cpath, self.fpath, compiler_options={"external_constant_evaluation_dynamic":True}, version=2.0, compiler_log_level="d:log.txt")
     146       
     147        nbr_of_evaluator_calls = 0
     148        with open("log.txt") as f:
     149            res = fnmatch.filter(f, "Succesfully connected external function '*' to the evaluator*")
     150            nbr_of_evaluator_calls = len(res)
     151       
     152        assert nbr_of_evaluator_calls == 1, "Wrong number of external function calls, check log."
     153       
     154        model = FMUModelME2(fmu_name, _connect_dll=False)
     155       
     156        assert model.get_variable_start("c.x0") == 1.0, model.get_variable_start("c.x0")
     157        assert model.get_variable_start("c.x1") == 2.0, model.get_variable_start("c.x1")
     158        assert model.get_variable_start("c.x2") == 3.0, model.get_variable_start("c.x2")
     159        assert model.get_variable_start("c.x3") == 3.0, model.get_variable_start("c.x3")
     160        assert model.get_variable_start("c.x4") == 4.0, model.get_variable_start("c.x4")
     161        assert model.get_variable_start("c.x5") == 5.0, model.get_variable_start("c.x5")
     162        assert model.get_variable_start("c.x6") == 6.0, model.get_variable_start("c.x6")
     163        assert model.get_variable_start("c.x7") == 7.0, model.get_variable_start("c.x7")
     164        assert model.get_variable_start("c.x8") == 8.0, model.get_variable_start("c.x8")
     165        assert model.get_variable_start("c.x9") == 9.0, model.get_variable_start("c.x9")
     166        assert model.get_variable_start("c.x10") == 10.0, model.get_variable_start("c.x10")
     167   
    35168class TestExternalStatic:
    36169
Note: See TracChangeset for help on using the changeset viewer.