Created
December 9, 2012 14:44
-
-
Save mtiller/4245357 to your computer and use it in GitHub Desktop.
An example of a Modelica testing library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Modelica_Test | |
"This is a stand-alone library to facilitate assertion based testing of Modelica code" | |
model AssertTrajectory | |
parameter Real expected[:,2]; | |
parameter Real eps=1e-6; | |
input Real actual; | |
protected | |
Integer cur; | |
algorithm | |
when initial() then | |
assert(size(expected,1)>0, "Error, expected trajectory contains no points"); | |
assert(expected[1,1]>=time, "Assertion failed because some points in the trajectory came before the start of the simulation"); | |
cur := 1; | |
if (expected[1,1]>=time and expected[1,1]<=time) then | |
assert(abs(actual-expected[cur,1])<eps, "Actual input value ("+String(actual)+") was not within "+String(eps)+" of the expected result: "+String(expected[cur,2])); | |
cur := 2; | |
end if; | |
end when; | |
when cur<=size(expected,1) and time>=expected[cur,2] then | |
assert(abs(actual-expected[cur,1])<eps, "Actual input value ("+String(actual)+") was not within "+String(eps)+" of the expected result: "+String(expected[cur,2])); | |
cur := pre(cur) + 1; | |
end when; | |
when terminal() then | |
assert(cur>size(expected,1), "Assertion failed because simulation ended before all trajectory points could be checked"); | |
end when; | |
end AssertTrajectory; | |
annotation (uses(Modelica(version="3.2"))); | |
model AssertBecomesTrueAt | |
parameter Real expected; | |
parameter Real eps=1e-6; | |
input Boolean actual; | |
protected | |
Boolean checked; | |
algorithm | |
when initial() then | |
checked := false; | |
assert(expected>time+eps, "Expected crossing time is not in the future at simulation start"); | |
end when; | |
when time>=expected-eps then | |
assert(not actual, "Signal became true before expected crossing time"); | |
end when; | |
when time>=expected+eps then | |
assert(actual, "Signal was not true by the expected crossing time"); | |
end when; | |
end AssertBecomesTrueAt; | |
package Tests | |
"A library to test the assertion primitives in this library" | |
package Trajectory "Tests on the AssertTrajectory model" | |
model CheckSuccess | |
Real x = time; | |
AssertTrajectory check_x(actual=x, expected=[0,0; 1,1; 2,2; 3,3]); | |
annotation(TestCase(action="simulate", result="success"), experiment(StopTime=4)); | |
end CheckSuccess; | |
model CheckFailure1 | |
"Check to make sure this fails if first point is before start" | |
Real x = time; | |
AssertTrajectory check_x(actual=x, expected=[-1,-1; 1,1; 2,2; 3,3]); | |
annotation(TestCase(action="simulate", result="failure"), experiment(StopTime=4)); | |
end CheckFailure1; | |
model CheckFailure2 | |
"Check to make sure values match during simulation" | |
Real x = time; | |
AssertTrajectory check_x(actual=x, expected=[0,0; 1,1; 2,2.5; 3,3]); | |
annotation(TestCase(action="simulate", result="failure"), experiment(StopTime=4)); | |
end CheckFailure2; | |
model CheckFailure3 "Check to make sure all points are checked" | |
Real x = time; | |
AssertTrajectory check_x(actual=x, expected=[0,0; 1,1; 2,2; 8,8]); | |
annotation(TestCase(action="simulate", result="failure"), experiment(StopTime=4)); | |
end CheckFailure3; | |
end Trajectory; | |
package BecomesTrueAt "Tests on the AssertBecomesTrueAt model" | |
model CheckSuccess | |
Real x = time; | |
AssertBecomesTrueAt check_event(actual=(x>2), expected=2); | |
annotation(TestCase(action="simulate", result="success"), experiment(StopTime=4)); | |
end CheckSuccess; | |
model CheckFailure1 "Check case of early transition" | |
Real x = time; | |
AssertBecomesTrueAt check_event(actual=(x>1), expected=2); | |
annotation(TestCase(action="simulate", result="failure"), experiment(StopTime=4)); | |
end CheckFailure1; | |
model CheckFailure2 "Check for case of late transition" | |
Real x = time; | |
AssertBecomesTrueAt check_event(actual=(x>3), expected=2); | |
annotation(TestCase(action="simulate", result="success"), experiment(StopTime=4)); | |
end CheckFailure2; | |
end BecomesTrueAt; | |
end Tests; | |
end Modelica_Test; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment