@Test
 public void shouldConvertToStream() {
   final Value<Integer> value = of(1, 2, 3);
   final Stream<Integer> stream = value.toStream();
   if (value.isSingleValued()) {
     assertThat(stream).isEqualTo(Stream.of(1));
   } else {
     assertThat(stream).isEqualTo(Stream.of(1, 2, 3));
   }
 }
Beispiel #2
0
 /**
  * Randomly chooses one of the given generators.
  *
  * @param generators A non-empty Iterable of generators
  * @param <T> Type to be generated
  * @return A new T generator
  * @throws java.lang.NullPointerException if generators is null
  * @throws java.lang.IllegalArgumentException if generators is empty
  */
 static <T> Gen<T> oneOf(Iterable<Gen<T>> generators) {
   Objects.requireNonNull(generators, "generators is null");
   final Stream<Gen<T>> stream = Stream.ofAll(generators);
   if (stream.isEmpty()) {
     throw new IllegalArgumentException("generators is empty");
   }
   @SuppressWarnings("unchecked")
   final Gen<T>[] array = stream.toJavaArray((Class<Gen<T>>) (Class) Gen.class);
   return oneOf(array);
 }
Beispiel #3
0
 /**
  * Chooses one of the given generators according to their frequency.
  *
  * @param generators A non-empty array of Tuples (frequency, generator)
  * @param <T> Type to be generated
  * @return A new T generator
  * @throws java.lang.NullPointerException if generators is null
  * @throws java.lang.IllegalArgumentException if generators is empty
  */
 @SafeVarargs
 @SuppressWarnings("varargs")
 static <T> Gen<T> frequency(Tuple2<Integer, Gen<T>>... generators) {
   Objects.requireNonNull(generators, "generators is null");
   if (generators.length == 0) {
     throw new IllegalArgumentException("generators is empty");
   }
   final Iterable<Tuple2<Integer, Gen<T>>> iterable = Stream.of(generators);
   return frequency(iterable);
 }
Beispiel #4
0
 /**
  * Chooses one of the given generators according to their frequency.
  *
  * @param generators A non-empty traversable of Tuples (frequency, generator)
  * @param <T> Type to be generated
  * @return A new T generator
  * @throws java.lang.NullPointerException if generators is null
  * @throws java.lang.IllegalArgumentException if generators is empty
  */
 static <T> Gen<T> frequency(Iterable<Tuple2<Integer, Gen<T>>> generators) {
   Objects.requireNonNull(generators, "generators is null");
   final Stream<Tuple2<Integer, Gen<T>>> stream = Stream.ofAll(generators);
   if (stream.isEmpty()) {
     throw new IllegalArgumentException("generators is empty");
   }
   final class Frequency {
     Gen<T> gen(int n, Stream<Tuple2<Integer, Gen<T>>> stream) {
       final int k = stream.head()._1;
       if (k < 0) {
         throw new IllegalArgumentException("negative frequency: " + k);
       }
       return (n <= k) ? stream.head()._2 : gen(n - k, stream.tail());
     }
   }
   final int size = stream.map(t -> t._1).sum().intValue();
   return choose(1, size).flatMap(n -> new Frequency().gen(n, stream));
 }
Beispiel #5
0
 private static long sumOfSquaresFrom1UpTo(int max) {
   return Stream.rangeClosed(1, max).map(i -> (long) Math.pow(i, 2)).sum().longValue();
 }
Beispiel #6
0
 private static long squareOfSumFrom1UpTo(int max) {
   return (long) Math.pow(Stream.rangeClosed(1, max).sum().longValue(), 2);
 }
Beispiel #7
0
 static <T> Gen<T> of(T seed, Function<? super T, ? extends T> next) {
   final Iterator<T> iterator = Stream.gen(seed, next).iterator();
   return ignored -> iterator.next();
 }
Beispiel #8
0
 public static Stream<Long> primeFactors(long num) {
   return Stream.rangeClosed(2L, (int) Math.sqrt(num))
       .findFirst(d -> num % d == 0)
       .map(d -> Stream.cons(d, () -> primeFactors(num / d)))
       .orElseGet(() -> Stream.of(num));
 }
Beispiel #9
0
 public static Stream<Integer> primes() {
   return Stream.ofAll(PRIMES_2_000_000);
 }