Exemplo n.º 1
0
func TestDirectedDenseAddRemove(t *testing.T) {
	dg := concrete.NewDirectedDenseGraph(10, false, math.Inf(1))
	dg.SetEdgeWeight(concrete.Edge{concrete.Node(0), concrete.Node(2)}, 1)

	if neighbors := dg.From(concrete.Node(0)); len(neighbors) != 1 || neighbors[0].ID() != 2 ||
		dg.Edge(concrete.Node(0), concrete.Node(2)) == nil {
		t.Errorf("Adding edge didn't create successor")
	}

	dg.RemoveEdge(concrete.Edge{concrete.Node(0), concrete.Node(2)})

	if neighbors := dg.From(concrete.Node(0)); len(neighbors) != 0 || dg.Edge(concrete.Node(0), concrete.Node(2)) != nil {
		t.Errorf("Removing edge didn't properly remove successor")
	}

	if neighbors := dg.To(concrete.Node(2)); len(neighbors) != 0 || dg.Edge(concrete.Node(0), concrete.Node(2)) != nil {
		t.Errorf("Removing directed edge wrongly kept predecessor")
	}

	dg.SetEdgeWeight(concrete.Edge{concrete.Node(0), concrete.Node(2)}, 2)
	// I figure we've torture tested From/To at this point
	// so we'll just use the bool functions now
	if dg.Edge(concrete.Node(0), concrete.Node(2)) == nil {
		t.Error("Adding directed edge didn't change successor back")
	} else if c1, c2 := dg.Weight(concrete.Edge{concrete.Node(2), concrete.Node(0)}), dg.Weight(concrete.Edge{concrete.Node(0), concrete.Node(2)}); math.Abs(c1-c2) < .000001 {
		t.Error("Adding directed edge affected cost in undirected manner")
	}
}
Exemplo n.º 2
0
func TestDenseLists(t *testing.T) {
	dg := concrete.NewDirectedDenseGraph(15, true, math.Inf(1))
	nodes := nodeSorter(dg.Nodes())

	if len(nodes) != 15 {
		t.Fatalf("Wrong number of nodes")
	}

	sort.Sort(nodes)

	for i, node := range dg.Nodes() {
		if i != node.ID() {
			t.Errorf("Node list doesn't return properly id'd nodes")
		}
	}

	edges := dg.Edges()
	if len(edges) != 15*14 {
		t.Errorf("Improper number of edges for passable dense graph")
	}

	dg.RemoveEdge(concrete.Edge{concrete.Node(12), concrete.Node(11)})
	edges = dg.Edges()
	if len(edges) != (15*14)-1 {
		t.Errorf("Removing edge didn't affect edge listing properly")
	}
}