Skip to content

Commit

Permalink
[NF] Added function evaluation tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
perost authored and OpenModelica-Hudson committed May 3, 2018
1 parent a523321 commit 9fbd53a
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 0 deletions.
23 changes: 23 additions & 0 deletions flattening/modelica/scodeinst/CevalFunc1.mo
@@ -0,0 +1,23 @@
// name: CevalFunc1
// keywords:
// status: correct
// cflags: -d=newInst
//
//

function f
input Real x;
output Real y;
algorithm
y := x * 2;
end f;

model CevalFunc1
constant Real x = f(3.0);
end CevalFunc1;

// Result:
// class CevalFunc1
// constant Real x = 6.0;
// end CevalFunc1;
// endResult
26 changes: 26 additions & 0 deletions flattening/modelica/scodeinst/CevalFuncAssert1.mo
@@ -0,0 +1,26 @@
// name: CevalFuncAssert1
// keywords:
// status: correct
// cflags: -d=newInst
//
//

function f
input Integer n;
output Integer res;
algorithm
assert(n <= 2, "f got n larger than 2", AssertionLevel.warning);
res := n;
end f;

model CevalFuncAssert1
constant Real x = f(10);
end CevalFuncAssert1;

// Result:
// class CevalFuncAssert1
// constant Real x = 10.0;
// end CevalFuncAssert1;
// [flattening/modelica/scodeinst/CevalFuncAssert1.mo:12:3-12:66:writable] Warning: assert triggered: f got n larger than 2
//
// endResult
34 changes: 34 additions & 0 deletions flattening/modelica/scodeinst/CevalFuncAssert2.mo
@@ -0,0 +1,34 @@
// name: CevalFuncAssert2
// keywords:
// status: correct
// cflags: -d=newInst
//
//

function f
input Integer n;
output Integer res;
algorithm
assert(n <= 2, "f got n larger than 2", AssertionLevel.error);
res := n;
end f;

model CevalFuncAssert2
constant Real x = f(10);
end CevalFuncAssert2;

// Result:
// function f
// input Integer n;
// output Integer res;
// algorithm
// assert(n <= 2, "f got n larger than 2");
// res := n;
// end f;
//
// class CevalFuncAssert2
// constant Real x = /*Real*/(f(10));
// end CevalFuncAssert2;
// [flattening/modelica/scodeinst/CevalFuncAssert2.mo:12:3-12:64:writable] Error: assert triggered: f got n larger than 2
//
// endResult
27 changes: 27 additions & 0 deletions flattening/modelica/scodeinst/CevalFuncFor1.mo
@@ -0,0 +1,27 @@
// name: CevalFunc2
// keywords:
// status: correct
// cflags: -d=newInst
//
//

function f
input Integer n;
output Integer res;
algorithm
res := 0;

for i in 1:n loop
res := res + i;
end for;
end f;

model CevalFunc1
constant Real x = f(10);
end CevalFunc1;

// Result:
// class CevalFunc1
// constant Real x = 55.0;
// end CevalFunc1;
// endResult
29 changes: 29 additions & 0 deletions flattening/modelica/scodeinst/CevalFuncFor2.mo
@@ -0,0 +1,29 @@
// name: CevalFuncFor2
// keywords:
// status: correct
// cflags: -d=newInst
//
//

function f
input Integer n;
output Integer res;
algorithm
res := 0;

for i in 1:n loop
for i in 1:i loop
res := res + i;
end for;
end for;
end f;

model CevalFuncFor2
constant Real x = f(10);
end CevalFuncFor2;

// Result:
// class CevalFuncFor2
// constant Real x = 220.0;
// end CevalFuncFor2;
// endResult
33 changes: 33 additions & 0 deletions flattening/modelica/scodeinst/CevalFuncIf1.mo
@@ -0,0 +1,33 @@
// name: CevalFuncIf1
// keywords:
// status: correct
// cflags: -d=newInst
//
//

function f
input Real x;
output Real y;
algorithm
if x > 4 then
y := 3.0;
elseif x > 2 then
y := 5.0;
else
y := 7.0;
end if;
end f;

model CevalFuncIf1
constant Real x = f(1.0);
constant Real y = f(3.0);
constant Real z = f(5.0);
end CevalFuncIf1;

// Result:
// class CevalFuncIf1
// constant Real x = 7.0;
// constant Real y = 5.0;
// constant Real z = 3.0;
// end CevalFuncIf1;
// endResult
23 changes: 23 additions & 0 deletions flattening/modelica/scodeinst/CevalFuncRecursive1.mo
@@ -0,0 +1,23 @@
// name: CevalFuncRecursive1
// keywords:
// status: correct
// cflags: -d=newInst
//
//

function fac
input Integer n;
output Integer res;
algorithm
res := if n > 1 then n * fac(n - 1) else n;
end fac;

model CevalFuncRecursive1
constant Real x = fac(6);
end CevalFuncRecursive1;

// Result:
// class CevalFuncRecursive1
// constant Real x = 720.0;
// end CevalFuncRecursive1;
// endResult
32 changes: 32 additions & 0 deletions flattening/modelica/scodeinst/CevalFuncRecursive2.mo
@@ -0,0 +1,32 @@
// name: CevalFuncRecursive2
// keywords:
// status: correct
// cflags: -d=newInst
//
//

function f
input Real x;
output Real y;
algorithm
y := f(x + 1);
end f;

model CevalFuncRecursive2
constant Real x = f(3.0);
end CevalFuncRecursive2;

// Result:
// function f
// input Real x;
// output Real y;
// algorithm
// y := f(x + 1.0);
// end f;
//
// class CevalFuncRecursive2
// constant Real x = f(3.0);
// end CevalFuncRecursive2;
// [flattening/modelica/scodeinst/CevalFuncRecursive2.mo:8:1-13:6:writable] Error: The recursion limit (--evalRecursionLimit=256) was exceeded during evaluation of f.
//
// endResult
30 changes: 30 additions & 0 deletions flattening/modelica/scodeinst/CevalFuncTerminate1.mo
@@ -0,0 +1,30 @@
// name: CevalFuncTerminate1
// keywords:
// status: correct
// cflags: -d=newInst
//
//

function f
output Integer n = 2;
algorithm
terminate("terminating");
end f;

model CevalFuncTerminate1
constant Real x = f();
end CevalFuncTerminate1;

// Result:
// function f
// output Integer n = 2;
// algorithm
// terminate("terminating");
// end f;
//
// class CevalFuncTerminate1
// constant Real x = /*Real*/(f());
// end CevalFuncTerminate1;
// [flattening/modelica/scodeinst/CevalFuncTerminate1.mo:11:3-11:27:writable] Error: terminate triggered: terminating
//
// endResult
31 changes: 31 additions & 0 deletions flattening/modelica/scodeinst/CevalFuncWhile1.mo
@@ -0,0 +1,31 @@
// name: CevalFuncWhile1
// keywords:
// status: correct
// cflags: -d=newInst
//
//

function f
input Integer n;
output Integer res;
protected
Integer i;
algorithm
i := n;
res := 0;

while i > 0 loop
res := res + i;
i := i - 1;
end while;
end f;

model CevalFuncWhile1
constant Real x = f(10);
end CevalFuncWhile1;

// Result:
// class CevalFuncWhile1
// constant Real x = 55.0;
// end CevalFuncWhile1;
// endResult
36 changes: 36 additions & 0 deletions flattening/modelica/scodeinst/CevalFuncWhile2.mo
@@ -0,0 +1,36 @@
// name: CevalFuncWhile2
// keywords:
// status: correct
// cflags: -d=newInst
//
//

function f
output Integer res;
algorithm
res := 0;
while true loop
res := res + 1;
end while;
end f;

model CevalFuncWhile2
constant Real x = f();
end CevalFuncWhile2;

// Result:
// function f
// output Integer res;
// algorithm
// res := 0;
// while true loop
// res := res + 1;
// end while;
// end f;
//
// class CevalFuncWhile2
// constant Real x = /*Real*/(f());
// end CevalFuncWhile2;
// [flattening/modelica/scodeinst/CevalFuncWhile2.mo:12:3-14:12:writable] Error: The loop iteration limit (--evalLoopLimit=100000) was exceeded during evaluation.
//
// endResult
11 changes: 11 additions & 0 deletions flattening/modelica/scodeinst/Makefile
Expand Up @@ -67,6 +67,17 @@ CevalDiv1.mo \
CevalExp1.mo \
CevalFill1.mo \
CevalFloor1.mo \
CevalFunc1.mo \
CevalFuncAssert1.mo \
CevalFuncAssert2.mo \
CevalFuncFor1.mo \
CevalFuncFor2.mo \
CevalFuncIf1.mo \
CevalFuncRecursive1.mo \
CevalFuncRecursive2.mo \
CevalFuncTerminate1.mo \
CevalFuncWhile1.mo \
CevalFuncWhile2.mo \
CevalIdentity1.mo \
CevalIf1.mo \
CevalInteger1.mo \
Expand Down

0 comments on commit 9fbd53a

Please sign in to comment.