public int intBetween( int min, int max ) { return randoms.intBetween( min, max ); }
/** * @return next code point limited to the alpha numeric characters. */ private int alphaNumericCodePoint() { int nextInt = generator.nextInt( 4 ); if ( nextInt == 0 ) { return intBetween( 'A', 'Z' ); } else if ( nextInt == 1 ) { return intBetween( 'a', 'z' ); } else { //We want digits being roughly as frequent as letters return intBetween( '0', '9' ); } }
/** * Generate next code point that is valid for composition of a string. * Additional limitation on code point range is given by method argument. * * @param maxCodePoint the maximum code point to consider * @return A pseudorandom valid code point */ private int nextValidCodePoint( int maxCodePoint ) { int codePoint; int type; do { codePoint = intBetween( Character.MIN_CODE_POINT, maxCodePoint ); type = Character.getType( codePoint ); } while ( type == Character.UNASSIGNED || type == Character.PRIVATE_USE || type == Character.SURROGATE ); return codePoint; }
/** * @see RandomValues */ public int[] nextIntArrayRaw( int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); int[] ints = new int[length]; for ( int i = 0; i < length; i++ ) { ints[i] = generator.nextInt(); } return ints; }
private char[] nextCharArrayRaw( int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); char[] array = new char[length]; for ( int i = 0; i < length; i++ ) { array[i] = nextCharRaw(); } return array; }
private String nextStringRaw( int minStringLength, int maxStringLength, CodePointFactory codePointFactory ) { int length = intBetween( minStringLength, maxStringLength ); StringBuilder sb = new StringBuilder( length ); for ( int i = 0; i < length; i++ ) { sb.appendCodePoint( codePointFactory.generate() ); } return sb.toString(); }
/** * @see RandomValues */ public float[] nextFloatArrayRaw( int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); float[] floats = new float[length]; for ( int i = 0; i < length; i++ ) { floats[i] = generator.nextFloat(); } return floats; }
/** * @see RandomValues */ public double[] nextDoubleArrayRaw( int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); double[] doubles = new double[length]; for ( int i = 0; i < length; i++ ) { doubles[i] = nextDouble(); } return doubles; }
/** * @see RandomValues */ public boolean[] nextBooleanArrayRaw( int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); boolean[] booleans = new boolean[length]; for ( int i = 0; i < length; i++ ) { booleans[i] = generator.nextBoolean(); } return booleans; }
/** * @see RandomValues */ public long[] nextLongArrayRaw( int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); long[] longs = new long[length]; for ( int i = 0; i < length; i++ ) { longs[i] = generator.nextLong(); } return longs; }
/** * @see RandomValues */ public short[] nextShortArrayRaw( int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); short[] shorts = new short[length]; for ( int i = 0; i < length; i++ ) { shorts[i] = (short) generator.nextInt(); } return shorts; }
/** * Create an randomly sized array filled with elements provided by factory. * * @param arrayFactory creates array with length equal to provided argument. * @param elementFactory generating random values of some type. * @param minLength minimum length of array (inclusive). * @param maxLength maximum length of array (inclusive). * @param <T> Generic type of elements in array. * @return a new array created by arrayFactory, filled with elements created by elementFactory. */ private <T> T[] nextArray( IntFunction<T[]> arrayFactory, ElementFactory<T> elementFactory, int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); T[] array = arrayFactory.apply( length ); for ( int i = 0; i < length; i++ ) { array[i] = elementFactory.generate(); } return array; }
/** * @see RandomValues */ public byte[] nextByteArrayRaw( int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); byte[] bytes = new byte[length]; int index = 0; while ( index < length ) { //For each random int we get up to four random bytes int rand = nextInt(); int numBytesToShift = Math.min( length - index, Integer.BYTES ); //byte 4 byte 3 byte 2 byte 1 //aaaaaaaa bbbbbbbb cccccccc dddddddd while ( numBytesToShift > 0 ) { bytes[index++] = (byte) rand; numBytesToShift--; rand >>= Byte.SIZE; } } return bytes; }
private TextValue nextTextValue( int minLength, int maxLength, CodePointFactory codePointFactory ) { // todo should we generate UTF8StringValue or StringValue? Or maybe both? Randomly? int length = intBetween( minLength, maxLength ); UTF8StringValueBuilder builder = new UTF8StringValueBuilder( nextPowerOf2( length ) ); for ( int i = 0; i < length; i++ ) { builder.addCodePoint( codePointFactory.generate() ); } return builder.build(); }
@Override public Generator<PropertyRecord> property() { return ( recordSize, format, recordId ) -> { PropertyRecord record = new PropertyRecord( recordId ); int maxProperties = random.intBetween( 1, 4 ); StandaloneDynamicRecordAllocator stringAllocator = new StandaloneDynamicRecordAllocator(); StandaloneDynamicRecordAllocator arrayAllocator = new StandaloneDynamicRecordAllocator(); record.setInUse( true ); int blocksOccupied = 0; for ( int i = 0; i < maxProperties && blocksOccupied < 4; ) { PropertyBlock block = new PropertyBlock(); // Dynamic records will not be written and read by the property record format, // that happens in the store where it delegates to a "sub" store. PropertyStore.encodeValue( block, random.nextInt( tokenBits ), random.nextValue(), stringAllocator, arrayAllocator, true ); int tentativeBlocksWithThisOne = blocksOccupied + block.getValueBlocks().length; if ( tentativeBlocksWithThisOne <= 4 ) { record.addPropertyBlock( block ); blocksOccupied = tentativeBlocksWithThisOne; } } record.setPrevProp( randomLongOrOccasionallyNull( propertyBits ) ); record.setNextProp( randomLongOrOccasionallyNull( propertyBits ) ); return record; }; }
private char[] nextCharArrayRaw( int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); char[] array = new char[length]; for ( int i = 0; i < length; i++ ) { array[i] = nextCharRaw(); } return array; }
/** * @see RandomValues */ public int[] nextIntArrayRaw( int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); int[] ints = new int[length]; for ( int i = 0; i < length; i++ ) { ints[i] = generator.nextInt(); } return ints; }
/** * @see RandomValues */ public boolean[] nextBooleanArrayRaw( int minLength, int maxLength ) { int length = intBetween( minLength, maxLength ); boolean[] booleans = new boolean[length]; for ( int i = 0; i < length; i++ ) { booleans[i] = generator.nextBoolean(); } return booleans; }
private String nextStringRaw( int minStringLength, int maxStringLength, CodePointFactory codePointFactory ) { int length = intBetween( minStringLength, maxStringLength ); StringBuilder sb = new StringBuilder( length ); for ( int i = 0; i < length; i++ ) { sb.appendCodePoint( codePointFactory.generate() ); } return sb.toString(); }
private TextValue nextTextValue( int minLength, int maxLength, CodePointFactory codePointFactory ) { // todo should we generate UTF8StringValue or StringValue? Or maybe both? Randomly? int length = intBetween( minLength, maxLength ); UTF8StringValueBuilder builder = new UTF8StringValueBuilder( nextPowerOf2( length ) ); for ( int i = 0; i < length; i++ ) { builder.addCodePoint( codePointFactory.generate() ); } return builder.build(); }