Search in sources :

Example 31 with Expression

use of com.googlecode.aviator.Expression in project aviatorscript by killme2008.

the class GrammarUnitTest method testExponent.

@Test
public void testExponent() {
    assertEquals(1, exec("0**0"));
    assertEquals(1, exec("1**0"));
    assertEquals(1.0, exec("1.2**0"));
    assertEquals(-9, exec("-3**2"));
    assertEquals(-1.0, exec("-1.2**0"));
    assertEquals(-1, exec("-1**0"));
    assertEquals(new BigDecimal("1"), exec("3M**0"));
    assertEquals(new BigInteger("-1"), exec("-2N**0"));
    assertEquals(1, exec("1 + 4/2**3"));
    assertEquals(1, exec("1 + 4/-2**3"));
    assertEquals(33.0, exec("1 + 4/2**-3"));
    assertEquals(5, exec("1 + 4/2**0"));
    assertEquals(-2.2, exec("1-4**2*5**-1"));
    assertEquals(-2.2, exec("1-(4**2)*(5**-1)"));
    assertEquals(Math.pow(2, 1000), exec("2**1000.0"));
    assertEquals(Math.pow(2, 1000), exec("2.0**1000.0"));
    assertEquals(Math.pow(2, 1000), exec("2.0**1000"));
    assertEquals(new BigDecimal("2.0").pow(1000, RuntimeUtils.getMathContext(null)), exec("2.0M**1000"));
    assertEquals(new BigDecimal("2.0").pow(1000, RuntimeUtils.getMathContext(null)), exec("2.0M**1000.001"));
    assertEquals(new BigInteger("2").pow(1000), exec("2N**1000.001"));
    assertEquals(new BigInteger("2").pow(1000), exec("2N**1000.001"));
    Expression exp = this.instance.compile("a-b/c**2.0*1000");
    assertEquals(-221.2222222222222, exp.execute(exp.newEnv("a", 1, "b", 2, "c", 3)));
    assertEquals(-221.2222222222222, exp.execute(exp.newEnv("a", 1, "b", -2, "c", -3)));
    assertEquals(322.2222222222222, exec("100-2/-3**2.0*1000"));
    assertEquals(-122.2222222222222, exec("100-2/(-3)**2.0*1000"));
    assertEquals(-122.2222222222222, exp.execute(exp.newEnv("a", 100, "b", 2, "c", -3)));
}
Also used : Expression(com.googlecode.aviator.Expression) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

Expression (com.googlecode.aviator.Expression)31 Test (org.junit.Test)16 HashMap (java.util.HashMap)8 NumberToken (com.googlecode.aviator.lexer.token.NumberToken)3 AviatorString (com.googlecode.aviator.runtime.type.AviatorString)3 Map (java.util.Map)3 PatternToken (com.googlecode.aviator.lexer.token.PatternToken)2 StringToken (com.googlecode.aviator.lexer.token.StringToken)2 CollectRep (com.usthe.common.entity.message.CollectRep)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 AviatorEvaluator (com.googlecode.aviator.AviatorEvaluator)1 AviatorEvaluatorInstance (com.googlecode.aviator.AviatorEvaluatorInstance)1 BaseExpression (com.googlecode.aviator.BaseExpression)1 LiteralExpression (com.googlecode.aviator.LiteralExpression)1 CompareNotSupportedException (com.googlecode.aviator.exception.CompareNotSupportedException)1 CompileExpressionErrorException (com.googlecode.aviator.exception.CompileExpressionErrorException)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 DelegateToken (com.googlecode.aviator.lexer.token.DelegateToken)1