Skip to content

Instantly share code, notes, and snippets.

@timaschew
Created February 23, 2011 19:59
Show Gist options
  • Save timaschew/841053 to your computer and use it in GitHub Desktop.
Save timaschew/841053 to your computer and use it in GitHub Desktop.
Yesss!!!!
@Test
public void testDigits() {
testResult("5", new Double(5));
testResult("123", new Double(123));
testResult("123.456", new Double(123.456));
}
@Test
public void testBooleanValues() {
testResult("true", true);
testResult("false", false);
}
@Test
public void debug() {
testResult("true == true", true);
}
@Test
public void testSimpleBoolean() {
testResult("1 > 2", false);
testResult("2 > 1", true);
testResult("1 < 2", true);
testResult("2 < 1", false);
testResult("1 < 1", false);
testResult("1 > 1", false);
testResult("x=5x<5", false);
testResult("x=5x<6", true);
testResult("x=5x>4", true);
testResult("3 == 3", true);
testResult("3 == 4", false);
testResult("true == true", true);
testResult("false == false", true);
testResult("true == false", false);
testResult("false == true", 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=true y=false x", true);
testResult("x=true y=false y", false);
testResult("x=5 y=x>4 y", true);
}
@Test
public void testOperators() {
testResult("4+2", (6));
testResult("4-2", (2));
testResult("4*2", (8));
testResult("4/2", (2));
testResult("1/4", (0.25));
testResult("3 * 3", (9)); // white space test
}
@Test
public void testComplexOps() {
testResult("1+2+3", (6));
testResult("1*2*3", (6));
testResult("1-2-3", (-4));
testResult("16/4/1", (4));
testResult("9/3*2", (6));
testResult("9*2/3", (6));
testResult("2*3-3*4/2+5", (5)); // mixed ops
}
@Test
public void testBrackets() {
testResult(" ( 3 * 3 ) ", (9));
testResult("2*(5+6)", (22));
testResult("(3+5)*(7+3)*(7-2)/4", (100));
testResult("((2*3-3)*4)/3", (4));
testResult("2*((4-2)*4/(3-2))*3", (48));
}
@Test
public void testForgottenBracket() {
testResult("2*(3+1", (8));
}
@Test
public void testZeroAndNegative() {
testResult("5-5", (0));
testResult("5-10", (-5));
}
@Test
public void testSimpleAssignment() {
testResult("x=7 x", (7));
testResult("x = 7 x", (7));
testResult("x = (3 * 3) x", (9));
}
@Test
public void testVarNames() {
testResult("x=10 x", (10));
testResult("y1=11 y1", (11));
testResult("hallo_ichbineine_123456789UltralangeVARIABLE = 10" +
"hallo_ichbineine_123456789UltralangeVARIABLE *= 1.5 hallo_ichbineine_123456789UltralangeVARIABLE" , (15));
}
@Test
public void testComplexAssignment() {
testResult("x=5*2*3/15 x", (2));
testResult("x = 5 + 2 + 10 x", (17));
testResult("x = (3 * 3) x = x * 2 x", (18));
testResult("x = (3 * 3) x = x * 2 y = x+2 y", (20));
}
@Test
public void testSimpleAssignmentEquals() {
testResult("x = 2 x", (2));
testResult("x = 2 x = x + 1 x", (3));
testResult("x = 2 x = x * 10 x", (20));
testResult("x = 3 x *= 10 x", (30));
testResult("x = 3 x += 10 x", (13));
}
@Test
public void testAssignmentEquals() {
testResult("x = 2 x *= 3 x", (6));
testResult("x=2x*=2+2*4 x", (20));
testResult("x = 2 x *= 5 y = x * 3 y", (30));
testResult("x = 10 x+= 3 x", (13));
testResult("x=5 x*=5 x", (25));
testResult("x=16 y=2 x/4*y", (8));
}
@Test
public void testFunnyStuff() {
try {
testResult("true = 23 false = 42", new Value(65.0));
Assert.fail();
} catch (IllegalArgumentException e) {
}
try {
testResult("x=2 y=5 x||y", new Value());
Assert.fail();
}catch (VerifyError e) {
}
}
@Test
public void testUndefinedVars() {
testResult("x = 3 * 3 y = x * z y", (0));
testResult("x", (0));
testResult("x + 5", (5));
testResult("x *= 4 x", (0));
testResult("x += 1 x", (1));
}
@Test
public void testSimpleIf() {
testResult("d = 9 "+
"b = 5 > 4 "+
"if (b) {"+
" d = 9.2"+
"} else {"+
" d = 18.3"+
"} d", 9.2);
}
@Test
public void testIfStatement() {
testResult("x=5 " +
"if (true) {" +
" x+=10" +
""+
"} x", 15);
testResult("x=5 " +
"if (false) {" +
" x+=10" +
"} x", 5);
// testResult("" +
// "if (false) {" +
// " x+=10" +
// "}", new Value()); // null
}
@Test
public void testBla() {
testResult("if (true) {45}", 45);
}
@Test
public void testIfScope() {
// there is no local scope for if/else statements
testResult("x=0" +
"if (true) {" +
" x=10" +
"} y=1 x", 10);
testResult("x=5" +
"if (false) {" +
" x=10" +
"} y=1 x+y", 6);
}
@Test
public void testOldIfFail() {
testResult(
"x=3" +
"z=4 " +
"if(false) {" +
" x=5" +
"}" +
"z=z+x z", 7);
}
@Test
public void testIfElseStatement() {
// skip if
testResult("x=5 " +
"if (false) {" +
" x+=10" +
"} else {" +
" x*=5" +
"} x", 25);
// skip else
testResult("x=5 " +
"if (true) {" +
" x+=10" +
"} else {" +
" x*=5" +
"} x", 15);
}
@Test
public void testNestedIfElse() {
// skip nested else and parent else
testResult(
"if (true) {" +
" if (true) {" +
" 10" +
" } else {" +
" 20" +
" }" +
"} else {" +
" if (true) {" +
" 100" +
" } else {" +
" 200" +
" }" +
"}", 10);
// skip nested else and parent else
testResult("x=1 " +
"if (true) {" +
" if (true) {" +
" x+=10" +
" } else {" +
" x+=20" +
" }" +
"} else {" +
" if (true) {" +
" x+=100" +
" } else {" +
" x+=200" +
" }" +
"} x", 11);
// skip nested if and parent else
testResult("x=1 " +
"if (true) {" +
" if (false) {" +
" x+=10" +
" } else {" +
" x+=20" +
" }" +
"} else {" +
" if (true) {" +
" x+=100" +
" } else {" +
" x+=200" +
" }" +
"} x", 21);
// skip parent if and nested else
testResult("x=1 " +
"if (false) {" +
" if (true) {" +
" x+=10" +
" } else {" +
" x+=20" +
" }" +
"} else {" +
" if (true) {" +
" x+=100" +
" } else {" +
" x+=200" +
" }" +
"} x", 101);
// skip parent if and nested if
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 " +
"} sum", 55);
testResult("x = 1" +
"while (x<10) { x+=1 }" +
"x", 10);
}
@Test
public void testSimpleFunction() {
testResult("pi() {3.14159265}" +
"x = pi() x*=2 x", 6.2831853);
}
@Test
public void testDebugFailVar1() {
testResult("" +
"sum (double:a double:b) {" +
" z = a + b " +
" z"+
"} sum (11 22) ", 33);
}
@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);
}
@Test
public void testFunctionWithRecursion() {
testResult(
"fakult(double:a) {" +
" if (a>1) {" +
" a * fakult(a-1)" +
" } else {" +
" 1" +
" }"+
"}" +
"fakult(5)", 120);
}
@Test
public void testAckermann() {
testResult("double:ack(double:n double:m) { "+
" while !(n==0) { " +
" if (m==0) {" +
" m=1" +
" } else { " +
" m = ack(n m - 1) " +
" }" +
" n = n - 1 " +
" } "+
" result = m + 1 " +
" result"+
"} " +
"ack(3 3)", 61);
// ACHTUNG, Ergebnis wächst unglaublich schnell bei Parameteränderungen!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment