示例#1
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);
  }
示例#2
0
 @Test
 public void testReturnArg() {
   args.set("it", 9);
   mainBlock.addReturnVar("it");
   int result = runMain();
   assertEquals(9, 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);
  }