Skip to content

Instantly share code, notes, and snippets.

@timaschew
Created January 7, 2011 00:00
Show Gist options
  • Save timaschew/768880 to your computer and use it in GitHub Desktop.
Save timaschew/768880 to your computer and use it in GitHub Desktop.
Tests für Boolsche Ausdrücke, IfElse, While, Funktionen mit/ohne Parameter, rekursive Funktionen
@Test
public void testBooleanValues() {
testResult("true", true);
testResult("false", false);
testResult("x=true y=false x", true);
testResult("x=true y=false y", false);
}
@Test
public void testFunnyStuff() {
try {
testResult("true = 23 false = 42", 65.0); // reservierte Namen
Assert.fail();
} catch (IllegalArgumentException e) {
}
testResult("x=2 y=5 x||y", new Value()); // null sozusagen
}
@Test
public void testSimpleBoolean() {
testResult("1 > 2", false);
testResult("2 > 1", true);
testResult("x=5x<5", false);
testResult("x=5x<6", true);
testResult("x=5x>4", true);
}
@Test
public void testDebugStuff() {
testResult("4 == 5", false);
}
@Test
public void testBooleanLogicExpressions() {
testResult("!true", false);
testResult("!false", true);
testResult("x=true !x", false);
testResult("x=false !x", true);
testResult("x=true y=true x==y", true);
testResult("x=true y=false x==y", false);
testResult("x=true y=true x&&y", true);
testResult("x=true y=false x&&y", false);
testResult("x=false y=true x&&y", false);
testResult("x=false y=false x&&y", false);
testResult("x=true y=true x||y", true);
testResult("x=true y=false x||y", true);
testResult("x=false y=true x||y", true);
testResult("x=false y=false x||y", false);
}
@Test
public void testBooleanWithBrackets() {
testResult("x=false y=true (x)", false);
}
@Test
public void testBooleanExpressions() {
testResult("false||true&&false", false);
testResult("false||true&&true", true);
testResult("true||false&&true", true);
}
@Test
public void testBooleanAssigment() {
testResult("x=5 y=x>4", true);
}
@Test
public void testIfStatement() {
testResult("x=5 " +
"if (true) {" +
" x+=10" +
"} x", 15);
testResult("x=5 " +
"if (false) {" +
" x+=10" +
"} x", 5);
}
@Test
public void testIfScope() {
// there is no local scope for if/else statements
testResult("x=0" +
"if (true) {" +
" x=10" +
"} y=1 x", 10);
}
@Test
public void testIfElseStatement() {
testResult("x=5 " +
"if (false) {" +
" x+=10" +
"} else {" +
" x*=5" +
"} x", 25);
testResult("x=5 " +
"if (true) {" +
" x+=10" +
"} else {" +
" x*=5" +
"} x", 15);
}
@Test
public void testNestedIfElse() {
testResult("x=1 " +
"if (true) {" +
" if (true) {" +
" x+=10" +
" } else {" +
" x+=20" +
" }" +
"} else {" +
" if (true) {" +
" x+=100" +
" } else {" +
" x+=200" +
" }" +
"} x", 11);
testResult("x=1 " +
"if (true) {" +
" if (false) {" +
" x+=10" +
" } else {" +
" x+=20" +
" }" +
"} else {" +
" if (true) {" +
" x+=100" +
" } else {" +
" x+=200" +
" }" +
"} x", 21);
testResult("x=1 " +
"if (false) {" +
" if (true) {" +
" x+=10" +
" } else {" +
" x+=20" +
" }" +
"} else {" +
" if (true) {" +
" x+=100" +
" } else {" +
" x+=200" +
" }" +
"} x", 101);
testResult("x=1 " +
"if (false) {" +
" if (true) {" +
" x+=10" +
" } else {" +
" x+=20" +
" }" +
"} else {" +
" if (false) {" +
" x+=100" +
" } else {" +
" x+=200" +
" }" +
"} x", 201);
}
@Test
public void testWhile() {
testResult("i=0 sum=0" +
"while (i<10) {" +
" i=i+1" +
" sum=sum+i " +
"}", 55);
testResult("x = 1" +
"while (x<10) { x+=1 }" +
"x", 10);
}
@Test
public void testSimpleFunction() {
testResult("pi() {3.14159265}" +
"x = pi() x*=2", 6.2831853);
}
@Test
public void testFunctionWithParam() {
testResult("x=5 y=10" +
"sum(a b) {" +
" tmp = a + b" +
"}" +
"sum(x y)", 15);
testResult("x=5 y=10" +
"sum(a b) {" +
" x = 999" +
" tmp = a + b" +
"}" +
"sum(x y) + x", 20);
}
@Test
public void testFunctionWithRecursion() {
testResult(
"fakult(a) {" +
" if (a>1) {" +
" a * fakult(a-1)" +
" } else {" +
" 1" +
" }"+
"}" +
"fakult(5)", 120);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment