Skip to content

Commit 7485750

Browse files
[TEST] Adds tests for ESTestCase randomSubset methods (#131745)
[TEST] Adds tests for ESTestCase randomSubset methods Adds tests for the following methods in `ESTestCase`: `randomSubsetOf`, `randomNonEmptySubsetOf` and `shuffledList`
1 parent e52a181 commit 7485750

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

test/framework/src/test/java/org/elasticsearch/test/test/ESTestCaseTests.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,77 @@ public void testRandomUniqueNormalUsageAlwayMoreThanOne() {
174174
assertThat(randomUnique(() -> randomAlphaOfLengthBetween(1, 20), 10), hasSize(greaterThan(0)));
175175
}
176176

177+
public void testRandomSubsetOfWithVarargs() {
178+
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));
179+
180+
// 0 <= subsetSize <= listSize
181+
int randomSubsetSize = randomInt(randomList.size());
182+
183+
// Uses the spread syntax to pass the list as an array of values (matching the var args parameter definition)
184+
List<Integer> result = ESTestCase.randomSubsetOf(randomSubsetSize, randomList.toArray(new Integer[0]));
185+
assertEquals(randomSubsetSize, result.size());
186+
assertTrue(randomList.containsAll(result));
187+
}
188+
189+
public void testRandomSubsetOfWithVarargsAndSizeTooLarge() {
190+
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));
191+
192+
// listSize < subsetSize
193+
int randomSubsetSize = randomIntBetween(randomList.size() + 1, 20);
194+
195+
assertThrows(IllegalArgumentException.class, () -> ESTestCase.randomSubsetOf(randomSubsetSize, randomList.toArray(new Integer[0])));
196+
}
197+
198+
public void testRandomSubsetOfWithVarargsAndNegativeSubsetSize() {
199+
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));
200+
int randomNegativeSubsetSize = -1 * randomIntBetween(1, 10);
201+
202+
assertThrows(IllegalArgumentException.class, () -> ESTestCase.randomSubsetOf(randomNegativeSubsetSize, randomList));
203+
}
204+
205+
public void testRandomSubsetOfWithCollection() {
206+
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));
207+
List<Integer> result = ESTestCase.randomSubsetOf(randomList);
208+
assertTrue(result.size() >= 0 && result.size() <= randomList.size());
209+
assertTrue(randomList.containsAll(result));
210+
}
211+
212+
public void testRandomNonEmptySubsetOf() {
213+
List<Integer> randomList = randomList(1, 10, () -> randomIntBetween(-100, 100));
214+
List<Integer> result = ESTestCase.randomNonEmptySubsetOf(randomList);
215+
assertTrue(result.size() >= 1 && result.size() <= randomList.size());
216+
assertTrue(randomList.containsAll(result));
217+
}
218+
177219
public void testRandomNonEmptySubsetOfThrowsOnEmptyCollection() {
178220
final var ex = expectThrows(IllegalArgumentException.class, () -> randomNonEmptySubsetOf(Collections.emptySet()));
179221
assertThat(ex.getMessage(), equalTo("Can't pick non-empty subset of an empty collection"));
180222
}
181223

224+
public void testRandomSubsetOfWithCollectionAndSizeTooLarge() {
225+
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));
226+
227+
// listSize < subsetSize
228+
int randomSubsetSize = randomIntBetween(randomList.size() + 1, 20);
229+
230+
assertThrows(IllegalArgumentException.class, () -> ESTestCase.randomSubsetOf(randomSubsetSize, randomList));
231+
}
232+
233+
public void testRandomSubsetOfWithCollectionAndNegativeSubsetSize() {
234+
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));
235+
int randomNegativeSubsetSize = -1 * randomIntBetween(1, 10);
236+
237+
assertThrows(IllegalArgumentException.class, () -> ESTestCase.randomSubsetOf(randomNegativeSubsetSize, randomList));
238+
}
239+
240+
public void testShuffledList() {
241+
List<Integer> randomList = randomList(100, () -> randomIntBetween(-100, 100));
242+
List<Integer> result = ESTestCase.shuffledList(randomList);
243+
assertEquals(randomList.size(), result.size());
244+
assertTrue(randomList.containsAll(result));
245+
assertTrue(result.containsAll(randomList));
246+
}
247+
182248
public void testRandomNonNegativeLong() {
183249
assertThat(randomNonNegativeLong(), greaterThanOrEqualTo(0L));
184250
}

0 commit comments

Comments
 (0)