This is an automated email from the ASF dual-hosted git repository.

bdeggleston pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/cassandra-accord.git


The following commit(s) were added to refs/heads/trunk by this push:
     new ef36616  add uniqueBestEffort to Gens
ef36616 is described below

commit ef36616441bd4ff4fec5379d986c75ad5a62ff7d
Author: Blake Eggleston <bl...@ultrablake.com>
AuthorDate: Thu Mar 28 10:22:54 2024 -0700

    add uniqueBestEffort to Gens
---
 accord-core/src/test/java/accord/utils/Gens.java | 59 ++++++++++++++++++++----
 1 file changed, 50 insertions(+), 9 deletions(-)

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 @@ public class Gens {
         @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 @@ public class Gens {
                 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 @@ public class Gens {
         @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 @@ public class Gens {
         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 @@ public class Gens {
 
         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 @@ public class Gens {
 
         private LongGenReset(Gen.LongGen fn)
         {
-            this.base = new GenReset<>(fn);
+            this.base = new GenReset<>(fn, false);
         }
         @Override
         public long nextLong(RandomSource random) {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org

Reply via email to