From db08842f51ea1ca640f305e8a2322ea24c884116 Mon Sep 17 00:00:00 2001 From: Anton Persson Date: Thu, 26 Jul 2018 16:35:02 +0200 Subject: [PATCH] Expose possibility to generate random value of given type and use min and max from default envelope settings when generating random points. --- .../java/org/neo4j/test/rule/RandomRule.java | 5 ++ .../neo4j/values/storable/RandomValues.java | 66 +++++++++++++++---- 2 files changed, 60 insertions(+), 11 deletions(-) diff --git a/community/random-values/src/main/java/org/neo4j/test/rule/RandomRule.java b/community/random-values/src/main/java/org/neo4j/test/rule/RandomRule.java index 2e8d38638a0d..34a26ad180f2 100644 --- a/community/random-values/src/main/java/org/neo4j/test/rule/RandomRule.java +++ b/community/random-values/src/main/java/org/neo4j/test/rule/RandomRule.java @@ -244,6 +244,11 @@ public Value nextValue() return randoms.nextValue(); } + public Value nextValue( RandomValues.Types type ) + { + return randoms.nextValue( type ); + } + // ============================ // Other utility methods // ============================ 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 17b0ac9c1b84..53478fda97c6 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 @@ -61,7 +61,7 @@ public class RandomValues { - enum Types + public enum Types { BOOLEAN, BYTE, @@ -655,7 +655,21 @@ public TextValue nextTextValue( int minLength, int maxLength ) */ public Value nextValue() { - Types type = nextType(); + return nextValue( nextType() ); + } + + /** + * Returns the next pseudorandom {@link Value} of given type + *

+ * The length of strings will be governed by {@link Configuration#stringMinLength()} and + * {@link Configuration#stringMaxLength()} and + * the length of arrays will be governed by {@link Configuration#arrayMinLength()} and + * {@link Configuration#arrayMaxLength()} + * + * @return the next pseudorandom {@link Value} of given type + */ + public Value nextValue( Types type ) + { switch ( type ) { case BOOLEAN: @@ -1257,7 +1271,9 @@ public Value nextTemporalValue() */ public PointValue nextCartesianPoint() { - return Values.pointValue( CoordinateReferenceSystem.Cartesian, generator.nextDouble(), generator.nextDouble() ); + double x = randomCartesianCoordinate(); + double y = randomCartesianCoordinate(); + return Values.pointValue( CoordinateReferenceSystem.Cartesian, x, y ); } /** @@ -1267,8 +1283,10 @@ public PointValue nextCartesianPoint() */ public PointValue nextCartesian3DPoint() { - return Values.pointValue( CoordinateReferenceSystem.Cartesian_3D, generator.nextDouble(), - generator.nextDouble(), generator.nextDouble() ); + double x = randomCartesianCoordinate(); + double y = randomCartesianCoordinate(); + double z = randomCartesianCoordinate(); + return Values.pointValue( CoordinateReferenceSystem.Cartesian_3D, x, y, z ); } /** @@ -1278,8 +1296,8 @@ public PointValue nextCartesian3DPoint() */ public PointValue nextGeographicPoint() { - double longitude = generator.nextDouble() * 360.0 - 180.0; - double latitude = generator.nextDouble() * 180.0 - 90.0; + double longitude = randomLongitude(); + double latitude = randomLatitude(); return Values.pointValue( CoordinateReferenceSystem.WGS84, longitude, latitude ); } @@ -1290,10 +1308,36 @@ public PointValue nextGeographicPoint() */ public PointValue nextGeographic3DPoint() { - double longitude = generator.nextDouble() * 360.0 - 180.0; - double latitude = generator.nextDouble() * 180.0 - 90.0; - return Values.pointValue( CoordinateReferenceSystem.WGS84_3D, longitude, latitude, - generator.nextDouble() * 10000 ); + double longitude = randomLongitude(); + double latitude = randomLatitude(); + double z = randomCartesianCoordinate(); + return Values.pointValue( CoordinateReferenceSystem.WGS84_3D, longitude, latitude, z ); + } + + private double randomLatitude() + { + double spatialDefaultMinLatitude = -90; + double spatialDefaultMaxLatitude = 90; + return doubleBetween( spatialDefaultMinLatitude, spatialDefaultMaxLatitude ); + } + + private double randomLongitude() + { + double spatialDefaultMinLongitude = -180; + double spatialDefaultMaxLongitude = 180; + return doubleBetween( spatialDefaultMinLongitude, spatialDefaultMaxLongitude ); + } + + private double randomCartesianCoordinate() + { + double spatialDefaultMinExtent = -1000000; + double spatialDefaultMaxExtent = 1000000; + return doubleBetween( spatialDefaultMinExtent, spatialDefaultMaxExtent ); + } + + private double doubleBetween( double min, double max ) + { + return generator.nextDouble() * (max - min) + min; } /**