Cover many more ops

(cherry picked from commit 3dbaae79cd4aa37724fb8969bbf595180e152f3e)
This commit is contained in:
Octavia Togami 2020-02-25 16:35:43 -08:00 committed by MattBDev
parent a464bde43b
commit 0e4a206f72
2 changed files with 78 additions and 2 deletions

View File

@ -462,8 +462,6 @@ class CompilingVisitor extends ExpressionBaseVisitor<MethodHandle> {
return (l, r) -> ExpressionHandles.boolToDouble(l != r);
case NEAR:
return (l, r) -> ExpressionHandles.boolToDouble(almostEqual2sComplement(l, r));
case GREATER_THAN_OR_EQUAL:
return (l, r) -> ExpressionHandles.boolToDouble(l >= r);
}
throw ExpressionHelper.evalException(ctx, "Invalid text for equality expr: " + ctx.op.getText());
});

View File

@ -101,6 +101,84 @@ class ExpressionTest extends BaseExpressionTest {
checkTestCase("!-2", 0);
}
@Test
void testComplement() {
checkTestCase("~0", ~0);
checkTestCase("~1", ~1);
checkTestCase("~-1", ~-1);
checkTestCase("~-2", ~-2);
// it drops the decimal!
checkTestCase("~0.1", ~0);
checkTestCase("~0.5", ~0);
checkTestCase("~1.9", ~1);
}
@Test
void testShift() {
checkTestCase("1<<4", 1 << 4);
// drops both decimals
checkTestCase("1.1<<4.1", 1 << 4);
checkTestCase("16>>2", 16 >> 2);
checkTestCase("16.9>>2.1", 16 >> 2);
}
@Test
void testComparisonOps() {
checkTestCase("1>=0", 1);
checkTestCase("1>0", 1);
checkTestCase("0>=0", 1);
checkTestCase("0>0", 0);
checkTestCase("0<=1", 1);
checkTestCase("0<1", 1);
checkTestCase("0<=0", 1);
checkTestCase("0<0", 0);
checkTestCase("1>=2", 0);
checkTestCase("1>2", 0);
checkTestCase("0>=1", 0);
checkTestCase("0>1", 0);
checkTestCase("2<=1", 0);
checkTestCase("2<1", 0);
checkTestCase("1<=0", 0);
checkTestCase("1<0", 0);
}
@Test
void testEqualityOps() {
checkTestCase("1==1", 1);
checkTestCase("0==1", 0);
checkTestCase("1==0", 0);
checkTestCase("1!=1", 0);
checkTestCase("0!=1", 1);
checkTestCase("1!=0", 1);
checkTestCase("1.1==1.1", 1);
// These aren't normally equal
checkTestCase("1!=0.999999999", 1);
// But they are _almost_ equal!
checkTestCase("1~=0.999999999", 1);
// On the other hand, these aren't
checkTestCase("1~=0.9", 0);
}
@Test
void testPostfixOps() {
checkTestCase("(-1)!", 0);
checkTestCase("0!", 1);
checkTestCase("1!", 1);
checkTestCase("2!", 2);
checkTestCase("2000!", Double.POSITIVE_INFINITY);
// it truncates
checkTestCase("2.9!", 2);
}
@Test
public void testErrors() {
// test lexer errors