| 1 |
# Copyright (C) 2017 Kouhei Sutou <[email protected]> |
| 2 |
# Copyright (C) 1993-2013 Yukihiro Matsumoto <[email protected]> |
| 3 |
|
| 4 |
# This file is based on test/test_tsort.rb in CRuby. |
| 5 |
|
| 6 |
class TSortHash < Hash |
| 7 |
include TSort |
| 8 |
alias tsort_each_node each_key |
| 9 |
def tsort_each_child(node, &block) |
| 10 |
fetch(node).each(&block) |
| 11 |
end |
| 12 |
end |
| 13 |
|
| 14 |
class TSortArray < Array |
| 15 |
include TSort |
| 16 |
alias tsort_each_node each_index |
| 17 |
def tsort_each_child(node, &block) |
| 18 |
fetch(node).each(&block) |
| 19 |
end |
| 20 |
end |
| 21 |
|
| 22 |
assert("tsort - dag") do |
| 23 |
h = TSortHash[{1=>[2, 3], 2=>[3], 3=>[]}] |
| 24 |
assert_equal([3, 2, 1], h.tsort) |
| 25 |
assert_equal([[3], [2], [1]], h.strongly_connected_components) |
| 26 |
end |
| 27 |
|
| 28 |
assert("tsort - cycle") do |
| 29 |
h = TSortHash[{1=>[2], 2=>[3, 4], 3=>[2], 4=>[]}] |
| 30 |
assert_equal([[4], [2, 3], [1]], |
| 31 |
h.strongly_connected_components.map {|nodes| nodes.sort}) |
| 32 |
assert_raise(TSort::Cyclic) { h.tsort } |
| 33 |
end |
| 34 |
|
| 35 |
assert("tsort - array") do |
| 36 |
a = TSortArray[[1], [0], [0], [2]] |
| 37 |
assert_equal([[0, 1], [2], [3]], |
| 38 |
a.strongly_connected_components.map {|nodes| nodes.sort}) |
| 39 |
|
| 40 |
a = TSortArray[[], [0]] |
| 41 |
assert_equal([[0], [1]], |
| 42 |
a.strongly_connected_components.map {|nodes| nodes.sort}) |
| 43 |
end |
| 44 |
|
| 45 |
assert("tsort - TSort.tsort") do |
| 46 |
g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]} |
| 47 |
each_node = lambda {|&b| g.each_key(&b) } |
| 48 |
each_child = lambda {|n, &b| g[n].each(&b) } |
| 49 |
assert_equal([4, 2, 3, 1], TSort.tsort(each_node, each_child)) |
| 50 |
g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]} |
| 51 |
assert_raise(TSort::Cyclic) { TSort.tsort(each_node, each_child) } |
| 52 |
end |
| 53 |
|
| 54 |
assert("tsort - TSort.tsort_each") do |
| 55 |
g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]} |
| 56 |
each_node = lambda {|&b| g.each_key(&b) } |
| 57 |
each_child = lambda {|n, &b| g[n].each(&b) } |
| 58 |
r = [] |
| 59 |
TSort.tsort_each(each_node, each_child) {|n| r << n } |
| 60 |
assert_equal([4, 2, 3, 1], r) |
| 61 |
|
| 62 |
r = TSort.tsort_each(each_node, each_child).map {|n| n.to_s } |
| 63 |
assert_equal(['4', '2', '3', '1'], r) |
| 64 |
end |
| 65 |
|
| 66 |
assert("tsort - TSort.strongly_connected_components") do |
| 67 |
g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]} |
| 68 |
each_node = lambda {|&b| g.each_key(&b) } |
| 69 |
each_child = lambda {|n, &b| g[n].each(&b) } |
| 70 |
assert_equal([[4], [2], [3], [1]], |
| 71 |
TSort.strongly_connected_components(each_node, each_child)) |
| 72 |
g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]} |
| 73 |
assert_equal([[4], [2, 3], [1]], |
| 74 |
TSort.strongly_connected_components(each_node, each_child)) |
| 75 |
end |
| 76 |
|
| 77 |
assert("tsort - TSort.each_strongly_connected_component") do |
| 78 |
g = {1=>[2, 3], 2=>[4], 3=>[2, 4], 4=>[]} |
| 79 |
each_node = lambda {|&b| g.each_key(&b) } |
| 80 |
each_child = lambda {|n, &b| g[n].each(&b) } |
| 81 |
r = [] |
| 82 |
TSort.each_strongly_connected_component(each_node, each_child) {|scc| |
| 83 |
r << scc |
| 84 |
} |
| 85 |
assert_equal([[4], [2], [3], [1]], r) |
| 86 |
g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]} |
| 87 |
r = [] |
| 88 |
TSort.each_strongly_connected_component(each_node, each_child) {|scc| |
| 89 |
r << scc |
| 90 |
} |
| 91 |
assert_equal([[4], [2, 3], [1]], r) |
| 92 |
|
| 93 |
r = TSort.each_strongly_connected_component(each_node, each_child).map {|scc| |
| 94 |
scc.map {|element| element.to_s} |
| 95 |
} |
| 96 |
assert_equal([['4'], ['2', '3'], ['1']], r) |
| 97 |
end |
| 98 |
|
| 99 |
assert("tsort - TSort.each_strongly_connected_component_from") do |
| 100 |
g = {1=>[2], 2=>[3, 4], 3=>[2], 4=>[]} |
| 101 |
each_child = lambda {|n, &b| g[n].each(&b) } |
| 102 |
r = [] |
| 103 |
TSort.each_strongly_connected_component_from(1, each_child) {|scc| |
| 104 |
r << scc |
| 105 |
} |
| 106 |
assert_equal([[4], [2, 3], [1]], r) |
| 107 |
|
| 108 |
r = TSort.each_strongly_connected_component_from(1, each_child).map {|scc| |
| 109 |
scc.map {|element| element.to_s} |
| 110 |
} |
| 111 |
assert_equal([['4'], ['2', '3'], ['1']], r) |
| 112 |
end |