Skip to content

Commit

Permalink
Add nextAlphaStringMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed May 11, 2018
1 parent 896d871 commit 4edae5c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
Expand Up @@ -150,6 +150,25 @@ public TextValue nextDigitString( int minLength, int maxLength )
return Values.utf8Value( bytes ); return Values.utf8Value( bytes );
} }


public TextValue nextAlphaString( int minLength, int maxLength )
{
int length = intBetween( minLength, maxLength );
byte[] bytes = new byte[length];
for ( int i = 0; i < length; i++ )
{
if (random.nextBoolean() )
{
bytes[i] = (byte) intBetween( 'A', 'Z' );
}
else
{
bytes[i] = (byte) intBetween( 'a', 'z' );
}
}

return Values.utf8Value( bytes );
}

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
@@ -0,0 +1,24 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package org.neo4j.values;

public class UTF8Utils
{
}
Expand Up @@ -25,6 +25,7 @@
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors;


import org.neo4j.values.storable.ByteValue; import org.neo4j.values.storable.ByteValue;
import org.neo4j.values.storable.DoubleValue; import org.neo4j.values.storable.DoubleValue;
Expand Down Expand Up @@ -162,21 +163,41 @@ public void nextNumberValue()
@Test @Test
public void nextDigitString() public void nextDigitString()
{ {
Set<Character> seenDigits = new HashSet<>( Arrays.asList( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ) ); Set<Integer> seenDigits = "0123456789".chars().boxed().collect( Collectors.toSet() );
for ( int i = 0; i < ITERATIONS; i++ ) for ( int i = 0; i < ITERATIONS; i++ )
{ {
TextValue textValue = randomValue.nextDigitString( 5, 10 ); TextValue textValue = randomValue.nextDigitString( 5, 10 );
String asString = textValue.stringValue(); String asString = textValue.stringValue();
for ( int j = 0; j < asString.length(); j++ ) for ( int j = 0; j < asString.length(); j++ )
{ {
char ch = asString.charAt( j ); int ch = asString.charAt( j );
assertTrue( Character.isDigit( ch )); assertTrue( Character.isDigit( ch ));
seenDigits.remove( ch ); seenDigits.remove( ch );
} }
} }
assertThat( seenDigits, empty() ); assertThat( seenDigits, empty() );
} }


@Test
public void nextAlphaString()
{
Set<Integer> seenDigits = "ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz".chars().boxed()
.collect( Collectors.toSet() );
for ( int i = 0; i < ITERATIONS; i++ )
{
TextValue textValue = randomValue.nextAlphaString( 5, 10 );
String asString = textValue.stringValue();
for ( int j = 0; j < asString.length(); j++ )
{
int ch = asString.charAt( j );
assertTrue( "Not a character " + ch, Character.isAlphabetic( ch ));
seenDigits.remove( ch );
}
}
assertThat( seenDigits, empty() );
}


private void checkDistribution( Supplier<Value> supplier ) private void checkDistribution( Supplier<Value> supplier )
{ {
Set<Value> values = new HashSet<>(); Set<Value> values = new HashSet<>();
Expand Down

0 comments on commit 4edae5c

Please sign in to comment.