Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ arithmeticExpression
;

multiplicativeExpression
: multiplicativeExpression (MULTIPLY | DIVIDE) arithmeticTerm
: multiplicativeExpression (MULTIPLY | DIVIDE | MOD) arithmeticTerm
| arithmeticTerm
;

Expand Down Expand Up @@ -334,6 +334,7 @@ ZERO : '0';
PLUS: '+';
MULTIPLY: '*';
DOT : '.';
MOD: '%';
EXPONENTLETTER
: 'E'
| 'e'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@ public Number evaluate(final Object ... args) {
final Class<?> leftValueClass = leftValue.getClass();
final Class<?> rightValueClass = rightValue.getClass();
if (!operandsToOperationMap.containsKey(leftValueClass)) {
throw new IllegalArgumentException(displayName + " requires left operand to be either Float or Integer.");
if (symbol == DataPrepperExpressionParser.MOD) {
throw new IllegalArgumentException(displayName + " requires left operand to be an Integer.");
} else {
throw new IllegalArgumentException(displayName + " requires left operand to be either Float or Integer.");
}
}
Map<Class<? extends Number>, BiFunction<Object, Object, Number>> rightOperandToOperation =
operandsToOperationMap.get(leftValueClass);
if (!rightOperandToOperation.containsKey(rightValueClass)) {
throw new IllegalArgumentException(displayName + " requires right operand to be either Float or Integer.");
if (symbol == DataPrepperExpressionParser.MOD) {
throw new IllegalArgumentException(displayName + " requires right operand to be an Integer.");
} else {
throw new IllegalArgumentException(displayName + " requires right operand to be either Float or Integer.");
}
}
final BiFunction<Object, Object, Number> operation = rightOperandToOperation.get(rightValueClass);
return operation.apply(leftValue, rightValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public ArithmeticBinaryOperator divideOperator() {
operandsToOperationMap = new HashMap<>();
final Map<Class<? extends Number>, BiFunction<Object, Object, Number>> intOperations =
Map.of(
Integer.class, (lhs, rhs) -> ((double)(int)lhs) /((double)(int)rhs),
Integer.class, (lhs, rhs) -> ((double)(int)lhs) / ((double)(int)rhs),
Float.class, (lhs, rhs) -> (float)(int)lhs / (Float) rhs,
Long.class, (lhs, rhs) -> (double)(int)lhs / (Long) rhs,
Double.class, (lhs, rhs) -> (double)(int)lhs / (Double) rhs
Expand Down Expand Up @@ -451,4 +451,18 @@ public ArithmeticBinaryOperator divideOperator() {

return new ArithmeticBinaryOperator(DataPrepperExpressionParser.DIVIDE, operandsToOperationMap);
}

@Bean
public ArithmeticBinaryOperator modOperator() {
final Map<Class<? extends Number>, Map<Class<? extends Number>, BiFunction<Object, Object, Number>>>
operandsToOperationMap = new HashMap<>();
final Map<Class<? extends Number>, BiFunction<Object, Object, Number>> intOperations =
Map.of(
Integer.class, (lhs, rhs) -> ((int)lhs) % ((int)rhs)
);

operandsToOperationMap.put(Integer.class, intOperations);

return new ArithmeticBinaryOperator(DataPrepperExpressionParser.MOD, operandsToOperationMap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ private Operator createDivideOperatorUnderTest() {
return new OperatorConfiguration().divideOperator();
}

private Operator createModOperatorUnderTest() {
return new OperatorConfiguration().modOperator();
}

@Mock
private ParserRuleContext ctx;

Expand All @@ -48,6 +52,8 @@ void testGetNumberOfOperands() {
assertThat(objectUnderTest.getNumberOfOperands(ctx), is(2));
objectUnderTest = createDivideOperatorUnderTest();
assertThat(objectUnderTest.getNumberOfOperands(ctx), is(2));
objectUnderTest = createModOperatorUnderTest();
assertThat(objectUnderTest.getNumberOfOperands(ctx), is(2));

when(ctx.getRuleIndex()).thenReturn(DataPrepperExpressionParser.RULE_arithmeticExpression);
objectUnderTest = createSubtractOperatorUnderTest();
Expand Down Expand Up @@ -82,6 +88,12 @@ void testShouldEvaluate() {
assertThat(objectUnderTest.shouldEvaluate(ctx), is(true));
when(ctx.getRuleIndex()).thenReturn(-1);
assertThat(objectUnderTest.shouldEvaluate(ctx), is(false));

objectUnderTest = createModOperatorUnderTest();
when(ctx.getRuleIndex()).thenReturn(DataPrepperExpressionParser.RULE_multiplicativeExpression);
assertThat(objectUnderTest.shouldEvaluate(ctx), is(true));
when(ctx.getRuleIndex()).thenReturn(-1);
assertThat(objectUnderTest.shouldEvaluate(ctx), is(false));
}

@Test
Expand All @@ -94,6 +106,8 @@ void testGetSymbol() {
assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.MULTIPLY));
objectUnderTest = createDivideOperatorUnderTest();
assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.DIVIDE));
objectUnderTest = createModOperatorUnderTest();
assertThat(objectUnderTest.getSymbol(), is(DataPrepperExpressionParser.MOD));
}

@Test
Expand All @@ -106,6 +120,10 @@ void testInvalid() {
assertThrows(RuntimeException.class, () -> objectUnderTest.evaluate(2, 1, 2));
objectUnderTest = createDivideOperatorUnderTest();
assertThrows(RuntimeException.class, () -> objectUnderTest.evaluate(2, 1, 2));
objectUnderTest = createModOperatorUnderTest();
assertThrows(RuntimeException.class, () -> objectUnderTest.evaluate(2, 1, 2));
assertThrows(RuntimeException.class, () -> objectUnderTest.evaluate(2.0, 1));
assertThrows(RuntimeException.class, () -> objectUnderTest.evaluate(2, 1.0));
}

@Test
Expand Down Expand Up @@ -204,4 +222,11 @@ void testEvalValidArgsForDivide() {
assertThat(objectUnderTest.evaluate(5.0, 2.0), is(2.5));
}

@Test
void testEvalValidArgsForMod() {
objectUnderTest = createModOperatorUnderTest();
assertThat(objectUnderTest.evaluate(5, 5), is(0));
assertThat(objectUnderTest.evaluate(5, 2), is(1));
assertThat(objectUnderTest.evaluate(6, 3), is(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ParseTreeEvaluatorListenerTest {
operatorConfiguration.subtractOperator(),
operatorConfiguration.multiplyOperator(),
operatorConfiguration.divideOperator(),
operatorConfiguration.modOperator(),
new NotOperator()
);
private final OperatorProvider operatorProvider = new OperatorProvider(operators);
Expand Down
Loading