aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Ahlborn <jtahlborn@yahoo.com>2018-07-17 21:49:09 +0000
committerJames Ahlborn <jtahlborn@yahoo.com>2018-07-17 21:49:09 +0000
commitabe506f27c2d6c789eb53a43bcdbe0a258c596dd (patch)
tree6e125e88d8bd68d058ff80241690ec5b9bcff3b9 /src
parent18a4998e71e239bba1712109f456dbea80067867 (diff)
downloadjackcess-abe506f27c2d6c789eb53a43bcdbe0a258c596dd.tar.gz
jackcess-abe506f27c2d6c789eb53a43bcdbe0a258c596dd.zip
test number functions
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@1183 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java2
-rw-r--r--src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultNumberFunctions.java11
-rw-r--r--src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java5
-rw-r--r--src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java41
4 files changed, 52 insertions, 7 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java
index 407ec52..eef7c77 100644
--- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java
+++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/BuiltinOperators.java
@@ -56,6 +56,8 @@ public class BuiltinOperators
public static final Value FALSE_VAL = new LongValue(0);
public static final Value EMPTY_STR_VAL = new StringValue("");
public static final Value ZERO_VAL = FALSE_VAL;
+ public static final Value NEG_ONE_VAL = TRUE_VAL;
+ public static final Value ONE_VAL = new LongValue(1);
private enum CoercionType {
diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultNumberFunctions.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultNumberFunctions.java
index 4389d9f..d93463f 100644
--- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultNumberFunctions.java
+++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultNumberFunctions.java
@@ -147,14 +147,15 @@ public class DefaultNumberFunctions
public static final Function SGN = registerFunc(new Func1NullIsNull("Sgn") {
@Override
protected Value eval1(EvalContext ctx, Value param1) {
- int signum = 0;
+ int val = 0;
if(param1.getType().isIntegral()) {
- int lv = param1.getAsLongInt();
- signum = ((lv > 0) ? 1 : ((lv < 0) ? -1 : 0));
+ val = param1.getAsLongInt();
} else {
- signum = param1.getAsBigDecimal().signum();
+ val = param1.getAsBigDecimal().signum();
}
- return BuiltinOperators.toValue(signum);
+ return ((val > 0) ? BuiltinOperators.ONE_VAL :
+ ((val < 0) ? BuiltinOperators.NEG_ONE_VAL :
+ BuiltinOperators.ZERO_VAL));
}
});
diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java
index 7831d6e..de1159a 100644
--- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java
+++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultTextFunctions.java
@@ -309,8 +309,9 @@ public class DefaultTextFunctions
int cmp = (ignoreCase ?
s1.compareToIgnoreCase(s2) : s1.compareTo(s2));
// stupid java doesn't return 1, -1, 0...
- cmp = ((cmp < 0) ? -1 : ((cmp > 0) ? 1 : 0));
- return BuiltinOperators.toValue(cmp);
+ return ((cmp < 0) ? BuiltinOperators.NEG_ONE_VAL :
+ ((cmp > 0) ? BuiltinOperators.ONE_VAL :
+ BuiltinOperators.ZERO_VAL));
}
});
diff --git a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java
index 39e5da3..58d99e0 100644
--- a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java
+++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java
@@ -21,6 +21,7 @@ import java.math.BigDecimal;
import com.healthmarketscience.jackcess.expr.EvalException;
import junit.framework.TestCase;
import static com.healthmarketscience.jackcess.impl.expr.ExpressionatorTest.eval;
+import static com.healthmarketscience.jackcess.impl.expr.ExpressionatorTest.toBD;
/**
*
@@ -171,6 +172,46 @@ public class DefaultFunctionsTest extends TestCase
}
}
+ public void testNumberFuncs() throws Exception
+ {
+ assertEquals(1, eval("=Abs(1)"));
+ assertEquals(1, eval("=Abs(-1)"));
+ assertEquals(toBD(1.1), eval("=Abs(-1.1)"));
+
+ assertEquals(Math.atan(0.2), eval("=Atan(0.2)"));
+ assertEquals(Math.sin(0.2), eval("=Sin(0.2)"));
+ assertEquals(Math.tan(0.2), eval("=Tan(0.2)"));
+ assertEquals(Math.cos(0.2), eval("=Cos(0.2)"));
+
+ assertEquals(Math.exp(0.2), eval("=Exp(0.2)"));
+ assertEquals(Math.log(0.2), eval("=Log(0.2)"));
+ assertEquals(Math.sqrt(4.3), eval("=Sqr(4.3)"));
+
+ assertEquals(3, eval("=Fix(3.5)"));
+ assertEquals(4, eval("=Fix(4)"));
+ assertEquals(-3, eval("=Fix(-3.5)"));
+ assertEquals(-4, eval("=Fix(-4)"));
+
+ assertEquals(1, eval("=Sgn(3.5)"));
+ assertEquals(1, eval("=Sgn(4)"));
+ assertEquals(-1, eval("=Sgn(-3.5)"));
+ assertEquals(-1, eval("=Sgn(-4)"));
+
+ assertEquals(3, eval("=Int(3.5)"));
+ assertEquals(4, eval("=Int(4)"));
+ assertEquals(-4, eval("=Int(-3.5)"));
+ assertEquals(-4, eval("=Int(-4)"));
+
+ assertEquals(toBD(4), eval("=Round(3.7)"));
+ assertEquals(4, eval("=Round(4)"));
+ assertEquals(toBD(-4), eval("=Round(-3.7)"));
+ assertEquals(-4, eval("=Round(-4)"));
+
+ assertEquals(toBD(3.73), eval("=Round(3.7345, 2)"));
+ assertEquals(4, eval("=Round(4, 2)"));
+ assertEquals(toBD(-3.73), eval("=Round(-3.7345, 2)"));
+ assertEquals(-4, eval("=Round(-4, 2)"));
+ }
public void testFinancialFuncs() throws Exception
{