From f46084c339f1729e52dba3823f3e02456e7c5fec Mon Sep 17 00:00:00 2001 From: fickludd Date: Mon, 22 May 2017 13:45:23 +0200 Subject: [PATCH] Equality tests --- .../java/org/neo4j/values/StringValue.java | 2 +- .../org/neo4j/values/ValueEqualityTest.java | 308 ++++++++++++++++++ .../java/org/neo4j/values/ValuesTest.java | 8 +- 3 files changed, 316 insertions(+), 2 deletions(-) create mode 100644 community/values/src/test/java/org/neo4j/values/ValueEqualityTest.java diff --git a/community/values/src/main/java/org/neo4j/values/StringValue.java b/community/values/src/main/java/org/neo4j/values/StringValue.java index af714011b0001..fc10f3c3dc86b 100644 --- a/community/values/src/main/java/org/neo4j/values/StringValue.java +++ b/community/values/src/main/java/org/neo4j/values/StringValue.java @@ -52,7 +52,7 @@ boolean equals( boolean x ) @Override boolean equals( char x ) { - return false; + return string.length() == 1 && string.charAt( 0 ) == x; } @Override diff --git a/community/values/src/test/java/org/neo4j/values/ValueEqualityTest.java b/community/values/src/test/java/org/neo4j/values/ValueEqualityTest.java new file mode 100644 index 0000000000000..f7be3157d095a --- /dev/null +++ b/community/values/src/test/java/org/neo4j/values/ValueEqualityTest.java @@ -0,0 +1,308 @@ +/* + * Copyright (c) 2002-2017 "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 . + */ +package org.neo4j.values; + +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * This test was faithfully converted (including personal remarks) from PropertyEqualityTest. + */ +@RunWith( value = Parameterized.class ) +public class ValueEqualityTest +{ + @Parameterized.Parameters( name = "{0}" ) + public static Iterable data() + { + return Arrays.asList( + // boolean properties + shouldMatch( true, true ), + shouldMatch( false, false ), + shouldNotMatch( true, false ), + shouldNotMatch( false, true ), + shouldNotMatch( true, 0 ), + shouldNotMatch( false, 0 ), + shouldNotMatch( true, 1 ), + shouldNotMatch( false, 1 ), + shouldNotMatch( false, "false" ), + shouldNotMatch( true, "true" ), + + //byte properties + shouldMatch( (byte) 42, (byte) 42 ), + shouldMatch( (byte) 42, (short) 42 ), + shouldNotMatch( (byte) 42, 42 + 256 ), + shouldMatch( (byte) 43, (int) 43 ), + shouldMatch( (byte) 43, (long) 43 ), + shouldMatch( (byte) 23, 23.0d ), + shouldMatch( (byte) 23, 23.0f ), + shouldNotMatch( (byte) 23, 23.5 ), + shouldNotMatch( (byte) 23, 23.5f ), + + //short properties + shouldMatch( (short) 11, (byte) 11 ), + shouldMatch( (short) 42, (short) 42 ), + shouldNotMatch( (short) 42, 42 + 65536 ), + shouldMatch( (short) 43, (int) 43 ), + shouldMatch( (short) 43, (long) 43 ), + shouldMatch( (short) 23, 23.0f ), + shouldMatch( (short) 23, 23.0d ), + shouldNotMatch( (short) 23, 23.5 ), + shouldNotMatch( (short) 23, 23.5f ), + + //int properties + shouldMatch( 11, (byte) 11 ), + shouldMatch( 42, (short) 42 ), + shouldNotMatch( 42, 42 + 4294967296L ), + shouldMatch( 43, 43 ), + shouldMatch( Integer.MAX_VALUE, Integer.MAX_VALUE ), + shouldMatch( 43, (long) 43 ), + shouldMatch( 23, 23.0 ), + shouldNotMatch( 23, 23.5 ), + shouldNotMatch( 23, 23.5f ), + + //long properties + shouldMatch( 11L, (byte) 11 ), + shouldMatch( 42L, (short) 42 ), + shouldMatch( 43L, (int) 43 ), + shouldMatch( 43L, (long) 43 ), + shouldMatch( 87L, (long) 87 ), + shouldMatch( Long.MAX_VALUE, Long.MAX_VALUE ), + shouldMatch( 23L, 23.0 ), + shouldNotMatch( 23L, 23.5 ), + shouldNotMatch( 23L, 23.5f ), + shouldMatch(9007199254740992L, 9007199254740992D), + // shouldMatch(9007199254740993L, 9007199254740992D), // is stupid, m'kay?! + + // floats goddamnit + shouldMatch( 11f, (byte) 11 ), + shouldMatch( 42f, (short) 42 ), + shouldMatch( 43f, (int) 43 ), + shouldMatch( 43f, (long) 43 ), + shouldMatch( 23f, 23.0 ), + shouldNotMatch( 23f, 23.5 ), + shouldNotMatch( 23f, 23.5f ), + shouldMatch( 3.14f, 3.14f ), + shouldNotMatch( 3.14f, 3.14d ), // Would be nice if they matched, but they don't + + // doubles + shouldMatch( 11d, (byte) 11 ), + shouldMatch( 42d, (short) 42 ), + shouldMatch( 43d, (int) 43 ), + shouldMatch( 43d, (long) 43 ), + shouldMatch( 23d, 23.0 ), + shouldNotMatch( 23d, 23.5 ), + shouldNotMatch( 23d, 23.5f ), + shouldNotMatch( 3.14d, 3.14f ), // this really is sheeeet + shouldMatch( 3.14d, 3.14d ), + + // strings + shouldMatch( "A", "A" ), + shouldMatch( 'A', 'A' ), + shouldMatch( 'A', "A" ), + shouldMatch( "A", 'A' ), + shouldNotMatch( "AA", 'A' ), + shouldNotMatch( "a", "A" ), + shouldNotMatch( "A", "a" ), + shouldNotMatch( "0", 0 ), + shouldNotMatch( '0', 0 ), + + // arrays + shouldMatch( new int[]{1, 2, 3}, new int[]{1, 2, 3} ), + shouldMatch( new int[]{1, 2, 3}, new long[]{1, 2, 3} ), + shouldMatch( new int[]{1, 2, 3}, new double[]{1.0, 2.0, 3.0} ), + shouldMatch( new String[]{"A", "B", "C"}, new String[]{"A", "B", "C"} ), + shouldMatch( new String[]{"A", "B", "C"}, new char[]{'A', 'B', 'C'} ), + shouldMatch( new char[]{'A', 'B', 'C'}, new String[]{"A", "B", "C"} ) + ); + } + + private Test currentTest; + + public ValueEqualityTest( Test currentTest ) + { + this.currentTest = currentTest; + } + + @org.junit.Test + public void runTest() + { + currentTest.checkAssertion(); + } + + public static Test shouldMatch( boolean propertyValue, Object value ) + { + return new Test( Values.booleanValue( propertyValue ), value, true ); + } + + public static Test shouldNotMatch( boolean propertyValue, Object value ) + { + return new Test( Values.booleanValue( propertyValue ), value, false ); + } + + public static Test shouldMatch( byte propertyValue, Object value ) + { + return new Test( Values.byteValue( propertyValue ), value, true ); + } + + public static Test shouldNotMatch( byte propertyValue, Object value ) + { + return new Test( Values.byteValue( propertyValue ), value, false ); + } + + public static Test shouldMatch( short propertyValue, Object value ) + { + return new Test( Values.shortValue( propertyValue ), value, true ); + } + + public static Test shouldNotMatch( short propertyValue, Object value ) + { + return new Test( Values.shortValue( propertyValue ), value, false ); + } + + public static Test shouldMatch( float propertyValue, Object value ) + { + return new Test( Values.floatValue( propertyValue ), value, true ); + } + + public static Test shouldNotMatch( float propertyValue, Object value ) + { + return new Test( Values.floatValue( propertyValue ), value, false ); + } + + public static Test shouldMatch( long propertyValue, Object value ) + { + return new Test( Values.longValue( propertyValue ), value, true ); + } + + public static Test shouldNotMatch( long propertyValue, Object value ) + { + return new Test( Values.longValue( propertyValue ), value, false ); + } + + public static Test shouldMatch( double propertyValue, Object value ) + { + return new Test( Values.doubleValue( propertyValue ), value, true ); + } + + public static Test shouldNotMatch( double propertyValue, Object value ) + { + return new Test( Values.doubleValue( propertyValue ), value, false ); + } + + public static Test shouldMatch( String propertyValue, Object value ) + { + return new Test( Values.stringValue( propertyValue ), value, true ); + } + + public static Test shouldNotMatch( String propertyValue, Object value ) + { + return new Test( Values.stringValue( propertyValue ), value, false ); + } + + public static Test shouldMatch( char propertyValue, Object value ) + { + return new Test( Values.charValue( propertyValue ), value, true ); + } + + public static Test shouldNotMatch( char propertyValue, Object value ) + { + return new Test( Values.charValue( propertyValue ), value, false ); + } + + public static Test shouldMatch( int[] propertyValue, Object value ) + { + return new Test( Values.intArrayValue( propertyValue ), value, true ); + } + + public static Test shouldNotMatch( int[] propertyValue, Object value ) + { + return new Test( Values.intArrayValue( propertyValue ), value, false ); + } + + public static Test shouldMatch( char[] propertyValue, Object value ) + { + return new Test( Values.charArrayValue( propertyValue ), value, true ); + } + + public static Test shouldNotMatch( char[] propertyValue, Object value ) + { + return new Test( Values.charArrayValue( propertyValue ), value, false ); + } + + public static Test shouldMatch( String[] propertyValue, Object value ) + { + return new Test( Values.stringArrayValue( propertyValue ), value, true ); + } + + public static Test shouldNotMatch( String[] propertyValue, Object value ) + { + return new Test( Values.stringArrayValue( propertyValue ), value, false ); + } + + private static class Test + { + final Value a; + final Value b; + final boolean shouldMatch; + + private Test( Value a, Object b, boolean shouldMatch ) + { + this.a = a; + this.b = Values.of( b ); + this.shouldMatch = shouldMatch; + } + + @Override + public String toString() + { + return String.format( "%s %s %s", a, shouldMatch ? "==" : "!=", b ); + + } + + void checkAssertion() + { + if ( shouldMatch ) + { + assertEquality( a, b ); + } + else + { + assertNonEquality( a, b ); + } + } + + void assertEquality( Value a, Value b ) + { + assertTrue( String.format( "Expected %s to be equal to %s but it wasn't.", a, b ), a.equals( b ) ); + assertTrue( String.format( "Expected %s and %s to share hash, but they didn't.", a, b ), + a.hashCode() == b.hashCode() ); + } + + void assertNonEquality( Value a, Value b ) + { + assertFalse( String.format( "Expected %s to not be equal to %s but it was.", a, b ), a.equals( b ) ); + } + } +} diff --git a/community/values/src/test/java/org/neo4j/values/ValuesTest.java b/community/values/src/test/java/org/neo4j/values/ValuesTest.java index bbbed5d9af478..a446f8e7d81e4 100644 --- a/community/values/src/test/java/org/neo4j/values/ValuesTest.java +++ b/community/values/src/test/java/org/neo4j/values/ValuesTest.java @@ -44,7 +44,7 @@ public class ValuesTest { @Test - public void shouldBeEqual() + public void shouldBeEqualToItself() { assertEqual( booleanValue( false ), booleanValue( false ) ); assertEqual( byteValue( (byte)0 ), byteValue( (byte)0 ) ); @@ -86,6 +86,12 @@ public void shouldBeEqual() assertEqual( stringArrayValue( new String[]{"hi"} ), stringArrayValue( new String[]{"hi"} ) ); } + @Test + public void shouldHandleCoercion() + { + + } + private void assertEqual( Value a, Value b ) { assertTrue( "should be equal", a.equals( b ) );