diff --git a/subprojects/common/analysis/src/test/java/hu/bme/mit/theta/analysis/algorithm/ArgStructuralEqualityTest.java b/subprojects/common/analysis/src/test/java/hu/bme/mit/theta/analysis/algorithm/ArgStructuralEqualityTest.java new file mode 100644 index 0000000000..9544102d5e --- /dev/null +++ b/subprojects/common/analysis/src/test/java/hu/bme/mit/theta/analysis/algorithm/ArgStructuralEqualityTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2023 Budapest University of Technology and Economics + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package hu.bme.mit.theta.analysis.algorithm; + +import hu.bme.mit.theta.analysis.Action; +import hu.bme.mit.theta.analysis.State; +import hu.bme.mit.theta.analysis.stubs.ActionStub; +import hu.bme.mit.theta.analysis.stubs.PartialOrdStub; +import hu.bme.mit.theta.analysis.stubs.StateStub; +import org.junit.Assert; +import org.junit.Test; + +public class ArgStructuralEqualityTest { + + private static ARG createArg(boolean variant) { + ARG arg = ARG.create(new PartialOrdStub()); + Action act = new ActionStub("A"); + + ArgNode s0 = arg.createInitNode(new StateStub("s0"), false); + ArgNode s10 = arg.createSuccNode(s0, act, new StateStub("s10"), + false); + ArgNode s20 = arg.createSuccNode(s10, act, new StateStub("s20"), + true); + ArgNode s21 = arg.createSuccNode(s10, act, new StateStub("s21"), + false); + ArgNode s11 = arg.createSuccNode(s0, act, new StateStub("s11"), + true); + if(variant) { + ArgNode s12a = arg.createSuccNode(s0, act, new StateStub("s12a"), + false); + } else { + ArgNode s12b = arg.createSuccNode(s0, act, new StateStub("s12b"), + false); + } + return arg; + } + + @Test + public void testARGEquals() { + var arg1 = createArg(true); + var arg2 = createArg(true); + var arg3 = createArg(false); + + Assert.assertNotEquals("Reference-based equality", arg1, arg2); + Assert.assertTrue("Structural equality (true)", ArgStructuralEquality.equals(arg1, arg2)); + Assert.assertFalse("Structural equality (false)", ArgStructuralEquality.equals(arg1, arg3)); + } + + @Test + public void testARGHashCode() { + var arg1 = createArg(true); + var arg2 = createArg(true); + var arg3 = createArg(false); + + Assert.assertNotEquals("Reference-based hashcode", arg1.hashCode(), arg2.hashCode()); + Assert.assertEquals("Structural hashcode (true)", ArgStructuralEquality.hashCode(arg1), ArgStructuralEquality.hashCode(arg2)); + Assert.assertNotEquals("Structural hashcode (false)", ArgStructuralEquality.hashCode(arg1), ArgStructuralEquality.hashCode(arg3)); + } + +} diff --git a/subprojects/common/analysis/src/test/java/hu/bme/mit/theta/analysis/stubs/ActionStub.java b/subprojects/common/analysis/src/test/java/hu/bme/mit/theta/analysis/stubs/ActionStub.java index 43bc32dc39..80cc43ed0b 100644 --- a/subprojects/common/analysis/src/test/java/hu/bme/mit/theta/analysis/stubs/ActionStub.java +++ b/subprojects/common/analysis/src/test/java/hu/bme/mit/theta/analysis/stubs/ActionStub.java @@ -17,6 +17,8 @@ import hu.bme.mit.theta.analysis.Action; +import java.util.Objects; + public class ActionStub implements Action { private final String label; @@ -29,4 +31,17 @@ public ActionStub(final String label) { public String toString() { return label; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ActionStub that = (ActionStub) o; + return Objects.equals(label, that.label); + } + + @Override + public int hashCode() { + return Objects.hash(label); + } } diff --git a/subprojects/common/analysis/src/test/java/hu/bme/mit/theta/analysis/stubs/StateStub.java b/subprojects/common/analysis/src/test/java/hu/bme/mit/theta/analysis/stubs/StateStub.java index e2206a07e3..ccfddf4410 100644 --- a/subprojects/common/analysis/src/test/java/hu/bme/mit/theta/analysis/stubs/StateStub.java +++ b/subprojects/common/analysis/src/test/java/hu/bme/mit/theta/analysis/stubs/StateStub.java @@ -17,6 +17,8 @@ import hu.bme.mit.theta.analysis.State; +import java.util.Objects; + public class StateStub implements State { private final String label; @@ -35,4 +37,16 @@ public String toString() { return label; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + StateStub stateStub = (StateStub) o; + return Objects.equals(label, stateStub.label); + } + + @Override + public int hashCode() { + return Objects.hash(label); + } }