Skip to content

Commit

Permalink
engine-fhir tests improvements (#1369)
Browse files Browse the repository at this point in the history
* log evaluator logic

* add test for log evaluator

* fix decimal value ranges

* truncated division for longs

* undo unnecessary check

* formatting

* indexof evaluation for nulls

* update engine tests

* update engine tests

* fix for CqlTestSuite library failing tests in new zealand timezone

* update tests
  • Loading branch information
antvaset committed Jun 21, 2024
1 parent a022fc5 commit 508bfad
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@ public static Object[][] dataMethod() {
"cql/CqlArithmeticFunctionsTest/Divide/Divide1Q1Q",
"cql/CqlArithmeticFunctionsTest/Ln/Ln1000D",
"cql/CqlArithmeticFunctionsTest/Ln/Ln1000",
"cql/CqlArithmeticFunctionsTest/Log/Log1Base1",
"cql/CqlArithmeticFunctionsTest/MaxValue/DecimalMaxValue",
"cql/CqlArithmeticFunctionsTest/MinValue/DecimalMinValue",
"cql/CqlArithmeticFunctionsTest/MinValue/LongMinValue",
"cql/CqlArithmeticFunctionsTest/Multiply/Multiply1CMBy2CM",
"cql/CqlArithmeticFunctionsTest/Truncated Divide/TruncatedDivide10LBy3L",
"cql/CqlComparisonOperatorsTest/Equal/QuantityEqCM1M01",
"cql/CqlComparisonOperatorsTest/Equivalent/EquivEqCM1M01",
"cql/CqlComparisonOperatorsTest/Greater/GreaterM1CM1",
Expand Down Expand Up @@ -113,8 +109,6 @@ public static Object[][] dataMethod() {
"cql/CqlListOperatorsTest/Equivalent/Equivalent123AndString123",
"cql/CqlListOperatorsTest/Equivalent/EquivalentABCAnd123",
"cql/CqlListOperatorsTest/Flatten/FlattenListNullAndNull",
"cql/CqlListOperatorsTest/IndexOf/IndexOfEmptyNull",
"cql/CqlListOperatorsTest/IndexOf/IndexOfNullIn1Null",
"cql/CqlListOperatorsTest/NotEqual/NotEqual123AndABC",
"cql/CqlListOperatorsTest/NotEqual/NotEqual123AndString123",
"cql/CqlListOperatorsTest/NotEqual/NotEqualABCAnd123",
Expand All @@ -140,10 +134,10 @@ public static Object[][] dataMethod() {
"cql/CqlTypesTest/Time/TimeUpperBoundMillis",
"cql/ValueLiteralsAndSelectors/Boolean/BooleanFalse",
"cql/ValueLiteralsAndSelectors/Boolean/BooleanTrue",
"cql/ValueLiteralsAndSelectors/Decimal/Decimal10Pow28",
"cql/ValueLiteralsAndSelectors/Decimal/DecimalNeg10Pow28",
"cql/ValueLiteralsAndSelectors/Decimal/Decimal10Pow20",
"cql/ValueLiteralsAndSelectors/Decimal/DecimalNeg10Pow20",
"cql/ValueLiteralsAndSelectors/Decimal/DecimalNegTenthStep",
"cql/ValueLiteralsAndSelectors/Decimal/DecimalPos10Pow28",
"cql/ValueLiteralsAndSelectors/Decimal/DecimalPos10Pow20",
"cql/ValueLiteralsAndSelectors/Decimal/DecimalPosTenthStep",
"cql/ValueLiteralsAndSelectors/Decimal/DecimalTenthStep",
"cql/ValueLiteralsAndSelectors/Null/Null",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@
<expression>Log(1, 1)</expression>
<output>null</output>
</test>
<test name="Log2Base1">
<expression>Log(2, 1)</expression>
<output>null</output>
</test>
<test name="Log1Base2">
<expression>Log(1, 2)</expression>
<output>0.0</output>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,30 +274,30 @@
<!-- EXPECT: Either some error saying the literal uses more
precision than the Decimal step size, or round to a whole step. -->
</test>
<test name="Decimal10Pow28ToZeroOneStepDecimalMaxValue">
<expression>9999999999999999999999999999.99999999</expression>
<output>10*1000000000000000000000000000.00000000-0.00000001</output>
<test name="Decimal10Pow20ToZeroOneStepDecimalMaxValue">
<expression>99999999999999999999.99999999</expression>
<output>10*10000000000000000000.00000000-0.00000001</output>
</test>
<test name="DecimalPos10Pow28ToZeroOneStepDecimalMaxValue">
<expression>+9999999999999999999999999999.99999999</expression>
<output>+10*1000000000000000000000000000.00000000-0.00000001</output>
<test name="DecimalPos10Pow20ToZeroOneStepDecimalMaxValue">
<expression>+99999999999999999999.99999999</expression>
<output>+10*10000000000000000000.00000000-0.00000001</output>
</test>
<test name="DecimalNeg10Pow28ToZeroOneStepDecimalMinValue">
<expression>-9999999999999999999999999999.99999999</expression>
<output>-10*1000000000000000000000000000.00000000+0.00000001</output>
<test name="DecimalNeg10Pow20ToZeroOneStepDecimalMinValue">
<expression>-99999999999999999999.99999999</expression>
<output>-10*10000000000000000000.00000000+0.00000001</output>
</test>
<test name="Decimal10Pow28">
<expression invalid="true">10000000000000000000000000000.00000000</expression>
<test name="Decimal10Pow20">
<expression invalid="true">100000000000000000000.00000000</expression>
<!-- TODO: Fix Engine to raise an error on this input. -->
<!-- EXPECT: Some error saying the literal is a greater magnitude than a Decimal supports. -->
</test>
<test name="DecimalPos10Pow28">
<expression invalid="true">+10000000000000000000000000000.00000000</expression>
<test name="DecimalPos10Pow20">
<expression invalid="true">+100000000000000000000.00000000</expression>
<!-- TODO: Fix Engine to raise an error on this input. -->
<!-- EXPECT: Some error saying the literal is a greater magnitude than a Decimal supports. -->
</test>
<test name="DecimalNeg10Pow28">
<expression invalid="true">-10000000000000000000000000000.00000000</expression>
<test name="DecimalNeg10Pow20">
<expression invalid="true">-100000000000000000000.00000000</expression>
<!-- TODO: Fix Engine to raise an error on this input. -->
<!-- EXPECT: Some error saying the literal is a greater magnitude than a Decimal supports. -->
</test>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
The operator uses the notion of equivalence to determine the index. The search is linear,
and returns the index of the first element that is equivalent to the element being searched for.
If the list is empty, or no element is found, the result is -1.
If the list argument is null, the result is null.
If either argument is null, the result is null.
*/

public class IndexOfEvaluator {

public static Object indexOf(Object source, Object elementToFind, State state) {
if (source == null) {
if (source == null || elementToFind == null) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ public static Object log(Object left, Object right) {
}

if (left instanceof BigDecimal) {
Double base = Math.log(((BigDecimal) right).doubleValue());
Double value = Math.log(((BigDecimal) left).doubleValue());
double base = ((BigDecimal) right).doubleValue();
double argument = ((BigDecimal) left).doubleValue();

if (base == 0) {
return Value.verifyPrecision(new BigDecimal(value), null);
// Logarithm is not defined for base 1.
if (base == 1) {
return null;
}

return Value.verifyPrecision(new BigDecimal(value / base), null);
return Value.verifyPrecision(BigDecimal.valueOf(Math.log(argument) / Math.log(base)), null);
}

throw new InvalidOperatorArgument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
The maximum operator is defined for the Integer, Long, Decimal, DateTime, and Time types.
For Integer, maximum returns the maximum signed 32-bit integer, 231 - 1.
For Long, maximum returns the maximum signed 64-bit Long, 263 - 1.
For Decimal, maximum returns the maximum representable decimal value, (1037 – 1) / 108 (9999999999999999999999999999.99999999).
For Decimal, maximum returns the maximum representable decimal value, (10^28 – 1) / 10^8 (99999999999999999999.99999999).
For DateTime, maximum returns the maximum representable date/time value, DateTime(9999, 12, 31, 23, 59, 59, 999).
For Time, maximum returns the maximum representable time value, Time(23, 59, 59, 999).
For any other type, attempting to invoke maximum results in an error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
The minimum operator is defined for the Integer, Long, Decimal, DateTime, and Time types.
For Integer, minimum returns the minimum signed 32-bit integer, -231.
For Long, minimum returns the minimum signed 32-bit integer, -263.
For Decimal, minimum returns the minimum representable decimal value, (-1037 – 1) / 108 (-9999999999999999999999999999.99999999).
For Decimal, minimum returns the minimum representable decimal value, (-10^28 + 1) / 10^8 (-99999999999999999999.99999999).
For DateTime, minimum returns the minimum representable date/time value, DateTime(1, 1, 1, 0, 0, 0, 0).
For Time, minimum returns the minimum representable time value, Time(0, 0, 0, 0).
For any other type, attempting to invoke minimum results in an error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/*
div(left Integer, right Integer) Integer
div(left Decimal, right Decimal) Decimal
div(left Long, right Long) Long
div(left Quantity, right Quantity) Quantity
The div operator performs truncated division of its arguments.
Expand All @@ -29,6 +30,12 @@ public static Object div(Object left, Object right, State state) {
}

return (Integer) left / (Integer) right;
} else if (left instanceof Long) {
if ((Long) right == 0) {
return null;
}

return (Long) left / (Long) right;
} else if (left instanceof BigDecimal) {
if (EqualEvaluator.equal(right, new BigDecimal("0.0"), state)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ public class Value {

public static final Integer MAX_INT = Integer.MAX_VALUE;
public static final Long MAX_LONG = Long.MAX_VALUE;
public static final BigDecimal MAX_DECIMAL = new BigDecimal("9999999999999999999999999999.99999999");

/**
* Set to (10<sup>28</sup> - 1) / 10<sup>8</sup>.
*/
public static final BigDecimal MAX_DECIMAL = new BigDecimal("99999999999999999999.99999999");
public static final Integer MIN_INT = Integer.MIN_VALUE;
public static final Long MIN_LONG = Long.MIN_VALUE;
public static final BigDecimal MIN_DECIMAL = new BigDecimal("-9999999999999999999999999999.99999999");

/**
* Set to (-10<sup>28</sup> + 1) / 10<sup>8</sup>.
*/
public static final BigDecimal MIN_DECIMAL = new BigDecimal("-99999999999999999999.99999999");

private Value() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ void log() {
assertThat(value, is(nullValue()));

value = engine.expression(library, "Log1Base1").value();
assertThat((BigDecimal) value, comparesEqualTo(BigDecimal.valueOf(0d)));
assertThat(value, is(nullValue()));

value = engine.expression(library, "Log1Base2").value();
assertThat((BigDecimal) value, comparesEqualTo(BigDecimal.valueOf(0d)));
Expand Down Expand Up @@ -367,7 +367,7 @@ void maximum() {
assertThat(value, is(Long.MAX_VALUE));

value = engine.expression(library, "DecimalMaxValue").value();
assertEquals(0, ((BigDecimal) value).compareTo(new BigDecimal("9999999999999999999999999999.99999999")));
assertEquals(0, ((BigDecimal) value).compareTo(new BigDecimal("99999999999999999999.99999999")));

value = engine.expression(library, "DateTimeMaxValue").value();
assertTrue(EquivalentEvaluator.equivalent(value, new DateTime(BigDecimal.ZERO, 9999, 12, 31, 23, 59, 59, 999)));
Expand All @@ -389,7 +389,7 @@ void minimum() {
assertThat(value, is(Long.MIN_VALUE));

value = engine.expression(library, "DecimalMinValue").value();
assertEquals(0, ((BigDecimal) value).compareTo(new BigDecimal("-9999999999999999999999999999.99999999")));
assertEquals(0, ((BigDecimal) value).compareTo(new BigDecimal("-99999999999999999999.99999999")));

value = engine.expression(library, "DateTimeMinValue").value();
assertTrue(EquivalentEvaluator.equivalent(value, new DateTime(BigDecimal.ZERO, 1, 1, 1, 0, 0, 0, 0)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,28 +152,28 @@ void all_value_literals_and_selectors() {
new BigDecimal(0).subtract(new BigDecimal(0.00000001).setScale(8, RoundingMode.HALF_EVEN))));
assertThat(((Quantity) value).getUnit(), is("mg"));

// define QuantityMax: 9999999999999999999999999999.99999999 'mg'
// define QuantityMax: 99999999999999999999.99999999 'mg'
value = results.forExpression("QuantityMax").value();
assertThat(value, instanceOf(Quantity.class));
assertThat(
((Quantity) value).getValue(),
comparesEqualTo(new BigDecimal("9999999999999999999999999999.99999999")));
comparesEqualTo(new BigDecimal("99999999999999999999.99999999")));
assertThat(((Quantity) value).getUnit(), is("mg"));

// define QuantityPosMax: +9999999999999999999999999999.99999999 'mg'
// define QuantityPosMax: +99999999999999999999.99999999 'mg'
value = results.forExpression("QuantityPosMax").value();
assertThat(value, instanceOf(Quantity.class));
assertThat(
((Quantity) value).getValue(),
comparesEqualTo(new BigDecimal("9999999999999999999999999999.99999999")));
comparesEqualTo(new BigDecimal("99999999999999999999.99999999")));
assertThat(((Quantity) value).getUnit(), is("mg"));

// define QuantityMin: -9999999999999999999999999999.99999999 'mg'
// define QuantityMin: -99999999999999999999.99999999 'mg'
value = results.forExpression("QuantityMin").value();
assertThat(value, instanceOf(Quantity.class));
assertThat(
((Quantity) value).getValue(),
comparesEqualTo(new BigDecimal("-9999999999999999999999999999.99999999")));
comparesEqualTo(new BigDecimal("-99999999999999999999.99999999")));
assertThat(((Quantity) value).getUnit(), is("mg"));

value = results.forExpression("DecimalZero").value();
Expand Down Expand Up @@ -330,14 +330,14 @@ void all_value_literals_and_selectors() {
// assertThat(((BigDecimal)result).setScale(7, RoundingMode.HALF_EVEN), comparesEqualTo(new
// BigDecimal(-1*Math.pow(10.0,-7)).setScale(7,RoundingMode.HALF_EVEN)));

// define DecimalMaxValue : 9999999999999999999999999999.99999999
// define DecimalMaxValue : 99999999999999999999.99999999
value = results.forExpression("DecimalMaxValue").value();
assertThat(((BigDecimal) value), comparesEqualTo(new BigDecimal("9999999999999999999999999999.99999999")));
// define DecimalPosMaxValue : +9999999999999999999999999999.99999999
assertThat(((BigDecimal) value), comparesEqualTo(new BigDecimal("99999999999999999999.99999999")));
// define DecimalPosMaxValue : +99999999999999999999.99999999
value = results.forExpression("DecimalPosMaxValue").value();
assertThat(((BigDecimal) value), comparesEqualTo(new BigDecimal("9999999999999999999999999999.99999999")));
// define DecimalMinValue: -9999999999999999999999999999.99999999
assertThat(((BigDecimal) value), comparesEqualTo(new BigDecimal("99999999999999999999.99999999")));
// define DecimalMinValue: -99999999999999999999.99999999
value = results.forExpression("DecimalMinValue").value();
assertThat(((BigDecimal) value), comparesEqualTo(new BigDecimal("-9999999999999999999999999999.99999999")));
assertThat(((BigDecimal) value), comparesEqualTo(new BigDecimal("-99999999999999999999.99999999")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,13 @@ void all_interval_operators() {
assertTrue(equivalent(value, new Time(15, 59, 59, 999)));

value = results.forExpression("IndexOfEmptyNull").value();
assertThat(value, is(-1));
assertThat(value, is(nullValue()));

value = results.forExpression("IndexOfNullEmpty").value();
assertThat(value, is(nullValue()));

value = results.forExpression("IndexOfNullIn1Null").value();
assertThat(value, is(1));
assertThat(value, is(nullValue()));

value = results.forExpression("IndexOf1In12").value();
assertThat(value, is(0));
Expand Down
Loading

0 comments on commit 508bfad

Please sign in to comment.