Package io.vavr.test

Interface Arbitrary<T>

Type Parameters:
T - The type of the arbitrary object.
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface Arbitrary<T>
Represents an arbitrary object of type T.
  • Method Details

    • apply

      Gen<T> apply(int size)
      Returns a generator for objects of type T. Use Gen.map(Function) and Gen.flatMap(Function) to combine object generators.

      Example:

       
       // represents arbitrary binary trees of a certain depth n
       final class ArbitraryTree implements Arbitrary<BinaryTree<Integer>> {
           @Override
           public Gen<BinaryTree<Integer>> apply(int n) {
               return Gen.choose(-1000, 1000).flatMap(value -> {
                        if (n == 0) {
                            return Gen.of(BinaryTree.leaf(value));
                        } else {
                            return Gen.frequency(
                                    Tuple.of(1, Gen.of(BinaryTree.leaf(value))),
                                    Tuple.of(4, Gen.of(BinaryTree.branch(apply(n / 2).get(), value, apply(n / 2).get())))
                            );
                        }
               });
           }
       }
      
       // tree generator with a size hint of 10
       final Gen<BinaryTree<Integer>> treeGen = new ArbitraryTree().apply(10);
      
       // stream sum of tree node values to console for 100 arbitrary trees
       Stream.of(() -> treeGen.apply(RNG.get())).map(Tree::sum).take(100).stdout();
       
       
      Parameters:
      size - A (not necessarily positive) size parameter which may be interpreted individually and is constant for all arbitrary objects regarding one property check.
      Returns:
      A generator for objects of type T.
    • distinct

      default Arbitrary<T> distinct()
      Returns an Arbitrary based on this Arbitrary which produces unique values.
      Returns:
      A new generator
    • distinctBy

      default Arbitrary<T> distinctBy(Comparator<? super T> comparator)
      Returns an Arbitrary based on this Arbitrary which produces unique values based on the given comparator.
      Parameters:
      comparator - A comparator
      Returns:
      A new generator
    • distinctBy

      default <U> Arbitrary<T> distinctBy(Function<? super T,? extends U> keyExtractor)
      Returns an Arbitrary based on this Arbitrary which produces unique values based on the given function.
      Type Parameters:
      U - key type
      Parameters:
      keyExtractor - A function
      Returns:
      A new generator
    • filter

      default Arbitrary<T> filter(Predicate<? super T> predicate)
      Returns an Arbitrary based on this Arbitrary which produces values that fulfill the given predicate.
      Parameters:
      predicate - A predicate
      Returns:
      A new generator
    • flatMap

      default <U> Arbitrary<U> flatMap(Function<? super T,? extends Arbitrary<? extends U>> mapper)
      Maps arbitrary objects T to arbitrary object U.
      Type Parameters:
      U - New type of arbitrary objects
      Parameters:
      mapper - A function that maps arbitrary Ts to arbitrary Us given a mapper.
      Returns:
      A new Arbitrary
    • intersperse

      default Arbitrary<T> intersperse(Arbitrary<T> other)
      Intersperses values from this arbitrary instance with those of another.
      Parameters:
      other - another T arbitrary to accept values from.
      Returns:
      A new T arbitrary
    • map

      default <U> Arbitrary<U> map(Function<? super T,? extends U> mapper)
      Maps arbitrary objects T to arbitrary object U.
      Type Parameters:
      U - Type of the mapped object
      Parameters:
      mapper - A function that maps an arbitrary T to an object of type U.
      Returns:
      A new generator
    • peek

      default Arbitrary<T> peek(Consumer<? super T> action)
    • transform

      default <U> U transform(Function<? super Arbitrary<T>,? extends U> f)
      Transforms this Arbitrary.
      Type Parameters:
      U - Type of transformation result
      Parameters:
      f - A transformation
      Returns:
      An instance of type U
      Throws:
      NullPointerException - if f is null
    • of

      @SafeVarargs static <U> Arbitrary<U> of(U... values)
      Generates an arbitrary value from a fixed set of values
      Type Parameters:
      U - Type of generator value
      Parameters:
      values - A fixed set of values
      Returns:
      A new generator
    • ofAll

      static <U> Arbitrary<U> ofAll(Gen<U> generator)
      Generates an arbitrary value from a given generator
      Type Parameters:
      U - Type of generator value
      Parameters:
      generator - A generator to produce arbitrary values
      Returns:
      A new generator
    • integer

      static Arbitrary<Integer> integer()
      Generates arbitrary integer values.
      Returns:
      A new Arbitrary of Integer
    • localDateTime

      static Arbitrary<LocalDateTime> localDateTime()
      Generates arbitrary LocalDateTimes with LocalDateTime.now() as median and ChronoUnit.DAYS as chronological unit.
      Returns:
      A new Arbitrary of LocalDateTime
      See Also:
    • localDateTime

      static Arbitrary<LocalDateTime> localDateTime(ChronoUnit unit)
      Generates arbitrary LocalDateTimes with LocalDateTime.now() as median.
      Parameters:
      unit - Chronological unit of size
      Returns:
      A new Arbitrary of LocalDateTime
      See Also:
    • localDateTime

      static Arbitrary<LocalDateTime> localDateTime(LocalDateTime median, ChronoUnit unit)
      Generates arbitrary LocalDateTimes. All generated values are drawn from a range with median as center and median +/- size as included boundaries. unit defines the chronological unit of size.

      Example:

       
       Arbitrary.localDateTime(LocalDateTime.now(), ChronoUnit.YEARS);
       
       
      Parameters:
      median - Center of the LocalDateTime range
      unit - Chronological unit of size
      Returns:
      A new Arbitrary of LocalDateTime
    • string

      static Arbitrary<String> string(Gen<Character> gen)
      Generates arbitrary strings based on a given alphabet represented by gen.

      Example:

       
       Arbitrary.string(
           Gen.frequency(
               Tuple.of(1, Gen.choose('A', 'Z')),
               Tuple.of(1, Gen.choose('a', 'z')),
               Tuple.of(1, Gen.choose('0', '9'))));
       
       
      Parameters:
      gen - A character generator
      Returns:
      a new Arbitrary of String
    • list

      static <T> Arbitrary<List<T>> list(Arbitrary<T> arbitraryT)
      Generates arbitrary lists based on a given element generator arbitraryT.

      Example:

       
       Arbitrary.list(Arbitrary.integer());
       
       
      Type Parameters:
      T - Component type of the List
      Parameters:
      arbitraryT - Arbitrary elements of type T
      Returns:
      a new Arbitrary of List<T>
    • stream

      static <T> Arbitrary<Stream<T>> stream(Arbitrary<T> arbitraryT)
      Generates arbitrary streams based on a given element generator arbitraryT.

      Example:

       
       Arbitrary.stream(Arbitrary.integer());
       
       
      Type Parameters:
      T - Component type of the Stream
      Parameters:
      arbitraryT - Arbitrary elements of type T
      Returns:
      a new Arbitrary of Stream<T>