
fj.test.Arbitrary Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionaljava-quickcheck_1.8 Show documentation
Show all versions of functionaljava-quickcheck_1.8 Show documentation
Functional Java is an open source library that supports closures for the Java programming language
The newest version!
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.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.Enumerator.charEnumerator;
import static fj.data.List.asString;
import static fj.data.List.list;
import fj.data.List;
import fj.data.Set;
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.*;
import static java.util.Locale.getAvailableLocales;
import static java.util.EnumSet.copyOf;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
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;
/**
* Common Gen helper functions.
*
* @version %build.number%
*/
public final class Arbitrary {
/**
* An arbitrary for functions.
*
* @param c The cogen for the function domain.
* @param a The arbitrary for the function codomain.
* @return An arbitrary for functions.
*/
public static Gen> arbF(final Cogen c, final Gen a) {
return promote(x -> c.cogen(x, a));
}
public static Gen> arbReader(Cogen aa, Gen ab) {
return arbF(aa, ab).map(Reader::unit);
}
/**
* An arbitrary for state.
*/
public static Gen> arbState(Gen as, Cogen cs, Gen aa) {
return arbF(cs, arbP2(as, aa)).map(State::unit);
}
/**
* An arbitrary for the LcgRng.
*/
public static Gen arbLcgRng() {
return Arbitrary.arbLong.map(LcgRng::new);
}
/**
* An arbitrary for functions.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for functions.
*/
public static Gen> arbFInvariant(final Gen a) {
return a.map(Function.constant());
}
/**
* An arbitrary for function-2.
*
* @param ca A cogen for the part of the domain of the function.
* @param cb A cogen 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 Gen> arbF2(final Cogen ca, final Cogen cb,
final Gen a) {
return arbF(ca, arbF(cb, a)).map(Function.uncurryF2());
}
/**
* An arbitrary for function-2.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-2.
*/
public static Gen> arbF2Invariant(final Gen a) {
return a.map(compose(Function.uncurryF2(), compose(Function.constant(), Function.constant())));
}
/**
* An arbitrary for function-3.
*
* @param ca A cogen for the part of the domain of the function.
* @param cb A cogen for the part of the domain of the function.
* @param cc A cogen 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 Gen> arbF3(final Cogen ca, final Cogen cb,
final Cogen cc, final Gen a) {
return arbF(ca, arbF(cb, arbF(cc, a))).map(Function.uncurryF3());
}
/**
* An arbitrary for function-3.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-3.
*/
public static Gen> arbF3Invariant(final Gen a) {
return a.map(compose(Function.uncurryF3(), compose(Function.constant(),
compose(
Function.constant(),
Function.constant()))));
}
/**
* An arbitrary for function-4.
*
* @param ca A cogen for the part of the domain of the function.
* @param cb A cogen for the part of the domain of the function.
* @param cc A cogen for the part of the domain of the function.
* @param cd A cogen 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 Gen> arbF4(final Cogen ca, final Cogen cb,
final Cogen cc, final Cogen cd,
final Gen a) {
return arbF(ca, arbF(cb, arbF(cc, arbF(cd, a)))).map(Function.uncurryF4());
}
/**
* An arbitrary for function-4.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-4.
*/
public static Gen> arbF4Invariant(final Gen a) {
return a.map(compose(Function.uncurryF4(),
compose(Function.constant(),
compose(Function.constant(),
compose(Function.constant(),
Function.constant())))));
}
/**
* An arbitrary for function-5.
*
* @param ca A cogen for the part of the domain of the function.
* @param cb A cogen for the part of the domain of the function.
* @param cc A cogen for the part of the domain of the function.
* @param cd A cogen for the part of the domain of the function.
* @param ce A cogen 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 Gen> arbF5(final Cogen ca,
final Cogen cb,
final Cogen cc,
final Cogen cd,
final Cogen ce,
final Gen a) {
return arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, a))))).map(Function.uncurryF5());
}
/**
* An arbitrary for function-5.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-5.
*/
public static Gen> arbF5Invariant(final Gen a) {
return a.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 cogen for the part of the domain of the function.
* @param cb A cogen for the part of the domain of the function.
* @param cc A cogen for the part of the domain of the function.
* @param cd A cogen for the part of the domain of the function.
* @param ce A cogen for the part of the domain of the function.
* @param cf A cogen 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 Gen> arbF6(final Cogen ca,
final Cogen cb,
final Cogen cc,
final Cogen cd,
final Cogen ce,
final Cogen cf,
final Gen a) {
return arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, a)))))).map(
Function.uncurryF6());
}
/**
* An arbitrary for function-6.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-6.
*/
public static Gen> arbF6Invariant(final Gen a) {
return a.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 cogen for the part of the domain of the function.
* @param cb A cogen for the part of the domain of the function.
* @param cc A cogen for the part of the domain of the function.
* @param cd A cogen for the part of the domain of the function.
* @param ce A cogen for the part of the domain of the function.
* @param cf A cogen for the part of the domain of the function.
* @param cg A cogen 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 Gen> arbF7(final Cogen ca,
final Cogen cb,
final Cogen cc,
final Cogen cd,
final Cogen ce,
final Cogen cf,
final Cogen cg,
final Gen a) {
return arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, arbF(cg, a))))))).map(
Function.uncurryF7());
}
/**
* An arbitrary for function-7.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-7.
*/
public static Gen> arbF7Invariant(final Gen a) {
return a.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 cogen for the part of the domain of the function.
* @param cb A cogen for the part of the domain of the function.
* @param cc A cogen for the part of the domain of the function.
* @param cd A cogen for the part of the domain of the function.
* @param ce A cogen for the part of the domain of the function.
* @param cf A cogen for the part of the domain of the function.
* @param cg A cogen for the part of the domain of the function.
* @param ch A cogen 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 Gen> arbF8(final Cogen ca,
final Cogen cb,
final Cogen cc,
final Cogen cd,
final Cogen ce,
final Cogen cf,
final Cogen cg,
final Cogen ch,
final Gen a) {
return arbF(ca, arbF(cb, arbF(cc, arbF(cd, arbF(ce, arbF(cf, arbF(cg, arbF(ch, a)))))))).map(
Function.uncurryF8());
}
/**
* An arbitrary for function-8.
*
* @param a The arbitrary for the function codomain.
* @return An arbitrary for function-8.
*/
public static Gen> arbF8Invariant(
final Gen a) {
return a.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 Gen arbBoolean = elements(true, false);
/**
* An arbitrary implementation for integer values.
*/
public static final Gen arbInteger = sized(i -> 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 Gen arbIntegerBoundaries = sized(i -> 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))));
/**
* An arbitrary implementation for long values.
*/
public static final Gen arbLong =
arbInteger.bind(arbInteger, i1 -> i2 -> (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 Gen arbLongBoundaries = sized(i -> 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))));
/**
* An arbitrary implementation for byte values.
*/
public static final Gen arbByte = arbInteger.map(i -> (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 Gen arbByteBoundaries = sized(i -> 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))));
/**
* An arbitrary implementation for short values.
*/
public static final Gen arbShort = arbInteger.map(i -> (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 Gen arbShortBoundaries = sized(i -> 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))));
/**
* An arbitrary implementation for character values.
*/
public static final Gen arbCharacter = choose(0, 65536).map(i -> (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 Gen arbCharacterBoundaries = sized(i -> 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))));
/**
* An arbitrary implementation for double values.
*/
public static final Gen arbDouble = sized(i -> 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 Gen arbDoubleBoundaries = sized(i -> 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))));
/**
* An arbitrary implementation for float values.
*/
public static final Gen arbFloat = arbDouble.map(d -> (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 Gen arbFloatBoundaries = sized(i -> 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))));
/**
* An arbitrary implementation for string values.
*/
public static final Gen arbString =
arbList(arbCharacter).map(List::asString);
/**
* An arbitrary implementation for string values with characters in the US-ASCII range.
*/
public static final Gen arbUSASCIIString =
arbList(arbCharacter).map(cs -> asString(cs.map(c -> (char) (c % 128))));
/**
* An arbitrary implementation for string values with alpha-numeric characters.
*/
public static final Gen arbAlphaNumString =
arbList(elements(range(charEnumerator, 'a', 'z').append(
range(charEnumerator, 'A', 'Z')).append(
range(charEnumerator, '0', '9')).toArray().array(Character[].class))).map(asString());
/**
* An arbitrary implementation for string buffer values.
*/
public static final Gen arbStringBuffer =
arbString.map(StringBuffer::new);
/**
* An arbitrary implementation for string builder values.
*/
public static final Gen arbStringBuilder =
arbString.map(StringBuilder::new);
/**
* 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 Gen> arbGen(final Gen aa) {
return sized(i -> {
if (i == 0)
return fail();
else
return aa.map(Gen::value).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 Gen