Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,77 @@ public void testRandomUniqueNormalUsageAlwayMoreThanOne() {
assertThat(randomUnique(() -> randomAlphaOfLengthBetween(1, 20), 10), hasSize(greaterThan(0)));
}

public void testRandomSubsetOfWithVarargs() {
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We could also randomize the list length of 10. The same goes for other places where we construct the initial list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 10 parameter is actually the maximum list size, not the hardcoded list size. The randomList function randomises the length between 0 and this value inclusively. I could have randomised this size too, but I figured hardcoding a max list size was acceptable, because even if I randomised it, I would still have to declare a max value somewhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right. Sounds good to me.


// 0 <= subsetSize <= listSize
int randomSubsetSize = randomInt(randomList.size());

// Uses the spread syntax to pass the list as an array of values (matching the var args parameter definition)
List<Integer> result = ESTestCase.randomSubsetOf(randomSubsetSize, randomList.toArray(new Integer[0]));
assertEquals(randomSubsetSize, result.size());
assertTrue(randomList.containsAll(result));
}

public void testRandomSubsetOfWithVarargsAndSizeTooLarge() {
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));

// listSize < subsetSize
int randomSubsetSize = randomIntBetween(randomList.size() + 1, 20);

assertThrows(IllegalArgumentException.class, () -> ESTestCase.randomSubsetOf(randomSubsetSize, randomList.toArray(new Integer[0])));
}

public void testRandomSubsetOfWithVarargsAndNegativeSubsetSize() {
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));
int randomNegativeSubsetSize = -1 * randomIntBetween(1, 10);

assertThrows(IllegalArgumentException.class, () -> ESTestCase.randomSubsetOf(randomNegativeSubsetSize, randomList));
}

public void testRandomSubsetOfWithCollection() {
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));
List<Integer> result = ESTestCase.randomSubsetOf(randomList);
assertTrue(result.size() >= 0 && result.size() <= randomList.size());
assertTrue(randomList.containsAll(result));
}

public void testRandomNonEmptySubsetOf() {
List<Integer> randomList = randomList(1, 10, () -> randomIntBetween(-100, 100));
List<Integer> result = ESTestCase.randomNonEmptySubsetOf(randomList);
assertTrue(result.size() >= 1 && result.size() <= randomList.size());
assertTrue(randomList.containsAll(result));
}

public void testRandomNonEmptySubsetOfThrowsOnEmptyCollection() {
final var ex = expectThrows(IllegalArgumentException.class, () -> randomNonEmptySubsetOf(Collections.emptySet()));
assertThat(ex.getMessage(), equalTo("Can't pick non-empty subset of an empty collection"));
}

public void testRandomSubsetOfWithCollectionAndSizeTooLarge() {
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));

// listSize < subsetSize
int randomSubsetSize = randomIntBetween(randomList.size() + 1, 20);

assertThrows(IllegalArgumentException.class, () -> ESTestCase.randomSubsetOf(randomSubsetSize, randomList));
}

public void testRandomSubsetOfWithCollectionAndNegativeSubsetSize() {
List<Integer> randomList = randomList(10, () -> randomIntBetween(-100, 100));
int randomNegativeSubsetSize = -1 * randomIntBetween(1, 10);

assertThrows(IllegalArgumentException.class, () -> ESTestCase.randomSubsetOf(randomNegativeSubsetSize, randomList));
}

public void testShuffledList() {
List<Integer> randomList = randomList(100, () -> randomIntBetween(-100, 100));
List<Integer> result = ESTestCase.shuffledList(randomList);
assertEquals(randomList.size(), result.size());
assertTrue(randomList.containsAll(result));
assertTrue(result.containsAll(randomList));
}

public void testRandomNonNegativeLong() {
assertThat(randomNonNegativeLong(), greaterThanOrEqualTo(0L));
}
Expand Down