提交 1feb6e22 authored 作者: Matthew Rocklin's avatar Matthew Rocklin

add tests for partially ordered sort

上级 848988cd
...@@ -3,7 +3,8 @@ import unittest ...@@ -3,7 +3,8 @@ import unittest
from theano import tensor from theano import tensor
from theano.gof.graph import ( from theano.gof.graph import (
Apply, as_string, clone, general_toposort, inputs, io_toposort, Apply, as_string, clone, general_toposort, inputs, io_toposort,
is_same_graph, Variable, dependence, sort_apply_nodes) is_same_graph, Variable, dependence, sort_apply_nodes, posort,
reverse_dict, _toposort)
from theano.gof.op import Op from theano.gof.op import Op
from theano.gof.type import Type from theano.gof.type import Type
...@@ -307,3 +308,24 @@ def test_sort_apply_nodes(): ...@@ -307,3 +308,24 @@ def test_sort_apply_nodes():
for a, b in zip(nodes[:-1], nodes[1:]): for a, b in zip(nodes[:-1], nodes[1:]):
assert str(a) <= str(b) assert str(a) <= str(b)
def test_reverse_dict():
d = {'a': (1, 2), 'b': (2, 3), 'c':()}
assert reverse_dict(d) == {1: ('a',), 2: ('a', 'b'), 3: ('b',)}
def test__toposort():
edges = {1: {4, 6, 7}, 2: {4, 6, 7}, 3: {5, 7}, 4: {6, 7}, 5: {7}}
order = _toposort(edges)
assert not any(a in edges.get(b, ()) for i, a in enumerate(order)
for b in order[i:])
def test_posort_easy():
nodes = "asdfghjkl"
cmp = lambda a,b: -1 if a<b else 1 if a>b else 0
assert posort(nodes, cmp) == list("adfghjkls")
def test_posort():
l = range(1,20)
cmps = [lambda a,b: a%10 - b%10, lambda a, b: (a/10)%2 - (b/10)%2,
lambda a,b: a-b]
assert posort(l, *cmps) == \
[10, 1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19]
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论