Skip to content

Commit

Permalink
Updated unit tests to assert important aspects
Browse files Browse the repository at this point in the history
  • Loading branch information
systay authored and pontusmelke committed Sep 10, 2017
1 parent 926c296 commit 152dbb2
Showing 1 changed file with 15 additions and 79 deletions.
Expand Up @@ -21,13 +21,11 @@


import org.junit.Test; import org.junit.Test;


import java.util.Arrays;
import java.util.Random; import java.util.Random;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;


import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.neo4j.values.storable.NumberValues.MAX_LENGTH;
import static org.neo4j.values.storable.NumberValues.hash; import static org.neo4j.values.storable.NumberValues.hash;


public class NumberValuesTest public class NumberValuesTest
Expand All @@ -36,14 +34,14 @@ public class NumberValuesTest
@Test @Test
public void shouldHashNaN() public void shouldHashNaN()
{ {
assertThat( hash( Double.NaN ), equalTo( Double.hashCode( Double.NaN ) ) ); assertThat( hash( Double.NaN ), equalTo( hash( Float.NaN ) ) );
} }


@Test @Test
public void shouldHashInfinite() public void shouldHashInfinite()
{ {
assertThat( hash( Double.NEGATIVE_INFINITY ), equalTo( Double.hashCode( Double.NEGATIVE_INFINITY ) ) ); assertThat( hash( Double.NEGATIVE_INFINITY ), equalTo( hash( Float.NEGATIVE_INFINITY ) ) );
assertThat( hash( Double.POSITIVE_INFINITY ), equalTo( Double.hashCode( Double.POSITIVE_INFINITY ) ) ); assertThat( hash( Double.POSITIVE_INFINITY ), equalTo( hash( Float.POSITIVE_INFINITY ) ) );
} }


@Test @Test
Expand All @@ -53,98 +51,36 @@ public void shouldHashIntegralDoubleAsLong()
} }


@Test @Test
public void shouldGiveSameResultsAsBuiltInHashForInts() public void shouldGiveSameResultEvenWhenArraysContainDifferentTypes()
{ {
int[] ints = new int[32]; int[] ints = new int[32];
Random r = ThreadLocalRandom.current(); long[] longs = new long[32];
for ( int i = 0; i < 32; i++ )
{
ints[i] = r.nextInt();
}

assertThat(hash(ints), equalTo(Arrays.hashCode( ints )));
}


@Test
public void shouldGiveSameResultsAsBuiltInHashForBytes()
{
byte[] bytes = new byte[32];
Random r = ThreadLocalRandom.current(); Random r = ThreadLocalRandom.current();
for ( int i = 0; i < 32; i++ ) for ( int i = 0; i < 32; i++ )
{ {
bytes[i] = (byte)r.nextInt(); int nextInt = r.nextInt();
ints[i] = nextInt;
longs[i] = nextInt;
} }


assertThat(hash(bytes), equalTo(Arrays.hashCode( bytes ))); assertThat( hash( ints ), equalTo( hash( longs ) ) );
} }


@Test @Test
public void shouldGiveSameResultsAsBuiltInHashForShorts() public void shouldGiveSameResultEvenWhenArraysContainDifferentTypes2()
{ {
byte[] bytes = new byte[32];
short[] shorts = new short[32]; short[] shorts = new short[32];
Random r = ThreadLocalRandom.current();
for ( int i = 0; i < 32; i++ )
{
shorts[i] = (short)r.nextInt();
}


assertThat(hash(shorts), equalTo(Arrays.hashCode( shorts )));
}

@Test
public void shouldGiveSameResultsAsBuiltInHashForLongs()
{
long[] longs = new long[32];
Random r = ThreadLocalRandom.current(); Random r = ThreadLocalRandom.current();
for ( int i = 0; i < 32; i++ ) for ( int i = 0; i < 32; i++ )
{ {
longs[i] = (long)r.nextInt(); byte nextByte = ((Number)(r.nextInt())).byteValue();
bytes[i] = nextByte;
shorts[i] = nextByte;
} }


assertThat(hash(longs), equalTo(Arrays.hashCode( longs ))); assertThat( hash( bytes ), equalTo( hash( shorts ) ) );
}

@Test
public void shouldHandleBigIntArrays()
{
int[] big = new int[NumberValues.MAX_LENGTH + 1];
int[] slightlySmaller = new int[MAX_LENGTH];
Arrays.fill( big, 1337 );
Arrays.fill( slightlySmaller, 1337 );

assertThat( hash( big ), equalTo( hash( slightlySmaller ) ) );
}

@Test
public void shouldHandleBigByteArrays()
{
byte[] big = new byte[NumberValues.MAX_LENGTH + 1];
byte[] slightlySmaller = new byte[MAX_LENGTH];
Arrays.fill( big, (byte) 1337 );
Arrays.fill( slightlySmaller, (byte) 1337 );

assertThat( hash( big ), equalTo( hash( slightlySmaller ) ) );
}

@Test
public void shouldHandleBigShortArrays()
{
short[] big = new short[NumberValues.MAX_LENGTH + 1];
short[] slightlySmaller = new short[MAX_LENGTH];
Arrays.fill( big, (short) 1337 );
Arrays.fill( slightlySmaller, (short) 1337 );

assertThat( hash( big ), equalTo( hash( slightlySmaller ) ) );
}

@Test
public void shouldHandleBigLongArrays()
{
long[] big = new long[NumberValues.MAX_LENGTH + 1];
long[] slightlySmaller = new long[MAX_LENGTH];
Arrays.fill( big, 1337L );
Arrays.fill( slightlySmaller, 1337L );

assertThat( hash( big ), equalTo( hash( slightlySmaller ) ) );
} }
} }

0 comments on commit 152dbb2

Please sign in to comment.