Пример #1
0
func TestSelfEqual(t *testing.T) {
	s := set.NewSet()

	if !set.Equal(s, s) {
		t.Error("Set is not equal to itself")
	}

	s.Add(1)

	if !set.Equal(s, s) {
		t.Error("Set ceases self equality after adding element")
	}
}
Пример #2
0
func TestEqual(t *testing.T) {
	s1 := set.NewSet()
	s2 := set.NewSet()

	if !set.Equal(s1, s2) {
		t.Error("Two different empty sets not equal")
	}

	s1.Add(1)
	if set.Equal(s1, s2) {
		t.Error("Two different sets with different elements not equal")
	}

	s2.Add(1)
	if !set.Equal(s1, s2) {
		t.Error("Two sets with same element not equal")
	}
}
Пример #3
0
func TestCopy(t *testing.T) {
	s1 := set.NewSet()
	s2 := set.NewSet()

	s1.Add(1)
	s1.Add(2)
	s1.Add(3)

	s2.Copy(s1)

	if !set.Equal(s1, s2) {
		t.Fatalf("Two sets not equal after copy")
	}

	s2.Remove(1)

	if set.Equal(s1, s2) {
		t.Errorf("Mutating one set mutated another after copy")
	}
}
Пример #4
0
// A Postdominates B if and only if all paths from B travel through A
//
// This returns all possible post-dominators for all nodes, it does not prune for strict postdominators, immediate postdominators etc
func PostDominators(end Node, graph Graph) map[int]*set.Set {
	allNodes := set.NewSet()
	nlist := graph.NodeList()
	dominators := make(map[int]*set.Set, len(nlist))
	for _, node := range nlist {
		allNodes.Add(node.ID())
	}

	for _, node := range nlist {
		dominators[node.ID()] = set.NewSet()
		if node.ID() == end.ID() {
			dominators[node.ID()].Add(end.ID())
		} else {
			dominators[node.ID()].Copy(allNodes)
		}
	}

	for somethingChanged := true; somethingChanged; {
		somethingChanged = false
		for _, node := range nlist {
			if node.ID() == end.ID() {
				continue
			}
			succs := graph.Successors(node)
			if len(succs) == 0 {
				continue
			}
			tmp := set.NewSet().Copy(dominators[succs[0].ID()])
			for _, succ := range succs[1:] {
				tmp.Intersection(tmp, dominators[succ.ID()])
			}

			dom := set.NewSet()
			dom.Add(node.ID())

			dom.Union(dom, tmp)
			if !set.Equal(dom, dominators[node.ID()]) {
				dominators[node.ID()] = dom
				somethingChanged = true
			}
		}
	}

	return dominators
}