forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReservoirSamplingTest.java
45 lines (33 loc) · 1.27 KB
/
ReservoirSamplingTest.java
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
package com.thealgorithms.randomized;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
public class ReservoirSamplingTest {
@Test
public void testSampleSizeEqualsStreamLength() {
int[] stream = {1, 2, 3, 4, 5};
int sampleSize = 5;
List<Integer> result = ReservoirSampling.sample(stream, sampleSize);
assertEquals(sampleSize, result.size());
assertTrue(Arrays.stream(stream).allMatch(result::contains));
}
@Test
public void testSampleSizeLessThanStreamLength() {
int[] stream = {10, 20, 30, 40, 50, 60};
int sampleSize = 3;
List<Integer> result = ReservoirSampling.sample(stream, sampleSize);
assertEquals(sampleSize, result.size());
for (int value : result) {
assertTrue(Arrays.stream(stream).anyMatch(x -> x == value));
}
}
@Test
public void testSampleSizeGreaterThanStreamLengthThrowsException() {
int[] stream = {1, 2, 3};
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
ReservoirSampling.sample(stream, 5);
});
assertEquals("Sample size cannot exceed stream size.", exception.getMessage());
}
}