fj.test.Arbitrary Maven / Gradle / Ivy
package fj.test;
import fj.F;
import fj.F2;
import fj.F3;
import fj.F4;
import fj.F5;
import fj.F6;
import fj.F7;
import fj.F8;
import fj.Function;
import fj.Bottom;
import static fj.Function.compose;
import static fj.P.p;
import fj.P1;
import fj.P2;
import fj.P3;
import fj.P4;
import fj.P5;
import fj.P6;
import fj.P7;
import fj.P8;
import fj.data.*;
import fj.LcgRng;
import fj.Ord;
import static fj.data.Either.left;
import static fj.data.Either.right;
import static fj.data.Enumerator.charEnumerator;
import static fj.data.List.asString;
import static fj.data.List.list;
import static fj.data.Option.some;
import fj.function.Effect1;
import static fj.data.Stream.range;
import static fj.test.Gen.choose;
import static fj.test.Gen.elements;
import static fj.test.Gen.fail;
import static fj.test.Gen.frequency;
import static fj.test.Gen.listOf;
import static fj.test.Gen.oneOf;
import static fj.test.Gen.promote;
import static fj.test.Gen.sized;
import static fj.test.Gen.value;
import static java.lang.Math.abs;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Date;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Locale;
import static java.util.Locale.getAvailableLocales;
import java.util.PriorityQueue;
import java.util.Properties;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.util.WeakHashMap;
import static java.util.EnumSet.copyOf;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;
/**
* The type used to generate arbitrary values of the given type parameter (A
). Common
* arbitrary implementations are provided.
*
* @version %build.number%
*/
public final class Arbitrary {
/**
* The generator associated with this arbitrary.
*/
@SuppressWarnings({"PublicField"})
public final Gen gen;
private Arbitrary(final Gen gen) {
this.gen = gen;
}
/**
* Constructs and arbitrary with the given generator.
*
* @param g The generator to construct an arbitrary with.
* @return A new arbitrary that uses the given generator.
*/
public static Arbitrary arbitrary(final Gen g) {
return new Arbitrary(g);
}
/**
* An arbitrary for functions.
*
* @param c The coarbitrary for the function domain.
* @param a The arbitrary for the function codomain.
* @return An arbitrary for functions.
*/
public static Arbitrary> arbF(final Coarbitrary c, final Arbitrary a) {
return arbitrary(promote(new F>() {
public Gen f(final A x) {
return c.coarbitrary(x, a.gen);
}
}));
}
public static Arbitrary> arbReader(Coarbitrary aa, Arbitrary ab) {
return arbitrary(Arbitrary.arbF(aa, ab).gen.map(f -> Reader.unit(f)));
}
/**
* An arbitrary for state.
*/
public static Arbitrary> arbState(Arbitrary as, Coarbitrary cs, Arbitrary aa) {
return arbitrary(arbF(cs, arbP2(as, aa)).gen.map(f -> State.unit(f)));
}
/**
* An arbitrary for the LcgRng.
*/
public static Arbitrary arbLcgRng() {
return arbitrary(Arbitrary.arbLong.gen.map(l -> new LcgRng(l)));
}
/**
* An arbitrary for functions.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for functions.
*/
public static Arbitrary> arbFInvariant(final Arbitrary a) {
return arbitrary(a.gen.map(Function.constant()));
}
/**
* An arbitrary for function-2.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-2.
*/
public static Arbitrary> arbF2(final Coarbitrary ca, final Coarbitrary cb,
final Arbitrary a) {
return arbitrary(arbF(ca, arbF(cb, a)).gen.map(Function.uncurryF2()));
}
/**
* An arbitrary for function-2.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-2.
*/
public static Arbitrary> arbF2Invariant(final Arbitrary a) {
return arbitrary(a.gen.map(
compose(Function.uncurryF2(), compose(Function.>constant(), Function.constant()))));
}
/**
* An arbitrary for function-3.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-3.
*/
public static Arbitrary> arbF3(final Coarbitrary ca, final Coarbitrary cb,
final Coarbitrary cc, final Arbitrary a) {
return arbitrary(arbF(ca, arbF(cb, arbF(cc, a))).gen.map(Function.uncurryF3()));
}
/**
* An arbitrary for function-3.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-3.
*/
public static Arbitrary> arbF3Invariant(final Arbitrary a) {
return arbitrary(a.gen.map(compose(Function.uncurryF3(), compose(Function.>>constant(),
compose(
Function.>constant(),
Function.constant())))));
}
/**
* An arbitrary for function-4.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param cd A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-4.
*/
public static Arbitrary> arbF4(final Coarbitrary ca, final Coarbitrary cb,
final Coarbitrary cc, final Coarbitrary cd,
final Arbitrary a) {
return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, a)))).gen.map(Function.uncurryF4()));
}
/**
* An arbitrary for function-4.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-4.
*/
public static Arbitrary> arbF4Invariant(final Arbitrary a) {
return arbitrary(a.gen.map(compose(Function.uncurryF4(),
compose(Function.>>>constant(),
compose(Function.>>constant(),
compose(Function.>constant(),
Function.constant()))))));
}
/**
* An arbitrary for function-5.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param cd A coarbitrary for the part of the domain of the function.
* @param ce A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-5.
*/
public static Arbitrary> arbF5(final Coarbitrary ca,
final Coarbitrary cb,
final Coarbitrary cc,
final Coarbitrary cd,
final Coarbitrary ce,
final Arbitrary a) {
return arbitrary(
arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, a))))).gen.map(Function.uncurryF5()));
}
/**
* An arbitrary for function-5.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-5.
*/
public static Arbitrary> arbF5Invariant(final Arbitrary a) {
return arbitrary(a.gen.map(compose(Function.uncurryF5(),
compose(Function.>>>>constant(),
compose(Function.>>>constant(),
compose(Function.>>constant(),
compose(Function.>constant(),
Function.constant())))))));
}
/**
* An arbitrary for function-6.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param cd A coarbitrary for the part of the domain of the function.
* @param ce A coarbitrary for the part of the domain of the function.
* @param cf A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-6.
*/
public static Arbitrary> arbF6(final Coarbitrary ca,
final Coarbitrary cb,
final Coarbitrary cc,
final Coarbitrary cd,
final Coarbitrary ce,
final Coarbitrary cf,
final Arbitrary a) {
return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, a)))))).gen.map(
Function.uncurryF6()));
}
/**
* An arbitrary for function-6.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-6.
*/
public static Arbitrary> arbF6Invariant(final Arbitrary a) {
return arbitrary(a.gen.map(compose(Function.uncurryF6(),
compose(Function.>>>>>constant(),
compose(Function.>>>>constant(),
compose(Function.>>>constant(),
compose(Function.>>constant(),
compose(Function.>constant(),
Function.constant()))))))));
}
/**
* An arbitrary for function-7.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param cd A coarbitrary for the part of the domain of the function.
* @param ce A coarbitrary for the part of the domain of the function.
* @param cf A coarbitrary for the part of the domain of the function.
* @param cg A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-7.
*/
public static Arbitrary> arbF7(final Coarbitrary ca,
final Coarbitrary cb,
final Coarbitrary cc,
final Coarbitrary cd,
final Coarbitrary ce,
final Coarbitrary cf,
final Coarbitrary cg,
final Arbitrary a) {
return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, arbF(cg, a))))))).gen.map(
Function.uncurryF7()));
}
/**
* An arbitrary for function-7.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-7.
*/
public static Arbitrary> arbF7Invariant(final Arbitrary a) {
return arbitrary(a.gen.map(compose(Function.uncurryF7(),
compose(Function.>>>>>>constant(),
compose(Function.>>>>>constant(),
compose(Function.>>>>constant(),
compose(Function.>>>constant(),
compose(Function.>>constant(),
compose(Function.>constant(),
Function.constant())))))))));
}
/**
* An arbitrary for function-8.
*
* @param ca A coarbitrary for the part of the domain of the function.
* @param cb A coarbitrary for the part of the domain of the function.
* @param cc A coarbitrary for the part of the domain of the function.
* @param cd A coarbitrary for the part of the domain of the function.
* @param ce A coarbitrary for the part of the domain of the function.
* @param cf A coarbitrary for the part of the domain of the function.
* @param cg A coarbitrary for the part of the domain of the function.
* @param ch A coarbitrary for the part of the domain of the function.
* @param a An arbitrary for the codomain of the function.
* @return An arbitrary for function-8.
*/
public static Arbitrary> arbF8(final Coarbitrary ca,
final Coarbitrary cb,
final Coarbitrary cc,
final Coarbitrary cd,
final Coarbitrary ce,
final Coarbitrary cf,
final Coarbitrary cg,
final Coarbitrary ch,
final Arbitrary a) {
return arbitrary(arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, arbF(cg, arbF(ch, a)))))))).gen.map(
Function.uncurryF8()));
}
/**
* An arbitrary for function-8.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-8.
*/
public static Arbitrary> arbF8Invariant(
final Arbitrary a) {
return arbitrary(a.gen.map(compose(Function.uncurryF8(),
compose(Function.>>>>>>>constant(),
compose(Function.>>>>>>constant(),
compose(Function.>>>>>constant(),
compose(
Function.>>>>constant(),
compose(Function.>>>constant(),
compose(
Function.>>constant(),
compose(Function.>constant(),
Function.constant()))))))))));
}
/**
* An arbitrary implementation for boolean values.
*/
public static final Arbitrary arbBoolean = arbitrary(elements(true, false));
/**
* An arbitrary implementation for integer values.
*/
public static final Arbitrary arbInteger = arbitrary(sized(new F>() {
public Gen f(final Integer i) {
return choose(-i, i);
}
}));
/**
* An arbitrary implementation for integer values that checks boundary values (0, 1, -1,
* max, min, max - 1, min + 1)
with a frequency of 1% each then generates from {@link
* #arbInteger} the remainder of the time (93%).
*/
public static final Arbitrary arbIntegerBoundaries = arbitrary(sized(new F>() {
@SuppressWarnings("unchecked")
public Gen f(final Integer i) {
return frequency(list(p(1, value(0)),
p(1, value(1)),
p(1, value(-1)),
p(1, value(Integer.MAX_VALUE)),
p(1, value(Integer.MIN_VALUE)),
p(1, value(Integer.MAX_VALUE - 1)),
p(1, value(Integer.MIN_VALUE + 1)),
p(93, arbInteger.gen)));
}
}));
/**
* An arbitrary implementation for long values.
*/
public static final Arbitrary arbLong =
arbitrary(arbInteger.gen.bind(arbInteger.gen, new F>() {
public F f(final Integer i1) {
return new F() {
public Long f(final Integer i2) {
return (long) i1 << 32L & i2;
}
};
}
}));
/**
* An arbitrary implementation for long values that checks boundary values (0, 1, -1, max,
* min, max - 1, min + 1)
with a frequency of 1% each then generates from {@link #arbLong}
* the remainder of the time (93%).
*/
public static final Arbitrary arbLongBoundaries = arbitrary(sized(new F>() {
@SuppressWarnings("unchecked")
public Gen f(final Integer i) {
return frequency(list(p(1, value(0L)),
p(1, value(1L)),
p(1, value(-1L)),
p(1, value(Long.MAX_VALUE)),
p(1, value(Long.MIN_VALUE)),
p(1, value(Long.MAX_VALUE - 1L)),
p(1, value(Long.MIN_VALUE + 1L)),
p(93, arbLong.gen)));
}
}));
/**
* An arbitrary implementation for byte values.
*/
public static final Arbitrary arbByte = arbitrary(arbInteger.gen.map(new F() {
public Byte f(final Integer i) {
return (byte) i.intValue();
}
}));
/**
* An arbitrary implementation for byte values that checks boundary values (0, 1, -1, max,
* min, max - 1, min + 1)
with a frequency of 1% each then generates from {@link #arbByte}
* the remainder of the time (93%).
*/
public static final Arbitrary arbByteBoundaries = arbitrary(sized(new F>() {
@SuppressWarnings("unchecked")
public Gen f(final Integer i) {
return frequency(list(p(1, value((byte) 0)),
p(1, value((byte) 1)),
p(1, value((byte) -1)),
p(1, value(Byte.MAX_VALUE)),
p(1, value(Byte.MIN_VALUE)),
p(1, value((byte) (Byte.MAX_VALUE - 1))),
p(1, value((byte) (Byte.MIN_VALUE + 1))),
p(93, arbByte.gen)));
}
}));
/**
* An arbitrary implementation for short values.
*/
public static final Arbitrary arbShort = arbitrary(arbInteger.gen.map(new F() {
public Short f(final Integer i) {
return (short) i.intValue();
}
}));
/**
* An arbitrary implementation for short values that checks boundary values (0, 1, -1, max,
* min, max - 1, min + 1)
with a frequency of 1% each then generates from {@link #arbShort}
* the remainder of the time (93%).
*/
public static final Arbitrary arbShortBoundaries = arbitrary(sized(new F>() {
@SuppressWarnings("unchecked")
public Gen f(final Integer i) {
return frequency(list(p(1, value((short) 0)),
p(1, value((short) 1)),
p(1, value((short) -1)),
p(1, value(Short.MAX_VALUE)),
p(1, value(Short.MIN_VALUE)),
p(1, value((short) (Short.MAX_VALUE - 1))),
p(1, value((short) (Short.MIN_VALUE + 1))),
p(93, arbShort.gen)));
}
}));
/**
* An arbitrary implementation for character values.
*/
public static final Arbitrary arbCharacter = arbitrary(choose(0, 65536).map(new F() {
public Character f(final Integer i) {
return (char) i.intValue();
}
}));
/**
* An arbitrary implementation for character values that checks boundary values (max, min,
* max - 1, min + 1)
with a frequency of 1% each then generates from {@link #arbCharacter}
* the remainder of the time (96%).
*/
public static final Arbitrary arbCharacterBoundaries = arbitrary(sized(new F>() {
@SuppressWarnings("unchecked")
public Gen f(final Integer i) {
return frequency(list(p(1, value(Character.MIN_VALUE)),
p(1, value((char) (Character.MIN_VALUE + 1))),
p(1, value(Character.MAX_VALUE)),
p(1, value((char) (Character.MAX_VALUE - 1))),
p(95, arbCharacter.gen)));
}
}));
/**
* An arbitrary implementation for double values.
*/
public static final Arbitrary arbDouble = arbitrary(sized(new F>() {
public Gen f(final Integer i) {
return choose((double) -i, i);
}
}));
/**
* An arbitrary implementation for double values that checks boundary values (0, 1, -1, max,
* min, min (normal), NaN, -infinity, infinity, max - 1)
with a frequency of 1% each then
* generates from {@link #arbDouble} the remainder of the time (91%).
*/
public static final Arbitrary arbDoubleBoundaries = arbitrary(sized(new F>() {
@SuppressWarnings("unchecked")
public Gen f(final Integer i) {
return frequency(list(p(1, value(0D)),
p(1, value(1D)),
p(1, value(-1D)),
p(1, value(Double.MAX_VALUE)),
p(1, value(Double.MIN_VALUE)),
p(1, value(Double.NaN)),
p(1, value(Double.NEGATIVE_INFINITY)),
p(1, value(Double.POSITIVE_INFINITY)),
p(1, value(Double.MAX_VALUE - 1D)),
p(91, arbDouble.gen)));
}
}));
/**
* An arbitrary implementation for float values.
*/
public static final Arbitrary arbFloat = arbitrary(arbDouble.gen.map(new F() {
public Float f(final Double d) {
return (float) d.doubleValue();
}
}));
/**
* An arbitrary implementation for float values that checks boundary values (0, 1, -1, max,
* min, NaN, -infinity, infinity, max - 1)
with a frequency of 1% each then generates from
* {@link #arbFloat} the remainder of the time (91%).
*/
public static final Arbitrary arbFloatBoundaries = arbitrary(sized(new F>() {
@SuppressWarnings("unchecked")
public Gen f(final Integer i) {
return frequency(list(p(1, value(0F)),
p(1, value(1F)),
p(1, value(-1F)),
p(1, value(Float.MAX_VALUE)),
p(1, value(Float.MIN_VALUE)),
p(1, value(Float.NaN)),
p(1, value(Float.NEGATIVE_INFINITY)),
p(1, value(Float.POSITIVE_INFINITY)),
p(1, value(Float.MAX_VALUE - 1F)),
p(91, arbFloat.gen)));
}
}));
/**
* An arbitrary implementation for string values.
*/
public static final Arbitrary arbString =
arbitrary(arbList(arbCharacter).gen.map(new F, String>() {
public String f(final List cs) {
return asString(cs);
}
}));
/**
* An arbitrary implementation for string values with characters in the US-ASCII range.
*/
public static final Arbitrary arbUSASCIIString =
arbitrary(arbList(arbCharacter).gen.map(new F, String>() {
public String f(final List cs) {
return asString(cs.map(new F() {
public Character f(final Character c) {
return (char) (c % 128);
}
}));
}
}));
/**
* An arbitrary implementation for string values with alpha-numeric characters.
*/
public static final Arbitrary arbAlphaNumString =
arbitrary(arbList(arbitrary(elements(range(charEnumerator, 'a', 'z').append(
range(charEnumerator, 'A', 'Z')).append(
range(charEnumerator, '0', '9')).toArray().array(Character[].class)))).gen.map(asString()));
/**
* An arbitrary implementation for string buffer values.
*/
public static final Arbitrary arbStringBuffer =
arbitrary(arbString.gen.map(new F() {
public StringBuffer f(final String s) {
return new StringBuffer(s);
}
}));
/**
* An arbitrary implementation for string builder values.
*/
public static final Arbitrary arbStringBuilder =
arbitrary(arbString.gen.map(new F() {
public StringBuilder f(final String s) {
return new StringBuilder(s);
}
}));
/**
* Returns an arbitrary implementation for generators.
*
* @param aa an arbitrary implementation for the type over which the generator is defined.
* @return An arbitrary implementation for generators.
*/
public static Arbitrary> arbGen(final Arbitrary aa) {
return arbitrary(sized(new F>>() {
@SuppressWarnings({"IfMayBeConditional"})
public Gen> f(final Integer i) {
if (i == 0)
return fail();
else
return aa.gen.map(new F>() {
public Gen f(final A a) {
return value(a);
}
}).resize(i - 1);
}
}));
}
/**
* Returns an arbitrary implementation for optional values.
*
* @param aa an arbitrary implementation for the type over which the optional value is defined.
* @return An arbitrary implementation for optional values.
*/
public static Arbitrary