diff options
author | James Ahlborn <jtahlborn@yahoo.com> | 2017-09-10 02:55:50 +0000 |
---|---|---|
committer | James Ahlborn <jtahlborn@yahoo.com> | 2017-09-10 02:55:50 +0000 |
commit | 7f3beb13c50bce5848da35e286b0149253bea190 (patch) | |
tree | 6327a405644126a0c2b62cb2cd67b61d51abaa3d /src | |
parent | 392dfba3ef4ec2151ba9ac6772a3361d7b4e65f3 (diff) | |
download | jackcess-7f3beb13c50bce5848da35e286b0149253bea190.tar.gz jackcess-7f3beb13c50bce5848da35e286b0149253bea190.zip |
fix some cast functions; more unit tests
git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/branches/exprs@1115 f203690c-595d-4dc9-a70b-905162fa7fd2
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java | 26 | ||||
-rw-r--r-- | src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java | 17 |
2 files changed, 37 insertions, 6 deletions
diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java index 706f42c..0b5b4ff 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctions.java @@ -295,7 +295,7 @@ public class DefaultFunctions public static final Function CBYTE = registerFunc(new Func1("CByte") { @Override protected Value eval1(EvalContext ctx, Value param1) { - Long lv = param1.getAsLong(); + long lv = roundToLong(param1); if((lv < 0) || (lv > 255)) { throw new IllegalStateException("Byte code '" + lv + "' out of range "); } @@ -307,7 +307,7 @@ public class DefaultFunctions @Override protected Value eval1(EvalContext ctx, Value param1) { BigDecimal bd = param1.getAsBigDecimal(); - bd.setScale(4, RoundingMode.HALF_EVEN); + bd = bd.setScale(4, RoundingMode.HALF_EVEN); return BuiltinOperators.toValue(bd); } }); @@ -341,8 +341,7 @@ public class DefaultFunctions public static final Function CINT = registerFunc(new Func1("CInt") { @Override protected Value eval1(EvalContext ctx, Value param1) { - // FIXME, fix rounding for this and clng - Long lv = param1.getAsLong(); + long lv = roundToLong(param1); if((lv < Short.MIN_VALUE) || (lv > Short.MAX_VALUE)) { throw new IllegalStateException("Int value '" + lv + "' out of range "); } @@ -353,7 +352,7 @@ public class DefaultFunctions public static final Function CLNG = registerFunc(new Func1("CLng") { @Override protected Value eval1(EvalContext ctx, Value param1) { - Long lv = param1.getAsLong(); + long lv = roundToLong(param1); if((lv < Integer.MIN_VALUE) || (lv > Integer.MAX_VALUE)) { throw new IllegalStateException("Long value '" + lv + "' out of range "); } @@ -368,7 +367,14 @@ public class DefaultFunctions if((db < Float.MIN_VALUE) || (db > Float.MAX_VALUE)) { throw new IllegalStateException("Single value '" + db + "' out of range "); } - return BuiltinOperators.toValue(db); + return BuiltinOperators.toValue((double)db.floatValue()); + } + }); + + public static final Function CSTR = registerFunc(new Func1("CStr") { + @Override + protected Value eval1(EvalContext ctx, Value param1) { + return BuiltinOperators.toValue(param1.getAsString()); } }); @@ -649,6 +655,14 @@ public class DefaultFunctions } } + private static long roundToLong(Value param) { + if(param.getType().isIntegral()) { + return param.getAsLong(); + } + return param.getAsBigDecimal().setScale(0, RoundingMode.HALF_EVEN) + .longValue(); + } + // https://www.techonthenet.com/access/functions/ // https://support.office.com/en-us/article/Access-Functions-by-category-b8b136c3-2716-4d39-94a2-658ce330ed83 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 0e2e85e..493795f 100644 --- a/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/impl/expr/DefaultFunctionsTest.java @@ -16,6 +16,7 @@ limitations under the License. package com.healthmarketscience.jackcess.impl.expr; +import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date; @@ -49,6 +50,22 @@ public class DefaultFunctionsTest extends TestCase assertEquals("f", eval("=Chr(102)")); assertEquals("\u263A", eval("=ChrW(9786)")); assertEquals("263A", eval("=Hex(9786)")); + + assertEquals("blah", eval("=Nz(\"blah\")")); + assertEquals("", eval("=Nz(Null)")); + assertEquals("blah", eval("=Nz(\"blah\",\"FOO\")")); + assertEquals("FOO", eval("=Nz(Null,\"FOO\")")); + + assertEquals("23072", eval("=Oct(9786)")); + assertEquals(" 9786", eval("=Str(9786)")); + assertEquals("-42", eval("=Str(-42)")); + + assertEquals(-1L, eval("=CBool(\"1\")")); + assertEquals(13L, eval("=CByte(\"13\")")); + assertEquals(14L, eval("=CByte(\"13.7\")")); + assertEquals(new BigDecimal("57.1235"), eval("=CCur(\"57.12346\")")); + assertEquals(new Double("57.12345"), eval("=CDbl(\"57.12345\")")); + } } |