mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-10 04:26:27 +08:00
Add tests for Average (#3652)
This commit is contained in:

committed by
GitHub

parent
d88b70113f
commit
6235fd6505
@ -5,25 +5,6 @@ package com.thealgorithms.maths;
|
|||||||
*/
|
*/
|
||||||
public class Average {
|
public class Average {
|
||||||
|
|
||||||
private static final double SMALL_VALUE = 0.00001f;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
assert Math.abs(
|
|
||||||
average(new double[] { 3, 6, 9, 12, 15, 18, 21 }) - 12
|
|
||||||
) <
|
|
||||||
SMALL_VALUE;
|
|
||||||
assert Math.abs(
|
|
||||||
average(new double[] { 5, 10, 15, 20, 25, 30, 35 }) - 20
|
|
||||||
) <
|
|
||||||
SMALL_VALUE;
|
|
||||||
assert Math.abs(
|
|
||||||
average(new double[] { 1, 2, 3, 4, 5, 6, 7, 8 }) - 4.5
|
|
||||||
) <
|
|
||||||
SMALL_VALUE;
|
|
||||||
int[] array = { 2, 4, 10 };
|
|
||||||
assert average(array) == 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate average of a list of numbers
|
* Calculate average of a list of numbers
|
||||||
*
|
*
|
||||||
@ -41,15 +22,15 @@ public class Average {
|
|||||||
/**
|
/**
|
||||||
* find average value of int array
|
* find average value of int array
|
||||||
*
|
*
|
||||||
* @param array the array contains element and the sum does not excess long
|
* @param numbers the array contains element and the sum does not excess long
|
||||||
* value limit
|
* value limit
|
||||||
* @return average value
|
* @return average value
|
||||||
*/
|
*/
|
||||||
public static int average(int[] array) {
|
public static int average(int[] numbers) {
|
||||||
long sum = 0;
|
long sum = 0;
|
||||||
for (int i = 0; i < array.length; ++i) {
|
for (int number : numbers) {
|
||||||
sum += array[i];
|
sum += number;
|
||||||
}
|
}
|
||||||
return (int) (sum / array.length);
|
return (int) (sum / numbers.length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,30 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
public class AverageTest {
|
public class AverageTest {
|
||||||
|
|
||||||
double[] numbers = { 3, 6, 9, 12, 15, 18, 21 };
|
private static final double SMALL_VALUE = 0.00001d;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAverage() {
|
public void testAverage_double_12() {
|
||||||
Assertions.assertEquals(12, Average.average(numbers));
|
double[] numbers = {3d, 6d, 9d, 12d, 15d, 18d, 21d};
|
||||||
|
Assertions.assertEquals(12d, Average.average(numbers), SMALL_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAverage_double_20() {
|
||||||
|
double[] numbers = {5d, 10d, 15d, 20d, 25d, 30d, 35d};
|
||||||
|
Assertions.assertEquals(20d, Average.average(numbers), SMALL_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAverage_double_4_5() {
|
||||||
|
double[] numbers = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d};
|
||||||
|
Assertions.assertEquals(4.5d, Average.average(numbers), SMALL_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAverage_int_5() {
|
||||||
|
int[] numbers = {2, 4, 10};
|
||||||
|
Assertions.assertEquals(5, Average.average(numbers));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user