Cleanup (graph algorithms' tests)

This commit is contained in:
Roman Shevchenko
2016-08-24 16:00:34 +03:00
parent 1b331cf247
commit bd16e0c293
5 changed files with 106 additions and 97 deletions
@@ -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<String, String> arcs = new HashMap<>();
arcs.put("a", "b");
@@ -39,21 +42,22 @@ public class ChunkGraphTest extends GraphTestCase {
final List<Chunk<String>> expectedNodes = new ArrayList<>();
Chunk<String> A = new Chunk<>("a");
expectedNodes.add(A);
Chunk<String> BC = new Chunk<>(toSet("b", "c"));
Chunk<String> BC = new Chunk<>(ContainerUtil.newHashSet("b", "c"));
expectedNodes.add(BC);
Chunk<String> DE = new Chunk<>(toSet("d", "e"));
Chunk<String> DE = new Chunk<>(ContainerUtil.newHashSet("d", "e"));
expectedNodes.add(DE);
checkVertices(expectedNodes, graph.getNodes().iterator());
checkVertices(expectedNodes, graph.getNodes());
final Map<Chunk<String>, Set<Chunk<String>>> 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<String, String> arcs = new HashMap<>();
arcs.put("a", "b");
@@ -64,52 +68,32 @@ public class ChunkGraphTest extends GraphTestCase {
final Graph<Chunk<String>> graph = getAlgorithmsInstance().computeSCCGraph(initGraph(arcs));
final List<Chunk<String>> expectedNodes = new ArrayList<>();
Chunk<String> ABC = new Chunk<>(toSet("a", "b", "c"));
Chunk<String> ABC = new Chunk<>(ContainerUtil.newHashSet("a", "b", "c"));
expectedNodes.add(ABC);
Chunk<String> D = new Chunk<>("d");
expectedNodes.add(D);
checkVertices(expectedNodes, graph.getNodes().iterator());
checkVertices(expectedNodes, graph.getNodes());
final Map<Chunk<String>, Set<Chunk<String>>> 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<Chunk<String>, Set<Chunk<String>>> expectedArcs, Graph<Chunk<String>> graph) {
for (Chunk<String> chunk : graph.getNodes()) {
final List<Chunk<String>> ins = new ArrayList<>();
final Iterator<Chunk<String>> insIterator = graph.getIn(chunk);
while (insIterator.hasNext()) {
ins.add(insIterator.next());
}
final Set<Chunk<String>> expectedIns = expectedArcs.get(chunk);
List<Chunk<String>> ins = ContainerUtil.newArrayList(() -> graph.getIn(chunk));
Set<Chunk<String>> expectedIns = expectedArcs.get(chunk);
assertTrue(expectedIns.size() == ins.size());
assertTrue(expectedIns.equals(new HashSet<>(ins)));
}
}
private static <T> Set<T> toSet(T... strings) {
return new HashSet<>(Arrays.asList(strings));
}
private static Set<Chunk<String>> toSet() {
return new HashSet<>();
}
private static Set<Chunk<String>> toSet(Chunk<String> c) {
return Collections.singleton(c);
}
private static void checkVertices(List<Chunk<String>> expected, Iterator<Chunk<String>> nodes) {
List<Chunk<String>> realNodes = new ArrayList<>();
while (nodes.hasNext()) {
realNodes.add(nodes.next());
}
private static void checkVertices(List<Chunk<String>> expected, Iterable<Chunk<String>> nodes) {
List<Chunk<String>> realNodes = ContainerUtil.newArrayList(nodes);
assertTrue(expected.size() == realNodes.size());
assertTrue(new HashSet<>(expected).equals(new HashSet<>(realNodes)));
}
}
}
@@ -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 {
}
};
}
}
}
@@ -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<String, String> graph = new HashMap<>();
graph.put("s", "");
@@ -32,6 +35,7 @@ public class KShortestPathsFinderTest extends GraphTestCase {
doTest(graph);
}
@Test
public void testOneEdge() {
final Map<String, String> 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<String, String> graph = new HashMap<>();
graph.put("s", "a");
@@ -48,12 +53,14 @@ public class KShortestPathsFinderTest extends GraphTestCase {
doTest(graph);
}
@Test
public void testOneVertex() {
Map<String, String> graph = new HashMap<>();
graph.put("s", "");
doTest(graph, "s", "s", 5, "s");
}
@Test
public void testTwoPaths() {
final Map<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> graph = new HashMap<>();
graph.put("s", "lo");
@@ -170,8 +185,8 @@ public class KShortestPathsFinderTest extends GraphTestCase {
}
private static void doTest(Map<String, String> graph, final String start, final String finish, final int k, String... expectedPaths) {
final Graph<String> generator = initGraph(graph);
final List<List<String>> paths = getAlgorithmsInstance().findKShortestPaths(generator, start, finish, k, new EmptyProgressIndicator());
Graph<String> generator = initGraph(graph);
List<List<String>> paths = getAlgorithmsInstance().findKShortestPaths(generator, start, finish, k, new EmptyProgressIndicator(ModalityState.NON_MODAL));
List<String> pathStrings = new ArrayList<>();
Set<Integer> sizes = new HashSet<>();
for (List<String> path : paths) {
@@ -185,4 +200,4 @@ public class KShortestPathsFinderTest extends GraphTestCase {
UsefulTestCase.assertOrderedEquals(pathStrings, expectedPaths);
}
}
}
}
@@ -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<String, String> graph = new HashMap<>();
@Test
public void test1() {
final Map<String, String> 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<String, String> graph = new HashMap<>();
@Test
public void test2() {
final Map<String, String> 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<String, String> graph = new HashMap<>();
@Test
public void test3() {
final Map<String, String> 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<String, String> graph = new HashMap<>();
@Test
public void test4() {
final Map<String, String> 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<String, String> graph = new HashMap<>();
@Test
public void test5() {
final Map<String, String> 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<String, String> graph, final String node, String... expected) {
final Set<List<String>> nodeCycles = getAlgorithmsInstance().findCycles(initGraph(graph), node);
checkResult(expected, nodeCycles);
private static void doTest(Map<String, String> graph, String node, String... expected) {
Set<String> 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<List<String>> cycles) {
Set<String> cycleStrings = new HashSet<>();
for (List<String> cycle : cycles) {
cycleStrings.add(StringUtil.join(cycle, ""));
}
UsefulTestCase.assertSameElements(cycleStrings, expected);
}
}
}
@@ -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<String, String> graph = new HashMap<>();
final Map<String, String> graph = new HashMap<>();
graph.put("a", "");
graph.put("b", "");
doTest(graph, "a", "a", "a");
}
@Test
public void testNoPath() {
final HashMap<String, String> graph = new HashMap<>();
final Map<String, String> 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<String, String> graph = new HashMap<>();
final Map<String, String> 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<String, String> graph = new HashMap<>();
final Map<String, String> 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<String, String> graph = new HashMap<>();
final Map<String, String> 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<String, String> graph = new HashMap<>();
final Map<String, String> 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<String, String> graph, final String from, final String to, final String expectedPath) {
final List<String> shortestPath = getShortestPath(graph, from, to);
assertNotNull(shortestPath);
assertEquals(expectedPath, StringUtil.join(shortestPath, ""));
private static void doTest(Map<String, String> graph, String from, String to, String expectedPath) {
List<String> shortestPath = getAlgorithmsInstance().findShortestPath(initGraph(graph), from, to);
if (expectedPath != null) {
assertNotNull(shortestPath);
assertEquals(expectedPath, StringUtil.join(shortestPath, ""));
}
else {
assertNull(shortestPath);
}
}
@Nullable
private static List<String> getShortestPath(Map<String, String> graph, final String from, final String to) {
return getAlgorithmsInstance().findShortestPath(initGraph(graph), from, to);
}
}
}