Skip to content

Commit

Permalink
Add methods for generating strings only in the BMP
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Sep 7, 2018
1 parent 15c0e12 commit 4ff5278
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
Expand Up @@ -241,6 +241,16 @@ public TextValue nextAlphaNumericTextValue( int minLength, int maxLength )
return randoms.nextAlphaNumericTextValue( minLength, maxLength );
}

public TextValue nextBasicMultilingualPlaneTextValue()
{
return randoms.nextBasicMultilingualPlaneTextValue();
}

public String nextBasicMultilingualPlaneString()
{
return nextBasicMultilingualPlaneTextValue().stringValue();
}

public <T> T[] selection( T[] among, int min, int max, boolean allowDuplicates )
{
return randoms.selection( among, min, max, allowDuplicates );
Expand Down
Expand Up @@ -602,6 +602,38 @@ public TextValue nextPrintableAsciiTextValue( int minLength, int maxLength )
return Values.utf8Value( bytes );
}

/**
* Returns the next pseudorandom {@link TextValue} consisting only of characters in the Basic Multilingual Plane(BMP).
* <p>
* The length of the text will be between {@link Configuration#stringMinLength()} and
* {@link Configuration#stringMaxLength()}
*
* @return a {@link TextValue} consisting only of characters in the BMP.
*/
public TextValue nextBasicMultilingualPlaneTextValue()
{
return nextBasicMultilingualPlaneTextValue( configuration.stringMinLength(), configuration.stringMaxLength() );
}

/**
* Returns the next pseudorandom {@link TextValue} consisting only of printable ascii characters.
*
* @param minLength the minimum length of the string
* @param maxLength the maximum length of the string
* @return a {@link TextValue} consisting only of printable ascii characters.
*/
public TextValue nextBasicMultilingualPlaneTextValue( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
UTF8StringValueBuilder builder = new UTF8StringValueBuilder( nextPowerOf2( length ) );

for ( int i = 0; i < length; i++ )
{
builder.addCodePoint( nextValidCodePoint( 0xFFFF ) );
}
return builder.build();
}

/**
* Returns the next pseudorandom {@link TextValue}.
* <p>
Expand Down Expand Up @@ -641,12 +673,24 @@ public TextValue nextTextValue( int minLength, int maxLength )
* @return A pseudorandom valid code point
*/
private int nextValidCodePoint()
{
return nextValidCodePoint( configuration.maxCodePoint() );
}

/**
* Generate next code point that is valid for composition of a string.
* Additional limitation on code point range is given by configuration.
*
* @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, configuration.maxCodePoint() );
codePoint = intBetween( Character.MIN_CODE_POINT, maxCodePoint );
type = Character.getType( codePoint );
}
while ( type == Character.UNASSIGNED ||
Expand Down
Expand Up @@ -35,6 +35,7 @@
import org.neo4j.values.AnyValue;

import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasItem;
Expand Down Expand Up @@ -326,6 +327,18 @@ public void nextValue()
assertThat( seen, empty() );
}

@Test
public void nextBasicMultilingualPlaneTextValue()
{
for ( int i = 0; i < ITERATIONS; i++ )
{
TextValue value = randomValues.nextBasicMultilingualPlaneTextValue();
//make sure the value fits in 16bits, meaning that the size of the char[]
//matches the number of code points.
assertThat( value.length(), equalTo( value.stringValue().length() ) );
}
}

private void assertKnownType( Class<? extends AnyValue> typeToCheck, Set<Class<? extends AnyValue>> types )
{
for ( Class<? extends AnyValue> type : types )
Expand Down

0 comments on commit 4ff5278

Please sign in to comment.