diff options
author | 2023-06-25 21:42:43 +0200 | |
---|---|---|
committer | 2023-06-29 02:23:00 +0200 | |
commit | 79a3fd092c79efc204d2980b07728258372871c4 (patch) | |
tree | e515ff637c1d1288281284d62d33175dd5d19f35 /subprojects/store-query/src/test | |
parent | feat: ordered query ResultSet (diff) | |
download | refinery-79a3fd092c79efc204d2980b07728258372871c4.tar.gz refinery-79a3fd092c79efc204d2980b07728258372871c4.tar.zst refinery-79a3fd092c79efc204d2980b07728258372871c4.zip |
refactor: query equality and hash code
Allow computing hash codes up to the renaming of variables.
Also introduces CheckLiteral instead of AssumeLiteral for more straightforward
naming.
Diffstat (limited to 'subprojects/store-query/src/test')
4 files changed, 76 insertions, 9 deletions
diff --git a/subprojects/store-query/src/test/java/tools/refinery/store/query/dnf/DnfBuilderLiteralEliminationTest.java b/subprojects/store-query/src/test/java/tools/refinery/store/query/dnf/DnfBuilderLiteralEliminationTest.java index e17496e3..9d9bf3c0 100644 --- a/subprojects/store-query/src/test/java/tools/refinery/store/query/dnf/DnfBuilderLiteralEliminationTest.java +++ b/subprojects/store-query/src/test/java/tools/refinery/store/query/dnf/DnfBuilderLiteralEliminationTest.java | |||
@@ -20,7 +20,7 @@ import tools.refinery.store.representation.Symbol; | |||
20 | import java.util.List; | 20 | import java.util.List; |
21 | 21 | ||
22 | import static org.hamcrest.MatcherAssert.assertThat; | 22 | import static org.hamcrest.MatcherAssert.assertThat; |
23 | import static tools.refinery.store.query.literal.Literals.assume; | 23 | import static tools.refinery.store.query.literal.Literals.check; |
24 | import static tools.refinery.store.query.literal.Literals.not; | 24 | import static tools.refinery.store.query.literal.Literals.not; |
25 | import static tools.refinery.store.query.tests.QueryMatchers.structurallyEqualTo; | 25 | import static tools.refinery.store.query.tests.QueryMatchers.structurallyEqualTo; |
26 | 26 | ||
@@ -47,7 +47,7 @@ class DnfBuilderLiteralEliminationTest { | |||
47 | void eliminateTrueAssumptionTest() { | 47 | void eliminateTrueAssumptionTest() { |
48 | var actual = Dnf.builder() | 48 | var actual = Dnf.builder() |
49 | .parameters(p, q) | 49 | .parameters(p, q) |
50 | .clause(assume(BoolTerms.constant(true)), friendView.call(p, q)) | 50 | .clause(check(BoolTerms.constant(true)), friendView.call(p, q)) |
51 | .build(); | 51 | .build(); |
52 | var expected = Dnf.builder().parameters(p, q).clause(friendView.call(p, q)).build(); | 52 | var expected = Dnf.builder().parameters(p, q).clause(friendView.call(p, q)).build(); |
53 | 53 | ||
@@ -75,7 +75,7 @@ class DnfBuilderLiteralEliminationTest { | |||
75 | var actual = Dnf.builder() | 75 | var actual = Dnf.builder() |
76 | .parameters(p, q) | 76 | .parameters(p, q) |
77 | .clause(friendView.call(p, q)) | 77 | .clause(friendView.call(p, q)) |
78 | .clause(friendView.call(q, p), assume(BoolTerms.constant(value))) | 78 | .clause(friendView.call(q, p), check(BoolTerms.constant(value))) |
79 | .build(); | 79 | .build(); |
80 | var expected = Dnf.builder().parameters(p, q).clause(friendView.call(p, q)).build(); | 80 | var expected = Dnf.builder().parameters(p, q).clause(friendView.call(p, q)).build(); |
81 | 81 | ||
diff --git a/subprojects/store-query/src/test/java/tools/refinery/store/query/dnf/HashCodeTest.java b/subprojects/store-query/src/test/java/tools/refinery/store/query/dnf/HashCodeTest.java new file mode 100644 index 00000000..0c8eaeed --- /dev/null +++ b/subprojects/store-query/src/test/java/tools/refinery/store/query/dnf/HashCodeTest.java | |||
@@ -0,0 +1,67 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.query.dnf; | ||
7 | |||
8 | import org.junit.jupiter.api.Test; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | import tools.refinery.store.query.term.Variable; | ||
11 | import tools.refinery.store.query.view.AnySymbolView; | ||
12 | import tools.refinery.store.query.view.KeyOnlyView; | ||
13 | import tools.refinery.store.representation.Symbol; | ||
14 | |||
15 | import static org.hamcrest.MatcherAssert.assertThat; | ||
16 | import static org.hamcrest.Matchers.is; | ||
17 | import static org.hamcrest.Matchers.not; | ||
18 | |||
19 | class HashCodeTest { | ||
20 | private static final Symbol<Boolean> person = Symbol.of("Person", 1); | ||
21 | private static final Symbol<Boolean> friend = Symbol.of("friend", 2); | ||
22 | private static final AnySymbolView personView = new KeyOnlyView<>(person); | ||
23 | private static final AnySymbolView friendView = new KeyOnlyView<>(friend); | ||
24 | private static final NodeVariable p = Variable.of("p"); | ||
25 | private static final NodeVariable q = Variable.of("q"); | ||
26 | |||
27 | @Test | ||
28 | void flatEqualsTest() { | ||
29 | var expected = Dnf.builder("Expected").parameters(q).clause(personView.call(q)).build(); | ||
30 | var actual = Dnf.builder("Actual").parameters(p).clause(personView.call(p)).build(); | ||
31 | |||
32 | assertThat(actual.hashCodeWithSubstitution(), is(expected.hashCodeWithSubstitution())); | ||
33 | } | ||
34 | |||
35 | @Test | ||
36 | void flatNotEqualsTest() { | ||
37 | var expected = Dnf.builder("Expected").parameters(q).clause(friendView.call(q, q)).build(); | ||
38 | var actual = Dnf.builder("Actual").parameters(p).clause(friendView.call(p, q)).build(); | ||
39 | |||
40 | assertThat(actual.hashCodeWithSubstitution(), not(expected.hashCodeWithSubstitution())); | ||
41 | } | ||
42 | |||
43 | @Test | ||
44 | void deepEqualsTest() { | ||
45 | var expected2 = Dnf.builder("Expected2").parameters(p).clause(personView.call(p)).build(); | ||
46 | var expected = Dnf.builder("Expected").parameters(q).clause( | ||
47 | expected2.call(q) | ||
48 | ).build(); | ||
49 | var actual = Dnf.builder("Actual").parameters(q).clause( | ||
50 | expected2.call(q) | ||
51 | ).build(); | ||
52 | |||
53 | assertThat(actual.hashCodeWithSubstitution(), is(expected.hashCodeWithSubstitution())); | ||
54 | } | ||
55 | |||
56 | @Test | ||
57 | void deepNotEqualsTest() { | ||
58 | var expected = Dnf.builder("Expected").parameters(q).clause( | ||
59 | Dnf.builder("Expected2").parameters(p).clause(personView.call(p)).build().call(q) | ||
60 | ).build(); | ||
61 | var actual = Dnf.builder("Actual").parameters(q).clause( | ||
62 | Dnf.builder("Actual2").parameters(p).clause(personView.call(p)).build().call(q) | ||
63 | ).build(); | ||
64 | |||
65 | assertThat(actual.hashCodeWithSubstitution(), not(expected.hashCodeWithSubstitution())); | ||
66 | } | ||
67 | } | ||
diff --git a/subprojects/store-query/src/test/java/tools/refinery/store/query/dnf/VariableDirectionTest.java b/subprojects/store-query/src/test/java/tools/refinery/store/query/dnf/VariableDirectionTest.java index c52d26b2..c509da4d 100644 --- a/subprojects/store-query/src/test/java/tools/refinery/store/query/dnf/VariableDirectionTest.java +++ b/subprojects/store-query/src/test/java/tools/refinery/store/query/dnf/VariableDirectionTest.java | |||
@@ -28,7 +28,7 @@ import static org.hamcrest.Matchers.hasItem; | |||
28 | import static org.hamcrest.Matchers.not; | 28 | import static org.hamcrest.Matchers.not; |
29 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | 29 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
30 | import static org.junit.jupiter.api.Assertions.assertThrows; | 30 | import static org.junit.jupiter.api.Assertions.assertThrows; |
31 | import static tools.refinery.store.query.literal.Literals.assume; | 31 | import static tools.refinery.store.query.literal.Literals.check; |
32 | import static tools.refinery.store.query.literal.Literals.not; | 32 | import static tools.refinery.store.query.literal.Literals.not; |
33 | import static tools.refinery.store.query.term.int_.IntTerms.*; | 33 | import static tools.refinery.store.query.term.int_.IntTerms.*; |
34 | 34 | ||
@@ -349,7 +349,7 @@ class VariableDirectionTest { | |||
349 | .clause( | 349 | .clause( |
350 | friendView.call(p, q), | 350 | friendView.call(p, q), |
351 | ageView.call(q, x), | 351 | ageView.call(q, x), |
352 | assume(greaterEq(x, y)) | 352 | check(greaterEq(x, y)) |
353 | ) | 353 | ) |
354 | .build(); | 354 | .build(); |
355 | 355 | ||
@@ -381,7 +381,7 @@ class VariableDirectionTest { | |||
381 | void boundPrivateDataVariableOutputTest(Literal literal) { | 381 | void boundPrivateDataVariableOutputTest(Literal literal) { |
382 | var dnfWithInput = Dnf.builder("WithInput") | 382 | var dnfWithInput = Dnf.builder("WithInput") |
383 | .parameter(x, ParameterDirection.IN) | 383 | .parameter(x, ParameterDirection.IN) |
384 | .clause(assume(greaterEq(x, constant(24)))) | 384 | .clause(check(greaterEq(x, constant(24)))) |
385 | .build(); | 385 | .build(); |
386 | var builder = Dnf.builder().clause(dnfWithInput.call(x), literal); | 386 | var builder = Dnf.builder().clause(dnfWithInput.call(x), literal); |
387 | var dnf = assertDoesNotThrow(builder::build); | 387 | var dnf = assertDoesNotThrow(builder::build); |
diff --git a/subprojects/store-query/src/test/java/tools/refinery/store/query/term/TermSubstitutionTest.java b/subprojects/store-query/src/test/java/tools/refinery/store/query/term/TermSubstitutionTest.java index 1cbc101a..1fae2492 100644 --- a/subprojects/store-query/src/test/java/tools/refinery/store/query/term/TermSubstitutionTest.java +++ b/subprojects/store-query/src/test/java/tools/refinery/store/query/term/TermSubstitutionTest.java | |||
@@ -9,8 +9,8 @@ import org.junit.jupiter.api.Assertions; | |||
9 | import org.junit.jupiter.params.ParameterizedTest; | 9 | import org.junit.jupiter.params.ParameterizedTest; |
10 | import org.junit.jupiter.params.provider.Arguments; | 10 | import org.junit.jupiter.params.provider.Arguments; |
11 | import org.junit.jupiter.params.provider.MethodSource; | 11 | import org.junit.jupiter.params.provider.MethodSource; |
12 | import tools.refinery.store.query.dnf.Dnf; | 12 | import tools.refinery.store.query.equality.DnfEqualityChecker; |
13 | import tools.refinery.store.query.equality.LiteralEqualityHelper; | 13 | import tools.refinery.store.query.equality.SubstitutingLiteralEqualityHelper; |
14 | import tools.refinery.store.query.substitution.Substitution; | 14 | import tools.refinery.store.query.substitution.Substitution; |
15 | import tools.refinery.store.query.term.bool.BoolTerms; | 15 | import tools.refinery.store.query.term.bool.BoolTerms; |
16 | import tools.refinery.store.query.term.int_.IntTerms; | 16 | import tools.refinery.store.query.term.int_.IntTerms; |
@@ -48,7 +48,7 @@ class TermSubstitutionTest { | |||
48 | void substitutionTest(AnyTerm term) { | 48 | void substitutionTest(AnyTerm term) { |
49 | var substitutedTerm1 = term.substitute(substitution); | 49 | var substitutedTerm1 = term.substitute(substitution); |
50 | Assertions.assertNotEquals(term, substitutedTerm1, "Original term is not equal to substituted term"); | 50 | Assertions.assertNotEquals(term, substitutedTerm1, "Original term is not equal to substituted term"); |
51 | var helper = new LiteralEqualityHelper(Dnf::equals, List.of(), List.of()); | 51 | var helper = new SubstitutingLiteralEqualityHelper(DnfEqualityChecker.DEFAULT, List.of(), List.of()); |
52 | Assertions.assertTrue(term.equalsWithSubstitution(helper, substitutedTerm1), "Terms are equal by helper"); | 52 | Assertions.assertTrue(term.equalsWithSubstitution(helper, substitutedTerm1), "Terms are equal by helper"); |
53 | // The {@link #substitution} is its own inverse. | 53 | // The {@link #substitution} is its own inverse. |
54 | var substitutedTerm2 = substitutedTerm1.substitute(substitution); | 54 | var substitutedTerm2 = substitutedTerm1.substitute(substitution); |