Merge pull request #266 from sk89q/expression

Fixed the query functions not comparing data values at all for their return values.
This commit is contained in:
Albert Pham 2013-09-25 11:59:47 -07:00
commit a65b574d9d
2 changed files with 77 additions and 7 deletions

View File

@ -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 // 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); if (type instanceof LValue) {
data.assign(dataValue); ((LValue) type).assign(typeId);
}
if (data instanceof LValue) {
((LValue) data).assign(dataValue);
}
return ret; return ret;
} }
@Dynamic @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 xp = x.getValue();
final double yp = y.getValue(); final double yp = y.getValue();
final double zp = z.getValue(); final double zp = z.getValue();
@ -403,7 +408,7 @@ public final class Functions {
} }
@Dynamic @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 xp = x.getValue();
final double yp = y.getValue(); final double yp = y.getValue();
final double zp = z.getValue(); final double zp = z.getValue();
@ -418,7 +423,7 @@ public final class Functions {
} }
@Dynamic @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 xp = x.getValue();
final double yp = y.getValue(); final double yp = y.getValue();
final double zp = z.getValue(); final double zp = z.getValue();

View File

@ -3,6 +3,7 @@ package com.sk89q.worldedit.expression;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static java.lang.Math.*; import static java.lang.Math.*;
import com.sk89q.worldedit.expression.runtime.ExpressionEnvironment;
import org.junit.*; import org.junit.*;
import com.sk89q.worldedit.expression.lexer.LexerException; import com.sk89q.worldedit.expression.lexer.LexerException;
@ -59,6 +60,26 @@ public class ExpressionTest {
compile("x("); compile("x(");
fail("Error expected"); fail("Error expected");
} catch (ParserException e) {} } 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 @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); 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 { private double simpleEval(String expressionString) throws ExpressionException {
final Expression expression = compile(expressionString); 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(); return expression.evaluate();
} }