Search in sources :

Example 21 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class SeqMapFunctionUnitTest method testCount_Collection.

@Test
public void testCount_Collection() {
    final List<String> strs = new LinkedList<String>();
    for (int i = 0; i < 10; i++) {
        strs.add("hello");
    }
    SeqMapFunction fun = new SeqMapFunction();
    Env env = TestUtils.getTestEnv();
    AviatorObject result = fun.call(env, AviatorRuntimeJavaType.valueOf(strs), new AviatorJavaType("string.length"));
    LinkedList array = (LinkedList) result.getValue(env);
    for (Object i : array) {
        assertEquals(5, i);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Env(com.googlecode.aviator.utils.Env) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 22 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class SeqMapFunctionUnitTest method testMap_String.

@Test
public void testMap_String() {
    Env env = TestUtils.getTestEnv();
    SeqMapFunction fun = new SeqMapFunction();
    AviatorObject result = fun.call(env, AviatorRuntimeJavaType.valueOf("hello"), new AviatorJavaType("string.length"));
    List list = (List) result.getValue(null);
    assertEquals(5, list.size());
    for (Object val : list) {
        assertEquals(val, 1);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) List(java.util.List) LinkedList(java.util.LinkedList) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Env(com.googlecode.aviator.utils.Env) Test(org.junit.Test)

Example 23 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class MathLogFunctionUnitTest method testCall.

@Test
public void testCall() {
    Env env = TestUtils.getTestEnv();
    assertEquals(2.197, this.function.call(env, AviatorNumber.valueOf(9)).getValue(null));
    assertEquals(4.394, this.function.call(env, AviatorNumber.valueOf(81)).getValue(null));
    assertEquals(5.991, this.function.call(env, AviatorNumber.valueOf(400)).getValue(null));
    assertEquals(new BigDecimal("5.991464547107981986870447152285084"), this.function.call(env, AviatorNumber.valueOf(new BigInteger("400"))).getValue(null));
    assertEquals(new BigDecimal("6.907755268982137002053974030719759"), this.function.call(env, AviatorNumber.valueOf(new BigDecimal("999.99999"))).getValue(null));
    env.put("a", 400);
    env.put("b", 9.0);
    assertEquals(5.991, this.function.call(env, new AviatorJavaType("a")).getValue(null));
    assertEquals(2.197, this.function.call(env, new AviatorJavaType("b")).getValue(null));
}
Also used : AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) BigInteger(java.math.BigInteger) Env(com.googlecode.aviator.utils.Env) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 24 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class SeqArrayFunction method variadicCall.

@Override
public AviatorObject variadicCall(final Map<String, Object> env, final AviatorObject... args) {
    if (args == null || args.length == 0) {
        throw new IllegalArgumentException("Missing arguments for seq.array");
    }
    AviatorObject clazzVar = args[0];
    if (clazzVar == null || clazzVar.getAviatorType() != AviatorType.JavaType) {
        throw new IllegalArgumentException("Invalid class:" + (clazzVar == null ? "null" : clazzVar.desc(env)));
    }
    try {
        String name = ((AviatorJavaType) clazzVar).getName();
        Class<?> clazz = null;
        if (TypeUtils.PRIMITIVE_TYPES.containsKey(name)) {
            clazz = TypeUtils.PRIMITIVE_TYPES.get(name);
        } else {
            assert (env instanceof Env);
            clazz = ((Env) env).resolveClassSymbol(name);
        }
        Object ret = Array.newInstance(clazz, args.length - 1);
        for (int i = 1; i < args.length; i++) {
            ArrayUtils.set(ret, i - 1, Reflector.boxArg(clazz, args[i].getValue(env)));
        }
        return AviatorRuntimeJavaType.valueOf(ret);
    } catch (Throwable t) {
        throw Reflector.sneakyThrow(t);
    }
}
Also used : AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) AviatorJavaType(com.googlecode.aviator.runtime.type.AviatorJavaType) AviatorObject(com.googlecode.aviator.runtime.type.AviatorObject) Env(com.googlecode.aviator.utils.Env)

Example 25 with Env

use of com.googlecode.aviator.utils.Env in project aviatorscript by killme2008.

the class AviatorNumberUnitTest method testArthOperationWithNumber.

public void testArthOperationWithNumber(OperatorType operatorType) {
    Env env = TestUtils.getTestEnv();
    switch(operatorType) {
        case Add:
            this.reset();
            assertEquals(6.3, this.a.add(this.b, env).getValue(env));
            this.reset();
            assertEquals(6.3, this.b.add(this.a, env).getValue(env));
            this.reset();
            assertEquals(7.6, this.a.add(this.d, env).getValue(env));
            this.reset();
            assertEquals(7.6, this.d.add(this.a, env).getValue(env));
            this.reset();
            assertEquals(1003, this.b.add(this.c, env).getValue(env));
            this.reset();
            assertEquals(1003, this.c.add(this.b, env).getValue(env));
            this.reset();
            assertEquals(7.3, this.b.add(this.d, env).getValue(env));
            this.reset();
            assertEquals(7.3, this.d.add(this.b, env).getValue(env));
            assertEquals(new BigInteger("92233720368547758074"), this.b.add(this.e, env).getValue(env));
            assertEquals(new BigInteger("92233720368547759071"), this.c.add(this.e, env).getValue(env));
            assertEquals(9.223372036854776E18, this.a.add(this.e, env).getValue(env));
            assertEquals(new BigDecimal("92233720368547758074.1001"), this.b.add(this.f, env).getValue(env));
            assertEquals(new BigDecimal("92233720368547759071.1001"), this.c.add(this.f, env).getValue(env));
            assertEquals(9.223372036854776E19, this.a.add(this.f, env).getValue(env));
            break;
        case Sub:
            this.reset();
            assertEquals(0.3, this.a.sub(this.b, env).getValue(env));
            this.reset();
            assertEquals(-0.3, this.b.sub(this.a, env).getValue(env));
            this.reset();
            assertEquals(-1.0, this.a.sub(this.d, env).getValue(env));
            this.reset();
            assertEquals(1.0, this.d.sub(this.a, env).getValue(env));
            this.reset();
            assertEquals(-997, this.b.sub(this.c, env).getValue(env));
            this.reset();
            assertEquals(997, this.c.sub(this.b, env).getValue(env));
            this.reset();
            assertEquals(-1.3, this.b.sub(this.d, env).getValue(env));
            this.reset();
            assertEquals(1.3, this.d.sub(this.b, env).getValue(env));
            assertEquals(new BigInteger("-92233720368547758068"), this.b.sub(this.e, env).getValue(env));
            assertEquals(new BigInteger("-92233720368547757071"), this.c.sub(this.e, env).getValue(env));
            assertEquals(-9.223372036854776E19, this.a.sub(this.e, env).getValue(env));
            assertEquals(new BigDecimal("-92233720368547758068.1001"), this.b.sub(this.f, env).getValue(env));
            assertEquals(new BigDecimal("-92233720368547757071.1001"), this.c.sub(this.f, env).getValue(env));
            assertEquals(-9.223372036854776E19, this.a.sub(this.f, env).getValue(env));
            break;
        case Mult:
            this.reset();
            assertEquals(9.9, this.a.mult(this.b, env).getValue(env));
            this.reset();
            assertEquals(9.9, this.b.mult(this.a, env).getValue(env));
            this.reset();
            assertEquals(14.19, this.a.mult(this.d, env).getValue(env));
            this.reset();
            assertEquals(14.19, this.d.mult(this.a, env).getValue(env));
            this.reset();
            assertEquals(3000, this.b.mult(this.c, env).getValue(env));
            this.reset();
            assertEquals(3000, this.c.mult(this.b, env).getValue(env));
            this.reset();
            assertEquals(12.9, this.b.mult(this.d, env).getValue(env));
            this.reset();
            assertEquals(12.9, this.d.mult(this.b, env).getValue(env));
            assertEquals(new BigInteger("276701161105643274213"), this.b.mult(this.e, env).getValue(env));
            assertEquals(new BigInteger("92233720368547758071000"), this.c.mult(this.e, env).getValue(env));
            assertEquals(3.043712728181611E20, this.a.mult(this.e, env).getValue(env));
            assertEquals(new BigDecimal("276701161105643274213.3003"), this.b.mult(this.f, env).getValue(env));
            assertEquals(new BigDecimal("92233720368547758071100.1000"), this.c.mult(this.f, env).getValue(env));
            assertEquals(3.043712728181611E20, this.a.mult(this.f, env).getValue(env));
            break;
        case Div:
            this.reset();
            // 3.3 3 1000 4.3
            assertEquals(1.1, (Double) this.a.div(this.b, env).getValue(env), 0.001);
            this.reset();
            assertEquals(0.90909090, (Double) this.b.div(this.a, env).getValue(env), 0.001);
            this.reset();
            assertEquals(0.76744, (Double) this.a.div(this.d, env).getValue(env), 0.001);
            this.reset();
            assertEquals(1.30303030, (Double) this.d.div(this.a, env).getValue(env), 0.001);
            this.reset();
            assertEquals(0, this.b.div(this.c, env).getValue(env));
            this.reset();
            assertEquals(333, this.c.div(this.b, env).getValue(env));
            this.reset();
            assertEquals(0.6976744, (Double) this.b.div(this.d, env).getValue(env), 0.001);
            this.reset();
            assertEquals(1.433333333, (Double) this.d.div(this.b, env).getValue(env), 0.001);
            assertEquals(new BigInteger("30744573456182586023"), this.e.div(this.b, env).getValue(env));
            assertEquals(new BigInteger("92233720368547758"), this.e.div(this.c, env).getValue(env));
            assertEquals(2.794961223289326E19, this.e.div(this.a, env).getValue(env));
            assertEquals(new BigDecimal("3.252606517456513302336211867796323E-20"), this.b.div(this.f, env).getValue(env));
            assertEquals(new BigDecimal("1.084202172485504434112070622598774E-17"), this.c.div(this.f, env).getValue(env));
            assertEquals(3.577867169202164E-20, this.a.div(this.f, env).getValue(env));
            break;
        case Mod:
            this.reset();
            assertEquals(0.3, (Double) this.a.mod(this.b, env).getValue(env), 0.001);
            this.reset();
            assertEquals(3.0, (Double) this.b.mod(this.a, env).getValue(env), 0.001);
            this.reset();
            assertEquals(3.3, (Double) this.a.mod(this.d, env).getValue(env), 0.001);
            this.reset();
            assertEquals(1.0, (Double) this.d.mod(this.a, env).getValue(env), 0.001);
            this.reset();
            assertEquals(3, this.b.mod(this.c, env).getValue(env));
            this.reset();
            assertEquals(1, this.c.mod(this.b, env).getValue(env));
            this.reset();
            assertEquals(3.0, (Double) this.b.mod(this.d, env).getValue(env), 0.001);
            this.reset();
            assertEquals(1.3, (Double) this.d.mod(this.b, env).getValue(env), 0.001);
            assertEquals(new BigInteger("2"), this.e.mod(this.b, env).getValue(env));
            assertEquals(new BigInteger("71"), this.e.mod(this.c, env).getValue(env));
            assertEquals(0.0, this.e.mod(this.a, env).getValue(env));
            assertEquals(new BigDecimal("3"), this.b.mod(this.f, env).getValue(env));
            assertEquals(new BigDecimal("1000"), this.c.mod(this.f, env).getValue(env));
            assertEquals(3.3, this.a.mod(this.f, env).getValue(env));
            assertEquals(new BigDecimal("2.1001"), this.f.mod(this.b, env).getValue(env));
            assertEquals(new BigDecimal("71.1001"), this.f.mod(this.c, env).getValue(env));
            assertEquals(0.0, this.f.mod(this.a, env).getValue(env));
            break;
    }
}
Also used : BigInteger(java.math.BigInteger) Env(com.googlecode.aviator.utils.Env) BigDecimal(java.math.BigDecimal)

Aggregations

Env (com.googlecode.aviator.utils.Env)28 AviatorJavaType (com.googlecode.aviator.runtime.type.AviatorJavaType)10 Test (org.junit.Test)8 AviatorObject (com.googlecode.aviator.runtime.type.AviatorObject)6 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4 LRUMap (com.googlecode.aviator.utils.LRUMap)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Import (com.googlecode.aviator.annotation.Import)1 InterpretContext (com.googlecode.aviator.code.interpreter.InterpretContext)1 LoadIR (com.googlecode.aviator.code.interpreter.ir.LoadIR)1 ExpressionRuntimeException (com.googlecode.aviator.exception.ExpressionRuntimeException)1 ExpressionSyntaxErrorException (com.googlecode.aviator.exception.ExpressionSyntaxErrorException)1 FunctionParam (com.googlecode.aviator.runtime.FunctionParam)1 ClassMethodFunction (com.googlecode.aviator.runtime.function.ClassMethodFunction)1