package tp;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import org.graphstream.graph.Edge;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.stream.file.FileSource;
import org.graphstream.stream.file.FileSourceDGS;
import org.junit.BeforeClass;
import org.junit.Test;

public class FlowUtilsTests {
  public static Graph g;

  @BeforeClass
  public static void init() {
    g = new SingleGraph("testGraph");
		FileSource fs = new FileSourceDGS();
		fs.addSink(g);

		try {
			InputStream is = TestMaxFlow.class.getClassLoader().getResourceAsStream("graph.dgs");
			fs.readAll(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
  }

  @Test
  public void testCut() {
    Set<Node> testSet = Arrays
      .stream(new Node[] {g.getNode("A")})
      .collect(Collectors.toSet());

    Set<Edge> expected = Arrays
      .stream(new Edge[] {g.getEdge("AB"), g.getEdge("AC")})
      .collect(Collectors.toSet());

    assertEquals("Cut on A", expected, FlowUtils.cut(testSet));

    testSet = Arrays
      .stream(new Node[] {
        g.getNode("A"),
        g.getNode("B"),
        g.getNode("C"),
        g.getNode("D"),
        g.getNode("E"),
        g.getNode("G")
      })
      .collect(Collectors.toSet());

    expected = Arrays
      .stream(new Edge[] {
        g.getEdge("DH"),
        g.getEdge("CF"),
        g.getEdge("GI")
      })
      .collect(Collectors.toSet());

    assertEquals("Cut on {A, B, C, D, E, G}", expected, FlowUtils.cut(testSet));
  }
}