import collections

from nengo.utils import graphs


def graph(edges=None):
    """Construct graph from edges."""
    g = collections.defaultdict(set)
    if edges is not None:
        g.update(edges)
    return g


def test_reversedict():
    edges = graph({"a": {"b", "c"}, "b": set(), "c": set()})
    r_edges = graphs.reverse_edges(edges)
    assert r_edges == {"a": set(), "b": {"a"}, "c": {"a"}}


def test_toposort():
    edges = graph({"a": {"b", "c"}, "b": {"c"}, "c": set()})
    assert graphs.toposort(edges) == ["a", "b", "c"]


def test_transitive_closure():
    edges = graph({"a": {}, "b": {"c", "d"}, "c": set(), "d": {"e"}, "e": set()})
    assert graphs.transitive_closure(edges) == {
        "a": set(),
        "b": {"c", "d", "e"},
        "c": set(),
        "d": {"e"},
        "e": set(),
    }


def test_add_edges():
    edges = graph({"a": {"b", "c"}})
    graphs.add_edges(edges, [("a", "d"), ("b", "c")])
    assert edges == {"a": {"b", "c", "d"}, "b": {"c"}}
