From bd16e0c2938396ebfa5f6c53a0ecc34dc691a66d Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 24 Aug 2016 16:00:34 +0300 Subject: [PATCH] Cleanup (graph algorithms' tests) --- .../intellij/util/graph/ChunkGraphTest.java | 62 +++++++------------ .../intellij/util/graph/GraphTestCase.java | 9 ++- .../util/graph/KShortestPathsFinderTest.java | 23 +++++-- .../intellij/util/graph/SearchCyclesTest.java | 57 ++++++++--------- .../intellij/util/graph/ShortestPathTest.java | 52 +++++++++------- 5 files changed, 106 insertions(+), 97 deletions(-) diff --git a/platform/platform-tests/testSrc/com/intellij/util/graph/ChunkGraphTest.java b/platform/platform-tests/testSrc/com/intellij/util/graph/ChunkGraphTest.java index b8bb8564d024..8ddc4e41f814 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/graph/ChunkGraphTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/graph/ChunkGraphTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,16 +16,19 @@ package com.intellij.util.graph; import com.intellij.util.Chunk; +import com.intellij.util.containers.ContainerUtil; +import org.junit.Test; import java.util.*; +import static org.junit.Assert.assertTrue; + /** * @author Eugene Zhuravlev - * Date: Sep 27, 2004 + * @since Sep 27, 2004 */ -@SuppressWarnings({"HardCodedStringLiteral"}) public class ChunkGraphTest extends GraphTestCase { - + @Test public void testGraph1() { final Map arcs = new HashMap<>(); arcs.put("a", "b"); @@ -39,21 +42,22 @@ public class ChunkGraphTest extends GraphTestCase { final List> expectedNodes = new ArrayList<>(); Chunk A = new Chunk<>("a"); expectedNodes.add(A); - Chunk BC = new Chunk<>(toSet("b", "c")); + Chunk BC = new Chunk<>(ContainerUtil.newHashSet("b", "c")); expectedNodes.add(BC); - Chunk DE = new Chunk<>(toSet("d", "e")); + Chunk DE = new Chunk<>(ContainerUtil.newHashSet("d", "e")); expectedNodes.add(DE); - checkVertices(expectedNodes, graph.getNodes().iterator()); + checkVertices(expectedNodes, graph.getNodes()); final Map, Set>> expectedArcs = new HashMap<>(); - expectedArcs.put(A, toSet()); - expectedArcs.put(BC, toSet(A)); - expectedArcs.put(DE, toSet(BC)); + expectedArcs.put(A, ContainerUtil.newHashSet()); + expectedArcs.put(BC, ContainerUtil.newHashSet(A)); + expectedArcs.put(DE, ContainerUtil.newHashSet(BC)); checkArcs(expectedArcs, graph); } + @Test public void testGraph2() { final Map arcs = new HashMap<>(); arcs.put("a", "b"); @@ -64,52 +68,32 @@ public class ChunkGraphTest extends GraphTestCase { final Graph> graph = getAlgorithmsInstance().computeSCCGraph(initGraph(arcs)); final List> expectedNodes = new ArrayList<>(); - Chunk ABC = new Chunk<>(toSet("a", "b", "c")); + Chunk ABC = new Chunk<>(ContainerUtil.newHashSet("a", "b", "c")); expectedNodes.add(ABC); Chunk D = new Chunk<>("d"); expectedNodes.add(D); - checkVertices(expectedNodes, graph.getNodes().iterator()); + checkVertices(expectedNodes, graph.getNodes()); final Map, Set>> expectedArcs = new HashMap<>(); - expectedArcs.put(ABC, toSet()); - expectedArcs.put(D, toSet(ABC)); + expectedArcs.put(ABC, ContainerUtil.newHashSet()); + expectedArcs.put(D, ContainerUtil.newHashSet(ABC)); checkArcs(expectedArcs, graph); } private static void checkArcs(Map, Set>> expectedArcs, Graph> graph) { for (Chunk chunk : graph.getNodes()) { - final List> ins = new ArrayList<>(); - final Iterator> insIterator = graph.getIn(chunk); - while (insIterator.hasNext()) { - ins.add(insIterator.next()); - } - final Set> expectedIns = expectedArcs.get(chunk); + List> ins = ContainerUtil.newArrayList(() -> graph.getIn(chunk)); + Set> expectedIns = expectedArcs.get(chunk); assertTrue(expectedIns.size() == ins.size()); assertTrue(expectedIns.equals(new HashSet<>(ins))); } } - private static Set toSet(T... strings) { - return new HashSet<>(Arrays.asList(strings)); - } - - private static Set> toSet() { - return new HashSet<>(); - } - - private static Set> toSet(Chunk c) { - return Collections.singleton(c); - } - - - private static void checkVertices(List> expected, Iterator> nodes) { - List> realNodes = new ArrayList<>(); - while (nodes.hasNext()) { - realNodes.add(nodes.next()); - } + private static void checkVertices(List> expected, Iterable> nodes) { + List> realNodes = ContainerUtil.newArrayList(nodes); assertTrue(expected.size() == realNodes.size()); assertTrue(new HashSet<>(expected).equals(new HashSet<>(realNodes))); } -} +} \ No newline at end of file diff --git a/platform/platform-tests/testSrc/com/intellij/util/graph/GraphTestCase.java b/platform/platform-tests/testSrc/com/intellij/util/graph/GraphTestCase.java index 418c4811c3ea..06642c019a5d 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/graph/GraphTestCase.java +++ b/platform/platform-tests/testSrc/com/intellij/util/graph/GraphTestCase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,15 +16,14 @@ package com.intellij.util.graph; import com.intellij.util.graph.impl.GraphAlgorithmsImpl; -import junit.framework.TestCase; import java.util.*; /** * @author nik */ -public abstract class GraphTestCase extends TestCase { - protected static GraphAlgorithmsImpl getAlgorithmsInstance() { +public abstract class GraphTestCase { + protected static GraphAlgorithms getAlgorithmsInstance() { return new GraphAlgorithmsImpl(); } @@ -60,4 +59,4 @@ public abstract class GraphTestCase extends TestCase { } }; } -} +} \ No newline at end of file diff --git a/platform/platform-tests/testSrc/com/intellij/util/graph/KShortestPathsFinderTest.java b/platform/platform-tests/testSrc/com/intellij/util/graph/KShortestPathsFinderTest.java index c99004f7bc72..375b328ab061 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/graph/KShortestPathsFinderTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/graph/KShortestPathsFinderTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,9 +15,11 @@ */ package com.intellij.util.graph; +import com.intellij.openapi.application.ModalityState; import com.intellij.openapi.progress.EmptyProgressIndicator; import com.intellij.openapi.util.text.StringUtil; import com.intellij.testFramework.UsefulTestCase; +import org.junit.Test; import java.util.*; @@ -25,6 +27,7 @@ import java.util.*; * @author nik */ public class KShortestPathsFinderTest extends GraphTestCase { + @Test public void testEmpty() { Map graph = new HashMap<>(); graph.put("s", ""); @@ -32,6 +35,7 @@ public class KShortestPathsFinderTest extends GraphTestCase { doTest(graph); } + @Test public void testOneEdge() { final Map graph = new HashMap<>(); graph.put("s", "t"); @@ -39,6 +43,7 @@ public class KShortestPathsFinderTest extends GraphTestCase { doTest(graph, "st"); } + @Test public void testNoPaths() { final Map graph = new HashMap<>(); graph.put("s", "a"); @@ -48,12 +53,14 @@ public class KShortestPathsFinderTest extends GraphTestCase { doTest(graph); } + @Test public void testOneVertex() { Map graph = new HashMap<>(); graph.put("s", ""); doTest(graph, "s", "s", 5, "s"); } + @Test public void testTwoPaths() { final Map graph = new HashMap<>(); graph.put("s", "ta"); @@ -62,6 +69,7 @@ public class KShortestPathsFinderTest extends GraphTestCase { doTest(graph, "st", "sat"); } + @Test public void testManyEdgesToTarget() { final Map graph = new HashMap<>(); graph.put("s", "a"); @@ -73,6 +81,7 @@ public class KShortestPathsFinderTest extends GraphTestCase { doTest(graph, "sat", "sabt", "sabct", "sabcdt"); } + @Test public void testManyEdgesFromSource() { final Map graph = new HashMap<>(); graph.put("s", "abcdt"); @@ -84,6 +93,7 @@ public class KShortestPathsFinderTest extends GraphTestCase { doTest(graph, "st", "sdt", "scdt", "sbcdt", "sabcdt"); } + @Test public void testTwoParts() { final Map graph = new HashMap<>(); graph.put("s", "ab"); @@ -96,6 +106,7 @@ public class KShortestPathsFinderTest extends GraphTestCase { doTest(graph, "sbct", "sabct", "sbdet", "sabdet"); } + @Test public void testHangingEdges() { final Map graph = new HashMap<>(); graph.put("s", "ae"); @@ -108,6 +119,7 @@ public class KShortestPathsFinderTest extends GraphTestCase { doTest(graph, "set"); } + @Test public void testSimpleCycle() { final Map graph = new HashMap<>(); graph.put("s", "t"); @@ -115,6 +127,7 @@ public class KShortestPathsFinderTest extends GraphTestCase { doTest(graph, 4, "st", "stst", "ststst", "stststst"); } + @Test public void testComplexCycle() { final Map graph = new HashMap<>(); graph.put("s", "p"); @@ -125,6 +138,7 @@ public class KShortestPathsFinderTest extends GraphTestCase { doTest(graph, 5, "spt", "spqt", "spqvpt", "spqvpqt", "spqvpqvpt"); } + @Test public void testHeap() { final Map graph = new HashMap<>(); graph.put("s", "a"); @@ -138,6 +152,7 @@ public class KShortestPathsFinderTest extends GraphTestCase { doTest(graph, "sabct", "sadeft", "sabdeft", "sabcdeft"); } + @Test public void testBigHeap() { final Map graph = new HashMap<>(); graph.put("s", "lo"); @@ -170,8 +185,8 @@ public class KShortestPathsFinderTest extends GraphTestCase { } private static void doTest(Map graph, final String start, final String finish, final int k, String... expectedPaths) { - final Graph generator = initGraph(graph); - final List> paths = getAlgorithmsInstance().findKShortestPaths(generator, start, finish, k, new EmptyProgressIndicator()); + Graph generator = initGraph(graph); + List> paths = getAlgorithmsInstance().findKShortestPaths(generator, start, finish, k, new EmptyProgressIndicator(ModalityState.NON_MODAL)); List pathStrings = new ArrayList<>(); Set sizes = new HashSet<>(); for (List path : paths) { @@ -185,4 +200,4 @@ public class KShortestPathsFinderTest extends GraphTestCase { UsefulTestCase.assertOrderedEquals(pathStrings, expectedPaths); } } -} +} \ No newline at end of file diff --git a/platform/platform-tests/testSrc/com/intellij/util/graph/SearchCyclesTest.java b/platform/platform-tests/testSrc/com/intellij/util/graph/SearchCyclesTest.java index df69e4020356..8480731b3e6b 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/graph/SearchCyclesTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/graph/SearchCyclesTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,20 +17,23 @@ package com.intellij.util.graph; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.testFramework.UsefulTestCase; +import org.junit.Test; import java.util.HashMap; -import java.util.HashSet; -import java.util.List; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; /** - * User: anna - * Date: Feb 13, 2005 + * @author anna + * @since Feb 13, 2005 */ public class SearchCyclesTest extends GraphTestCase { - public void test1() throws Exception{ - final HashMap graph = new HashMap<>(); + @Test + public void test1() { + final Map graph = new HashMap<>(); graph.put("a", "bd"); graph.put("b", "d"); graph.put("c", "a"); @@ -39,8 +42,9 @@ public class SearchCyclesTest extends GraphTestCase { doTest(graph, "a", "da", "bdca"); } - public void test2() throws Exception{ - final HashMap graph = new HashMap<>(); + @Test + public void test2() { + final Map graph = new HashMap<>(); graph.put("a", "b"); graph.put("b", "d"); graph.put("c", "a"); @@ -50,8 +54,9 @@ public class SearchCyclesTest extends GraphTestCase { doTest(graph, "a", "bdca"); } - public void test3() throws Exception{ - final HashMap graph = new HashMap<>(); + @Test + public void test3() { + final Map graph = new HashMap<>(); graph.put("a", "bd"); graph.put("b", "d"); graph.put("d", "a"); @@ -59,8 +64,9 @@ public class SearchCyclesTest extends GraphTestCase { doTest(graph, "a", "da"); } - public void test4() throws Exception { - final HashMap graph = new HashMap<>(); + @Test + public void test4() { + final Map graph = new HashMap<>(); graph.put("a", "b"); graph.put("b", "d"); graph.put("c", "a"); @@ -71,8 +77,9 @@ public class SearchCyclesTest extends GraphTestCase { doTest(graph, "a", "bdeca"); } - public void test5() throws Exception{ - final HashMap graph = new HashMap<>(); + @Test + public void test5() { + final Map graph = new HashMap<>(); graph.put("a", "be"); graph.put("b", "d"); graph.put("c", "a"); @@ -82,16 +89,10 @@ public class SearchCyclesTest extends GraphTestCase { doTest(graph, "a", "bdeca", "eca"); } - private static void doTest(HashMap graph, final String node, String... expected) { - final Set> nodeCycles = getAlgorithmsInstance().findCycles(initGraph(graph), node); - checkResult(expected, nodeCycles); + private static void doTest(Map graph, String node, String... expected) { + Set cycles = getAlgorithmsInstance().findCycles(initGraph(graph), node).stream() + .map(cycle -> StringUtil.join(cycle, "")) + .collect(Collectors.toSet()); + assertThat(cycles).containsExactlyInAnyOrder(expected); } - - private static void checkResult(String[] expected, Set> cycles) { - Set cycleStrings = new HashSet<>(); - for (List cycle : cycles) { - cycleStrings.add(StringUtil.join(cycle, "")); - } - UsefulTestCase.assertSameElements(cycleStrings, expected); - } -} +} \ No newline at end of file diff --git a/platform/platform-tests/testSrc/com/intellij/util/graph/ShortestPathTest.java b/platform/platform-tests/testSrc/com/intellij/util/graph/ShortestPathTest.java index e7cb63644d3d..9c5d59cb9993 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/graph/ShortestPathTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/graph/ShortestPathTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,34 +17,41 @@ package com.intellij.util.graph; import com.intellij.openapi.util.text.StringUtil; -import org.jetbrains.annotations.Nullable; +import org.junit.Test; import java.util.HashMap; import java.util.List; import java.util.Map; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + /** - * User: anna - * Date: Feb 11, 2005 + * @author anna + * @since Feb 11, 2005 */ public class ShortestPathTest extends GraphTestCase { + @Test public void testEmptyPath() { - final HashMap graph = new HashMap<>(); + final Map graph = new HashMap<>(); graph.put("a", ""); graph.put("b", ""); doTest(graph, "a", "a", "a"); } + @Test public void testNoPath() { - final HashMap graph = new HashMap<>(); + final Map graph = new HashMap<>(); graph.put("a", "c"); graph.put("b", "a"); graph.put("c", "a"); - assertNull(getShortestPath(graph, "a", "b")); + doTest(graph, "a", "b", null); } + @Test public void test1() { - final HashMap graph = new HashMap<>(); + final Map graph = new HashMap<>(); graph.put("a", ""); graph.put("b", "ac"); graph.put("c", "ab"); @@ -52,8 +59,9 @@ public class ShortestPathTest extends GraphTestCase { doTest(graph, "b", "a", "ba"); } + @Test public void test2() { - final HashMap graph = new HashMap<>(); + final Map graph = new HashMap<>(); graph.put("a", "cd"); graph.put("b", "a"); graph.put("c", "d"); @@ -61,8 +69,9 @@ public class ShortestPathTest extends GraphTestCase { doTest(graph, "c", "b", "cdb"); } + @Test public void test3() { - final HashMap graph = new HashMap<>(); + final Map graph = new HashMap<>(); graph.put("a", "bd"); graph.put("b", "d"); graph.put("c", "a"); @@ -70,8 +79,9 @@ public class ShortestPathTest extends GraphTestCase { doTest(graph, "b", "c", "bdc"); } + @Test public void test4() { - final HashMap graph = new HashMap<>(); + final Map graph = new HashMap<>(); graph.put("a", "be"); graph.put("b", "d"); graph.put("c", "a"); @@ -81,14 +91,14 @@ public class ShortestPathTest extends GraphTestCase { doTest(graph, "b", "c", "bdec"); } - private static void doTest(HashMap graph, final String from, final String to, final String expectedPath) { - final List shortestPath = getShortestPath(graph, from, to); - assertNotNull(shortestPath); - assertEquals(expectedPath, StringUtil.join(shortestPath, "")); + private static void doTest(Map graph, String from, String to, String expectedPath) { + List shortestPath = getAlgorithmsInstance().findShortestPath(initGraph(graph), from, to); + if (expectedPath != null) { + assertNotNull(shortestPath); + assertEquals(expectedPath, StringUtil.join(shortestPath, "")); + } + else { + assertNull(shortestPath); + } } - - @Nullable - private static List getShortestPath(Map graph, final String from, final String to) { - return getAlgorithmsInstance().findShortestPath(initGraph(graph), from, to); - } -} +} \ No newline at end of file