示例#1
0
  @Test
  public void testSimpleMath() {
    Expr four = ValueExpr.createValue(4);
    Expr five = ValueExpr.createValue(5);

    Expr expr = new MathExpr(MathExpr.Operation.ADD, four, five);
    mainBlock.addStatement(new ReturnStatement(expr));

    int result = runMain();
    assertEquals(9, result);
  }
示例#2
0
  @Test
  public void testSimpleMath2() {
    // 2 * 3 * 2
    Expr two = ValueExpr.createValue(2);
    Expr three = ValueExpr.createValue(3);

    Expr multExpr = new MathExpr(MathExpr.Operation.MULT, two, three);
    Expr expr = new MathExpr(MathExpr.Operation.MULT, multExpr, two);
    mainBlock.addStatement(new ReturnStatement(expr));

    int result = runMain();
    assertEquals(12, result);
  }
示例#3
0
  @Test
  public void testIfFalse() {
    // if it == 0 return 1
    args.set("it", 2);

    Expr expr =
        new MathExpr(MathExpr.Operation.EQ, ValueExpr.createVar("it"), ValueExpr.createValue(0));
    Block block = new Block();
    block.addReturnValue(1);
    mainBlock.addStatement(new IfStatement(expr, block));
    mainBlock.addReturnValue(0);

    int result = runMain();
    assertEquals(0, result);
  }
示例#4
0
  @Test
  public void testWhile() {
    // if it == 0 return 1
    args.set("it", 5);

    Expr expr =
        new MathExpr(MathExpr.Operation.GT, ValueExpr.createVar("it"), ValueExpr.createValue(0));
    mainBlock.addSimpleAssign("total", 0);
    Block block = new Block();
    // subtract 1 from it
    MathExpr decrementIt =
        new MathExpr(MathExpr.Operation.SUB, ValueExpr.createVar("it"), ValueExpr.createValue(1));
    block.addStatement(new Assignment("it", decrementIt));
    // add 2 to total
    MathExpr addTwoToTotal =
        new MathExpr(
            MathExpr.Operation.ADD, ValueExpr.createVar("total"), ValueExpr.createValue(2));
    block.addStatement(new Assignment("total", addTwoToTotal));

    mainBlock.addStatement(new WhileStatement(expr, block));
    mainBlock.addReturnVar("total");

    int result = runMain();
    assertEquals(10, result);
  }