fj.test.Coarbitrary Maven / Gradle / Ivy
package fj.test;
import fj.*;
import static fj.Function.curry;
import static fj.P.p;
import fj.data.*;
import static fj.data.Array.array;
import static fj.data.List.fromString;
import static fj.data.List.nil;
import static fj.test.Variant.variant;
import static java.lang.Double.doubleToRawLongBits;
import static java.lang.Float.floatToRawIntBits;
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.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 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;
/**
* Transforms a type and a generator to produce a new generator. This function is used to generate
* {@link Arbitrary arbitrary} functions.
*
* @version %build.number%
*/
public abstract class Coarbitrary {
/**
* Transforms the given value and generator to a new generator with a high probability of being
* independent.
*
* @param a The value to produce the generator from.
* @param g The generator to produce the new generator from.
* @return A new generator with a high probability of being independent.
*/
public abstract Gen coarbitrary(A a, Gen g);
/**
* A curried version of {@link #coarbitrary(Object, Gen)}.
*
* @param a The value to produce the generator from.
* @return A curried version of {@link #coarbitrary(Object, Gen)}.
*/
public final F, Gen> coarbitrary(final A a) {
return new F, Gen>() {
public Gen f(final Gen g) {
return coarbitrary(a, g);
}
};
}
/**
* Composes the given function with this coarbitrary to produce a new coarbitrary.
*
* @param f The function to compose.
* @return A new coarbitrary composed with the given function.
*/
public final Coarbitrary compose(final F f) {
return new Coarbitrary() {
public Gen coarbitrary(final B b, final Gen g) {
return Coarbitrary.this.coarbitrary(f.f(b), g);
}
};
}
/**
* Contra-maps this coarbitrary using the given function.
*
* @param f The function to co-map with.
* @return A contra-mapped coarbitrary.
*/
public final Coarbitrary contramap(final F f) {
return new Coarbitrary() {
public Gen coarbitrary(final B b, final Gen g) {
return Coarbitrary.this.coarbitrary(f.f(b), g);
}
};
}
/**
* A coarbitrary for a function.
*
* @param a An arbitrary for the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function.
*/
public static Coarbitrary> coarbF(final Arbitrary a, final Coarbitrary c) {
return new Coarbitrary>() {
public Gen coarbitrary(final F f, final Gen g) {
return a.gen.bind(new F>() {
public Gen f(final A a) {
return c.coarbitrary(f.f(a), g);
}
});
}
};
}
/**
* A coarbitrary for a function-2.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-2.
*/
public static Coarbitrary> coarbF2(final Arbitrary aa, final Arbitrary ab,
final Coarbitrary c) {
return new Coarbitrary>() {
public Gen coarbitrary(final F2 f, final Gen g) {
return coarbF(aa, coarbF(ab, c)).coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-3.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-3.
*/
public static Coarbitrary> coarbF3(final Arbitrary aa, final Arbitrary ab,
final Arbitrary ac, final Coarbitrary c) {
return new Coarbitrary>() {
public Gen coarbitrary(final F3 f, final Gen g) {
return coarbF(aa, coarbF(ab, coarbF(ac, c))).coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-4.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param ad An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-4.
*/
public static Coarbitrary> coarbF4(final Arbitrary aa, final Arbitrary ab,
final Arbitrary ac, final Arbitrary ad,
final Coarbitrary c) {
return new Coarbitrary>() {
public Gen coarbitrary(final F4 f, final Gen g) {
return coarbF(aa, coarbF(ab, coarbF(ac, coarbF(ad, c)))).coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-5.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param ad An arbitrary for part of the domain of the function.
* @param ae An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-5.
*/
public static Coarbitrary> coarbF5(final Arbitrary aa,
final Arbitrary ab,
final Arbitrary ac,
final Arbitrary ad,
final Arbitrary ae,
final Coarbitrary c) {
return new Coarbitrary>() {
public Gen coarbitrary(final F5 f, final Gen g) {
return coarbF(aa, coarbF(ab, coarbF(ac, coarbF(ad, coarbF(ae, c))))).coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-6.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param ad An arbitrary for part of the domain of the function.
* @param ae An arbitrary for part of the domain of the function.
* @param af An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-6.
*/
public static Coarbitrary> coarbF6(final Arbitrary aa,
final Arbitrary ab,
final Arbitrary ac,
final Arbitrary ad,
final Arbitrary ae,
final Arbitrary af,
final Coarbitrary c) {
return new Coarbitrary>() {
public Gen coarbitrary(final F6 f, final Gen g) {
return coarbF(aa, coarbF(ab, coarbF(ac, coarbF(ad, coarbF(ae, coarbF(af, c)))))).coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-7.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param ad An arbitrary for part of the domain of the function.
* @param ae An arbitrary for part of the domain of the function.
* @param af An arbitrary for part of the domain of the function.
* @param ag An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-7.
*/
public static Coarbitrary> coarbF7(final Arbitrary aa,
final Arbitrary ab,
final Arbitrary ac,
final Arbitrary ad,
final Arbitrary ae,
final Arbitrary af,
final Arbitrary ag,
final Coarbitrary c) {
return new Coarbitrary>() {
public Gen coarbitrary(final F7 f, final Gen g) {
return coarbF(aa, coarbF(ab, coarbF(ac, coarbF(ad, coarbF(ae, coarbF(af, coarbF(ag, c)))))))
.coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for a function-8.
*
* @param aa An arbitrary for part of the domain of the function.
* @param ab An arbitrary for part of the domain of the function.
* @param ac An arbitrary for part of the domain of the function.
* @param ad An arbitrary for part of the domain of the function.
* @param ae An arbitrary for part of the domain of the function.
* @param af An arbitrary for part of the domain of the function.
* @param ag An arbitrary for part of the domain of the function.
* @param ah An arbitrary for part of the domain of the function.
* @param c A coarbitrary for the codomain of the function.
* @return A coarbitrary for a function-8.
*/
public static Coarbitrary> coarbF8(final Arbitrary aa,
final Arbitrary ab,
final Arbitrary ac,
final Arbitrary ad,
final Arbitrary ae,
final Arbitrary af,
final Arbitrary ag,
final Arbitrary ah,
final Coarbitrary c) {
return new Coarbitrary>() {
public Gen coarbitrary(final F8 f, final Gen g) {
return coarbF(aa, coarbF(ab, coarbF(ac, coarbF(ad, coarbF(ae, coarbF(af, coarbF(ag, coarbF(ah, c))))))))
.coarbitrary(curry(f), g);
}
};
}
/**
* A coarbitrary for booleans.
*/
public static final Coarbitrary coarbBoolean = new Coarbitrary() {
public Gen coarbitrary(final Boolean b, final Gen g) {
return variant(b ? 0 : 1, g);
}
};
/**
* A coarbitrary for integers.
*/
public static final Coarbitrary coarbInteger = new Coarbitrary() {
public Gen coarbitrary(final Integer i, final Gen g) {
return variant(i >= 0 ? 2 * i : -2 * i + 1, g);
}
};
/**
* A coarbitrary for bytes.
*/
public static final Coarbitrary coarbByte = new Coarbitrary() {
public Gen coarbitrary(final Byte b, final Gen g) {
return variant(b >= 0 ? 2 * b : -2 * b + 1, g);
}
};
/**
* A coarbitrary for shorts.
*/
public static final Coarbitrary coarbShort = new Coarbitrary() {
public Gen coarbitrary(final Short s, final Gen g) {
return variant(s >= 0 ? 2 * s : -2 * s + 1, g);
}
};
/**
* A coarbitrary for longs.
*/
public static final Coarbitrary coarbLong = new Coarbitrary() {
public Gen coarbitrary(final Long l, final Gen g) {
return variant(l >= 0L ? 2L * l : -2L * l + 1L, g);
}
};
/**
* A coarbitrary for characters.
*/
public static final Coarbitrary coarbCharacter = new Coarbitrary() {
public Gen coarbitrary(final Character c, final Gen g) {
return variant(c << 1, g);
}
};
/**
* A coarbitrary for floats.
*/
public static final Coarbitrary coarbFloat = new Coarbitrary() {
public Gen coarbitrary(final Float f, final Gen g) {
return coarbInteger.coarbitrary(floatToRawIntBits(f), g);
}
};
/**
* A coarbitrary for doubles.
*/
public static final Coarbitrary coarbDouble = new Coarbitrary() {
public Gen coarbitrary(final Double d, final Gen g) {
return coarbLong.coarbitrary(doubleToRawLongBits(d), g);
}
};
/**
* A coarbitrary for the optional value.
*
* @param ca A coarbitrary for the type of the optional value.
* @return A coarbitrary for the optional value.
*/
public static Coarbitrary