Class Gens


  • public class Gens
    extends Object
    Class with variables and methods providing various generators.
    Author:
    Herbert Praehofer
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Gen<Integer> allInts
      A generator for in values int values in the full range of int.
      private static List<Character> alphabet
      List of letters from 'a' to 'z'
      static Gen<Boolean> bools
      A generator for Boolean value with equal probability.
      static Gen<FList<Integer>> intLists
      A generator for list of integers from 0 to 9
      static Gen<Integer> intRands
      A generator for integer values (in full range of integer)
      static Gen<Character> letters
      A generator for creating letters from 'a' to 'z' with same probability.
      static Gen<Double> percents
      Creates a generator for percentage values.
      static Gen<Integer> posInts
      A generator of positive int values
      (package private) Gen<Integer> primes100
      A generator for prime numbers from 2 to 100.
      private static Random RAND
      A random number generator used for creating random values
      static Gen<Double> rands
      A generator for double values (in full range of double)
    • Constructor Summary

      Constructors 
      Constructor Description
      Gens()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Gen<Integer> intsFromTo​(int from, int to)
      Creates a generator for int values between from and to.
      static Gen<Integer> intsTo​(int to)
      Creates a generator for int values between 0 and to.
      private static boolean isPrime​(int n)  
      static <A> Gen<List<A>> lists​(int maxLength, A... elems)
      Creates a generator for generating lists from given elements of given maximum length.
      static <A> Gen<FList<A>> lists​(int maxLength, Gen<A> elemGen)
      Creates a generator for generating lists of given maximum length.
      static <A> Gen<A> oneOf​(A... as)
      Creates a generator which creates values from the given set of values with equal probability.
      static <A> Gen<A> oneOf​(List<A> as)
      Creates a generator which creates values from the given set of values with equal probability.
      static <A,​B>
      Gen<Pair<A,​B>>
      pair​(Gen<A> aGen, Gen<B> bGen)
      A generator of pairs of values.
      static Gen<Double> randsFromTo​(double from, double to)
      Creates a generator for double values between from and to
      static <A> Gen<A> single​(A a)
      Creates a generator which is capable to create a single value.
      static <A,​B>
      Gen<Tuple2<A,​B>>
      tuple2​(Gen<A> aGen, Gen<B> bGen)
      A generator of tuples of two values.
      static <A,​B,​C>
      Gen<Tuple3<A,​B,​C>>
      tuple3​(Gen<A> aGen, Gen<B> bGen, Gen<C> cGen)
      A generator of tuples of three values.
      static Gen<Boolean> withProb​(double prob)
      Creates generator for Boolean values where true values are created with the given probability.
      static Gen<String> words​(int maxLength)
      Creates a generator for generating words from random letters of given maximum length.
    • Field Detail

      • RAND

        private static final Random RAND
        A random number generator used for creating random values
      • rands

        public static Gen<Double> rands
        A generator for double values (in full range of double)
      • intRands

        public static Gen<Integer> intRands
        A generator for integer values (in full range of integer)
      • percents

        public static Gen<Double> percents
        Creates a generator for percentage values.
      • posInts

        public static Gen<Integer> posInts
        A generator of positive int values
      • allInts

        public static Gen<Integer> allInts
        A generator for in values int values in the full range of int.
      • bools

        public static Gen<Boolean> bools
        A generator for Boolean value with equal probability.
      • alphabet

        private static List<Character> alphabet
        List of letters from 'a' to 'z'
      • letters

        public static Gen<Character> letters
        A generator for creating letters from 'a' to 'z' with same probability.
      • intLists

        public static Gen<FList<Integer>> intLists
        A generator for list of integers from 0 to 9
      • primes100

        Gen<Integer> primes100
        A generator for prime numbers from 2 to 100.
    • Constructor Detail

      • Gens

        public Gens()
    • Method Detail

      • randsFromTo

        public static Gen<Double> randsFromTo​(double from,
                                              double to)
        Creates a generator for double values between from and to
        Parameters:
        from - the lower value
        to - the upper value
        Returns:
        the generator for integer values between from and to
      • intsFromTo

        public static Gen<Integer> intsFromTo​(int from,
                                              int to)
        Creates a generator for int values between from and to.
        Parameters:
        from - the lower value (inclusive)
        to - the upper value (exclusive)
        Returns:
        the generator for integer values between from and to
      • intsTo

        public static Gen<Integer> intsTo​(int to)
        Creates a generator for int values between 0 and to.
        Parameters:
        to - the upper limit (exclusive)
        Returns:
        the generator for integer values between 0 and to
      • withProb

        public static Gen<Boolean> withProb​(double prob)
        Creates generator for Boolean values where true values are created with the given probability.
        Parameters:
        prob - the probability for true
        Returns:
        the generator of Boolean values
      • oneOf

        public static <A> Gen<A> oneOf​(A... as)
        Creates a generator which creates values from the given set of values with equal probability.
        Type Parameters:
        A - the type of value
        Parameters:
        as - the values
        Returns:
        the generator for the values
      • oneOf

        public static <A> Gen<A> oneOf​(List<A> as)
        Creates a generator which creates values from the given set of values with equal probability.
        Type Parameters:
        A - the type of value
        Parameters:
        as - the list of values
        Returns:
        the generator for the values
      • single

        public static <A> Gen<A> single​(A a)
        Creates a generator which is capable to create a single value. For every call to sample the same value is returned.
        Type Parameters:
        A - the type of the value
        Parameters:
        a - the constant value
        Returns:
        the generator creating the same value
      • words

        public static Gen<String> words​(int maxLength)
        Creates a generator for generating words from random letters of given maximum length.
        Parameters:
        maxLength - the maximum length
        Returns:
        the generator for words
      • lists

        public static <A> Gen<List<A>> lists​(int maxLength,
                                             A... elems)
        Creates a generator for generating lists from given elements of given maximum length.
        Type Parameters:
        A - the element type
        Parameters:
        maxLength - the maximum length
        elems - the elements
        Returns:
        the generator for lists
      • lists

        public static <A> Gen<FList<A>> lists​(int maxLength,
                                              Gen<A> elemGen)
        Creates a generator for generating lists of given maximum length. The elements are generated by the given generator.
        Type Parameters:
        A - the element type
        Parameters:
        maxLength - the maximum length
        elemGen - the generator for elements
        Returns:
        the generator for lists
      • pair

        public static <A,​B> Gen<Pair<A,​B>> pair​(Gen<A> aGen,
                                                            Gen<B> bGen)
        A generator of pairs of values. The values are generated by two given generators.
        Type Parameters:
        A - the type of the elements of the first generator
        B - the type of the elements of the second generator
        Parameters:
        aGen - the first generator
        bGen - the second generator
        Returns:
        the generator of pairs of values
      • tuple2

        public static <A,​B> Gen<Tuple2<A,​B>> tuple2​(Gen<A> aGen,
                                                                Gen<B> bGen)
        A generator of tuples of two values. The values are generated by two given generators.
        Type Parameters:
        A - the type of the elements of the first generator
        B - the type of the elements of the second generator
        Parameters:
        aGen - the first generator
        bGen - the second generator
        Returns:
        the generator of tuple of two values
      • tuple3

        public static <A,​B,​C> Gen<Tuple3<A,​B,​C>> tuple3​(Gen<A> aGen,
                                                                                Gen<B> bGen,
                                                                                Gen<C> cGen)
        A generator of tuples of three values. The values are generated by two given generators.
        Type Parameters:
        A - the type of the elements of the first generator
        B - the type of the elements of the second generator
        C - the type of the elements of the third generator
        Parameters:
        aGen - the first generator
        bGen - the second generator
        cGen - the third generator
        Returns:
        the generator of tuple of three values
      • isPrime

        private static boolean isPrime​(int n)