### Question:
SetGenerator implements Generator<Set<T>> { @Override public Set<T> next() { final int size = randomValues.randomInteger(sizeRange.lowerBound(), sizeRange.upperBound()); final Set<T> setOfT = new HashSet<>(); int missCount = 0; while (setOfT.size() < size) { final int sizeBefore = setOfT.size(); setOfT.add(generatorOfT.next()); if (setOfT.size() == sizeBefore) { missCount++; } if (missCount >= 2000) { throw new IllegalStateException(format("unable to generate set of size %s because the value generator returned too many duplicate values", size)); } } return setOfT; } SetGenerator(final RandomValues randomValues, final Generator<? extends T> generatorOfT, final Range<Integer> sizeRange); @Override Set<T> next();  }### Answer:
@Test public void generatesEmptySetForNegativeSize() { assertThat(RDG.set(generatingFrom(1), -1).next()).isEmpty(); }
@Test public void generatesEmptySetForNegativeFixedRangeSize() { assertThat(RDG.set(generatingFrom(1), fixed(-1)).next()).isEmpty(); }
@Test public void generatesEmptySetForNegativeClosedRangeSize() { assertThat(RDG.set(generatingFrom(1), closed(-10, -1)).next()).isEmpty(); }
@Test public void canGenerateSetOfObjectsOfType() { final Generator<Set<Type>> generator = RDG.set(randomConcreteTypes(), 10); for (final Type type : generator.next()) { assertThat(type).isInstanceOf(ConcreteType.class); } }
@Test public void generatesSetAtTheMaximumSize() { final Generator<Set<Integer>> generator = RDG.set(generatingRandomIntegers(), fixed(1000)); assertThat(generator.next()).hasSize(MAX_SIZE); }
@Test public void doesNotGenerateSetOfElementsLargerThanTheMaximumSize() { final Generator<Set<Integer>> generator = RDG.set(generatingRandomIntegers(), fixed(5000)); assertThat(generator.next()).hasSize(MAX_SIZE); }
@Test public void generatesSetWithDefaultSize() { final Generator<Set<Integer>> generator = RDG.set(generatingFrom(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)); assertThat(generator.next()) .hasSize(15) .containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); }
@Test public void generatesSetWithFixedSize() { final Generator<Set<Integer>> generator = RDG.set(generatingFrom(1, 2, 3, 4, 5, 6), 5); assertThat(generator.next()) .hasSize(5) .containsExactly(1, 2, 3, 4, 5); }
@Test public void generatesSetWithFixedSizeRange() { final Generator<Set<Integer>> generator = RDG.set(generatingFrom(1, 2, 3, 4, 5, 6), fixed(5)); assertThat(generator.next()) .hasSize(5) .containsExactly(1, 2, 3, 4, 5); }
@Test public void generatesSetOfSpecifiedSizeEvenWhenValueGeneratorReturnsDuplicates() { final Generator<Set<Integer>> generator = RDG.set(generatingFrom(1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4), 3); assertThat(generator.next()) .hasSize(3) .containsExactly(1, 2, 3); }
@Test(expected = IllegalStateException.class) public void throwsAnExceptionWhenValueGeneratorGeneratesTooManyDuplicatesAndTheFullSetSizeCannotBeAchieved() { final Generator<Integer> fixedValueGenerator = () -> 1; RDG.set(fixedValueGenerator, 3).next(); }