blob: 425966bf0b36f036ac4ba447f98098ef483d6de9 (
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
;; 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 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)))
(deftest test-add-loops
(let [tg1 (add-loops test-graph-1)]
(is (every? (fn [n] (contains? (get-neighbors tg1 n) n)) (:nodes tg1))))
(is (= (add-loops empty-graph) empty-graph)))
(deftest test-remove-loops
(let [tg1 (remove-loops (add-loops test-graph-1))]
(is (not-any? (fn [n] (contains? (get-neighbors tg1 n) n)) (:nodes tg1))))
(is (= (remove-loops 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-lazy-walk
(is (= (lazy-walk test-graph-2 :h) [:h]))
(is (= (lazy-walk test-graph-2 :j) [:j :i])))
(deftest test-transitive-closure
(let [tc-1 (transitive-closure test-graph-1)
tc-2 (transitive-closure test-graph-2)
get (fn [n] (set (get-neighbors tc-2 n)))]
(is (every? #(= #{:a :b :c :d :e} (set %))
(map (partial get-neighbors tc-1) (:nodes tc-1))))
(is (= (get :a) #{:a :b :c :d :e}))
(is (= (get :h) #{}))
(is (= (get :j) #{:i :j}))
(is (= (get :g) #{:a :b :c :d :e :f}))))
(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-component-graph
(let [cg (component-graph test-graph-2)
ecg (component-graph empty-graph)]
(is (= (:nodes cg) (set (scc test-graph-2))))
(is (= (get-neighbors cg #{:a :b :c :d :e})
#{#{:a :b :c :d :e}}))
(is (= (get-neighbors cg #{:g})
#{#{:a :b :c :d :e} #{:f}}))
(is (= (get-neighbors cg #{:i :j})
#{#{:i :j}}))
(is (= (get-neighbors cg #{:h})
#{}))
(is (= (apply max (map count (self-recursive-sets cg))) 1))
(is (= ecg empty-graph))))
(deftest test-recursive-component?
(let [sccs (scc test-graph-2)]
(is (= (set (filter (partial recursive-component? test-graph-2) sccs))
#{#{:i :j} #{:b :c :a :d :e} #{:f}}))))
(deftest test-self-recursive-sets
(is (= (set (self-recursive-sets test-graph-2))
(set (filter
(partial recursive-component? test-graph-2)
(scc test-graph-2)))))
(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
|