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
|
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
class SolutionTest {
@Test
void isArray() {
assertEquals(int[].class, Solution.multiplyAll(new int[]{1}).apply(1).getClass());
}
@Test
void hasCorrectLength() {
assertEquals(2, Solution.multiplyAll(new int[]{1, 2}).apply(1).length);
}
@Test
void basicTests() {
assertArrayEquals(new int[]{1, 2, 3}, Solution.multiplyAll(new int[]{1, 2, 3}).apply(1));
assertArrayEquals(new int[]{2, 4, 6}, Solution.multiplyAll(new int[]{1, 2, 3}).apply(2));
assertArrayEquals(new int[]{0, 0, 0}, Solution.multiplyAll(new int[]{1, 2, 3}).apply(0));
assertArrayEquals(new int[0], Solution.multiplyAll(new int[0]).apply(10), "should return an empty array");
}
@Test
void randomTests() {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for(int i = 0; i < 200; ++i) {
int l = rnd.nextInt(10);
int[] array = rnd.ints(0, 100).limit(l).toArray();
int n = rnd.nextInt(100);
int[] expected = Arrays.stream(array).map(v -> v * n).toArray();
assertArrayEquals(expected, Solution.multiplyAll(array).apply(n));
}
}
}
|