blob: 248203fd22ba51e84888f2ad33f23da7cfe28c5c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
;; Copyright (c) Jeffrey Straszheim. All rights reserved. The use and
;; distribution terms for this software are covered by the Eclipse Public
;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
;; be found in the file epl-v10.html at the root of this distribution. By
;; using this software in any fashion, you are agreeing to be bound by the
;; terms of this license. You must not remove this notice, or any other,
;; from this software.
;;
;; test-graph
;;
;; Basic Graph Theory Algorithms Tests
;;
;; straszheimjeffrey (gmail)
;; Created 23 June 2009
(ns clojure.contrib.test-contrib.test-graph
(use :reload clojure.contrib.test-is
clojure.contrib.graph))
(def empty-graph (struct directed-graph #{} {}))
(def test-graph-1
(struct directed-graph
#{:a :b :c :d :e}
{:a #{:b :c}
:b #{:a :c}
:c #{:d :e}
:d #{:a :b}
:e #{:d}}))
(deftest test-reverse-graph
(is (= (reverse-graph test-graph-1)
(struct directed-graph
#{:a :b :c :d :e}
{:c #{:b :a}
:e #{:c}
:d #{:c :e}
:b #{:d :a}
:a #{:d :b}})))
(is (= (reverse-graph (reverse-graph test-graph-1))
test-graph-1))
(is (= (reverse-graph empty-graph) empty-graph)))
(def test-graph-2
(struct directed-graph
#{:a :b :c :d :e :f :g :h :i :j}
{:a #{:b :c}
:b #{:a :c}
:c #{:d :e}
:d #{:a :b}
:e #{:d}
:f #{:f}
:g #{:a :f}
:h #{}
:i #{:j}
:j #{:i}}))
(deftest test-post-ordered-nodes
(is (= (set (post-ordered-nodes test-graph-2))
#{:a :b :c :d :e :f :g :h :i :j}))
(is (empty? (post-ordered-nodes empty-graph))))
(deftest test-scc
(is (= (set (scc test-graph-2))
#{#{:h} #{:g} #{:i :j} #{:b :c :a :d :e} #{:f}}))
(is (empty? (scc empty-graph))))
(deftest test-self-recursive-sets
(is (= (set (self-recursive-sets test-graph-2))
#{#{:i :j} #{:b :c :a :d :e} #{:f}}))
(is (empty? (self-recursive-sets empty-graph))))
(def test-graph-3
(struct directed-graph
#{:a :b :c :d :e :f}
{:a #{:b}
:b #{:c}
:c #{:d}
:d #{:e}
:e #{:f}
:f #{}}))
(def test-graph-4
(struct directed-graph
#{:a :b :c :d :e :f :g :h}
{:a #{}
:b #{:a}
:c #{:a}
:d #{:a :b}
:e #{:d :c}
:f #{:e}
:g #{:d}
:h #{:f}}))
(def test-graph-5
(struct directed-graph
#{:a :b :c :d :e :f :g :h}
{:a #{}
:b #{}
:c #{:b}
:d #{}
:e #{}
:f #{}
:g #{:f}
:h #{}}))
(deftest test-dependency-list
(is (thrown-with-msg? Exception #".*Fixed point overflow.*"
(dependency-list test-graph-2)))
(is (= (dependency-list test-graph-3)
[#{:f} #{:e} #{:d} #{:c} #{:b} #{:a}]))
(is (= (dependency-list test-graph-4)
[#{:a} #{:b :c} #{:d} #{:g :e} #{:f} #{:h}]))
(is (= (dependency-list test-graph-5)
[#{:f :b :a :d :h :e} #{:g :c}]))
(is (= (dependency-list empty-graph)
[#{}])))
(deftest test-stratification-list
(is (thrown-with-msg? Exception #".*Fixed point overflow.*"
(stratification-list test-graph-2 test-graph-2)))
(is (= (stratification-list test-graph-4 test-graph-5)
[#{:a} #{:b :c} #{:d} #{:e} #{:f :g} #{:h}]))
(is (= (stratification-list empty-graph empty-graph)
[#{}])))
(comment
(run-tests)
)
;; End of file
|