From bce07b91dbaa49942b51a32d0c21a6f5552f2efc Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 18 May 2012 14:37:34 +0000 Subject: [PATCH] Tests for variable filtering with FMU (refs #3398) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@25019 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../fmi/CallFMUBeforeInitializationTests.java | 14 ++ .../sysdyn/tests/fmi/FMUControlTestSuite.java | 3 +- .../sysdyn/tests/fmi/FMURealTest.java | 2 +- .../sysdyn/tests/fmi/FMUResultFilterTest.java | 142 ++++++++++++++++++ 4 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUResultFilterTest.java diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/CallFMUBeforeInitializationTests.java b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/CallFMUBeforeInitializationTests.java index 86a095c8..c3974d9e 100644 --- a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/CallFMUBeforeInitializationTests.java +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/CallFMUBeforeInitializationTests.java @@ -37,6 +37,13 @@ public class CallFMUBeforeInitializationTests { // control should throw this exception: There is no model to get the variables from } + try { + control.filterVariables(".*"); + fail("filterVariables returned something before loading fmu"); + } catch (FMUJNIException e) { + // control should throw this exception: There is no model to get the variables from + } + try { double d = control.getRealValue("dummy"); fail("Got a Real value before loading fmu: " + d); @@ -179,6 +186,13 @@ public class CallFMUBeforeInitializationTests { fail(e.getMessage()); } + try { + control.filterVariables(".*"); + } catch (FMUJNIException e) { + // control should NOT throw this exception: Variables are found in loadFMU + fail(e.getMessage()); + } + try { double[] array = new double[0]; diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUControlTestSuite.java b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUControlTestSuite.java index 322ce185..89b1e1cf 100644 --- a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUControlTestSuite.java +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUControlTestSuite.java @@ -18,7 +18,8 @@ import org.junit.runners.Suite.SuiteClasses; CallFMUBeforeInitializationTests.class, FMUIntegerTest.class, FMUBooleanTest.class, - FMURealTest.class }) + FMURealTest.class, + FMUResultFilterTest.class}) public class FMUControlTestSuite { } diff --git a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMURealTest.java b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMURealTest.java index 532f7e78..f996b7a9 100644 --- a/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMURealTest.java +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMURealTest.java @@ -20,7 +20,7 @@ public class FMURealTest { /* Below is the model used for testing. - Model is located in Models\FMUTests\FMUIntegerTest + Model is located in Models\FMUTests\FMURealTest model FMURealTestModel parameter Real r1 = 1.0; 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 index 00000000..0cbb9f5a --- /dev/null +++ b/org.simantics.sysdyn.tests/src/org/simantics/sysdyn/tests/fmi/FMUResultFilterTest.java @@ -0,0 +1,142 @@ +package org.simantics.sysdyn.tests.fmi; + +import java.util.Arrays; +import java.util.List; + +import junit.framework.Assert; + +import org.junit.Test; +import org.simantics.modelica.fmi.FMUControlJNI; +import org.simantics.modelica.fmi.FMUJNIException; + + +/** + * Tests result filtering + * @author Teemu Lempinen + * + */ +public class FMUResultFilterTest { + + /* + Below is the model used for testing. + Model is located in Models\FMUTests\FMURealTest + + model FMURealTestModel + parameter Real r1 = 1.0; + parameter Real r2 = 1.0; + Real valve; + Real stock(start=0.0,fixed=true); + parameter Real time = 0; // Time parameter is set at each time step by simulator + equation + if initial() or r1 < 0 or r1 >= 0 then (r1 + r2) else pre(valve); + der(stock) = valve; + end FMURealTestModel; + + + The condition for valve is automatically generated in sysdyn. It is a hack for getting + valve to notice a change in i1 and i2. + + Results always contain "time" for each modelica class, hence result size is +1 + */ + + @Test + public void testReturn3() { + + FMUControlJNI control = new FMUControlJNI(); + + String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu"; + + + try { + + + + control.loadFMUFile(path); + + String[] bothRealParameters = control.filterVariables("r(.*)"); + Assert.assertEquals("Filter \"r(.*)\" returned " + bothRealParameters.length + " results. Should have returned 3", bothRealParameters.length, 3); + List result = Arrays.asList(bothRealParameters); + Assert.assertTrue("Result did not contain r1", result.contains("r1")); + Assert.assertTrue("Result did not contain r2", result.contains("r2")); + Assert.assertTrue("Result did not contain time", result.contains("time")); + + control.unloadFMU(); + } catch (FMUJNIException e) { + Assert.fail("Exception in native fmu code: " + e.getMessage()); + } + } + + @Test + public void testReturn2() { + + FMUControlJNI control = new FMUControlJNI(); + + String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu"; + + + try { + + control.loadFMUFile(path); + + String[] stock = control.filterVariables("stock"); + Assert.assertEquals("Filter \"stock\" returned " + stock.length + " results. Should have returned 2", stock.length, 2); + List result = Arrays.asList(stock); + Assert.assertTrue("Result did not contain stock", result.contains("stock")); + Assert.assertTrue("Result did not contain time", result.contains("time")); + + control.unloadFMU(); + } catch (FMUJNIException e) { + Assert.fail("Exception in native fmu code: " + e.getMessage()); + } + } + + @Test + public void testReturn1() { + + FMUControlJNI control = new FMUControlJNI(); + + String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu"; + + + try { + control.loadFMUFile(path); + + String[] empty = control.filterVariables("empty"); + Assert.assertEquals("Filter \"empty\" returned " + empty.length + " results. Should have returned 1", empty.length, 1); + + List result = Arrays.asList(empty); + Assert.assertTrue("Result did not contain time", result.contains("time")); + + control.unloadFMU(); + } catch (FMUJNIException e) { + Assert.fail("Exception in native fmu code: " + e.getMessage()); + } + } + + @Test + public void testReturn1WhenNullFilter() { + + FMUControlJNI control = new FMUControlJNI(); + + String path = ".\\Models\\FMUTests\\FMURealTest\\FMURealTestModel.fmu"; + + + try { + control.loadFMUFile(path); + + String[] NULL = control.filterVariables(null); + Assert.assertEquals("Filter null returned " + NULL.length + " results. Should have returned 1", NULL.length, 1); + + List result = Arrays.asList(NULL); + Assert.assertTrue("Result did not contain time", result.contains("time")); + + control.unloadFMU(); + } catch (FMUJNIException e) { + Assert.fail("Exception in native fmu code: " + e.getMessage()); + } + } + + + + +} -- 2.47.1