]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Tests for variable filtering with FMU (refs #3398)
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 18 May 2012 14:37:34 +0000 (14:37 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Fri, 18 May 2012 14:37:34 +0000 (14:37 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25019 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/CallFMUBeforeInitializationTests.java
org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUControlTestSuite.java
org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMURealTest.java
org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUResultFilterTest.java [new file with mode: 0644]

index 86a095c847f5d9a3ce29320ecbe01d91b3ff9ea0..c3974d9ee8b578007da135a7674977d3cb58532b 100644 (file)
@@ -37,6 +37,13 @@ public class CallFMUBeforeInitializationTests {
                        // control should throw this exception: There is no model to get the variables from\r
                }\r
                \r
+               try {\r
+                       control.filterVariables(".*");\r
+                       fail("filterVariables returned something before loading fmu");\r
+               } catch (FMUJNIException e) {\r
+                       // control should throw this exception: There is no model to get the variables from\r
+               }\r
+               \r
                try {\r
                        double d = control.getRealValue("dummy");\r
                        fail("Got a Real value before loading fmu: " + d);\r
@@ -179,6 +186,13 @@ public class CallFMUBeforeInitializationTests {
                        fail(e.getMessage());\r
                }\r
                \r
+               try {\r
+                       control.filterVariables(".*");\r
+               } catch (FMUJNIException e) {\r
+                       // control should NOT throw this exception: Variables are found in loadFMU\r
+                       fail(e.getMessage());\r
+               }\r
+               \r
                \r
                try {\r
                        double[] array = new double[0];\r
index 322ce185a88cebc9a33a1548f301361b67892500..89b1e1cfe954249d690f2bb613f45a0e5430db40 100644 (file)
@@ -18,7 +18,8 @@ import org.junit.runners.Suite.SuiteClasses;
        CallFMUBeforeInitializationTests.class, \r
        FMUIntegerTest.class, \r
        FMUBooleanTest.class, \r
-       FMURealTest.class })\r
+       FMURealTest.class,\r
+       FMUResultFilterTest.class})\r
 public class FMUControlTestSuite {\r
 \r
 }\r
index 532f7e78896e0a9a72bcb1b047ce54efe21e034f..f996b7a915f68d0ea55a49e1f286d38b62553589 100644 (file)
@@ -20,7 +20,7 @@ public class FMURealTest {
        \r
        /*\r
        Below is the model used for testing. \r
-       Model is located in Models\FMUTests\FMUIntegerTest \r
+       Model is located in Models\FMUTests\FMURealTest \r
        \r
        model FMURealTestModel\r
                parameter Real r1 = 1.0;\r
diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUResultFilterTest.java b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUResultFilterTest.java
new file mode 100644 (file)
index 0000000..0cbb9f5
--- /dev/null
@@ -0,0 +1,142 @@
+package org.simantics.sysdyn.tests.fmi;\r
+\r
+import java.util.Arrays;\r
+import java.util.List;\r
+\r
+import junit.framework.Assert;\r
+\r
+import org.junit.Test;\r
+import org.simantics.modelica.fmi.FMUControlJNI;\r
+import org.simantics.modelica.fmi.FMUJNIException;\r
+\r
+\r
+/**\r
+ * Tests result filtering\r
+ * @author Teemu Lempinen\r
+ *\r
+ */\r
+public class FMUResultFilterTest {\r
+       \r
+       /*\r
+       Below is the model used for testing. \r
+       Model is located in Models\FMUTests\FMURealTest \r
+       \r
+       model FMURealTestModel\r
+               parameter Real r1 = 1.0;\r
+               parameter Real r2 = 1.0;\r
+               Real valve;\r
+               Real stock(start=0.0,fixed=true);\r
+               parameter Real time = 0; // Time parameter is set at each time step by simulator\r
+       equation\r
+               if initial() or r1 < 0 or r1 >= 0 then (r1 + r2) else pre(valve);\r
+               der(stock) = valve;\r
+       end FMURealTestModel;\r
+       \r
+       \r
+       The condition for valve is automatically generated in sysdyn. It is a hack for getting \r
+       valve to notice a change in i1 and i2. \r
+       \r
+       Results always contain "time" for each modelica class, hence result size is +1\r
+        */\r
+\r
+       @Test\r
+       public void testReturn3() {\r
+\r
+               FMUControlJNI control = new FMUControlJNI();\r
+\r
+               String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu";\r
+\r
+\r
+               try {\r
+                       \r
+               \r
+\r
+                       control.loadFMUFile(path);\r
+\r
+                       String[] bothRealParameters = control.filterVariables("r(.*)");\r
+                       Assert.assertEquals("Filter \"r(.*)\" returned " + bothRealParameters.length + " results. Should have returned 3", bothRealParameters.length, 3);\r
+                       List<String> result = Arrays.asList(bothRealParameters);\r
+                       Assert.assertTrue("Result did not contain r1", result.contains("r1"));\r
+                       Assert.assertTrue("Result did not contain r2", result.contains("r2"));\r
+                       Assert.assertTrue("Result did not contain time", result.contains("time"));\r
+\r
+                       control.unloadFMU();\r
+               } catch (FMUJNIException e) {\r
+                       Assert.fail("Exception in native fmu code: " + e.getMessage());\r
+               }\r
+       }\r
+       \r
+       @Test\r
+       public void testReturn2() {\r
+\r
+               FMUControlJNI control = new FMUControlJNI();\r
+\r
+               String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu";\r
+\r
+\r
+               try {\r
+\r
+                       control.loadFMUFile(path);\r
+\r
+                       String[] stock = control.filterVariables("stock");\r
+                       Assert.assertEquals("Filter \"stock\" returned " + stock.length + " results. Should have returned 2", stock.length, 2);\r
+                       List<String> result = Arrays.asList(stock);\r
+                       Assert.assertTrue("Result did not contain stock", result.contains("stock"));\r
+                       Assert.assertTrue("Result did not contain time", result.contains("time"));\r
+                       \r
+                       control.unloadFMU();\r
+               } catch (FMUJNIException e) {\r
+                       Assert.fail("Exception in native fmu code: " + e.getMessage());\r
+               }\r
+       }\r
+       \r
+       @Test\r
+       public void testReturn1() {\r
+\r
+               FMUControlJNI control = new FMUControlJNI();\r
+\r
+               String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu";\r
+\r
+\r
+               try {\r
+                       control.loadFMUFile(path);\r
+                       \r
+                       String[] empty = control.filterVariables("empty");\r
+                       Assert.assertEquals("Filter \"empty\" returned " + empty.length + " results. Should have returned 1", empty.length, 1);\r
+                       \r
+                       List<String> result = Arrays.asList(empty);\r
+                       Assert.assertTrue("Result did not contain time", result.contains("time"));\r
+                       \r
+                       control.unloadFMU();\r
+               } catch (FMUJNIException e) {\r
+                       Assert.fail("Exception in native fmu code: " + e.getMessage());\r
+               }\r
+       }\r
+       \r
+       @Test\r
+       public void testReturn1WhenNullFilter() {\r
+\r
+               FMUControlJNI control = new FMUControlJNI();\r
+\r
+               String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu";\r
+\r
+\r
+               try {\r
+                       control.loadFMUFile(path);\r
+                       \r
+                       String[] NULL = control.filterVariables(null);\r
+                       Assert.assertEquals("Filter null returned " + NULL.length + " results. Should have returned 1", NULL.length, 1);\r
+                       \r
+                       List<String> result = Arrays.asList(NULL);\r
+                       Assert.assertTrue("Result did not contain time", result.contains("time"));\r
+                       \r
+                       control.unloadFMU();\r
+               } catch (FMUJNIException e) {\r
+                       Assert.fail("Exception in native fmu code: " + e.getMessage());\r
+               }\r
+       }\r
+       \r
+\r
+       \r
+\r
+}\r