1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import org.junit.runners.JUnit4;
import java.util.Arrays;
public class SolutionTest {
@Test
public void basicTests() {
assertEquals(Arrays.toString(new double[] { 2, 2, 2, 2 }), Arrays.toString(Solution.averages(new int[] { 2, 2, 2, 2, 2 })));
assertEquals(Arrays.toString(new double[] { 0, 0, 0, 0 }), Arrays.toString(Solution.averages(new int[] { 2, -2, 2, -2, 2 })));
assertEquals(Arrays.toString(new double[] { 2, 4, 3, -4.5 }), Arrays.toString(Solution.averages(new int[] { 1, 3, 5, 1, -10 })));
}
@Test
public void nullEmptyTests() {
assertEquals("input 'null' should return an empty array", 0, Solution.averages(null).length);
assertEquals("Empty array as input should return an empty array", 0, Solution.averages(new int[0]).length);
assertEquals("Array with only one value as input should return an empty array", 0, Solution.averages(new int[] { 2 }).length);
}
@Test
public void randomTests() {
for(int r = 0; r < 100; r++)
{
int len = (int)Math.floor(Math.random() * 20);
int[] numbers = new int[len];
for(int k = 0; k < len; k++)
{
numbers[k] = (int)Math.floor(Math.random() * 41) - 20;
}
double[] expected = new double[0];
if(numbers.length > 1)
{
expected = new double[numbers.length - 1];
for(int i=0;i<numbers.length-1;i++)
{
expected[i] = ((double)numbers[i] + numbers[i+1]) / 2;
}
}
assertArrayEquals("Should work for random input", expected, Solution.averages(numbers), 0.001);
}
}
}
|