Something went wrong. Try again.
a fork of EvalEx by ezylang with a handful of breaking changes
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177/* Copyright 2012-2022 Udo Klimaschewski
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*/package com.ezylang.evalex;
import static org.assertj.core.api.Assertions.assertThat;import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.ezylang.evalex.parser.ParseException;import java.math.BigDecimal;import java.util.Arrays;import java.util.List;import org.junit.jupiter.api.Test;
class ExpressionEvaluatorArrayTest extends BaseExpressionEvaluatorTest {
@Test void testSimpleArray() throws ParseException, EvaluationException { List<BigDecimal> array = List.of(new BigDecimal(99)); Expression expression = createExpression("a[0]").with("a", array);
assertThat(expression.evaluate().getStringValue()).isEqualTo("99"); }
@Test void testMultipleEntriesArray() throws ParseException, EvaluationException { List<BigDecimal> array = Arrays.asList(new BigDecimal(2), new BigDecimal(4), new BigDecimal(6)); Expression expression = createExpression("a[0]+a[1]+a[2]").with("a", array);
assertThat(expression.evaluate().getStringValue()).isEqualTo("12"); }
@Test void testExpressionArray() throws ParseException, EvaluationException { List<BigDecimal> array = List.of(new BigDecimal(3)); Expression expression = createExpression("a[4-x]").with("a", array).and("x", new BigDecimal(4));
assertThat(expression.evaluate().getStringValue()).isEqualTo("3"); }
@Test void testNestedArray() throws ParseException, EvaluationException { List<BigDecimal> arrayA = List.of(new BigDecimal(3)); List<BigDecimal> arrayB = Arrays.asList(new BigDecimal(2), new BigDecimal(4), new BigDecimal(6)); Expression expression = createExpression("a[b[6-4]-x]") .with("a", arrayA) .and("b", arrayB) .and("x", new BigDecimal(6));
assertThat(expression.evaluate().getStringValue()).isEqualTo("3"); }
@Test void testStringArray() throws ParseException, EvaluationException { List<String> array = Arrays.asList("Hello", "beautiful", "world"); Expression expression = createExpression("a[0] + \" \" + a[1] + \" \" + a[2]").with("a", array);
assertThat(expression.evaluate().getStringValue()).isEqualTo("Hello beautiful world"); }
@Test void testBooleanArray() throws ParseException, EvaluationException { List<Boolean> array = Arrays.asList(true, true, false); Expression expression = createExpression("a[0] + \" \" + a[1] + \" \" + a[2]").with("a", array);
assertThat(expression.evaluate().getStringValue()).isEqualTo("true true false"); }
@Test void testArrayOfArray() throws EvaluationException, ParseException { List<BigDecimal> subArray1 = Arrays.asList(new BigDecimal(1), new BigDecimal(2)); List<BigDecimal> subArray2 = Arrays.asList(new BigDecimal(4), new BigDecimal(8));
List<List<BigDecimal>> array = Arrays.asList(subArray1, subArray2);
Expression expression1 = createExpression("a[0][0]").with("a", array); Expression expression2 = createExpression("a[0][1]").with("a", array); Expression expression3 = createExpression("a[1][0]").with("a", array); Expression expression4 = createExpression("a[1][1]").with("a", array);
assertThat(expression1.evaluate().getStringValue()).isEqualTo("1"); assertThat(expression2.evaluate().getStringValue()).isEqualTo("2"); assertThat(expression3.evaluate().getStringValue()).isEqualTo("4"); assertThat(expression4.evaluate().getStringValue()).isEqualTo("8"); }
@Test void testMixedArray() throws ParseException, EvaluationException { List<?> array = Arrays.asList("Hello", new BigDecimal(4), true); Expression expression1 = createExpression("a[0]").with("a", array); Expression expression2 = createExpression("a[1]").with("a", array); Expression expression3 = createExpression("a[2]").with("a", array);
assertThat(expression1.evaluate().getStringValue()).isEqualTo("Hello"); assertThat(expression2.evaluate().getStringValue()).isEqualTo("4"); assertThat(expression3.evaluate().getStringValue()).isEqualTo("true"); }
@Test void testArrayAndList() throws EvaluationException, ParseException { Expression expression = createExpression("values[i-1] * factors[i-1]") .with("values", List.of(2, 3, 4)) .and("factors", new Object[] {2, 4, 6}) .and("i", 1);
assertThat(expression.evaluate().getStringValue()).isEqualTo("4"); }
@Test void testArrayTypes() throws EvaluationException, ParseException { Expression expression = createExpression("decimals[1] + integers[1] + doubles[1] + strings[1] + booleans[1]") .with("decimals", new BigDecimal[] {new BigDecimal(1), new BigDecimal(2)}) .and("integers", new Integer[] {1, 2}) .and("doubles", new Double[] {1.1, 2.2}) .and("strings", new String[] {" Hello ", " World "}) .and("booleans", new Boolean[] {true, false});
assertThat(expression.evaluate().getStringValue()).isEqualTo("6.2 World false"); }
@Test void testThrowsUnsupportedDataTypeForArray() { assertThatThrownBy(() -> createExpression("a[0]").with("a", "aString").evaluate()) .isInstanceOf(EvaluationException.class) .hasMessage("Unsupported data types in operation"); }
@Test void testThrowsUnsupportedDataTypeForIndex() { assertThatThrownBy( () -> { List<?> array = List.of("Hello"); createExpression("a[b]").with("a", array).and("b", "anotherString").evaluate(); }) .isInstanceOf(EvaluationException.class) .hasMessage("Unsupported data types in operation"); }
@Test void testArrayIndexOutOfBounds() { assertThatThrownBy( () -> { List<?> array = List.of("Hello"); createExpression("a[1]").with("a", array).evaluate(); }) .isInstanceOf(EvaluationException.class) .hasMessage("Index 1 out of bounds for array of length 1"); }
@Test void testArrayNegativeIndex() { assertThatThrownBy( () -> { List<?> array = List.of("Hello"); createExpression("a[-1]").with("a", array).evaluate(); }) .isInstanceOf(EvaluationException.class) .hasMessage("Index -1 out of bounds for array of length 1"); }}