package util;
import java.util.*;

public class GraphTests {
	// WP
	static Graph<String> G1 = new Graph<String>(
			"a => d:3, b:3; b => c:4;" +
			"c => a:3, d:1, e:2; d => f:6, e:2;" +
			"e => b:1, g:1; f => g:9; g");
	// book
	static Graph<String> G2 = new Graph<String>(
			"a => b:1; b => c:1, e:1, f:1;" +
			"c => d:1, g:1; d => c:1, h:1;" +
			"e => a:1, f:1; f => g:1;" +
			"g => f:1, h:1; h => h:1");
	static Graph<String> G3 = new Graph<String>(
			"a => d:3, b:3; b => c:4;" +
			"c => a:3, d:1, e:2; d => f:6, e:2;" +
			"e => b:1, g:1; f => g:9; g");
	
	static int counter = 0, passed = 0, failed = 0;
	static void test(Object result, Object goal) {
		if (result.equals(goal) && ++passed > 0)
			System.err.printf("Test #%d passed...\n", ++counter);
		else if (++failed > 0)
			System.err.printf("Test #%d failed: %s != %s\n",
					++counter, result, goal);
	}
	
	static void testBasics() {
		test(G1.size(), 7);
		test(G1.hasNode("b"), true);
		test(G1.hasNode("w"), false);
		test(G1.hasEdge("a", "b"), true);
		test(G1.hasEdge("b", "a"), false);
		test(G1.nodes(), new HashSet<String>(Arrays.asList(
				new String[] {"a", "b", "c", "d", "e", "f", "g"})));
	}
	
	static void testPathfinding() {
		G1.dijkstras("a");
		G2.bellmanFord("a");
		G3.floydWarshall();
		test(G1.pathLength("a", "e"), 5.0);
		test(G2.pathLength("a", "f"), 2.0);
		test(G3.pathLength("a", "e"), 5.0);
	}
	
	static void testMaxFlow() {
		test(G1.edmondsKarp("a", "g"), 5.0);
	}
	
	static void testSCCs() {
		Set<Set<String>> G2SCCs = new HashSet<Set<String>>();
		G2SCCs.add(new HashSet<String>(Arrays.asList(new String[] {"a", "b", "e"})));
		G2SCCs.add(new HashSet<String>(Arrays.asList(new String[] {"f", "g"})));
		G2SCCs.add(new HashSet<String>(Arrays.asList(new String[] {"c", "d"})));
		G2SCCs.add(new HashSet<String>(Arrays.asList(new String[] {"h"})));
		test(G2.SCCs(), G2SCCs);
	}
	
	public static void main(String[] args) {
		testBasics();
		testPathfinding();
		testMaxFlow();
		testSCCs();
		System.out.printf("%d passed, %d failed\n",
				passed, failed);
	}
}

