Search in sources :

Example 26 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class SimpleSpeedTest method test_speed.

@Ignore
@Test
public void test_speed() throws SecurityException, NoSuchMethodException {
    final SimpleSpeedTest z = new SimpleSpeedTest();
    final String elstr = "num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7)-z.abc(i)";
    final Context context = Lang.context("{num:0}");
    context.set("z", z);
    System.out.println("\n" + Strings.dup('=', 100));
    Stopwatch sw = Stopwatch.run(new Atom() {

        public void run() {
            int num = 0;
            for (int i = 0; i < max; i++) num = num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7) - z.abc(i);
        //System.out.println("Num: " + num);
        }
    });
    System.out.println("\n" + Strings.dup('=', 100));
    Stopwatch sw3 = Stopwatch.run(new Atom() {

        public void run() {
            try {
                context.set("num", 0);
                for (int i = 0; i < max; i++) context.set("num", El.eval(context.set("i", i), elstr));
            //System.out.println("Num: " + context.getInt("num"));
            } catch (Exception e) {
                throw Lang.wrapThrow(e);
            }
        }
    });
    System.out.println("\n" + Strings.dup('=', 100));
    Stopwatch sw4 = Stopwatch.run(new Atom() {

        public void run() {
            try {
                El el2pre = new El(elstr);
                context.set("num", 0);
                context.set("z", z);
                for (int i = 0; i < max; i++) context.set("num", el2pre.eval(context.set("i", i)));
            //System.out.println("Num: " + context.getInt("num"));
            } catch (Exception e) {
                throw Lang.wrapThrow(e);
            }
        }
    });
    System.out.println("\n" + Strings.dup('=', 100));
    Stopwatch sw5 = Stopwatch.run(new Atom() {

        public void run() {
            try {
                El el2pre = new El(elstr);
                context.set("num", 0);
                context.set("z", z);
                for (int i = 0; i < max; i++) context.set("num", el2pre.eval(context.set("i", i)));
            //System.out.println("Num: " + context.getInt("num"));
            } catch (Exception e) {
                throw Lang.wrapThrow(e);
            }
        }
    });
    System.out.println("\n" + Strings.dup('=', 100));
    System.out.printf("\n%20s : %s", "Invoke", sw.toString());
    System.out.printf("\n%20s : %s", "Reflect", sw3.toString());
    System.out.printf("\n%20s : %s", "Reflect", sw4.toString());
    System.out.printf("\n%20s : %s", "Reflect", sw5.toString());
    System.out.println();
}
Also used : Context(org.nutz.lang.util.Context) El(org.nutz.el.El) Stopwatch(org.nutz.lang.Stopwatch) Atom(org.nutz.trans.Atom) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 27 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class El2Test method testIssue292.

@Test
public void testIssue292() {
    Context context = Lang.context();
    context.set("a", 123);
    context.set("b", 20);
    Object o = El.eval(context, "a>b?a:b");
    assertEquals(123, o);
}
Also used : Context(org.nutz.lang.util.Context) SimpleSpeedTest(org.nutz.el.speed.SimpleSpeedTest) Test(org.junit.Test)

Example 28 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class El2Test method testIssue293.

@Test
public void testIssue293() {
    Context context = Lang.context();
    context.set("static", new Issue293());
    context.set("a", Issue293.class);
    assertEquals("xxx", El.eval(context, "a.printParam(a.info)"));
}
Also used : Context(org.nutz.lang.util.Context) Issue293(org.nutz.el.issue.Issue293) SimpleSpeedTest(org.nutz.el.speed.SimpleSpeedTest) Test(org.junit.Test)

Example 29 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class El2Test method complexOperation.

@SuppressWarnings("unused")
@Test
public void complexOperation() {
    assertEquals(1000 + 100.0 * 99 - (600 - 3 * 15) % (((68 - 9) - 3) * 2 - 100) + 10000 % 7 * 71, El.eval("1000+100.0*99-(600-3*15)%(((68-9)-3)*2-100)+10000%7*71"));
    assertEquals(6.7 - 100 > 39.6 ? true ? 4 + 5 : 6 - 1 : !(100 % 3 - 39.0 < 27) ? 8 * 2 - 199 : 100 % 3, El.eval("6.7-100>39.6 ? 5==5? 4+5:6-1 : !(100%3-39.0<27) ? 8*2-199: 100%3"));
    Context vars = Lang.context();
    vars.set("i", 100);
    vars.set("pi", 3.14f);
    vars.set("d", -3.9);
    vars.set("b", (byte) 4);
    vars.set("bool", false);
    vars.set("t", "");
    String t = "i * pi + (d * b - 199) / (1 - d * pi) - (2 + 100 - i / pi) % 99 ==i * pi + (d * b - 199) / (1 - d * pi) - (2 + 100 - i / pi) % 99";
    // t =
    // "i * pi + (d * b - 199) / (1 - d * pi) - (2 + 100 - i / pi) % 99";
    assertEquals(true, El.eval(vars, t));
    // assertEquals('A' == ('A') || 'B' == 'B' && "ABCD" == "" && 'A' ==
    // 'A', el.eval(vars,
    // "'A' == 'A' || 'B' == 'B' && 'ABCD' == t &&  'A' == 'A'"));
    assertEquals(true || true && false && true, El.eval(vars, "'A' == 'A' || 'B' == 'B' && 'ABCD' == t &&  'A' == 'A'"));
}
Also used : Context(org.nutz.lang.util.Context) SimpleSpeedTest(org.nutz.el.speed.SimpleSpeedTest) Test(org.junit.Test)

Example 30 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class El2Test method field.

/**
     * 属性测试
     */
@Test
public void field() {
    class abc {

        @SuppressWarnings("unused")
        public String name = "jk";
    }
    Context context = Lang.context();
    context.set("a", new abc());
    assertEquals("jk", El.eval(context, "a.name"));
// 这个功能放弃
// assertFalse((Boolean)El.eval("java.lang.Boolean.FALSE"));
// assertFalse((Boolean)El.eval("Boolean.FALSE"));
}
Also used : Context(org.nutz.lang.util.Context) SimpleSpeedTest(org.nutz.el.speed.SimpleSpeedTest) Test(org.junit.Test)

Aggregations

Context (org.nutz.lang.util.Context)40 Test (org.junit.Test)29 SimpleSpeedTest (org.nutz.el.speed.SimpleSpeedTest)24 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)3 ServletContext (javax.servlet.ServletContext)3 El (org.nutz.el.El)3 Map (java.util.Map)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BigDecimal (java.math.BigDecimal)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 Ignore (org.junit.Ignore)1 ElException (org.nutz.el.ElException)1 Issue293 (org.nutz.el.issue.Issue293)1 Issue303 (org.nutz.el.issue.Issue303)1