Skip to content

Commit

Permalink
#415: Decided to test the ParseGraph by the AutoEqualityTest.
Browse files Browse the repository at this point in the history
Although it is not a useful usecase, it is possible to create to
identical parse graphs with different scopedepth, by creating a token
that alternates its scope delimiter setting. That means that the scope
depth stored within the parse graph is actually not a cached value and
must therefor be included within the equals method. In that case, we can
test the equality of the ParseGraph by the AutoEqualityTest.
  • Loading branch information
mvanaken committed Mar 19, 2024
1 parent f31e41e commit 702c690
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 80 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/io/parsingdata/metal/data/ParseGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ public boolean equals(final Object obj) {
&& Objects.equals(head, ((ParseGraph)obj).head)
&& Objects.equals(tail, ((ParseGraph)obj).tail)
&& Objects.equals(branched, ((ParseGraph)obj).branched)
&& Objects.equals(scopeDepth, ((ParseGraph)obj).scopeDepth)
&& Objects.equals(definition, ((ParseGraph)obj).definition);
// The size and scopeDepth fields are excluded from equals() and hashCode() because it is cached data.
// The size field is excluded from equals() and hashCode() because it is cached data.
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions core/src/test/java/io/parsingdata/metal/AutoEqualityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public class AutoEqualityTest {
io.parsingdata.metal.expression.logical.Not.class,
// Data structures
CoreValue.class, ParseValue.class, ParseReference.class, ParseState.class,
NotAValue.class, ImmutableList.class,
NotAValue.class, ParseGraph.class, ImmutableList.class,
ParseValueCache.class,
// Inputs
Slice.class,
Expand All @@ -184,7 +184,7 @@ public class AutoEqualityTest {
// Utility classes.
Selection.class, ConstantFactory.class,
// Multiple constructors
Environment.class, ParseGraph.class
Environment.class
);

public static final Object OTHER_TYPE = new Object() {};
Expand Down Expand Up @@ -436,10 +436,10 @@ public void basicNoHashCollisions(final Object object, final Object same, final
}
}

public static void assertEquals(final Object o, final Object object) {
private static void assertEquals(final Object o, final Object object) {
Assertions.assertEquals(o, object, String.format("Objects should be equal:\n%s\n%s\n", o, object));
}
public static void assertNotEquals(final Object o, final Object object) {
private static void assertNotEquals(final Object o, final Object object) {
Assertions.assertNotEquals(o, object, String.format("Objects should not be equal:\n%s\n%s\n", o, object));
}

Expand Down
75 changes: 0 additions & 75 deletions core/src/test/java/io/parsingdata/metal/data/ParseGraphTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public class ParseGraphTest {
private static ParseGraph pg;
private static ParseGraph pgc;
private static ParseGraph pgl;
private static List<ParseGraph> pgs;
private static Token aDef;
private static ParseValue a;
private static ParseValue b;
Expand Down Expand Up @@ -97,7 +96,6 @@ public static void setup() {
pg = makeSimpleGraph();
pgc = makeCycleGraph();
pgl = makeLongGraph();
pgs = makeGraphList();
}

private static ParseGraph makeSimpleGraph() {
Expand Down Expand Up @@ -252,79 +250,6 @@ public void testCurrent() {
assertFalse(EMPTY.addBranch(NONE).current().isPresent());
}

private static List<ParseGraph> makeGraphList() {
return List.of(
EMPTY,
EMPTY.add(a),
EMPTY.add(b),
EMPTY.add(a).add(b),
EMPTY.add(b).add(a),

EMPTY.addBranch(t),
EMPTY.addBranch(s),
EMPTY.add(a).addBranch(s),
EMPTY.add(a).addBranch(t),
EMPTY.add(b).addBranch(s),
EMPTY.add(b).addBranch(t),
EMPTY.add(a).addBranch(t).addBranch(s),
EMPTY.add(a).addBranch(s).addBranch(t),
EMPTY.add(b).addBranch(s).addBranch(t),
EMPTY.add(b).addBranch(t).addBranch(s),

EMPTY.add(a).addBranch(t).add(a),
EMPTY.add(a).addBranch(t).add(b),
EMPTY.add(b).addBranch(s).add(a),
EMPTY.add(b).addBranch(t).add(b),
EMPTY.add(a).addBranch(t).add(a).addBranch(s),
EMPTY.add(a).addBranch(s).add(b).addBranch(t),
EMPTY.add(b).addBranch(s).add(a).addBranch(t),
EMPTY.add(b).addBranch(t).add(b).addBranch(s),

EMPTY.add(a).addBranch(t).add(a).addBranch(s).add(a),
EMPTY.add(a).addBranch(t).add(a).addBranch(s).add(b),
EMPTY.add(a).addBranch(s).add(b).addBranch(t).add(a),
EMPTY.add(a).addBranch(s).add(b).addBranch(t).add(b),
EMPTY.add(b).addBranch(s).add(a).addBranch(t).add(a),
EMPTY.add(b).addBranch(s).add(a).addBranch(t).add(b),
EMPTY.add(b).addBranch(t).add(b).addBranch(s).add(a),
EMPTY.add(b).addBranch(t).add(b).addBranch(s).add(b)
);
}

public static Stream<Arguments> nonEqualityTest() {
final List<Arguments> args = new ArrayList<>();
for (int i = 0; i < pgs.size(); i++) {
for (int j = 0; j < pgs.size(); j++) {
if (i != j) {
args.add(arguments(i + "," + j, pgs.get(i), pgs.get(j)));
}
}
}
return args.stream();
}

@ParameterizedTest
@MethodSource
public void nonEqualityTest(final String testnr, final ParseGraph first, final ParseGraph second) {
AutoEqualityTest.assertNotEquals(first, second);
AutoEqualityTest.assertNotEquals(first.hashCode(), second.hashCode());
}

public static Stream<Arguments> equalityTest() {
final List<Arguments> args = new ArrayList<>();
for (int i = 0; i < pgs.size(); i++) {
args.add(arguments(i, pgs.get(i), pgs.get(i)));
}
return args.stream();
}

@ParameterizedTest
@MethodSource
public void equalityTest(final int testnr, final ParseGraph first, final ParseGraph second) {
AutoEqualityTest.assertEquals(first, second);
AutoEqualityTest.assertEquals(first.hashCode(), second.hashCode());
}

public static Stream<Arguments> scopeDepthTest() {
return Stream.of(
// Add branches with and without scope delimited tokens.
Expand Down

0 comments on commit 702c690

Please sign in to comment.