Esempio n. 1
0
func TestDepthFirstSearchInGraph(t *testing.T) {
	//Create some nodes.
	a := graph.Node{Value: Letter{"A"}}
	b := graph.Node{Value: Letter{"B"}}
	c := graph.Node{Value: Letter{"C"}}
	d := graph.Node{Value: Letter{"D"}}
	e := graph.Node{Value: Letter{"E"}}
	f := graph.Node{Value: Letter{"F"}}
	g := graph.Node{Value: Letter{"G"}}
	h := graph.Node{Value: Letter{"H"}}

	graph := graph.NewGraph()

	//Add directed node-pairs to graph.
	graph.InsertEdge(&a, &b)
	graph.InsertEdge(&a, &d)
	graph.InsertEdge(&b, &d)
	graph.InsertEdge(&b, &c)
	graph.InsertEdge(&c, &e)
	graph.InsertEdge(&e, &g)
	graph.InsertEdge(&e, &f)
	graph.InsertEdge(&f, &h)

	expectedDepthFirstSearch := graph.GetDFS()
	correctDfs := []Letter{{"A"}, {"B"}, {"D"}, {"C"}, {"E"}, {"G"}, {"F"}, {"H"}}

	//Equal length.
	if len(correctDfs) != len(expectedDepthFirstSearch) {
		t.Errorf("Length of DFS (%d) is not equal length of correct DFS (%d)!\n", len(expectedDepthFirstSearch), len(correctDfs))
	}

	//Check nodes in depth first search.
	for index, node := range expectedDepthFirstSearch {
		if node.String() != correctDfs[index].String() {
			t.Error("Faen i helvete")
		}
	}

}
Esempio n. 2
0
func TestDirectedGraph(test *testing.T) {
	//Create some nodes.
	a := graph.Node{Value: Letter{"A"}}
	b := graph.Node{Value: Letter{"B"}}
	c := graph.Node{Value: Letter{"C"}}
	d := graph.Node{Value: Letter{"D"}}
	e := graph.Node{Value: Letter{"E"}}
	f := graph.Node{Value: Letter{"F"}}
	g := graph.Node{Value: Letter{"G"}}
	h := graph.Node{Value: Letter{"H"}}

	graph := graph.NewGraph()

	//Add directed node-pairs to graph.
	graph.InsertEdge(&a, &b)
	graph.InsertEdge(&a, &d)
	graph.InsertEdge(&b, &d)
	graph.InsertEdge(&b, &c)
	graph.InsertEdge(&c, &e)
	graph.InsertEdge(&e, &g)
	graph.InsertEdge(&e, &f)
	graph.InsertEdge(&f, &h)

	//Test number of nodes in graph.
	if graph.GetNumberOfNodes() != 8 {
		test.Fatalf("Graph should contain 8 nodes, not %d!\n", graph.GetNumberOfNodes())
	}

	//Node A should be root node.
	if graph.Root.Value != a.Value {
		test.Errorf("Node A should be root node, not node %v\n", graph.Root.Value)
	}

	//Test node A.
	if a.GetInDegree() != 0 {
		test.Errorf("Node A in-degree should be 0, not %d\n", a.GetInDegree())
	}
	if a.GetOutDegree() != 2 {
		test.Errorf("Node A out-degree should be 2, not %d\n", a.GetInDegree())
	}

	//Test node B.
	if b.GetInDegree() != 1 {
		test.Errorf("Node A in-degree should be 1, not %d\n", b.GetInDegree())
	}
	if b.GetOutDegree() != 2 {
		test.Errorf("Node A out-degree should be 2, not %d\n", b.GetInDegree())
	}

	//Test node C.
	if c.GetInDegree() != 1 {
		test.Errorf("Node A in-degree should be 1, not %d\n", c.GetInDegree())
	}
	if c.GetOutDegree() != 1 {
		test.Errorf("Node A out-degree should be 1, not %d\n", c.GetInDegree())
	}

	//Test node D.
	if d.GetInDegree() != 2 {
		test.Errorf("Node A in-degree should be 2, not %d\n", d.GetInDegree())
	}
	if d.GetOutDegree() != 0 {
		test.Errorf("Node A out-degree should be 0, not %d\n", d.GetInDegree())
	}

	//Test node E.
	if e.GetInDegree() != 1 {
		test.Errorf("Node A in-degree should be 1, not %d\n", e.GetInDegree())
	}
	if e.GetOutDegree() != 2 {
		test.Errorf("Node A out-degree should be 2, not %d\n", e.GetInDegree())
	}

	//Test node F.
	if f.GetInDegree() != 1 {
		test.Errorf("Node A in-degree should be 1, not %d\n", f.GetInDegree())
	}
	if f.GetOutDegree() != 1 {
		test.Errorf("Node A out-degree should be 1, not %d\n", f.GetInDegree())
	}

	//Test node G.
	if g.GetInDegree() != 1 {
		test.Errorf("Node A in-degree should be 1, not %d\n", g.GetInDegree())
	}
	if g.GetOutDegree() != 0 {
		test.Errorf("Node A out-degree should be 0, not %d\n", g.GetInDegree())
	}

	//Test node H.
	if h.GetInDegree() != 1 {
		test.Errorf("Node A in-degree should be 1, not %d\n", h.GetInDegree())
	}
	if h.GetOutDegree() != 0 {
		test.Errorf("Node A out-degree should be 0, not %d\n", h.GetInDegree())
	}

}