Skip to content

Instantly share code, notes, and snippets.

@timaschew
Created February 23, 2011 02:35
Show Gist options
  • Save timaschew/839880 to your computer and use it in GitHub Desktop.
Save timaschew/839880 to your computer and use it in GitHub Desktop.
nur funktionen
/*
Schema für parameter und rückgabewert-typen:
<TYPE>:<NAME>
Bsp: Parameter= double:a
Bsp: Funktion= boolean:equal(double:a double:b) {...}
Die Funktionen haben als Default double als Rückgabetyp, falls dieser nicht angegeben ist
*/
@Test
public void testSimpleFunction() {
testResult("pi() {3.14159265}" +
"x = pi() x*=2 x", 6.2831853);
}
@Test
public void testFunctionWithDirectParams() {
testResult("sum (double:a double:b) {" +
" z = a + b " +
" z" +
"} sum (2 3)", 5);
}
@Test
public void testFunctionWithVariables() {
testResult("x=5 y=10 " +
"sum(double:a double:b) {" +
" tmp = a + b" +
" tmp"+
"}" +
"sum(x y)", 15);
}
@Test
public void testFunctionWithLocalScope() {
testResult("x=5 y=10 " +
"sum(double:a double:b) {" +
" x = 999" +
" tmp = a + b" +
" tmp"+
"}" +
"sum(x y) + x", 20);
}
@Test
public void testFunctionWithMoreParams() {
testResult("sum (double:a double:b double:c) {" +
" z = a + b + c " +
" z" +
"} sum (2 3 4)", 9);
}
@Test
public void testFunctionWithBooleanReturnType() {
testResult("boolean:equal (double:a double:b ) {" +
" z = a == b " +
" z" +
"} equal (4 5)", false);
testResult("boolean:equal (double:a double:b ) {" +
" z = a == b " +
" z" +
"} equal (4 4)", true);
}
@Test
public void testFunctionWithImplicitDoubleType() {
testResult("double:sum (double:a double:b) {" +
" z = a + b " +
" z" +
"} sum (2 3)", 5);
}
@Test
public void testFunctionWithMixedTypes() {
testResult("boolean:equal (double:a double:b ) {" +
" z = a == b " +
" z" +
"} r = equal (4 5) r", false);
testResult("boolean:equal (double:a double:b ) {" +
" z = a == b " +
" z" +
"} r = equal (4 5)" +
"x = 1"+
"if (r) {" +
" x = 42" +
"} else {" +
" x = 13" +
"} x", 13);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment