From e2f1abd7e41ba4a3e1eba1e6d2271ffba32284bd Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Wed, 25 Sep 2013 00:35:04 +0200 Subject: [PATCH] Fixed the query functions not comparing data values at all for their return values. Also made them accept RValue as well and added some test cases for them and for the overloader bug i fixed last time. --- .../expression/runtime/Functions.java | 19 ++++-- .../worldedit/expression/ExpressionTest.java | 65 +++++++++++++++++++ 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/expression/runtime/Functions.java b/src/main/java/com/sk89q/worldedit/expression/runtime/Functions.java index 018d217ea..51d1a2f36 100644 --- a/src/main/java/com/sk89q/worldedit/expression/runtime/Functions.java +++ b/src/main/java/com/sk89q/worldedit/expression/runtime/Functions.java @@ -377,18 +377,23 @@ public final class Functions { } - private static double queryInternal(LValue type, LValue data, double typeId, double dataValue) throws EvaluationException { + private static double queryInternal(RValue type, RValue data, double typeId, double dataValue) throws EvaluationException { // Compare to input values and determine return value - final double ret = typeId == type.getValue() && typeId == type.getValue() ? 1.0 : 0.0; + final double ret = (typeId == type.getValue() && dataValue == data.getValue()) ? 1.0 : 0.0; - type.assign(typeId); - data.assign(dataValue); + if (type instanceof LValue) { + ((LValue) type).assign(typeId); + } + + if (data instanceof LValue) { + ((LValue) data).assign(dataValue); + } return ret; } @Dynamic - public static double query(RValue x, RValue y, RValue z, LValue type, LValue data) throws EvaluationException { + public static double query(RValue x, RValue y, RValue z, RValue type, RValue data) throws EvaluationException { final double xp = x.getValue(); final double yp = y.getValue(); final double zp = z.getValue(); @@ -403,7 +408,7 @@ public final class Functions { } @Dynamic - public static double queryAbs(RValue x, RValue y, RValue z, LValue type, LValue data) throws EvaluationException { + public static double queryAbs(RValue x, RValue y, RValue z, RValue type, RValue data) throws EvaluationException { final double xp = x.getValue(); final double yp = y.getValue(); final double zp = z.getValue(); @@ -418,7 +423,7 @@ public final class Functions { } @Dynamic - public static double queryRel(RValue x, RValue y, RValue z, LValue type, LValue data) throws EvaluationException { + public static double queryRel(RValue x, RValue y, RValue z, RValue type, RValue data) throws EvaluationException { final double xp = x.getValue(); final double yp = y.getValue(); final double zp = z.getValue(); diff --git a/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java b/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java index 26acd4533..ab30d0b27 100644 --- a/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java +++ b/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java @@ -3,6 +3,7 @@ package com.sk89q.worldedit.expression; import static org.junit.Assert.*; import static java.lang.Math.*; +import com.sk89q.worldedit.expression.runtime.ExpressionEnvironment; import org.junit.*; import com.sk89q.worldedit.expression.lexer.LexerException; @@ -59,6 +60,26 @@ public class ExpressionTest { compile("x("); fail("Error expected"); } catch (ParserException e) {} + + // test overloader errors + try { + compile("atan2(1)"); + fail("Error expected"); + } catch (ParserException e) { + assertEquals("Error position", 0, e.getPosition()); + } + try { + compile("atan2(1, 2, 3)"); + fail("Error expected"); + } catch (ParserException e) { + assertEquals("Error position", 0, e.getPosition()); + } + try { + compile("rotate(1, 2, 3)"); + fail("Error expected"); + } catch (ParserException e) { + assertEquals("Error position", 0, e.getPosition()); + } } @Test @@ -110,8 +131,52 @@ public class ExpressionTest { assertEquals(127, simpleEval("x=1;y=2;z=3;switch (3) { case 1: x=5; case 2: y=6; default: z=7 } x*100+y*10+z"), 0); } + @Test + public void testQuery() throws Exception { + assertEquals(1, simpleEval("a=1;b=2;query(3,4,5,a,b); a==3 && b==4"), 0); + assertEquals(1, simpleEval("a=1;b=2;queryAbs(3,4,5,a*1,b*1); a==1 && b==2"), 0); + assertEquals(1, simpleEval("a=1;b=2;queryRel(3,4,5,(a),(b)); a==300 && b==400"), 0); + assertEquals(1, simpleEval("query(3,4,5,3,4)"), 0); + assertEquals(1, simpleEval("!query(3,4,5,3,2)"), 0); + assertEquals(1, simpleEval("!queryAbs(3,4,5,10,40)"), 0); + assertEquals(1, simpleEval("!queryRel(3,4,5,100,200)"), 0); + } + private double simpleEval(String expressionString) throws ExpressionException { final Expression expression = compile(expressionString); + + expression.setEnvironment(new ExpressionEnvironment() { + @Override + public int getBlockType(double x, double y, double z) { + return (int) x; + } + + @Override + public int getBlockData(double x, double y, double z) { + return (int) y; + } + + @Override + public int getBlockTypeAbs(double x, double y, double z) { + return (int) x*10; + } + + @Override + public int getBlockDataAbs(double x, double y, double z) { + return (int) y*10; + } + + @Override + public int getBlockTypeRel(double x, double y, double z) { + return (int) x*100; + } + + @Override + public int getBlockDataRel(double x, double y, double z) { + return (int) y*100; + } + }); + return expression.evaluate(); }