Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds EvaluationException to acos and acosr functions for invalid para… #489

Merged
merged 1 commit into from
Jul 7, 2024
Merged
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 @@ -15,6 +15,8 @@
*/
package com.ezylang.evalex.functions;

import static java.math.BigDecimal.valueOf;

import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.parser.Token;
Expand All @@ -28,6 +30,7 @@
*/
public abstract class AbstractFunction implements FunctionIfc {

protected static final BigDecimal MINUS_ONE = valueOf(-1);
private final List<FunctionParameterDefinition> functionParameterDefinitions = new ArrayList<>();

private final boolean hasVarArgs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,34 @@
*/
package com.ezylang.evalex.functions.trigonometric;

import static java.math.BigDecimal.ONE;

import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;
import java.math.BigDecimal;

/** Returns the arc-cosine (in degrees). */
@FunctionParameter(name = "value")
public class AcosFunction extends AbstractFunction {
@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues) {
Expression expression, Token functionToken, EvaluationValue... parameterValues)
throws EvaluationException {

BigDecimal parameterValue = parameterValues[0].getNumberValue();

return expression.convertDoubleValue(
Math.toDegrees(Math.acos(parameterValues[0].getNumberValue().doubleValue())));
if (parameterValue.compareTo(ONE) > 0) {
throw new EvaluationException(
functionToken, "Illegal acos(x) for x > 1: x = " + parameterValue);
}
if (parameterValue.compareTo(MINUS_ONE) < 0) {
throw new EvaluationException(
functionToken, "Illegal acos(x) for x < -1: x = " + parameterValue);
}
return expression.convertDoubleValue(Math.toDegrees(Math.acos(parameterValue.doubleValue())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,35 @@
*/
package com.ezylang.evalex.functions.trigonometric;

import static java.math.BigDecimal.ONE;

import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;
import java.math.BigDecimal;

/** Returns the arc-cosine (in radians). */
@FunctionParameter(name = "cosine")
public class AcosRFunction extends AbstractFunction {
@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues) {
Expression expression, Token functionToken, EvaluationValue... parameterValues)
throws EvaluationException {

BigDecimal parameterValue = parameterValues[0].getNumberValue();

if (parameterValue.compareTo(ONE) > 0) {
throw new EvaluationException(
functionToken, "Illegal acosr(x) for x > 1: x = " + parameterValue);
}
if (parameterValue.compareTo(MINUS_ONE) < 0) {
throw new EvaluationException(
functionToken, "Illegal acosr(x) for x < -1: x = " + parameterValue);
}

return expression.convertDoubleValue(
Math.acos(parameterValues[0].getNumberValue().doubleValue()));
return expression.convertDoubleValue(Math.acos(parameterValue.doubleValue()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.ezylang.evalex.functions.trigonometric;

import static java.math.BigDecimal.ONE;
import static java.math.BigDecimal.valueOf;

import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.Expression;
Expand All @@ -30,8 +29,6 @@
@FunctionParameter(name = "value")
public class AsinFunction extends AbstractFunction {

private static final BigDecimal MINUS_ONE = valueOf(-1);

@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ void testAcos(String expression, String expectedResult)
assertExpressionHasExpectedResult(expression, expectedResult);
}

@Test
void testAcosThrowsExceptionPositive() {
assertThatThrownBy(() -> new Expression("ACOS(1.5)").evaluate())
.isInstanceOf(EvaluationException.class)
.hasMessage("Illegal acos(x) for x > 1: x = 1.5");
}

@Test
void testAcosThrowsExceptionNegative() {
assertThatThrownBy(() -> new Expression("ACOS(-1.5)").evaluate())
.isInstanceOf(EvaluationException.class)
.hasMessage("Illegal acos(x) for x < -1: x = -1.5");
}

@ParameterizedTest
@CsvSource(
delimiter = ':',
Expand Down Expand Up @@ -75,6 +89,20 @@ void testAcosR(String expression, String expectedResult)
assertExpressionHasExpectedResult(expression, expectedResult);
}

@Test
void testAcosRThrowsExceptionPositive() {
assertThatThrownBy(() -> new Expression("ACOSR(1.5)").evaluate())
.isInstanceOf(EvaluationException.class)
.hasMessage("Illegal acosr(x) for x > 1: x = 1.5");
}

@Test
void testAcosRThrowsExceptionNegative() {
assertThatThrownBy(() -> new Expression("ACOSR(-1.5)").evaluate())
.isInstanceOf(EvaluationException.class)
.hasMessage("Illegal acosr(x) for x < -1: x = -1.5");
}

@ParameterizedTest
@CsvSource(
delimiter = ':',
Expand Down