From 1789dec5b18f884722deedd35ff771ca5cf79a76 Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Fri, 6 Oct 2023 14:09:21 +0200 Subject: [PATCH] added hashCode contract for getPatternMatchFingerprint and stability tests --- .../vallang/specification/IValueTests.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/test/java/io/usethesource/vallang/specification/IValueTests.java b/src/test/java/io/usethesource/vallang/specification/IValueTests.java index 32b49d94f..f07643367 100644 --- a/src/test/java/io/usethesource/vallang/specification/IValueTests.java +++ b/src/test/java/io/usethesource/vallang/specification/IValueTests.java @@ -11,6 +11,15 @@ import org.junit.jupiter.params.provider.ArgumentsSource; import io.usethesource.vallang.IConstructor; +import io.usethesource.vallang.IInteger; +import io.usethesource.vallang.IList; +import io.usethesource.vallang.IMap; +import io.usethesource.vallang.INode; +import io.usethesource.vallang.IRational; +import io.usethesource.vallang.IReal; +import io.usethesource.vallang.ISet; +import io.usethesource.vallang.IString; +import io.usethesource.vallang.ITuple; import io.usethesource.vallang.IValue; import io.usethesource.vallang.IValueFactory; import io.usethesource.vallang.ValueProvider; @@ -44,6 +53,31 @@ public void testHashCodeContract(IValue val1, IValue val2) { assertTrue(!val1.equals(val2) || val1.hashCode() == val2.hashCode()); } + + @ParameterizedTest @ArgumentsSource(ValueProvider.class) + public void testFingerprintContract(IValue val1, IValue val2) { + if (val1.equals(val2)) { + assertEquals(val1.getPatternMatchFingerprint(), val2.getPatternMatchFingerprint(), "" + val1.toString() + " and " + val2.toString() + " are equal but do not have the same fingerprint?"); + } + assertTrue(!val1.equals(val2) || val1.getPatternMatchFingerprint() == val2.getPatternMatchFingerprint()); + } + + @ParameterizedTest @ArgumentsSource(ValueProvider.class) + public void testFingerprintStability(IInteger integer, IString string, IReal real, IRational rational, IList list, ISet set, IMap map, ITuple tuple, IConstructor constructor, INode node) { + // if we really want to change these codes, we should be aware that we are breaking all previously compiled and released Rascal code. + + assertEquals(integer.hashCode(), integer.getPatternMatchFingerprint()); + assertEquals(string.hashCode(), string.getPatternMatchFingerprint()); + assertEquals(real.hashCode(), real.getPatternMatchFingerprint()); + assertEquals(rational.hashCode(), rational.getPatternMatchFingerprint()); + assertEquals("list".hashCode(), list.getPatternMatchFingerprint()); + assertEquals("set".hashCode(), set.getPatternMatchFingerprint()); + assertEquals("map".hashCode(), map.getPatternMatchFingerprint()); + assertEquals("tuple".hashCode() << 2 + tuple.arity(), tuple.getPatternMatchFingerprint()); + assertEquals(constructor.getName().hashCode() << 2 + constructor.arity(), constructor.getPatternMatchFingerprint()); + assertEquals(node.getName().hashCode() << 2 + node.arity(), node.getPatternMatchFingerprint()); + } + @ParameterizedTest @ArgumentsSource(ValueProvider.class) public void testWysiwyg(IValueFactory vf, TypeStore store, IValue val) throws FactTypeUseException, IOException { StandardTextReader reader = new StandardTextReader();