package org.psjava.algo.graph.dfs;

import static org.junit.Assert.*;

import org.junit.Assert;
import org.junit.Test;
import org.psjava.ds.graph.NewGraphFromGraph;
import org.psjava.ds.graph.DirectedEdge;
import org.psjava.ds.graph.OldGraph;
import org.psjava.ds.graph.TestGraphFactory;
import org.psjava.ds.map.MutableMap;
import org.psjava.util.VisitorStopper;

public class DFSCoreTest {

	String res;

	@Test
	public void testSimpleTraverseScenario() {
		OldGraph<String, DirectedEdge<String>> tg = TestGraphFactory.createDirected(new String[][] { { "1", "2" }, { "2", "3" }, { "3", "1" } });
		res = "";
		MutableMap<String, DFSStatus> status = DFSCore.createInitialStatus(tg.getVertices());
		DFSCore.traverse(NewGraphFromGraph.createFromDirected(tg), status, "1", new DFSVisitor<String, DirectedEdge<String>>() {
			@Override
			public void onDiscovered(String vertex, int depth, VisitorStopper stopper) {
				res += "N" + vertex;
			}

			@Override
			public void onBackEdgeFound(DirectedEdge<String> e) {
				res += "B" + e.from() + e.to();
			}

			@Override
			public void onFinish(String vertex, int depth) {
				res += "F" + vertex;
			}

			@Override
			public void onWalkDown(DirectedEdge<String> edge) {
				res += "D" + edge.from() + edge.to();
			}

			@Override
			public void onWalkUp(DirectedEdge<String> edge) {
				res += "U" + edge.to() + edge.from();
			}
		});
		assertEquals("N1D12N2D23N3B31F3U32F2U21F1", res);
	}

	@Test
	public void testStopper() {
		OldGraph<String, DirectedEdge<String>> g = TestGraphFactory.createDirected(new String[][] { { "A", "B" }, { "B", "C" }, { "C", "D" } });
		MutableMap<String, DFSStatus> status = DFSCore.createInitialStatus(g.getVertices());
		res = "";
		DFSCore.traverse(NewGraphFromGraph.createFromDirected(g), status, "A", new DFSVisitorBase<String, DirectedEdge<String>>() {
			@Override
			public void onDiscovered(String vertex, int depth, VisitorStopper stopper) {
				res += vertex;
				if (vertex.equals("C"))
					stopper.stop();
			};
		});
		Assert.assertEquals("ABC", res);
	}

}

@@ -145,28 +145,16 @@
 s.graph.
-NewGraphFrom
 Graph;%0D%0A
@@ -199,46 +199,8 @@
 e;%0D%0A
-import org.psjava.ds.graph.OldGraph;%0D%0A
 impo
@@ -414,35 +414,32 @@
 Scenario() %7B%0D%0A%09%09
-Old
 Graph%3CString, Di
@@ -458,17 +458,16 @@
 tring%3E%3E 
-t
 g = Test
@@ -485,32 +485,35 @@
 y.createDirected
+New
 (new String%5B%5D%5B%5D 
@@ -642,17 +642,16 @@
 lStatus(
-t
 g.getVer
@@ -684,48 +684,9 @@
 rse(
-NewGraphFromGraph.createFromDirected(tg)
+g
 , st
@@ -1441,11 +1441,8 @@
 %0D%0A%09%09
-Old
 Grap
@@ -1508,16 +1508,19 @@
 Directed
+New
 (new Str
@@ -1530,21 +1530,18 @@
 %5B%5D%5B%5D
- %7B %7B 
+%7B%7B
 %22A%22, %22B%22
  %7D, 
@@ -1540,22 +1540,20 @@
  %22B%22
- 
 %7D, %7B
- 
 %22B%22, %22C%22
  %7D, 
@@ -1552,22 +1552,20 @@
  %22C%22
- 
 %7D, %7B
- 
 %22C%22, %22D%22
  %7D %7D
@@ -1560,19 +1560,17 @@
 %22C%22, %22D%22
- %7D 
+%7D
 %7D);%0D%0A%09%09M
@@ -1690,47 +1690,9 @@
 rse(
-NewGraphFromGraph.createFromDirected(g)
+g
 , st

introduce new graph to DFSCoreTest.java