ifesdjeen commented on code in PR #3689:
URL: https://github.com/apache/cassandra/pull/3689#discussion_r1850383500


##########
test/harry/main/org/apache/cassandra/harry/gen/Generators.java:
##########
@@ -18,20 +18,338 @@
 
 package org.apache.cassandra.harry.gen;
 
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.UUID;
 import java.util.function.Supplier;
 
-import accord.utils.Gen;
+import org.apache.cassandra.harry.gen.rng.JdkRandomEntropySource;
+import org.apache.cassandra.harry.util.BitSet;
 import org.apache.cassandra.locator.InetAddressAndPort;
+import org.apache.cassandra.utils.TimeUUID;
 
 public class Generators
 {
+    /**
+     * Create a generator that will produce _at most_ N values.
+     *
+     * Since type T may contain less entropy than requested population, it is
+     * not guaranteed to produce exactly N values.
+     */
+    public static <T> Generator<T> population(Generator<T> gen, int population)
+    {
+        return new Generator<T>()
+        {
+            final long[] seeds = new long[population];
+            final EntropySource local = new JdkRandomEntropySource(0L);
+            boolean initialized = false;
+            @Override
+            public T generate(EntropySource rng)
+            {
+                // Generate a fixed number of seeds
+                if (!initialized)
+                {
+                    for (int i = 0; i < population; i++)
+                        seeds[i] = rng.next();
+                    initialized = true;
+                }
+
+                // pick a seed to generate an item from
+                local.seed(seeds[rng.nextInt(0, population)]);
+
+                return gen.generate(local);
+            }
+        };
+    }
+
+    public static Generator<BitSet> bitSet(int size)
+    {
+        return new Generator<BitSet>()
+        {
+            public BitSet generate(EntropySource rng)
+            {
+                BitSet bitSet = BitSet.allUnset(size);
+                for (int i = 0; i < size; i++)
+                    if (rng.nextBoolean())
+                        bitSet.set(i);
+                return bitSet;
+            }
+        };
+    }
+
+    public static Generator<String> ascii(int minLength, int maxLength)
+    {
+        return new StringGenerator(minLength, maxLength, 0, 127);
+    }
+
+    public static Generator<String> utf8(int minLength, int maxLength)
+    {
+        return rng -> {
+            int length = rng.nextInt(minLength, maxLength);
+            int[] codePoints = new int[length];
+            for (int i = 0; i < length; i++)
+            {
+                int next;
+                // Exclude surrogate range, generate values before and after it
+                if (rng.nextBoolean())
+                    next = rng.nextInt(0x0000, 0xD800);
+                else
+                    next = rng.nextInt(0xD801, 0xDFFF);
+                codePoints[i] = next;
+            }
+
+            return new String(codePoints, 0, codePoints.length);
+        };
+    }
+
+    public static Generator<String> englishAlphabet(int minLength, int 
maxLength)
+    {
+        return new StringGenerator(minLength, maxLength, 97, 122);
+    }
+
+    public static Generator<Byte> int8()
+    {
+        return rng -> (byte) rng.nextInt();
+    }
+
+    public static Generator<Short> int16()
+    {
+        return rng -> (short) rng.nextInt();
+    }
+
+    public static Generator<Integer> int32()
+    {
+        return EntropySource::nextInt;
+    }
+
+    public static Generator<Integer> int32(int min, int max)
+    {
+        return rng -> rng.nextInt(min, max);
+    }
+
+    public static Generator<Long> int64()
+    {
+        return new LongGenerator();
+    }
+
+    public static Generator<Long> int64(long min, long max)
+    {
+        return rng -> rng.nextLong(min, max);
+    }
+
+    public static Generator<Boolean> bool()
+    {
+        return EntropySource::nextBoolean;
+    }
+
+    public static Generator<Double> doubles()
+    {
+        return EntropySource::nextDouble;
+    }
+
+    public static Generator<Float> floats()
+    {
+        return EntropySource::nextFloat;
+    }
+
+    public static Generator<InetAddress> inetAddr()
+    {
+        return new InetAddressGenerator();
+    }
+
+    public static <T> Generator<T> inetAddr(Generator<T> delegate)
+    {
+        return new UniqueGenerator<>(delegate, 10);
+    }
+
+    public static Generator<ByteBuffer> bytes(int minSize, int maxSize)
+    {
+        return rng -> {
+            int size = rng.nextInt(minSize, maxSize);
+            byte[] bytes = new byte[size];
+            for (int i = 0; i < size; )
+                for (long v = rng.next(),
+                     n = Math.min(size - i, Long.SIZE / Byte.SIZE);
+                     n-- > 0; v >>= Byte.SIZE)
+                    bytes[i++] = (byte) v;
+            return ByteBuffer.wrap(bytes);
+        };
+    }
+
+    public static Generator<UUID> uuidGen()
+    {
+        return rng -> new UUID(rng.next(), rng.next());
+    }
+
+    public static Generator<BigInteger> bigInt()
+    {
+        return rng -> BigInteger.valueOf(rng.next());
+    }
+
+    public static Generator<BigDecimal> bigDecimal()
+    {
+        return rng -> BigDecimal.valueOf(rng.next());
+    }
+
+    public static Generator<TimeUUID> timeuuid()
+    {
+        return new Generator<TimeUUID>()
+        {
+            public TimeUUID generate(EntropySource rng)
+            {
+                return TimeUUID.atUnixMicrosWithLsb(rng.nextLong(0, 
Long.MAX_VALUE),
+                                                    makeClockSeqAndNode(rng));
+            }
+
+            private long makeClockSeqAndNode(EntropySource rng)
+            {
+                long clock = rng.nextInt();
+
+                long lsb = 0;
+                lsb |= 0x8000000000000000L;                 // variant (2 bits)
+                lsb |= (clock & 0x0000000000003FFFL) << 48; // clock sequence 
(14 bits)
+                lsb |= makeNode(rng);                       // 6 bytes
+                return lsb;
+            }
+
+            private long makeNode(EntropySource rng)
+            {
+                // ideally, we'd use the MAC address, but java doesn't expose 
that.
+                long v = rng.next();
+                byte[] hash = new byte[] { (byte) (0xff & v),
+                                           (byte) (0xff & (v << 8)),
+                                           (byte) (0xff & (v << 16)),
+                                           (byte) (0xff & (v << 24)),
+                                           (byte) (0xff & (v << 32)),
+                                           (byte) (0xff & (v << 40))
+                };
+                long node = 0;
+                for (int i = 0; i < 6; i++)
+                    node |= (0x00000000000000ff & (long)hash[i]) << (5-i)*8;
+                assert (0xff00000000000000L & node) == 0;
+
+                return node | 0x0000010000000000L;
+            }
+        };
+    }
+
+    public static <T> TrackingGenerator<T> tracking(Generator<T> delegate)
+    {
+        return new TrackingGenerator<>(delegate);
+    }
+
+    public static class TrackingGenerator<T> implements Generator<T>
+    {
+        private final Set<T> generated;
+        private final Generator<T> delegate;
+        public TrackingGenerator(Generator<T> delegate)
+        {
+            this.generated = new HashSet<>();
+            this.delegate = delegate;
+        }
+
+        public Iterable<T> generated()
+        {
+            return generated;
+        }
+
+        @Override
+        public T generate(EntropySource rng)
+        {
+            T next = delegate.generate(rng);
+            generated.add(next);
+            return next;
+        }
+    }
+
+    public static <T> Generator<T> unique(Generator<T> delegate)

Review Comment:
   Removed `population`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to