Skip to content

Commit

Permalink
Added negative test for list equalities
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed Aug 21, 2017
1 parent 046ea5f commit afed813
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
Expand Up @@ -21,11 +21,14 @@

import org.junit.Test;

import java.util.Arrays;

import org.neo4j.values.storable.LongValue;
import org.neo4j.values.storable.Values;

import static java.lang.String.format;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.neo4j.values.storable.Values.booleanArray;
import static org.neo4j.values.storable.Values.byteArray;
import static org.neo4j.values.storable.Values.charArray;
Expand Down Expand Up @@ -113,12 +116,12 @@ public void shouldNotEqual()
// different value types
assertNotEqualValues( list( true, true ), intArray( new int[]{0, 0} ) );
assertNotEqualValues( list( true, true ), longArray( new long[]{0L, 0L} ) );
assertNotEqualValues( list( true, true ), shortArray( new short[]{(short)0,(short) 0} ) );
assertNotEqualValues( list( true, true ), shortArray( new short[]{(short) 0, (short) 0} ) );
assertNotEqualValues( list( true, true ), floatArray( new float[]{0.0f, 0.0f} ) );
assertNotEqualValues( list( true, true ), doubleArray( new double[]{0.0, 0.0} ) );
assertNotEqualValues( list( true, true ), charArray( new char[]{'T','T'} ) );
assertNotEqualValues( list( true, true ), stringArray( new String[]{"True","True"} ) );
assertNotEqualValues( list( true, true ), byteArray( new byte[]{(byte)0,(byte)0} ) );
assertNotEqualValues( list( true, true ), charArray( new char[]{'T', 'T'} ) );
assertNotEqualValues( list( true, true ), stringArray( new String[]{"True", "True"} ) );
assertNotEqualValues( list( true, true ), byteArray( new byte[]{(byte) 0, (byte) 0} ) );

// wrong or missing items
assertNotEqualValues( list( true ), booleanArray( new boolean[]{true, false} ) );
Expand All @@ -140,7 +143,7 @@ public void shouldNotEqual()
assertNotEqualValues( list( "hi", "hello" ), stringArray( new String[]{"hi"} ) );
assertNotEqualValues( list( "hello" ), stringArray( new String[]{"hi"} ) );

assertNotEqualValues( list(1,'b'),charArray( new char[]{'a','b'} ) );
assertNotEqualValues( list( 1, 'b' ), charArray( new char[]{'a', 'b'} ) );
}

@Test
Expand All @@ -150,7 +153,7 @@ public void shouldCoerce()
list( new String[]{"h"}, 3.0 ),
list( new char[]{'h'}, 3 ) );

assertEqualValues( list("a",'b'),charArray( new char[]{'a','b'} ) );
assertEqualValues( list( "a", 'b' ), charArray( new char[]{'a', 'b'} ) );
}

@Test
Expand All @@ -166,13 +169,13 @@ public void shouldNestCorrectly()
{
assertEqual(
list(
booleanArray( new boolean[]{true, false} ),
intArray( new int[]{1, 2} ),
stringArray( new String[]{"Hello", "World"} ) ),
booleanArray( new boolean[]{true, false} ),
intArray( new int[]{1, 2} ),
stringArray( new String[]{"Hello", "World"} ) ),
list(
booleanArray( new boolean[]{true, false} ),
intArray( new int[]{1, 2} ),
stringArray( new String[]{"Hello", "World"} ) ) );
booleanArray( new boolean[]{true, false} ),
intArray( new int[]{1, 2} ),
stringArray( new String[]{"Hello", "World"} ) ) );

assertNotEqual(
list(
Expand Down Expand Up @@ -203,7 +206,7 @@ public void shouldRecurseAndCoerce()
list( 'a', list( "b", list( 'c' ) ) ) );
}

private ListValue[] lists =
private ListValue[] equivalentLists =
{VirtualValues.list( Values.longValue( 1L ), Values.longValue( 4L ), Values.longValue( 7L ) ),

range( 1L, 8L, 3L ),
Expand All @@ -218,14 +221,35 @@ public void shouldRecurseAndCoerce()
VirtualValues.take( list( 1L, 4L, 7L, 10L, 13L ), 3 ),
VirtualValues.transform( list( 0L, 3L, 6L ),
anyValue -> Values.longValue( ((LongValue) anyValue).longValue() + 1L ) ),
VirtualValues.reverse( list( 7L, 4L, 1L ) )};
VirtualValues.reverse( list( 7L, 4L, 1L ) ),
VirtualValues.concat( list( 1L, 4L ), list( 7L ) )
};

private ListValue[] nonEquivalentLists =
{VirtualValues.list( Values.longValue( 1L ), Values.longValue( 4L ), Values.longValue( 7L ) ),

range( 2L, 9L, 3L ),
VirtualValues.fromArray( Values.longArray( new long[]{3L, 6L, 9L} ) ),
VirtualValues.filter( range( 1L, 100, 1L ), anyValue ->
{
long l = ((LongValue) anyValue).longValue();
return l == 4L || l == 7L || l == 10L;
} ),
VirtualValues.slice( list( -2L, 1L, 5L, 8L, 11L ), 1, 4 ),
VirtualValues.drop( list( -2L, 6L, 9L, 12L ), 1 ),
VirtualValues.take( list( 7L, 10L, 13L, 10L, 13L ), 3 ),
VirtualValues.transform( list( 0L, 3L, 6L ),
anyValue -> Values.longValue( ((LongValue) anyValue).longValue() + 8L ) ),
VirtualValues.reverse( list( 15L, 12L, 9L ) ),
VirtualValues.concat( list( 10L, 13L ), list( 16L ) )
};

@Test
public void shouldTreatDifferentListImplementationSimilar()
{
for ( ListValue list1 : lists )
for ( ListValue list1 : equivalentLists )
{
for ( ListValue list2 : lists )
for ( ListValue list2 : equivalentLists )
{
assertEqual( list1, list2 );
assertArrayEquals(
Expand All @@ -235,4 +259,24 @@ public void shouldTreatDifferentListImplementationSimilar()
}
}
}

@Test
public void shouldNotTreatDifferentListImplementationSimilarOfNonEquivalentListsSimilar()
{
for ( ListValue list1 : nonEquivalentLists )
{
for ( ListValue list2 : nonEquivalentLists )
{
if ( list1 == list2 )
{
continue;
}
assertNotEqual( list1, list2 );
assertFalse(
format( "%s.asArray != %s.toArray", list1.getClass().getSimpleName(),
list2.getClass().getSimpleName() ),
Arrays.equals( list1.asArray(), list2.asArray() ) );
}
}
}
}
Expand Up @@ -116,8 +116,8 @@ public static void assertEqual( VirtualValue a, VirtualValue b )

public static void assertNotEqual( VirtualValue a, VirtualValue b )
{
assertFalse( "should not equal", a.equals( b ) );
assertFalse( "should not equal", b.equals( a ) );
assertFalse( a + " should not equal " + b, a.equals( b ) );
assertFalse( b + "should not equal " + a, b.equals( a ) );
}

public static void assertEqualValues( VirtualValue a, Value b )
Expand Down

0 comments on commit afed813

Please sign in to comment.