package classycle.graph;

import java.util.Arrays;
import java.util.HashSet;

import junit.framework.TestCase;

public class GraphTestCase extends TestCase {
  static class MockAttributes implements Attributes {
    private final String _name;
    public MockAttributes(String name) {
      _name = name;
    }

    public String toString() {
      return _name;
    }
  }

  protected AtomicVertex _externalVertex = new AtomicVertex(null);

  public GraphTestCase(String s) {
    super(s);
  }

  protected void check(String[] expectedStrongComponents, int[][] nodeLinks) {
    HashSet expectedFingerPrints = new HashSet();
    for (int i = 0; i < expectedStrongComponents.length; i++) {
      expectedFingerPrints.add(expectedStrongComponents[i]);
    }
    AtomicVertex[] graph = createGraph(nodeLinks);
    StrongComponentProcessor processor = new StrongComponentProcessor();
    process(processor, graph);
    StrongComponent[] components = processor.getStrongComponents();
    for (int i = 0; i < components.length; i++) {
      String fingerPrint = createFingerPrint(components[i]);
      assertTrue("'" + fingerPrint + "' not expected", 
                 expectedFingerPrints.contains(fingerPrint));
    }
    assertEquals("number of strong components", 
                 expectedStrongComponents.length, components.length);
  }
  
  protected void process(StrongComponentProcessor processor, 
                         AtomicVertex[] graph) {
    processor.deepSearchFirst(graph);
  }
  
  private String createFingerPrint(StrongComponent component) {
    int[] vertices = new int[component.getNumberOfVertices()];
    for (int i = 0; i < vertices.length; i++) {
      vertices[i] = Integer.parseInt(((MockAttributes) component.getVertex(i)
                                     .getAttributes()).toString());
    }
    Arrays.sort(vertices);
    StringBuffer result = new StringBuffer();
    result.append(component.getLongestWalk()).append(':');
    for (int i = 0; i < vertices.length; i++) {
      result.append(' ').append(Integer.toString(vertices[i]));
    }
    return new String(result);
  }
  protected AtomicVertex[] createGraph(int[][] nodeLinks) {
    AtomicVertex[] result = new AtomicVertex[nodeLinks.length];
    for (int i = 0; i < result.length; i++) {
      result[i] = new AtomicVertex(new MockAttributes(Integer.toString(i)));
    }
    for (int i = 0; i < result.length; i++) {
      int[] links = nodeLinks[i];
      for (int j = 0; j < links.length; j++) {
        int link = links[j];
        result[i].addOutgoingArcTo(link < 0 ? _externalVertex : result[link]);
      }
    }
    return result;
  }

} //class
@@ -501,20 +501,33 @@
 otected 
-void
+StrongComponent%5B%5D
  check(S
@@ -560,16 +560,53 @@
 onents, 
+%0A                                    
 int%5B%5D%5B%5D 
@@ -920,16 +920,20 @@
 ocessor(
+true
 );%0A    p
@@ -1379,16 +1379,40 @@
 length);
+%0A%0A    return components;
 %0A  %7D%0A  %0A
@@ -2179,16 +2179,17 @@
 t);%0A  %7D%0A
+%0A
   protec
@@ -2701,21 +2701,20 @@
 result;%0A
+
   %7D%0A
-%0A
 %7D //clas

change - check return components