Skip to content

Commit

Permalink
Add nextStringMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed May 11, 2018
1 parent bc02024 commit aa59147
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
Expand Up @@ -193,6 +193,38 @@ public TextValue nextAlphaNumericString( int minLength, int maxLength )
return Values.utf8Value( bytes ); return Values.utf8Value( bytes );
} }


public TextValue nextString( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
UTF8StringValueBuilder builder = new UTF8StringValueBuilder( nextPowerOf2( length ) );

for ( int i = 0; i < length; i++ )
{
boolean validCodePoint = false;
while ( !validCodePoint )
{
int codePoint = intBetween( Character.MIN_CODE_POINT, Character.MAX_CODE_POINT );
switch ( Character.getType( codePoint ) )
{
case Character.UNASSIGNED:
case Character.PRIVATE_USE:
case Character.SURROGATE:
continue;
default:
builder.addCodePoint( codePoint );
validCodePoint = true;
}
}
}
return builder.build();
}

private int nextPowerOf2( int i )
{
return 1 << (32 - Integer.numberOfLeadingZeros( i ));
}


private int intBetween( int min, int max ) private int intBetween( int min, int max )
{ {
return min + random.nextInt( max - min + 1 ); return min + random.nextInt( max - min + 1 );
Expand Down
Expand Up @@ -64,40 +64,40 @@ TextValue build()
return Values.utf8Value( bytes, 0, length ); return Values.utf8Value( bytes, 0, length );
} }


void addCodePoint( int codepoint ) void addCodePoint( int codePoint )
{ {
assert codepoint >= 0; assert codePoint >= 0;
if ( codepoint < 0x80 ) if ( codePoint < 0x80 )
{ {
//one byte is all it takes //one byte is all it takes
add( (byte) codepoint ); add( (byte) codePoint );
} }
else if ( codepoint < 0x800 ) else if ( codePoint < 0x800 )
{ {
//Require two bytes - will be laid out like: //Require two bytes - will be laid out like:
//b1 b2 //b1 b2
//110xxxxx 10xxxxxx //110xxxxx 10xxxxxx
add( (byte) (0b1100_0000 | (0b0001_1111 & (codepoint >> 6))) ); add( (byte) (0b1100_0000 | (0b0001_1111 & (codePoint >> 6))) );
add( (byte) (0b1000_0000 | (0b0011_1111 & codepoint)) ); add( (byte) (0b1000_0000 | (0b0011_1111 & codePoint)) );
} }
else if ( codepoint < 0x10000 ) else if ( codePoint < 0x10000 )
{ {
//Require three bytes - will be laid out like: //Require three bytes - will be laid out like:
//b1 b2 b3 //b1 b2 b3
//1110xxxx 10xxxxxx 10xxxxxx //1110xxxx 10xxxxxx 10xxxxxx
add( (byte) (0b1110_0000 | (0b0000_1111 & (codepoint >> 12))) ); add( (byte) (0b1110_0000 | (0b0000_1111 & (codePoint >> 12))) );
add( (byte) (0b1000_0000 | (0b0011_1111 & (codepoint >> 6))) ); add( (byte) (0b1000_0000 | (0b0011_1111 & (codePoint >> 6))) );
add( (byte) (0b1000_0000 | (0b0011_1111 & codepoint)) ); add( (byte) (0b1000_0000 | (0b0011_1111 & codePoint)) );
} }
else else
{ {
//Require four bytes - will be laid out like: //Require four bytes - will be laid out like:
//b1 b2 b3 //b1 b2 b3
//11110xxx 10xxxxxx 10xxxxxx //11110xxx 10xxxxxx 10xxxxxx
add( (byte) (0b1111_0000 | (0b0001_1111 & (codepoint >> 18))) ); add( (byte) (0b1111_0000 | (0b0001_1111 & (codePoint >> 18))) );
add( (byte) (0b1000_0000 | (0b0011_1111 & (codepoint >> 12))) ); add( (byte) (0b1000_0000 | (0b0011_1111 & (codePoint >> 12))) );
add( (byte) (0b1000_0000 | (0b0011_1111 & (codepoint >> 6))) ); add( (byte) (0b1000_0000 | (0b0011_1111 & (codePoint >> 6))) );
add( (byte) (0b1000_0000 | (0b0011_1111 & codepoint)) ); add( (byte) (0b1000_0000 | (0b0011_1111 & codePoint)) );
} }
} }
} }
Expand Up @@ -120,7 +120,6 @@ public void nextShortValueBounded()
checkBounded( () -> randomValue.nextShortValue( BOUND ) ); checkBounded( () -> randomValue.nextShortValue( BOUND ) );
} }



@Test @Test
public void nextByteValueUnbounded() public void nextByteValueUnbounded()
{ {
Expand Down Expand Up @@ -216,6 +215,18 @@ public void nextAlphaNumericString()
assertThat( seenDigits, empty() ); assertThat( seenDigits, empty() );
} }


@Test
public void nextString()
{
for ( int i = 0; i < ITERATIONS; i++ )
{
TextValue textValue = randomValue.nextString( 10, 20 );
String asString = textValue.stringValue();
int length = asString.codePointCount( 0, asString.length() );
assertThat( length, greaterThanOrEqualTo( 10 ) );
assertThat( length, lessThanOrEqualTo( 20 ) );
}
}


private void checkDistribution( Supplier<Value> supplier ) private void checkDistribution( Supplier<Value> supplier )
{ {
Expand Down

0 comments on commit aa59147

Please sign in to comment.