|
| 1 | +/* |
| 2 | + Copyright 2012-2024 Udo Klimaschewski |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | +package com.ezylang.evalex.functions.basic; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import com.ezylang.evalex.EvaluationException; |
| 21 | +import com.ezylang.evalex.Expression; |
| 22 | +import com.ezylang.evalex.parser.ParseException; |
| 23 | +import java.util.List; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +/** |
| 27 | + * Unit tests for the AVERAGE function with arrays. |
| 28 | + * |
| 29 | + * @author oswaldo.bapvic.jr |
| 30 | + */ |
| 31 | +class AverageArrayTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + void testAverageSingleArray() throws EvaluationException, ParseException { |
| 35 | + Integer[] numbers = {1, 2, 3}; |
| 36 | + |
| 37 | + Expression expression = new Expression("AVERAGE(numbers)").with("numbers", numbers); |
| 38 | + |
| 39 | + assertThat(expression.evaluate().getNumberValue().doubleValue()).isEqualTo(2); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void testAverageMultipleArray() throws EvaluationException, ParseException { |
| 44 | + List<Number> numbers1 = List.of(1, 2, 3); |
| 45 | + List<Number> numbers2 = List.of(4, 5, 6); |
| 46 | + |
| 47 | + Expression expression = |
| 48 | + new Expression("AVERAGE(numbers1, numbers2)") |
| 49 | + .with("numbers1", numbers1) |
| 50 | + .with("numbers2", numbers2); |
| 51 | + |
| 52 | + assertThat(expression.evaluate().getNumberValue().doubleValue()).isEqualTo(3.5); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testAverageMixedArrayNumber() throws EvaluationException, ParseException { |
| 57 | + Double[] numbers = {1.0, 2.0, 3.0}; |
| 58 | + |
| 59 | + Expression expression = new Expression("AVERAGE(numbers, 4)").with("numbers", numbers); |
| 60 | + |
| 61 | + assertThat(expression.evaluate().getNumberValue().doubleValue()).isEqualTo(2.5); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testAverageNestedArray() throws EvaluationException, ParseException { |
| 66 | + Integer[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
| 67 | + |
| 68 | + Expression expression = new Expression("AVERAGE(numbers)").with("numbers", numbers); |
| 69 | + assertThat(expression.evaluate().getNumberValue().doubleValue()).isEqualTo(5); |
| 70 | + } |
| 71 | +} |
0 commit comments