org.hamcrest
hamcrest-core
diff --git a/community/random-values/src/main/java/org/neo4j/values/Generator.java b/community/random-values/src/main/java/org/neo4j/values/Generator.java
deleted file mode 100644
index 4ae5f062ae04a..0000000000000
--- a/community/random-values/src/main/java/org/neo4j/values/Generator.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright (c) 2002-2018 "Neo Technology,"
- * Network Engine for Objects in Lund AB [http://neotechnology.com]
- *
- * This file is part of Neo4j.
- *
- * Neo4j is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
- */
-package org.neo4j.values;
-
-/**
- * This class is meant as a gap so that we don't need a direct dependency on a random number generator.
- *
- * For example by wrapping in a generator we can support both {@code java.util.Random} and {@code java.util
- * .SplittableRandom}
- */
-public interface Generator
-{
- /**
- * Return a pseudorandom normally distributed long
- * @return a pseudorandom normally distributed long
- */
- long nextLong();
-
- /**
- * Return a pseudorandom normally distributed boolean
- * @return a pseudorandom normally distributed boolean
- */
- boolean nextBoolean();
-
- /**
- * Return a pseudorandom normally distributed int
- * @return a pseudorandom normally distributed int
- */
- int nextInt();
-
- /**
- * Return a pseudorandom normally distributed long between 0 (inclusive) and the given bound(exlusive)
- * @param bound the exclusive upper bound for the number generation
- * @return a pseudorandom normally distributed int
- */
- int nextInt( int bound );
-
- /**
- * Return a pseudorandom normally distributed float
- * @return a pseudorandom normally distributed float
- */
- float nextFloat();
-
- /**
- * Return a pseudorandom normally distributed double
- * @return a pseudorandom normally distributed double
- */
- double nextDouble();
-}
diff --git a/community/random-values/src/main/java/org/neo4j/values/RandomGenerator.java b/community/random-values/src/main/java/org/neo4j/values/RandomGenerator.java
deleted file mode 100644
index cc24f9d26c9cb..0000000000000
--- a/community/random-values/src/main/java/org/neo4j/values/RandomGenerator.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright (c) 2002-2018 "Neo Technology,"
- * Network Engine for Objects in Lund AB [http://neotechnology.com]
- *
- * This file is part of Neo4j.
- *
- * Neo4j is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
- */
-package org.neo4j.values;
-
-import java.util.Random;
-
-public class RandomGenerator implements Generator
-{
- private final Random random;
-
- public RandomGenerator( Random random )
- {
- this.random = random;
- }
-
- @Override
- public long nextLong()
- {
- return random.nextLong();
- }
-
- @Override
- public boolean nextBoolean()
- {
- return random.nextBoolean();
- }
-
- @Override
- public int nextInt()
- {
- return random.nextInt();
- }
-
- @Override
- public int nextInt( int bound )
- {
- return random.nextInt( bound );
- }
-
- @Override
- public float nextFloat()
- {
- return random.nextFloat();
- }
-
- @Override
- public double nextDouble()
- {
- return random.nextDouble();
- }
-}
diff --git a/community/random-values/src/main/java/org/neo4j/values/SplittableRandomGenerator.java b/community/random-values/src/main/java/org/neo4j/values/SplittableRandomGenerator.java
deleted file mode 100644
index 5e1bf70784840..0000000000000
--- a/community/random-values/src/main/java/org/neo4j/values/SplittableRandomGenerator.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * Copyright (c) 2002-2018 "Neo Technology,"
- * Network Engine for Objects in Lund AB [http://neotechnology.com]
- *
- * This file is part of Neo4j.
- *
- * Neo4j is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
- */
-package org.neo4j.values;
-
-import java.util.SplittableRandom;
-
-public class SplittableRandomGenerator implements Generator
-{
- private final SplittableRandom random;
-
- public SplittableRandomGenerator( SplittableRandom random )
- {
- this.random = random;
- }
-
- @Override
- public long nextLong()
- {
- return random.nextLong();
- }
-
- @Override
- public boolean nextBoolean()
- {
- return random.nextBoolean();
- }
-
- @Override
- public int nextInt()
- {
- return random.nextInt();
- }
-
- @Override
- public int nextInt( int bound )
- {
- return random.nextInt( bound );
- }
-
- @Override
- public float nextFloat()
- {
- //this is a safe cast since nextDouble returns values in [0,1.0)
- return (float) random.nextDouble();
- }
-
- @Override
- public double nextDouble()
- {
- return random.nextDouble();
- }
-}
diff --git a/community/random-values/src/main/java/org/neo4j/values/storable/RandomValues.java b/community/random-values/src/main/java/org/neo4j/values/storable/RandomValues.java
index f4cb34b34c9ed..a596f9b1cf227 100644
--- a/community/random-values/src/main/java/org/neo4j/values/storable/RandomValues.java
+++ b/community/random-values/src/main/java/org/neo4j/values/storable/RandomValues.java
@@ -19,6 +19,7 @@
*/
package org.neo4j.values.storable;
+import java.lang.reflect.Array;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
@@ -34,10 +35,6 @@
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
-import org.neo4j.values.Generator;
-import org.neo4j.values.RandomGenerator;
-import org.neo4j.values.SplittableRandomGenerator;
-
import static java.lang.Math.abs;
import static java.time.LocalDate.ofEpochDay;
import static java.time.LocalDateTime.ofInstant;
@@ -126,6 +123,8 @@ public int arrayMaxLength()
}
}
+ public static Configuration DEFAULT_CONFIGURATION = new Default();
+
private final Generator generator;
private final Configuration configuration;
@@ -133,7 +132,7 @@ public int arrayMaxLength()
private RandomValues( Generator generator )
{
- this( generator, new Default() );
+ this( generator, DEFAULT_CONFIGURATION );
}
private RandomValues( Generator generator, Configuration configuration )
@@ -250,6 +249,16 @@ public BooleanValue nextBooleanValue()
return Values.booleanValue( generator.nextBoolean() );
}
+ /**
+ * Returns the next pseudorandom uniformly distributed {@code boolean}
+ *
+ * @return the next pseudorandom uniformly distributed {@code boolean}
+ */
+ public boolean nextBoolean()
+ {
+ return generator.nextBoolean();
+ }
+
/**
* Returns the next pseudorandom uniformly distributed {@link IntValue}
*
@@ -260,6 +269,16 @@ public IntValue nextIntValue()
return intValue( generator.nextInt() );
}
+ /**
+ * Returns the next pseudorandom uniformly distributed {@code int}
+ *
+ * @return the next pseudorandom uniformly distributed {@code int}
+ */
+ public int nextInt()
+ {
+ return generator.nextInt();
+ }
+
/**
* Returns the next pseudorandom uniformly distributed {@link IntValue} between 0 (inclusive) and the specified
* value (exclusive)
@@ -333,7 +352,8 @@ public ByteValue nextByteValue( byte bound )
}
/**
- * Returns the next pseudorandom uniformly distributed {@link FloatValue}
+ * Returns the next pseudorandom uniformly distributed {@link FloatValue} between 0 (inclusive) and the specified
+ * 1.0 (exclusive)
*
* @return the next pseudorandom uniformly distributed {@link FloatValue}
*/
@@ -342,6 +362,17 @@ public FloatValue nextFloatValue()
return floatValue( generator.nextFloat() );
}
+ /**
+ * Returns the next pseudorandom uniformly distributed {@code float} between 0 (inclusive) and the specified
+ * 1.0 (exclusive)
+ *
+ * @return the next pseudorandom uniformly distributed {@code float}
+ */
+ public float nextFloat()
+ {
+ return generator.nextFloat();
+ }
+
/**
* Returns the next pseudorandom uniformly distributed {@link DoubleValue}
*
@@ -419,9 +450,9 @@ public TextValue nextDigitString( int minLength, int maxLength )
*
* @return a {@link TextValue} consisting only of ascii alphabetic characters.
*/
- public TextValue nextAlphaString()
+ public TextValue nextAlphaTextValue()
{
- return nextAlphaString( configuration.stringMinLength(), configuration.stringMaxLength() );
+ return nextAlphaTextValue( configuration.stringMinLength(), configuration.stringMaxLength() );
}
/**
@@ -431,7 +462,7 @@ public TextValue nextAlphaString()
* @param maxLength the maximum length of the string
* @return a {@link TextValue} consisting only of ascii alphabetic characters.
*/
- public TextValue nextAlphaString( int minLength, int maxLength )
+ public TextValue nextAlphaTextValue( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
byte[] bytes = new byte[length];
@@ -458,9 +489,9 @@ public TextValue nextAlphaString( int minLength, int maxLength )
*
* @return a {@link TextValue} consisting only of ascii alphabetic and numerical characters.
*/
- public TextValue nextAlphaNumericString()
+ public TextValue nextAlphaNumericTextValue()
{
- return nextAlphaNumericString( configuration.stringMinLength(), configuration.stringMaxLength() );
+ return nextAlphaNumericTextValue( configuration.stringMinLength(), configuration.stringMaxLength() );
}
/**
@@ -470,7 +501,7 @@ public TextValue nextAlphaNumericString()
* @param maxLength the maximum length of the string
* @return a {@link TextValue} consisting only of ascii alphabetic and numerical characters.
*/
- public TextValue nextAlphaNumericString( int minLength, int maxLength )
+ public TextValue nextAlphaNumericTextValue( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
byte[] bytes = new byte[length];
@@ -506,9 +537,9 @@ public TextValue nextAlphaNumericString( int minLength, int maxLength )
*
* @return a {@link TextValue} consisting only of ascii characters.
*/
- public TextValue nextAsciiString()
+ public TextValue nextAsciiTextValue()
{
- return nextAsciiString( configuration.stringMinLength(), configuration.stringMaxLength() );
+ return nextAsciiTextValue( configuration.stringMinLength(), configuration.stringMaxLength() );
}
/**
@@ -518,7 +549,7 @@ public TextValue nextAsciiString()
* @param maxLength the maximum length of the string
* @return a {@link TextValue} consisting only of ascii characters.
*/
- public TextValue nextAsciiString( int minLength, int maxLength )
+ public TextValue nextAsciiTextValue( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
byte[] bytes = new byte[length];
@@ -538,9 +569,9 @@ public TextValue nextAsciiString( int minLength, int maxLength )
*
* @return a {@link TextValue} consisting only of printable ascii characters.
*/
- public TextValue nextPrintableAsciiString()
+ public TextValue nextPrintableAsciiTextValue()
{
- return nextPrintableAsciiString( configuration.stringMinLength(), configuration.stringMaxLength() );
+ return nextPrintableAsciiTextValue( configuration.stringMinLength(), configuration.stringMaxLength() );
}
/**
@@ -550,7 +581,7 @@ public TextValue nextPrintableAsciiString()
* @param maxLength the maximum length of the string
* @return a {@link TextValue} consisting only of printable ascii characters.
*/
- public TextValue nextPrintableAsciiString( int minLength, int maxLength )
+ public TextValue nextPrintableAsciiTextValue( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
byte[] bytes = new byte[length];
@@ -570,9 +601,9 @@ public TextValue nextPrintableAsciiString( int minLength, int maxLength )
*
* @return a generator {@link TextValue}.
*/
- public TextValue nextString()
+ public TextValue nextTextValue()
{
- return nextString( configuration.stringMinLength(), configuration.stringMaxLength() );
+ return nextTextValue( configuration.stringMinLength(), configuration.stringMaxLength() );
}
/**
@@ -582,7 +613,7 @@ public TextValue nextString()
* @param maxLength the maximum length of the string
* @return a generator {@link TextValue}.
*/
- public TextValue nextString( int minLength, int maxLength )
+ public TextValue nextTextValue( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
UTF8StringValueBuilder builder = new UTF8StringValueBuilder( nextPowerOf2( length ) );
@@ -634,7 +665,7 @@ public Value nextValue()
case SHORT:
return nextShortValue();
case STRING:
- return nextString();
+ return nextTextValue();
case INT:
return nextIntValue();
case LONG:
@@ -1097,7 +1128,7 @@ public TextArray nextStringArray( int minLength, int maxLength )
String[] strings = new String[length];
for ( int i = 0; i < length; i++ )
{
- strings[i] = nextString().stringValue();
+ strings[i] = nextTextValue().stringValue();
}
return Values.stringArray( strings );
}
@@ -1289,26 +1320,71 @@ public void among( List among, Consumer action )
}
}
- private Instant randomInstant()
+ public int intBetween( int min, int max )
{
- return Instant.ofEpochSecond(
- nextLong( LocalDateTime.MIN.toEpochSecond( UTC ), LocalDateTime.MAX.toEpochSecond( UTC ) ),
- nextLong( NANOS_PER_SECOND ) );
+ return min + generator.nextInt( max - min + 1 );
}
- private int nextPowerOf2( int i )
+ public long nextLong( long bound )
{
- return 1 << (32 - Integer.numberOfLeadingZeros( i ));
+ return abs( generator.nextLong() ) % bound;
}
- private int intBetween( int min, int max )
+ public long nextLong( )
{
- return min + generator.nextInt( max - min + 1 );
+ return generator.nextLong();
}
- private long nextLong( long bound )
+ @SuppressWarnings( "unchecked" )
+ public T[] selection( T[] among, int min, int max, boolean allowDuplicates )
{
- return abs( generator.nextLong() ) % bound;
+ assert min <= max;
+ int diff = min == max ? 0 : generator.nextInt( max - min );
+ int length = min + diff;
+ T[] result = (T[]) Array.newInstance( among.getClass().getComponentType(), length );
+ for ( int i = 0; i < length; i++ )
+ {
+ while ( true )
+ {
+ T candidate = among( among );
+ if ( !allowDuplicates && contains( result, candidate ) )
+ { // Try again
+ continue;
+ }
+ result[i] = candidate;
+ break;
+ }
+ }
+ return result;
+ }
+
+ private static boolean contains( T[] array, T contains )
+ {
+ for ( T item : array )
+ {
+ if ( nullSafeEquals( item, contains ) )
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static boolean nullSafeEquals( T first, T other )
+ {
+ return first == null ? other == null : first.equals( other );
+ }
+
+ private Instant randomInstant()
+ {
+ return Instant.ofEpochSecond(
+ nextLong( LocalDateTime.MIN.toEpochSecond( UTC ), LocalDateTime.MAX.toEpochSecond( UTC ) ),
+ nextLong( NANOS_PER_SECOND ) );
+ }
+
+ private int nextPowerOf2( int i )
+ {
+ return 1 << (32 - Integer.numberOfLeadingZeros( i ));
}
private long nextLong( long origin, long bound )
diff --git a/community/random-values/src/main/java/org/neo4j/values/storable/UTF8StringValueBuilder.java b/community/random-values/src/main/java/org/neo4j/values/storable/UTF8StringValueBuilder.java
index 182fd8f6f3395..e7ddc28b7cfd9 100644
--- a/community/random-values/src/main/java/org/neo4j/values/storable/UTF8StringValueBuilder.java
+++ b/community/random-values/src/main/java/org/neo4j/values/storable/UTF8StringValueBuilder.java
@@ -89,8 +89,8 @@ else if ( codePoint < 0x10000 )
else
{
//Require four bytes - will be laid out like:
- //b1 b2 b3
- //11110xxx 10xxxxxx 10xxxxxx
+ //b1 b2 b3 b4
+ //11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
add( (byte) (0b1111_0000 | (0b0001_1111 & (codePoint >> 18))) );
add( (byte) (0b1000_0000 | (0b0011_1111 & (codePoint >> 12))) );
add( (byte) (0b1000_0000 | (0b0011_1111 & (codePoint >> 6))) );
diff --git a/community/random-values/src/test/java/org/neo4j/values/storable/RandomValuesTest.java b/community/random-values/src/test/java/org/neo4j/values/storable/RandomValuesTest.java
index ab089752edda9..b68e3bb399546 100644
--- a/community/random-values/src/test/java/org/neo4j/values/storable/RandomValuesTest.java
+++ b/community/random-values/src/test/java/org/neo4j/values/storable/RandomValuesTest.java
@@ -222,7 +222,7 @@ public void nextAlphaString()
.collect( Collectors.toSet() );
for ( int i = 0; i < ITERATIONS; i++ )
{
- TextValue textValue = randomValues.nextAlphaString( 5, 10 );
+ TextValue textValue = randomValues.nextAlphaTextValue( 5, 10 );
String asString = textValue.stringValue();
for ( int j = 0; j < asString.length(); j++ )
{
@@ -241,7 +241,7 @@ public void nextAlphaNumericString()
.collect( Collectors.toSet() );
for ( int i = 0; i < ITERATIONS; i++ )
{
- TextValue textValue = randomValues.nextAlphaNumericString( 10, 20 );
+ TextValue textValue = randomValues.nextAlphaNumericTextValue( 10, 20 );
String asString = textValue.stringValue();
for ( int j = 0; j < asString.length(); j++ )
{
@@ -259,7 +259,7 @@ public void nextAsciiString()
{
for ( int i = 0; i < ITERATIONS; i++ )
{
- TextValue textValue = randomValues.nextAsciiString( 10, 20 );
+ TextValue textValue = randomValues.nextAsciiTextValue( 10, 20 );
String asString = textValue.stringValue();
int length = asString.length();
assertThat( length, greaterThanOrEqualTo( 10 ) );
@@ -272,7 +272,7 @@ public void nextPrintableAsciiString()
{
for ( int i = 0; i < ITERATIONS; i++ )
{
- TextValue textValue = randomValues.nextPrintableAsciiString( 10, 20 );
+ TextValue textValue = randomValues.nextPrintableAsciiTextValue( 10, 20 );
String asString = textValue.stringValue();
int length = asString.length();
assertThat( length, greaterThanOrEqualTo( 10 ) );
@@ -285,7 +285,7 @@ public void nextString()
{
for ( int i = 0; i < ITERATIONS; i++ )
{
- TextValue textValue = randomValues.nextString( 10, 20 );
+ TextValue textValue = randomValues.nextTextValue( 10, 20 );
String asString = textValue.stringValue();
int length = asString.codePointCount( 0, asString.length() );
assertThat( length, greaterThanOrEqualTo( 10 ) );
diff --git a/stresstests/pom.xml b/stresstests/pom.xml
index 551c169f5583a..fb9091eda8cec 100644
--- a/stresstests/pom.xml
+++ b/stresstests/pom.xml
@@ -80,7 +80,13 @@
test-jar
test
-