add uniqueBestEffort to Gens
diff --git a/accord-core/src/test/java/accord/utils/Gens.java b/accord-core/src/test/java/accord/utils/Gens.java
index 4b1a210..35e4567 100644
--- a/accord-core/src/test/java/accord/utils/Gens.java
+++ b/accord-core/src/test/java/accord/utils/Gens.java
@@ -399,7 +399,12 @@
@Override
public ListDSL<T> unique()
{
- return new ListDSL<>(new GenReset<>(fn));
+ return new ListDSL<>(new GenReset<>(fn, false));
+ }
+
+ public ListDSL<T> uniqueBestEffort()
+ {
+ return new ListDSL<>(new GenReset<>(fn, true));
}
@Override
@@ -411,7 +416,16 @@
int size = sizeGen.nextInt(r);
List<T> list = new ArrayList<>(size);
for (int i = 0; i < size; i++)
- list.add(fn.next(r));
+ {
+ try
+ {
+ list.add(fn.next(r));
+ }
+ catch (IgnoreGenResult e)
+ {
+ // ignore
+ }
+ }
return list;
};
}
@@ -429,7 +443,12 @@
@Override
public ArrayDSL<T> unique()
{
- return new ArrayDSL<>(type, new GenReset<>(fn));
+ return new ArrayDSL<>(type, new GenReset<>(fn, false));
+ }
+
+ public ArrayDSL<T> uniqueBestEffort()
+ {
+ return new ArrayDSL<>(type, new GenReset<>(fn, true));
}
@Override
@@ -522,22 +541,44 @@
void reset();
}
+ private static final class IgnoreGenResult extends RuntimeException
+ {
+ private static final IgnoreGenResult INSTANCE = new IgnoreGenResult();
+ private IgnoreGenResult()
+ {
+ super(null, null, false, false);
+ }
+ }
+
private static class GenReset<T> implements Gen<T>, Reset
{
private final Set<T> seen = new HashSet<>();
private final Gen<T> fn;
+ private final boolean bestEffort;
- private GenReset(Gen<T> fn)
+ private GenReset(Gen<T> fn, boolean bestEffort)
{
this.fn = fn;
+ this.bestEffort = bestEffort;
}
@Override
public T next(RandomSource random)
{
- T value;
- while (!seen.add((value = fn.next(random)))) {}
- return value;
+ if (!bestEffort)
+ {
+ T value;
+ while (!seen.add((value = fn.next(random)))) {}
+ return value;
+ }
+ else
+ {
+ T value = null;
+ int i;
+ for (i = 0; i < 42 && !seen.add((value = fn.next(random))); i++) {}
+ if (i == 42) throw IgnoreGenResult.INSTANCE;
+ return value;
+ }
}
@Override
@@ -553,7 +594,7 @@
private IntGenReset(Gen.IntGen fn)
{
- this.base = new GenReset<>(fn);
+ this.base = new GenReset<>(fn, false);
}
@Override
public int nextInt(RandomSource random) {
@@ -572,7 +613,7 @@
private LongGenReset(Gen.LongGen fn)
{
- this.base = new GenReset<>(fn);
+ this.base = new GenReset<>(fn, false);
}
@Override
public long nextLong(RandomSource random) {