From 956d5889bf6390fd5ca34893547a5bcbb91b06b1 Mon Sep 17 00:00:00 2001 From: rec0de Date: Tue, 26 Jun 2018 18:18:31 +0200 Subject: [PATCH] Add server tests --- MaxFlow/Iksburg5 | 9 + MaxFlow/Iksburg6 | 17 + MaxFlow/Iksburg7 | 9 + MaxFlow/src/frame/AllTests.java | 1068 +++++++++++++++++++------------ 4 files changed, 708 insertions(+), 395 deletions(-) create mode 100644 MaxFlow/Iksburg5 create mode 100644 MaxFlow/Iksburg6 create mode 100644 MaxFlow/Iksburg7 diff --git a/MaxFlow/Iksburg5 b/MaxFlow/Iksburg5 new file mode 100644 index 0000000..12b3f3d --- /dev/null +++ b/MaxFlow/Iksburg5 @@ -0,0 +1,9 @@ +digraph { +A -> B [label="5"]; +B -> C [label="4"]; +C -> D [label="8"]; +C -> E [label="9"]; +A -> D [label="5"]; +A -> E [label="6"]; +D -> E [label="7"]; +} \ No newline at end of file diff --git a/MaxFlow/Iksburg6 b/MaxFlow/Iksburg6 new file mode 100644 index 0000000..6d425fa --- /dev/null +++ b/MaxFlow/Iksburg6 @@ -0,0 +1,17 @@ +digraph { +A -> B [label="5"]; +B -> C [label="4"]; +C -> D [label="8"]; +C -> E [label="9"]; +A -> D [label="5"]; +A -> E [label="6"]; +D -> E [label="7"]; +D -> F [label="10"]; +D -> G [label="3"]; +E -> G [label="5"]; +F -> G [label="8"]; +G -> H [label="10"]; +G -> I [label="5"]; +H -> J [label="35"]; +I -> J [label="20"]; +} \ No newline at end of file diff --git a/MaxFlow/Iksburg7 b/MaxFlow/Iksburg7 new file mode 100644 index 0000000..7e65a7a --- /dev/null +++ b/MaxFlow/Iksburg7 @@ -0,0 +1,9 @@ +digraph { +A -> B [label="10"]; +A -> C [label="10"]; +A -> D [label="8"]; +B -> C [label="3"]; +B -> E [label="5"]; +C -> E [label="15"]; +D -> E [label="3"]; +} \ No newline at end of file diff --git a/MaxFlow/src/frame/AllTests.java b/MaxFlow/src/frame/AllTests.java index 91c0c30..615508f 100644 --- a/MaxFlow/src/frame/AllTests.java +++ b/MaxFlow/src/frame/AllTests.java @@ -1,13 +1,13 @@ package frame; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.time.Duration; import java.util.ArrayList; import java.util.Scanner; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import lab.MaxFlow; @@ -16,178 +16,550 @@ import lab.MaxFlow; * MaxFlowTestCase.java */ -@DisplayName("Max Flow") +/** + * + */ public class AllTests { - protected int cutFlow(ArrayList graph, String[] cut) { - int usedEdgeCapacity = 0; - int cutFlow = 0; + private Duration timeout = Duration.ofMillis(1000); - for (String edge : cut) { - for (String graphElement : graph) { - if (graphElement.matches(".*" + edge + ".*")) { - Scanner s = new Scanner(graphElement); - String diff = s.findInLine("\\\".*?\\\""); - s.close(); - String[] edgeLabel = diff.split("\"|-"); - usedEdgeCapacity = Integer.parseInt(edgeLabel[2].trim()); - } - } - cutFlow += usedEdgeCapacity; - } - return cutFlow; + ////////////////// ************************************************************[new + ////////////////// tests] + @Test + public final void testFindMaxFlow_Iksburg7_A_to_E() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "E" }; + assertEquals(21, testMaxFlow("Iksburg7", sources, destinations), "MaxFlow not correct!"); + }); } - protected boolean isEdgeMarkedRight(ArrayList graph, String edge) { - int maxEdgeCapacity = 0; - int usedEdgeCapacity = 0; - boolean isBold = false; + @Test + public final void testEdges_Iksburg7_A_E() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "E" }; + ArrayList flowGraph = new MaxFlow("Iksburg7").findResidualNetwork(sources, destinations); - // calculate edge - for (String graphElement : graph) { - if (graphElement.matches(".*" + edge + ".*")) { - Scanner s = new Scanner(graphElement); - String diff = s.findInLine("\\\".*?\\\""); - s.close(); - String[] edgeLabel = diff.split("\"|-"); - maxEdgeCapacity = Integer.parseInt(edgeLabel[1].trim()); - usedEdgeCapacity = Integer.parseInt(edgeLabel[2].trim()); - } - } - - // verify if edge is bold or not - for (String graphElement : graph) { - if (graphElement.matches(".*" + edge + ".*")) { - if (graphElement.matches(".*style[ ]*=[ ]*bold.*")) { - isBold = true; - } else - isBold = false; - } - - } - - if (((maxEdgeCapacity != usedEdgeCapacity) && (isBold)) || ((maxEdgeCapacity == usedEdgeCapacity) && (!isBold))) - return true; - else - return false; + assertTrue(isEdgeMarkedRight(flowGraph, "A[ ]*->[ ]*B"), "Edge A->B was wrongly bolded!"); + assertTrue(isEdgeMarkedRight(flowGraph, "A[ ]*->[ ]*C"), "Edge A->C was wrongly bolded!"); + assertTrue(isEdgeMarkedRight(flowGraph, "A[ ]*->[ ]*D"), "Edge A->D was wrongly bolded!"); + assertTrue(isEdgeMarkedRight(flowGraph, "B[ ]*->[ ]*C"), "Edge B->C was wrongly bolded!"); + assertTrue(isEdgeMarkedRight(flowGraph, "B[ ]*->[ ]*E"), "Edge B->E was wrongly bolded!"); + assertTrue(isEdgeMarkedRight(flowGraph, "C[ ]*->[ ]*E"), "Edge C->E was wrongly bolded!"); + assertTrue(isEdgeMarkedRight(flowGraph, "D[ ]*->[ ]*E"), "Edge D->E was wrongly bolded!"); + }); } - protected boolean areSourcesSinksMarkedRight(ArrayList graph, String[] sources, String[] destinations) { - boolean correct = true; - int sourceMatches = 0; - int destinationMatches = 0; - - for (String source : sources) { - correct = false; - for (String graphElement : graph) { - if (graphElement.matches(".*=[ ]*doublecircle.*")) { - if (graphElement.matches(".*" + source + ".*")) { - sourceMatches++; - correct = true; - } - } - } - if (!correct) - return false; - } - for (String destination : destinations) { - correct = false; - for (String graphElement : graph) { - if (graphElement.matches(".*=[ ]*circle.*")) { - if (graphElement.matches(".*" + destination + ".*")) { - destinationMatches++; - correct = true; - } - } - } - if (!correct) - return false; - } - - return (sourceMatches == sources.length) && (destinationMatches == destinations.length); + @Test + public final void testFindMaxFlow_Iksburg7_AC_to_E() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A", "C" }; + String[] destinations = { "E" }; + int maxFlow = testMaxFlow("Iksburg7", sources, destinations); + assertEquals(23, maxFlow, "MaxFlow not correct!"); + }); } - protected int testMaxFlow(String filename, String[] sources, String[] destinations) { - MaxFlow mFlow = new MaxFlow(filename); - return mFlow.findMaxFlow(sources, destinations); + @Test + public final void testFindMaxFlow_Iksburg7_AB_to_ED() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A", "B" }; + String[] destinations = { "E", "D" }; + int maxFlow = testMaxFlow("Iksburg7", sources, destinations); + assertEquals(26, maxFlow, "MaxFlow not correct!"); + }); } - - - @Nested - @DisplayName("Iksburg 1") - class Iksburg1 { - @Test - @DisplayName("max flow from A to F") - public void testFindMaxFlow_Iksburg1_A_to_F() { + @Test + public final void testSourcesAndSinks_Iksburg7_AB_to_ED() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A", "B" }; + String[] destinations = { "E", "D" }; + ArrayList flowGraph = new MaxFlow("Iksburg7").findResidualNetwork(sources, destinations); + assertTrue(areSourcesSinksMarkedRight(flowGraph, sources, destinations), + "Sources and/or sinks are not marked correctly!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg5_A_to_E() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "E" }; + assertEquals(15, testMaxFlow("Iksburg5", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg5_C_to_E() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "C" }; + String[] destinations = { "E" }; + assertEquals(16, testMaxFlow("Iksburg5", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg5_AC_to_DE() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A", "C" }; + String[] destinations = { "D", "E" }; + assertEquals(28, testMaxFlow("Iksburg5", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg5_no_source() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "X" }; + String[] destinations = { "E" }; + int x = testMaxFlow("Iksburg5", sources, destinations); + assertEquals(-1, x, "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg5_no_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "Y" }; + assertEquals(-2, testMaxFlow("Iksburg5", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg5_no_source_no_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "X" }; + String[] destinations = { "Y" }; + assertEquals(-3, testMaxFlow("Iksburg5", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg5_no_path() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "E" }; + String[] destinations = { "A" }; + assertEquals(-4, testMaxFlow("Iksburg5", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg5_source_identical_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "B" }; + String[] destinations = { "B" }; + assertEquals(Integer.MAX_VALUE, testMaxFlow("Iksburg5", sources, destinations), "MaxFlow not correct!"); + }); + } + + /////////////////////// ---------------------- test file 6 + + @Test + public final void testFindMaxFlow_Iksburg6_HI_to_J() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "H", "I" }; + String[] destinations = { "J" }; + assertEquals(55, testMaxFlow("Iksburg6", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testCuts_Iksburg6_HI_J() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "H", "I" }; + String[] destinations = { "J" }; + String[] cut = new String[] { "H[ ]*->[ ]*J", "I[ ]*->[ ]*J" }; + ArrayList flowGraph = new MaxFlow("Iksburg6").findResidualNetwork(sources, destinations); + + assertEquals(55, cutFlow(flowGraph, cut), "The cut is different from max flow!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg6_CJ_to_G() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "C", "J" }; + String[] destinations = { "G" }; + int maxFlow = testMaxFlow("Iksburg6", sources, destinations); + assertEquals(13, maxFlow, "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg6_D_to_H() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "D" }; + String[] destinations = { "H" }; + assertEquals(10, testMaxFlow("Iksburg6", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testCuts_Iksburg6_D_H() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "D" }; + String[] destinations = { "H" }; + String[] cut = new String[] { "G[ ]*->[ ]*H" }; + ArrayList flowGraph = new MaxFlow("Iksburg6").findResidualNetwork(sources, destinations); + + assertEquals(10, cutFlow(flowGraph, cut), "The cut is different from max flow!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg6_A_to_E() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "E" }; + assertEquals(15, testMaxFlow("Iksburg6", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg6_A_to_J() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "J" }; + assertEquals(14, testMaxFlow("Iksburg6", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg6_no_source() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "X" }; + String[] destinations = { "E" }; + int x = testMaxFlow("Iksburg6", sources, destinations); + assertEquals(-1, x, "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg6_no_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "Z" }; + assertEquals(-2, testMaxFlow("Iksburg6", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg6_no_source_no_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "X" }; + String[] destinations = { "Y" }; + assertEquals(-3, testMaxFlow("Iksburg6", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg6_no_path() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "E" }; + String[] destinations = { "A" }; + assertEquals(-4, testMaxFlow("Iksburg6", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg6_source_identical_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "B" }; + String[] destinations = { "B" }; + assertEquals(Integer.MAX_VALUE, testMaxFlow("Iksburg6", sources, destinations), "MaxFlow not correct!"); + }); + } + + ////////////////// ************************************************************[new + ////////////////// tests] + + @Test + public final void testFindMaxFlow_Iksburg1_A_to_F() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "A" }; String[] destinations = { "F" }; assertEquals(140, testMaxFlow("Iksburg1", sources, destinations), "MaxFlow not correct!"); - } + }); + } - // new test - @Test - @DisplayName("max flow from B to F") - public void testFindMaxFlow_Iksburg1_B_to_F() { + // new test + @Test + public final void testFindMaxFlow_Iksburg1_B_to_F() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "A" }; String[] destinations = { "F" }; assertEquals(140, testMaxFlow("Iksburg1", sources, destinations), "MaxFlow not correct!"); - } + }); + } - @Test - @DisplayName("no sources, from X to F") - public void testFindMaxFlow_Iksburg1_no_source() { + @Test + public final void testFindMaxFlow_Iksburg1_no_source() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "X" }; String[] destinations = { "F" }; assertEquals(-1, testMaxFlow("Iksburg1", sources, destinations), "MaxFlow not correct!"); - } + }); + } - @Test - @DisplayName("no destinations, from A to Y") - public void testFindMaxFlow_Iksburg1_no_dest() { + @Test + public final void testFindMaxFlow_Iksburg1_no_dest() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "A" }; String[] destinations = { "Y" }; assertEquals(-2, testMaxFlow("Iksburg1", sources, destinations), "MaxFlow not correct!"); - } + }); + } - @Test - @DisplayName("no sources/destinations, from X to Y") - public void testFindMaxFlow_Iksburg1_no_source_no_dest() { + @Test + public final void testFindMaxFlow_Iksburg1_no_source_no_dest() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "X" }; String[] destinations = { "Y" }; assertEquals(-3, testMaxFlow("Iksburg1", sources, destinations), "MaxFlow not correct!"); - } + }); + } - @Test - @DisplayName("no path, from F to A") - public void testFindMaxFlow_Iksburg1_no_path() { + @Test + public final void testFindMaxFlow_Iksburg1_no_path() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "F" }; String[] destinations = { "A" }; assertEquals(-4, testMaxFlow("Iksburg1", sources, destinations), "MaxFlow not correct!"); - } + }); + } - @Test - @DisplayName("sources equals destinations, form B to B") - public void testFindMaxFlow_Iksburg1_source_identical_dest() { + @Test + public final void testFindMaxFlow_Iksburg1_source_identical_dest() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "B" }; String[] destinations = { "B" }; assertEquals(Integer.MAX_VALUE, testMaxFlow("Iksburg1", sources, destinations), "MaxFlow not correct!"); - } + }); + } - @Test - @DisplayName("marking of sources/destinations, from A to F") - public void testSourcesAndSinks_Iksburg1_A_to_F() { + @Test + public final void testFindMaxFlow_Iksburg2_A_to_H() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "H" }; + assertEquals(240, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg2_no_source() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "X" }; + String[] destinations = { "H" }; + assertEquals(-1, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg2_no_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "Y" }; + assertEquals(-2, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg2_no_source_no_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "X" }; + String[] destinations = { "Y" }; + assertEquals(-3, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg2_no_path() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "H" }; + String[] destinations = { "A" }; + assertEquals(-4, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg2_source_identical_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "F" }; + String[] destinations = { "F" }; + assertEquals(Integer.MAX_VALUE, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg3_AB_to_FG() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A", "B" }; + String[] destinations = { "F", "G" }; + assertEquals(30, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg3_no_source() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "X", "Y" }; + String[] destinations = { "F", "G" }; + assertEquals(-1, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg3_no_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A", "B" }; + String[] destinations = { "X", "Y" }; + assertEquals(-2, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg3_no_source_no_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "X", "Y" }; + String[] destinations = { "Z", "W" }; + assertEquals(-3, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg3_no_path() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "F" }; + String[] destinations = { "B" }; + assertEquals(-4, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testFindMaxFlow_Iksburg3_source_identical_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A", "B" }; + String[] destinations = { "A", "B" }; + assertEquals(Integer.MAX_VALUE, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); + }); + } + + // new test + @Test + public final void testFindMaxFlow_Iksburg4_Luisenplatz_Kantplatz_to_ILPlatz() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "Luisenplatz", "Kantplatz" }; + String[] destinations = { "Ilse_Langner_Platz" }; + assertEquals(42, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); + }); + } + + // new test + @Test + public final void testFindMaxFlow_Iksburg4_no_source() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "Someplatz" }; + String[] destinations = { "Ilse_Langner_Platz" }; + assertEquals(-1, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); + }); + } + + // new test + @Test + public final void testFindMaxFlow_Iksburg4_no_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "Luisenplatz" }; + String[] destinations = { "Someplatz" }; + assertEquals(-2, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); + }); + } + + // new test + @Test + public final void testFindMaxFlow_Iksburg4_no_source_no_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "XPlatz", "YPlatz" }; + String[] destinations = { "ZPlatz", "WPlatz" }; + assertEquals(-3, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); + }); + } + + // new test + @Test + public final void testFindMaxFlow_Iksburg4_no_path() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "Marktplatz" }; + String[] destinations = { "Luisenplatz" }; + assertEquals(-4, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); + }); + } + + // new test + @Test + public final void testFindMaxFlow_Iksburg4_source_identical_dest() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "Luisenplatz", "Kantplatz" }; + String[] destinations = { "Luisenplatz", "Kantplatz" }; + assertEquals(Integer.MAX_VALUE, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); + }); + } + + @Test + public final void testSourcesAndSinks_Iksburg1_A_to_F() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "A" }; String[] destinations = { "F" }; ArrayList flowGraph = new MaxFlow("Iksburg1").findResidualNetwork(sources, destinations); assertTrue(areSourcesSinksMarkedRight(flowGraph, sources, destinations), "Sources and/or sinks are not marked correctly!"); - } + }); + } - @Test - @DisplayName("marking of edges, from A to F") - public void testEdges_Iksburg1_A_F() { + @Test + public final void testSourcesAndSinks_Iksburg2_A_to_H() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "H" }; + ArrayList flowGraph = new MaxFlow("Iksburg2").findResidualNetwork(sources, destinations); + + assertTrue(areSourcesSinksMarkedRight(flowGraph, sources, destinations), + "Sources and/or sinks are not marked correctly!"); + }); + } + + @Test + public final void testSourcesAndSinks_Iksburg3_AB_to_FG() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A", "B" }; + String[] destinations = { "F", "G" }; + ArrayList flowGraph = new MaxFlow("Iksburg3").findResidualNetwork(sources, destinations); + + assertTrue(areSourcesSinksMarkedRight(flowGraph, sources, destinations), + "Sources and/or sinks are not marked correctly!"); + }); + } + + // new test + @Test + public final void testSourcesAndSinks_Iksburg4_Luisenplatz_Kantplatz_to_ILPlatz() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "Luisenplatz", "Kantplatz" }; + String[] destinations = { "Ilse_Langner_Platz" }; + ArrayList flowGraph = new MaxFlow("Iksburg4").findResidualNetwork(sources, destinations); + + assertTrue(areSourcesSinksMarkedRight(flowGraph, sources, destinations), + "Sources and/or sinks are not marked correctly!"); + }); + } + + @Test + public final void testEdges_Iksburg1_A_F() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "A" }; String[] destinations = { "F" }; ArrayList flowGraph = new MaxFlow("Iksburg1").findResidualNetwork(sources, destinations); @@ -200,89 +572,12 @@ public class AllTests { assertTrue(isEdgeMarkedRight(flowGraph, "C[ ]*->[ ]*E"), "Edge C->E was wrongly bolded!"); assertTrue(isEdgeMarkedRight(flowGraph, "D[ ]*->[ ]*F"), "Edge D->F was wrongly bolded!"); assertTrue(isEdgeMarkedRight(flowGraph, "E[ ]*->[ ]*F"), "Edge E->F was wrongly bolded!"); - } - - @Test - @DisplayName("cuts, from A to F") - public void testCuts_Iksburg1_A_F() { - String[] sources = { "A" }; - String[] destinations = { "F" }; - String[] cut1 = new String[] { "A[ ]*->[ ]*B", "A[ ]*->[ ]*C" }; - String[] cut2 = new String[] { "D[ ]*->[ ]*F", "E[ ]*->[ ]*F" }; - ArrayList flowGraph = new MaxFlow("Iksburg1").findResidualNetwork(sources, destinations); - - assertEquals(140, cutFlow(flowGraph, cut1), "The cut1 is different from max flow!"); - - assertEquals(140, cutFlow(flowGraph, cut2), "The cut2 is different from max flow!"); - } + }); } - @Nested - @DisplayName("Iksburg 2") - class Iksburg2 { - - @Test - @DisplayName("max flow from A to H") - public void testFindMaxFlow_Iksburg2_A_to_H() { - String[] sources = { "A" }; - String[] destinations = { "H" }; - assertEquals(240, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("no sources, from X to H") - public void testFindMaxFlow_Iksburg2_no_source() { - String[] sources = { "X" }; - String[] destinations = { "H" }; - assertEquals(-1, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("no destinations, from A to Y") - public void testFindMaxFlow_Iksburg2_no_dest() { - String[] sources = { "A" }; - String[] destinations = { "Y" }; - assertEquals(-2, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("no sources/destinations, from X to Y") - public void testFindMaxFlow_Iksburg2_no_source_no_dest() { - String[] sources = { "X" }; - String[] destinations = { "Y" }; - assertEquals(-3, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("no path, from H to A") - public void testFindMaxFlow_Iksburg2_no_path() { - String[] sources = { "H" }; - String[] destinations = { "A" }; - assertEquals(-4, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("sources equals destinations, from F to F") - public void testFindMaxFlow_Iksburg2_source_identical_dest() { - String[] sources = { "F" }; - String[] destinations = { "F" }; - assertEquals(Integer.MAX_VALUE, testMaxFlow("Iksburg2", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("marking of sources/destinations, from A to H") - public void testSourcesAndSinks_Iksburg2_A_to_H() { - String[] sources = { "A" }; - String[] destinations = { "H" }; - ArrayList flowGraph = new MaxFlow("Iksburg2").findResidualNetwork(sources, destinations); - - assertTrue(areSourcesSinksMarkedRight(flowGraph, sources, destinations), - "Sources and/or sinks are not marked correctly!"); - } - - @Test - @DisplayName("marking of edges, from A to H") - public void testEdges_Iksburg2_A_H() { + @Test + public final void testEdges_Iksburg2_A_H() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "A" }; String[] destinations = { "H" }; ArrayList flowGraph = new MaxFlow("Iksburg2").findResidualNetwork(sources, destinations); @@ -300,93 +595,12 @@ public class AllTests { assertTrue(isEdgeMarkedRight(flowGraph, "E[ ]*->[ ]*H"), "Edge E->H was wrongly bolded!"); assertTrue(isEdgeMarkedRight(flowGraph, "F[ ]*->[ ]*H"), "Edge F->H was wrongly bolded!"); assertTrue(isEdgeMarkedRight(flowGraph, "G[ ]*->[ ]*H"), "Edge G->H was wrongly bolded!"); - } - - @Test - @DisplayName("cuts, from A to H") - public void testCuts_Iksburg2_A_H() { - String[] sources = { "A" }; - String[] destinations = { "H" }; - String[] cut1 = new String[] { "A[ ]*->[ ]*B", "A[ ]*->[ ]*D", "A[ ]*->[ ]*C" }; - String[] cut2 = new String[] { "B[ ]*->[ ]*E", "B[ ]*->[ ]*F", "C[ ]*->[ ]*E", "C[ ]*->[ ]*F", - "C[ ]*->[ ]*G", "D[ ]*->[ ]*F", "D[ ]*->[ ]*G" }; - String[] cut3 = new String[] { "B[ ]*->[ ]*E", "C[ ]*->[ ]*E", "F[ ]*->[ ]*H", "G[ ]*->[ ]*H" }; - ArrayList flowGraph = new MaxFlow("Iksburg2").findResidualNetwork(sources, destinations); - - assertEquals(240, cutFlow(flowGraph, cut1), "The cut1 is different from max flow!"); - - assertEquals(240, cutFlow(flowGraph, cut2), "The cut2 is different from max flow!"); - - assertEquals(240, cutFlow(flowGraph, cut3), "The cut3 is different from max flow!"); - } + }); } - @Nested - @DisplayName("Iksburg 3") - class Iksburg3 { - - @Test - @DisplayName("max flow from {A, B} to {F, G}") - public void testFindMaxFlow_Iksburg3_AB_to_FG() { - String[] sources = { "A", "B" }; - String[] destinations = { "F", "G" }; - assertEquals(30, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("no sources, from {X, Y} to {F, G}") - public void testFindMaxFlow_Iksburg3_no_source() { - String[] sources = { "X", "Y" }; - String[] destinations = { "F", "G" }; - assertEquals(-1, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("no destinations, from {A, B} to {X, Y}") - public void testFindMaxFlow_Iksburg3_no_dest() { - String[] sources = { "A", "B" }; - String[] destinations = { "X", "Y" }; - assertEquals(-2, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("no sources/destinations, from {X, Y} to {Z, W}") - public void testFindMaxFlow_Iksburg3_no_source_no_dest() { - String[] sources = { "X", "Y" }; - String[] destinations = { "Z", "W" }; - assertEquals(-3, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("no path, from F to B") - public void testFindMaxFlow_Iksburg3_no_path() { - String[] sources = { "F" }; - String[] destinations = { "B" }; - assertEquals(-4, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("sources equals destinations, form {A, B} to {A, B}") - public void testFindMaxFlow_Iksburg3_source_identical_dest() { - String[] sources = { "A", "B" }; - String[] destinations = { "A", "B" }; - assertEquals(Integer.MAX_VALUE, testMaxFlow("Iksburg3", sources, destinations), "MaxFlow not correct!"); - } - - @Test - @DisplayName("marking of sources/destinations, from {A, B} to {F, G}") - public void testSourcesAndSinks_Iksburg3_AB_to_FG() { - String[] sources = { "A", "B" }; - String[] destinations = { "F", "G" }; - ArrayList flowGraph = new MaxFlow("Iksburg3").findResidualNetwork(sources, destinations); - - assertTrue(areSourcesSinksMarkedRight(flowGraph, sources, destinations), - "Sources and/or sinks are not marked correctly!"); - } - - @Test - @DisplayName("marking of edges, from {A, B} to {F, G}") - public void testEdges_Iksburg3_AB_FG() { + @Test + public final void testEdges_Iksburg3_AB_FG() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "A", "B" }; String[] destinations = { "F", "G" }; ArrayList flowGraph = new MaxFlow("Iksburg3").findResidualNetwork(sources, destinations); @@ -401,104 +615,13 @@ public class AllTests { assertTrue(isEdgeMarkedRight(flowGraph, "D[ ]*->[ ]*G"), "Edge D->G was wrongly bolded!"); assertTrue(isEdgeMarkedRight(flowGraph, "D[ ]*->[ ]*E"), "Edge D->E was wrongly bolded!"); assertTrue(isEdgeMarkedRight(flowGraph, "E[ ]*->[ ]*G"), "Edge E->G was wrongly bolded!"); - } - - @Test - @DisplayName("cuts, from {A, B} to {F, G}") - public void testCuts_Iksburg3_AB_FG() { - String[] sources = { "A", "B" }; - String[] destinations = { "F", "G" }; - String[] cut1 = new String[] { "A[ ]*->[ ]*C", "A[ ]*->[ ]*D", "B[ ]*->[ ]*D", "B[ ]*->[ ]*E" }; - String[] cut2 = new String[] { "A[ ]*->[ ]*C", "D[ ]*->[ ]*C", "D[ ]*->[ ]*F", "D[ ]*->[ ]*G", - "E[ ]*->[ ]*G" }; - String[] cut3 = new String[] { "C[ ]*->[ ]*F", "D[ ]*->[ ]*F", "D[ ]*->[ ]*G", "E[ ]*->[ ]*G" }; - ArrayList flowGraph = new MaxFlow("Iksburg3").findResidualNetwork(sources, destinations); - - assertEquals(30, cutFlow(flowGraph, cut1), "The cut1 is different from max flow!"); - - assertEquals(30, cutFlow(flowGraph, cut2), "The cut2 is different from max flow!"); - - assertEquals(30, cutFlow(flowGraph, cut3), "The cut3 is different from max flow!"); - } - + }); } - @Nested - @DisplayName("Iksburg 4") - class Iksburg4 { - - // new test - @Test - @DisplayName("max flow from Luisenplatz_Kantplatz to ILPlatz") - public void testFindMaxFlow_Iksburg4_Luisenplatz_Kantplatz_to_ILPlatz() { - String[] sources = { "Luisenplatz", "Kantplatz" }; - String[] destinations = { "Ilse_Langner_Platz" }; - assertEquals(42, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); - } - - // new test - @Test - @DisplayName("no sources, from Someplatz to Ilse_Langner_Platz") - public void testFindMaxFlow_Iksburg4_no_source() { - String[] sources = { "Someplatz" }; - String[] destinations = { "Ilse_Langner_Platz" }; - assertEquals(-1, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); - } - - // new test - @Test - @DisplayName("no destinations, max flow from Luisenplatz to Someplatz") - public void testFindMaxFlow_Iksburg4_no_dest() { - String[] sources = { "Luisenplatz" }; - String[] destinations = { "Someplatz" }; - assertEquals(-2, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); - } - - // new test - @Test - @DisplayName("no sources/destinations, from {XPlatz, YPlatz} to {ZPlatz, WPlatz}") - public void testFindMaxFlow_Iksburg4_no_source_no_dest() { - String[] sources = { "XPlatz", "YPlatz" }; - String[] destinations = { "ZPlatz", "WPlatz" }; - assertEquals(-3, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); - } - - // new test - @Test - @DisplayName("no path, from Marktplatz to Luisenplatz") - public void testFindMaxFlow_Iksburg4_no_path() { - String[] sources = { "Marktplatz" }; - String[] destinations = { "Luisenplatz" }; - assertEquals(-4, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); - } - - // new test - @Test - @DisplayName("sources equals destinations, from {Luisenplatz, Kantplatz} to {Luisenplatz, Kantplatz}") - public void testFindMaxFlow_Iksburg4_source_identical_dest() { - String[] sources = { "Luisenplatz", "Kantplatz" }; - String[] destinations = { "Luisenplatz", "Kantplatz" }; - assertEquals(Integer.MAX_VALUE, testMaxFlow("Iksburg4", sources, destinations), "MaxFlow not correct!"); - } - - // new test - @Test - @DisplayName("marking of sources/destinations, from {Luisenplatz, Kantplatz} to Ilse_Langner_Platz") - public void testSourcesAndSinks_Iksburg4_Luisenplatz_Kantplatz_to_ILPlatz() { - String[] sources = { "Luisenplatz", "Kantplatz" }; - String[] destinations = { "Ilse_Langner_Platz" }; - ArrayList flowGraph = new MaxFlow("Iksburg4").findResidualNetwork(sources, destinations); - - assertTrue(areSourcesSinksMarkedRight(flowGraph, sources, destinations), - "Sources and/or sinks are not marked correctly!"); - } - - - - // new test - @Test - @DisplayName("marking of edges, from {Luisenplatz, Kantplatz} to Ilse_Langner_Platz") - public void testEdges_Iksburg4_Luisenplatz_Kantplatz_to_ILPlatz() { + // new test + @Test + public final void testEdges_Iksburg4_Luisenplatz_Kantplatz_to_ILPlatz() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "Luisenplatz", "Kantplatz" }; String[] destinations = { "Ilse_Langner_Platz" }; ArrayList flowGraph = new MaxFlow("Iksburg4").findResidualNetwork(sources, destinations); @@ -537,11 +660,66 @@ public class AllTests { "Edge Platz_der_deutschen_Einheit -> Marktplatz was wrongly bolded!"); assertTrue(isEdgeMarkedRight(flowGraph, "Kopernikusplatz[ ]*->[ ]*Ilse_Langner_Platz"), "Edge Kopernikusplatz -> Ilse_Langner_Platz was wrongly bolded!"); - } - - @Test - @DisplayName("cuts, from {Luisenplatz, Kantplatz} to Ilse_Langner_Platz") - public void testCuts_Iksburg4_Luisenplatz_Kantplatz_to_ILPlatz() { + }); + } + + @Test + public final void testCuts_Iksburg1_A_F() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "F" }; + String[] cut1 = new String[] { "A[ ]*->[ ]*B", "A[ ]*->[ ]*C" }; + String[] cut2 = new String[] { "D[ ]*->[ ]*F", "E[ ]*->[ ]*F" }; + ArrayList flowGraph = new MaxFlow("Iksburg1").findResidualNetwork(sources, destinations); + + assertEquals(140, cutFlow(flowGraph, cut1), "The cut1 is different from max flow!"); + + assertEquals(140, cutFlow(flowGraph, cut2), "The cut2 is different from max flow!"); + }); + } + + @Test + public final void testCuts_Iksburg2_A_H() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A" }; + String[] destinations = { "H" }; + String[] cut1 = new String[] { "A[ ]*->[ ]*B", "A[ ]*->[ ]*D", "A[ ]*->[ ]*C" }; + String[] cut2 = new String[] { "B[ ]*->[ ]*E", "B[ ]*->[ ]*F", "C[ ]*->[ ]*E", "C[ ]*->[ ]*F", + "C[ ]*->[ ]*G", "D[ ]*->[ ]*F", "D[ ]*->[ ]*G" }; + String[] cut3 = new String[] { "B[ ]*->[ ]*E", "C[ ]*->[ ]*E", "F[ ]*->[ ]*H", "G[ ]*->[ ]*H" }; + ArrayList flowGraph = new MaxFlow("Iksburg2").findResidualNetwork(sources, destinations); + + assertEquals(240, cutFlow(flowGraph, cut1), "The cut1 is different from max flow!"); + + assertEquals(240, cutFlow(flowGraph, cut2), "The cut2 is different from max flow!"); + + assertEquals(240, cutFlow(flowGraph, cut3), "The cut3 is different from max flow!"); + }); + } + + @Test + public final void testCuts_Iksburg3_AB_FG() { + assertTimeoutPreemptively(timeout, () -> { + String[] sources = { "A", "B" }; + String[] destinations = { "F", "G" }; + String[] cut1 = new String[] { "A[ ]*->[ ]*C", "A[ ]*->[ ]*D", "B[ ]*->[ ]*D", "B[ ]*->[ ]*E" }; + String[] cut2 = new String[] { "A[ ]*->[ ]*C", "D[ ]*->[ ]*C", "D[ ]*->[ ]*F", "D[ ]*->[ ]*G", + "E[ ]*->[ ]*G" }; + String[] cut3 = new String[] { "C[ ]*->[ ]*F", "D[ ]*->[ ]*F", "D[ ]*->[ ]*G", "E[ ]*->[ ]*G" }; + ArrayList flowGraph = new MaxFlow("Iksburg3").findResidualNetwork(sources, destinations); + + assertEquals(30, cutFlow(flowGraph, cut1), "The cut1 is different from max flow!"); + + assertEquals(30, cutFlow(flowGraph, cut2), "The cut2 is different from max flow!"); + + assertEquals(30, cutFlow(flowGraph, cut3), "The cut3 is different from max flow!"); + }); + } + + // new test + @Test + public final void testCuts_Iksburg4_Luisenplatz_Kantplatz_to_ILPlatz() { + assertTimeoutPreemptively(timeout, () -> { String[] sources = { "Luisenplatz", "Kantplatz" }; String[] destinations = { "Ilse_Langner_Platz" }; String[] cut1 = new String[] { "Kopernikusplatz[ ]*->[ ]*Ilse_Langner_Platz", @@ -557,9 +735,109 @@ public class AllTests { assertEquals(42, cutFlow(flowGraph, cut1), "The cut1 is different from max flow!"); assertEquals(42, cutFlow(flowGraph, cut2), "The cut2 is different from max flow!"); assertEquals(42, cutFlow(flowGraph, cut3), "The cut3 is different from max flow!"); - } + }); } + private int cutFlow(final ArrayList graph, final String[] cut) { + int usedEdgeCapacity = 0; + int cutFlow = 0; + for (String edge : cut) { + for (String graphElement : graph) { + if (graphElement.matches(".*" + edge + ".*")) { + Scanner s = new Scanner(graphElement); + String diff = s.findInLine("\\\".*?\\\""); + s.close(); + String[] edgeLabel = diff.split("\"|-"); + usedEdgeCapacity = Integer.valueOf(edgeLabel[2].trim()).intValue(); + } + } + cutFlow += usedEdgeCapacity; + } + return cutFlow; + } + + private boolean isEdgeMarkedRight(final ArrayList graph, final String edge) { + int maxEdgeCapacity = 0; + int usedEdgeCapacity = 0; + boolean isBold = false; + + // calculate edge + for (String graphElement : graph) { + if (graphElement.matches(".*" + edge + ".*")) { + Scanner s = new Scanner(graphElement); + String diff = s.findInLine("\\\".*?\\\""); + s.close(); + String[] edgeLabel = diff.split("\"|-"); + maxEdgeCapacity = Integer.valueOf(edgeLabel[1].trim()).intValue(); + usedEdgeCapacity = Integer.valueOf(edgeLabel[2].trim()).intValue(); + } + } + + // verify if edge is bold or not + for (String graphElement : graph) { + if (graphElement.matches(".*" + edge + ".*")) { + if (graphElement.matches(".*style[ ]*=[ ]*bold.*")) { + isBold = true; + } else + isBold = false; + } + + } + + if (((maxEdgeCapacity != usedEdgeCapacity) && (isBold)) || ((maxEdgeCapacity == usedEdgeCapacity) && (!isBold))) + return true; + else + return false; + } + + /* + * private boolean matchEdge(final ArrayList graph, final String edge, + * final int maxValue, final int actValue) { for (String graphElement : graph) { + * if(graphElement.matches(".*"+edge+".*")) { return + * graphElement.matches(".*\"[ ]*"+maxValue+"[ ]*-[ ]*"+actValue+"[ ]*\".*"); } + * } return false; } + */ + + private boolean areSourcesSinksMarkedRight(final ArrayList graph, final String[] sources, + final String[] destinations) { + boolean correct = true; + int sourceMatches = 0; + int destinationMatches = 0; + + for (String source : sources) { + correct = false; + for (String graphElement : graph) { + if (graphElement.matches(".*=[ ]*doublecircle.*")) { + if (graphElement.matches(".*" + source + ".*")) { + sourceMatches++; + correct = true; + } + } + } + if (!correct) + return false; + } + for (String destination : destinations) { + correct = false; + for (String graphElement : graph) { + if (graphElement.matches(".*=[ ]*circle.*")) { + if (graphElement.matches(".*" + destination + ".*")) { + destinationMatches++; + correct = true; + } + } + } + if (!correct) + return false; + } + + return (sourceMatches == sources.length) && (destinationMatches == destinations.length); + } + + private int testMaxFlow(final String filename, final String[] sources, final String[] destinations) { + MaxFlow mFlow = new MaxFlow(filename); + return mFlow.findMaxFlow(sources, destinations); + } }