From f44a08d9d5810269bd0c3fae9ac304e06c5f7d8a Mon Sep 17 00:00:00 2001 From: oowekyala Date: Fri, 14 Jul 2017 16:32:02 +0200 Subject: [PATCH] Fix memo tests --- .../pmd/lang/java/oom/DataStructureTest.java | 112 ++++++------------ 1 file changed, 34 insertions(+), 78 deletions(-) diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/DataStructureTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/DataStructureTest.java index 8d3e2ae8a2..37af812f68 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/DataStructureTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/DataStructureTest.java @@ -130,100 +130,56 @@ public class DataStructureTest extends ParserTst { @Test public void memoizationTest() { - - final List res = new ArrayList<>(); - final PackageStats toplevel = Metrics.getTopLevelPackageStats(); - ASTCompilationUnit acu = parseAndVisitForClass15(MetricsVisitorTestData.class); - acu.jjtAccept(new JavaParserVisitorReducedAdapter() { - @Override - public Object visit(ASTMethodOrConstructorDeclaration node, Object data) { - res.add((int) toplevel.compute(opMetricKey, node, true, Version.STANDARD)); - return super.visit(node, data); - } - - - @Override - public Object visit(ASTAnyTypeDeclaration node, Object data) { - res.add((int) toplevel.compute(classMetricKey, node, true, Version.STANDARD)); - return super.visit(node, data); - } - }, null); - - System.out.println(); - - final List cmp = new ArrayList<>(); - - acu.jjtAccept(new JavaParserVisitorReducedAdapter() { - @Override - public Object visit(ASTMethodOrConstructorDeclaration node, Object data) { - cmp.add((int) toplevel.compute(opMetricKey, node, false, Version.STANDARD)); - return super.visit(node, data); - } - - - @Override - public Object visit(ASTAnyTypeDeclaration node, Object data) { - cmp.add((int) toplevel.compute(classMetricKey, node, false, Version.STANDARD)); - return super.visit(node, data); - } - }, null); - - assertEquals(res, cmp); + List expected = visitWith(acu, true); + List real = visitWith(acu, false); + assertEquals(expected, real); } @Test public void forceMemoizationTest() { - - final List res = new ArrayList<>(); - final PackageStats toplevel = Metrics.getTopLevelPackageStats(); - ASTCompilationUnit acu = parseAndVisitForClass15(MetricsVisitorTestData.class); - acu.jjtAccept(new JavaParserVisitorReducedAdapter() { - @Override - public Object visit(ASTMethodOrConstructorDeclaration node, Object data) { - res.add((int) toplevel.compute(opMetricKey, node, true, Version.STANDARD)); - return super.visit(node, data); - } + List reference = visitWith(acu, true); + List real = visitWith(acu, true); + assertEquals(reference.size(), real.size()); - @Override - public Object visit(ASTAnyTypeDeclaration node, Object data) { - res.add((int) toplevel.compute(classMetricKey, node, true, Version.STANDARD)); - return super.visit(node, data); - } - }, null); - - System.out.println(); - - final List cmp = new ArrayList<>(); - - acu.jjtAccept(new JavaParserVisitorReducedAdapter() { - @Override - public Object visit(ASTMethodOrConstructorDeclaration node, Object data) { - cmp.add((int) toplevel.compute(opMetricKey, node, true, Version.STANDARD)); - return super.visit(node, data); - } - - - @Override - public Object visit(ASTAnyTypeDeclaration node, Object data) { - cmp.add((int) toplevel.compute(classMetricKey, node, true, Version.STANDARD)); - return super.visit(node, data); - } - }, null); - - for (int i = 0; i < res.size(); i++) { - assertNotEquals(res.get(i), cmp.get(i)); + // we force recomputation so each result should be different + for (int i = 0; i < reference.size(); i++) { + assertNotEquals(reference.get(i), real.get(i)); } - } + private List visitWith(ASTCompilationUnit acu, final boolean force) { + final PackageStats toplevel = Metrics.getTopLevelPackageStats(); + + final List result = new ArrayList<>(); + + acu.jjtAccept(new JavaParserVisitorReducedAdapter() { + @Override + public Object visit(ASTMethodOrConstructorDeclaration node, Object data) { + result.add((int) toplevel.compute(opMetricKey, node, force, Version.STANDARD)); + return super.visit(node, data); + } + + + @Override + public Object visit(ASTAnyTypeDeclaration node, Object data) { + result.add((int) toplevel.compute(classMetricKey, node, force, Version.STANDARD)); + return super.visit(node, data); + } + }, null); + + return result; + } + + + /** * Test metric. */