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 Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      Gen<T> apply​(int size)
      Returns a generator for objects of type T.
      default Arbitrary<T> distinct()
      Returns an Arbitrary based on this Arbitrary which produces unique values.
      default Arbitrary<T> distinctBy​(java.util.Comparator<? super T> comparator)
      Returns an Arbitrary based on this Arbitrary which produces unique values based on the given comparator.
      default <U> Arbitrary<T> distinctBy​(java.util.function.Function<? super T,​? extends U> keyExtractor)
      Returns an Arbitrary based on this Arbitrary which produces unique values based on the given function.
      default Arbitrary<T> filter​(java.util.function.Predicate<? super T> predicate)
      Returns an Arbitrary based on this Arbitrary which produces values that fulfill the given predicate.
      default <U> Arbitrary<U> flatMap​(java.util.function.Function<? super T,​? extends Arbitrary<? extends U>> mapper)
      Maps arbitrary objects T to arbitrary object U.
      static Arbitrary<java.lang.Integer> integer()
      Generates arbitrary integer values.
      default Arbitrary<T> intersperse​(Arbitrary<T> other)
      Intersperses values from this arbitrary instance with those of another.
      static <T> Arbitrary<List<T>> list​(Arbitrary<T> arbitraryT)
      Generates arbitrary lists based on a given element generator arbitraryT.
      static Arbitrary<java.time.LocalDateTime> localDateTime()
      Generates arbitrary LocalDateTimes with LocalDateTime.now() as median and ChronoUnit.DAYS as chronological unit.
      static Arbitrary<java.time.LocalDateTime> localDateTime​(java.time.LocalDateTime median, java.time.temporal.ChronoUnit unit)
      Generates arbitrary LocalDateTimes.
      static Arbitrary<java.time.LocalDateTime> localDateTime​(java.time.temporal.ChronoUnit unit)
      Generates arbitrary LocalDateTimes with LocalDateTime.now() as median.
      default <U> Arbitrary<U> map​(java.util.function.Function<? super T,​? extends U> mapper)
      Maps arbitrary objects T to arbitrary object U.
      static <U> Arbitrary<U> of​(U... values)
      Generates an arbitrary value from a fixed set of values
      static <U> Arbitrary<U> ofAll​(Gen<U> generator)
      Generates an arbitrary value from a given generator
      default Arbitrary<T> peek​(java.util.function.Consumer<? super T> action)  
      static <T> Arbitrary<Stream<T>> stream​(Arbitrary<T> arbitraryT)
      Generates arbitrary streams based on a given element generator arbitraryT.
      static Arbitrary<java.lang.String> string​(Gen<java.lang.Character> gen)
      Generates arbitrary strings based on a given alphabet represented by gen.
      default <U> U transform​(java.util.function.Function<? super Arbitrary<T>,​? extends U> f)
      Transforms this Arbitrary.
    • Method Detail

      • 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​(java.util.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​(java.util.function.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​(java.util.function.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​(java.util.function.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​(java.util.function.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​(java.util.function.Consumer<? super T> action)
      • transform

        default <U> U transform​(java.util.function.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:
        java.lang.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<java.lang.Integer> integer()
        Generates arbitrary integer values.
        Returns:
        A new Arbitrary of Integer
      • localDateTime

        static Arbitrary<java.time.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(LocalDateTime, ChronoUnit)
      • localDateTime

        static Arbitrary<java.time.LocalDateTime> localDateTime​(java.time.temporal.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(LocalDateTime, ChronoUnit)
      • localDateTime

        static Arbitrary<java.time.LocalDateTime> localDateTime​(java.time.LocalDateTime median,
                                                                java.time.temporal.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<java.lang.String> string​(Gen<java.lang.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>