graphs 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +1 -0
- data/lib/graph.rb +571 -0
- data/lib/graphs/gdf.rb +225 -0
- data/lib/graphs/json.rb +71 -0
- data/tests/edge_tests.rb +92 -0
- data/tests/gdf_tests.rb +377 -0
- data/tests/graph_tests.rb +703 -0
- data/tests/json_tests.rb +149 -0
- data/tests/node_tests.rb +141 -0
- data/tests/tests.rb +24 -0
- metadata +137 -0
- metadata.gz.sig +0 -0
@@ -0,0 +1,703 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
# -*- coding: UTF-8 -*-
|
3
|
+
|
4
|
+
class Graph_test < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@@sample_graph = Graph.new(
|
8
|
+
[
|
9
|
+
{'label'=>'foo', 'id'=>2},
|
10
|
+
{'label'=>'bar', 'id'=>1},
|
11
|
+
{'label'=>'chuck', 'id'=>3}
|
12
|
+
],
|
13
|
+
[
|
14
|
+
{'node1'=>'foo', 'node2'=>'bar'},
|
15
|
+
{'node1'=>'bar', 'node2'=>'foo'},
|
16
|
+
{'node1'=>'bar', 'node2'=>'chuck'},
|
17
|
+
{'node1'=>'foo', 'node2'=>'chuck'}
|
18
|
+
]
|
19
|
+
)
|
20
|
+
|
21
|
+
@@sample_graph_1 = Graph.new(
|
22
|
+
[
|
23
|
+
{'label'=>'bar', 'num'=>3},
|
24
|
+
{'label'=>'foo', 'num'=>42},
|
25
|
+
{'label'=>'chuck', 'num'=>78}
|
26
|
+
],
|
27
|
+
[
|
28
|
+
{'node1'=>'foo', 'node2'=>'bar', 'time'=>1.0},
|
29
|
+
{'node1'=>'bar', 'node2'=>'foo', 'time'=>2.5},
|
30
|
+
{'node1'=>'foo', 'node2'=>'chuck', 'time'=>3.1}
|
31
|
+
]
|
32
|
+
)
|
33
|
+
|
34
|
+
@@empty = Graph.new
|
35
|
+
|
36
|
+
@@directed = Graph.new(
|
37
|
+
[
|
38
|
+
{'label' => 'foo'},
|
39
|
+
{'label' => 'bar'}
|
40
|
+
],
|
41
|
+
[ { 'node1' => 'foo', 'node2' => 'bar' }]
|
42
|
+
)
|
43
|
+
|
44
|
+
@@directed.attrs[:directed] = true
|
45
|
+
end
|
46
|
+
|
47
|
+
# == Graph#attrs == #
|
48
|
+
|
49
|
+
def test_empty_graph_attrs
|
50
|
+
g = @@empty
|
51
|
+
assert_equal({:directed => true}, g.attrs)
|
52
|
+
assert_equal(true, g.directed?)
|
53
|
+
|
54
|
+
g.attrs['mode'] = 'static'
|
55
|
+
g.attrs['defaultedgetype'] = 'directed'
|
56
|
+
|
57
|
+
assert_equal('static', g.attrs['mode'])
|
58
|
+
assert_equal('directed', g.attrs['defaultedgetype'])
|
59
|
+
end
|
60
|
+
|
61
|
+
# == Graph::intersection == #
|
62
|
+
|
63
|
+
def test_intersection_no_graphs
|
64
|
+
assert_equal(nil, Graph::intersection)
|
65
|
+
assert_equal(nil, Graph::intersection({:same_fields => true}))
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_intersection_2_empty_graphs
|
69
|
+
g = @@empty
|
70
|
+
h = g.clone
|
71
|
+
|
72
|
+
assert_equal(h, Graph::intersection(g, g))
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_intersection_4_empty_graphs_intersection
|
76
|
+
g = @@empty
|
77
|
+
h = g.clone
|
78
|
+
|
79
|
+
assert_equal(h, Graph::intersection(g, g, g, g))
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_intersection_one_node_graph_and_empty_graph
|
83
|
+
g = Graph.new([{'label'=>'foo'}])
|
84
|
+
|
85
|
+
assert_equal(@@empty, Graph::intersection(g, @@empty))
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_intersection_sample_graph_and_itself_5_times
|
89
|
+
g = @@sample_graph
|
90
|
+
h = g.clone
|
91
|
+
|
92
|
+
assert_equal(h, Graph::intersection(g, g, g, g, g))
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_intersection_sample_graph_and_itself_5_times_and_empty_graph
|
96
|
+
g = @@sample_graph
|
97
|
+
|
98
|
+
assert_equal(@@empty, Graph::intersection(g, g, @@empty, g, g, g))
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_intersection_one_node_graph_and_one_other_node_graph
|
102
|
+
g = Graph.new([{'label'=>'foo'}])
|
103
|
+
h = Graph.new([{'label'=>'bar'}])
|
104
|
+
|
105
|
+
assert_equal(@@empty, Graph::intersection(g, h))
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_intersection_sample_graph_and_no_graph
|
109
|
+
g = @@sample_graph
|
110
|
+
|
111
|
+
assert_equal(nil, Graph::intersection(g, 2))
|
112
|
+
assert_equal(nil, Graph::intersection(g, true))
|
113
|
+
assert_equal(nil, Graph::intersection(g, false))
|
114
|
+
assert_equal(nil, Graph::intersection(g, ['foo', 'bar']))
|
115
|
+
assert_equal(nil, Graph::intersection(g, 'foo'))
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_intersection_2_graphs_same_nodes_different_fields
|
119
|
+
g1 = @@sample_graph.clone
|
120
|
+
g2 = @@sample_graph_1.clone
|
121
|
+
|
122
|
+
assert_equal(@@empty, Graph::intersection(g1, g2))
|
123
|
+
# test for side effects
|
124
|
+
assert_equal(@@sample_graph, g1)
|
125
|
+
assert_equal(@@sample_graph_1, g2)
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_intersection_2_graphs_same_nodes_different_fields_same_fields_option
|
129
|
+
g1 = @@sample_graph.clone
|
130
|
+
g2 = @@sample_graph_1.clone
|
131
|
+
|
132
|
+
intersec = Graph.new(
|
133
|
+
[
|
134
|
+
{'label'=>'foo'},
|
135
|
+
{'label'=>'bar'},
|
136
|
+
{'label'=>'chuck'}
|
137
|
+
],
|
138
|
+
[
|
139
|
+
{'node1'=>'foo', 'node2'=>'bar'},
|
140
|
+
{'node1'=>'bar', 'node2'=>'foo'},
|
141
|
+
{'node1'=>'foo', 'node2'=>'chuck'}
|
142
|
+
]
|
143
|
+
)
|
144
|
+
|
145
|
+
assert_equal(intersec, Graph::intersection(g1, g2, :same_fields => true))
|
146
|
+
# test for side effects
|
147
|
+
assert_equal(@@sample_graph, g1)
|
148
|
+
assert_equal(@@sample_graph_1, g2)
|
149
|
+
end
|
150
|
+
|
151
|
+
# == Graph#new == #
|
152
|
+
|
153
|
+
def test_new_empty_graph
|
154
|
+
g = @@empty
|
155
|
+
|
156
|
+
assert_equal([], g.nodes)
|
157
|
+
assert_equal([], g.edges)
|
158
|
+
end
|
159
|
+
|
160
|
+
# == Graph#clone == #
|
161
|
+
|
162
|
+
def test_empty_graph_clone
|
163
|
+
g = @@empty
|
164
|
+
h = g.clone
|
165
|
+
|
166
|
+
assert_equal(g, h)
|
167
|
+
|
168
|
+
h.nodes.push({})
|
169
|
+
|
170
|
+
assert_equal(0, g.nodes.length)
|
171
|
+
assert_not_equal(g, h)
|
172
|
+
end
|
173
|
+
|
174
|
+
# == Graph#== == #
|
175
|
+
|
176
|
+
def test_equal_graphs
|
177
|
+
g1 = @@sample_graph
|
178
|
+
g2 = @@sample_graph.clone()
|
179
|
+
|
180
|
+
assert_equal(true, g1==g2)
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_equal_graph_and_non_graph
|
184
|
+
|
185
|
+
g = Graph.new
|
186
|
+
|
187
|
+
assert_equal(false, g==[])
|
188
|
+
assert_equal(false, g=={})
|
189
|
+
assert_equal(false, g==0)
|
190
|
+
assert_equal(false, g==false)
|
191
|
+
assert_equal(false, g==nil)
|
192
|
+
assert_equal(false, g==Graph::Node.new)
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
# == Graph::NodeArray#set_default == #
|
197
|
+
|
198
|
+
def test_nodearray_set_default_unexisting_property
|
199
|
+
g = Graph.new([{'name'=>'foo'}, {'name'=>'bar'}])
|
200
|
+
g.nodes.set_default 'age' => 21
|
201
|
+
|
202
|
+
assert_equal(21, g.nodes[0]['age'])
|
203
|
+
assert_equal(21, g.nodes[1]['age'])
|
204
|
+
|
205
|
+
assert_equal(21, g.nodes[0]['age'])
|
206
|
+
assert_equal(21, g.nodes[1]['age'])
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_nodearray_set_default_existing_property
|
210
|
+
g = Graph.new([{'name'=>'foo', 'age'=>42}, {'name'=>'bar'}])
|
211
|
+
g.nodes.set_default 'age' => 21
|
212
|
+
|
213
|
+
assert_equal(21, g.nodes[0]['age'])
|
214
|
+
assert_equal(21, g.nodes[1]['age'])
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_nodearray_set_default_unexisting_property_before_push
|
218
|
+
g = Graph.new([{'name'=>'foo'}])
|
219
|
+
g.nodes.set_default 'city' => 'Paris'
|
220
|
+
g.nodes.push({'name' => 'bar'})
|
221
|
+
|
222
|
+
assert_equal('Paris', g.nodes[0]['city'])
|
223
|
+
assert_equal('Paris', g.nodes[0]['city'])
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_nodearray_set_default_existing_property_before_push
|
227
|
+
g = Graph.new([{'name'=>'foo', 'city'=>'London'}])
|
228
|
+
g.nodes.set_default 'city' => 'Paris'
|
229
|
+
g.nodes.push({'name' => 'bar'})
|
230
|
+
|
231
|
+
assert_equal('Paris', g.nodes[0]['city'])
|
232
|
+
assert_equal('Paris', g.nodes[0]['city'])
|
233
|
+
end
|
234
|
+
|
235
|
+
# == Graph::edgeArray#set_default == #
|
236
|
+
|
237
|
+
def test_edgearray_set_default_unexisting_property
|
238
|
+
g = Graph.new([],[{'node1'=>'foo', 'node2'=>'bar'}])
|
239
|
+
g.edges.set_default 'directed' => true
|
240
|
+
|
241
|
+
assert_equal(true, g.edges[0]['directed'])
|
242
|
+
end
|
243
|
+
|
244
|
+
def test_edgearray_set_default_existing_property
|
245
|
+
g = Graph.new([], [{'node1'=>'foo', 'node2'=>'bar', 'directed'=>true},
|
246
|
+
{'node1'=>'bar', 'node2'=>'foo'}])
|
247
|
+
g.edges.set_default 'directed' => false
|
248
|
+
|
249
|
+
assert_equal(false, g.edges[0]['directed'])
|
250
|
+
assert_equal(false, g.edges[1]['directed'])
|
251
|
+
end
|
252
|
+
|
253
|
+
def test_edgearray_set_default_unexisting_property_before_push
|
254
|
+
g = Graph.new([], [{'node1'=>'foo', 'node2'=>'bar'}])
|
255
|
+
g.edges.set_default 'directed' => true
|
256
|
+
g.edges.push({'node1' => 'bar', 'node2'=>'foo'})
|
257
|
+
|
258
|
+
assert_equal(true, g.edges[0]['directed'])
|
259
|
+
assert_equal(true, g.edges[0]['directed'])
|
260
|
+
end
|
261
|
+
|
262
|
+
def test_edgearray_set_default_existing_property_before_push
|
263
|
+
g = Graph.new([], [{'node1'=>'foo', 'node2'=>'bar', 'directed'=>true}])
|
264
|
+
g.edges.set_default 'node2' => 'foo'
|
265
|
+
g.edges.push({'node1' => 'bar', 'node2' => 'foo'})
|
266
|
+
|
267
|
+
assert_equal('foo', g.edges[0]['node2'])
|
268
|
+
assert_equal('foo', g.edges[0]['node2'])
|
269
|
+
end
|
270
|
+
|
271
|
+
# == Graph#& == #
|
272
|
+
|
273
|
+
def test_AND_2_empty_graphs
|
274
|
+
g1 = g2 = @@empty
|
275
|
+
|
276
|
+
assert_equal(g1, g1 & g2)
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_one_node_graph_AND_empty_graph
|
280
|
+
g = Graph.new([{'label'=>'foo'}])
|
281
|
+
|
282
|
+
assert_equal(@@empty, g & @@empty)
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_empty_graph_AND_one_node_graph
|
286
|
+
g = Graph.new([{'label'=>'foo'}])
|
287
|
+
|
288
|
+
assert_equal(@@empty, @@empty & g)
|
289
|
+
end
|
290
|
+
|
291
|
+
def test_sample_graph_AND_itself
|
292
|
+
g = @@sample_graph
|
293
|
+
|
294
|
+
assert_equal(g, g & g)
|
295
|
+
end
|
296
|
+
|
297
|
+
def test_one_node_graph_AND_one_other_node_graph
|
298
|
+
g = Graph.new([{'label'=>'foo'}])
|
299
|
+
h = Graph.new([{'label'=>'bar'}])
|
300
|
+
|
301
|
+
assert_equal(@@empty, g & h)
|
302
|
+
end
|
303
|
+
|
304
|
+
def test_sample_graph_AND_no_graph
|
305
|
+
g = @@sample_graph
|
306
|
+
|
307
|
+
assert_equal(nil, g & 2)
|
308
|
+
assert_equal(nil, g & true)
|
309
|
+
assert_equal(nil, g & false)
|
310
|
+
assert_equal(nil, g & ['foo', 'bar'])
|
311
|
+
assert_equal(nil, g & {'foo'=>'bar'})
|
312
|
+
assert_equal(nil, g & 'foo')
|
313
|
+
end
|
314
|
+
|
315
|
+
def test_AND_2_graphs_same_nodes_different_labels
|
316
|
+
g1 = @@sample_graph
|
317
|
+
g2 = @@sample_graph_1
|
318
|
+
|
319
|
+
assert_equal(@@empty, g1 & g2)
|
320
|
+
end
|
321
|
+
|
322
|
+
# == Graph#^ == #
|
323
|
+
|
324
|
+
def test_XOR_2_empty_graphs
|
325
|
+
g = @@empty
|
326
|
+
assert_equal(g, g ^ g)
|
327
|
+
end
|
328
|
+
|
329
|
+
def test_one_node_graph_XOR_empty_graph
|
330
|
+
g = Graph.new([{'label'=>'foo'}])
|
331
|
+
|
332
|
+
assert_equal(g, g ^ @@empty)
|
333
|
+
end
|
334
|
+
|
335
|
+
def test_empty_graph_XOR_one_node_graph
|
336
|
+
g = Graph.new([{'label'=>'foo'}])
|
337
|
+
|
338
|
+
assert_equal(g, @@empty ^ g)
|
339
|
+
end
|
340
|
+
|
341
|
+
def test_sample_graph_XOR_itself
|
342
|
+
g = @@sample_graph
|
343
|
+
|
344
|
+
assert_equal(@@empty, g ^ g)
|
345
|
+
end
|
346
|
+
|
347
|
+
def test_one_node_graph_XOR_one_other_node_graph
|
348
|
+
g1 = Graph.new([{'label'=>'foo'}])
|
349
|
+
g2 = Graph.new([{'label'=>'bar'}])
|
350
|
+
g3 = Graph.new(g1.nodes+g2.nodes)
|
351
|
+
g4 = Graph.new(g2.nodes+g1.nodes)
|
352
|
+
|
353
|
+
assert_equal(g3, g1 ^ g2)
|
354
|
+
assert_equal(g4, g2 ^ g1)
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_sample_graph_XOR_no_graph
|
358
|
+
g = @@sample_graph
|
359
|
+
|
360
|
+
assert_equal(nil, g ^ 2)
|
361
|
+
assert_equal(nil, g ^ true)
|
362
|
+
assert_equal(nil, g ^ false)
|
363
|
+
assert_equal(nil, g ^ ['foo', 'bar'])
|
364
|
+
assert_equal(nil, g ^ {'foo'=>'bar'})
|
365
|
+
assert_equal(nil, g ^ 'foo')
|
366
|
+
end
|
367
|
+
|
368
|
+
def test_XOR_2_graphs_same_nodes_different_labels
|
369
|
+
g1 = @@sample_graph
|
370
|
+
g2 = @@sample_graph_1
|
371
|
+
g3 = Graph.new(g1.nodes+g2.nodes, g1.edges+g2.edges)
|
372
|
+
|
373
|
+
assert_equal(g3, g1 ^ g2)
|
374
|
+
end
|
375
|
+
|
376
|
+
# == Graph#+ == #
|
377
|
+
|
378
|
+
def test_empty_graph_plus_empty_graph
|
379
|
+
assert_equal(@@empty, @@empty+@@empty)
|
380
|
+
end
|
381
|
+
|
382
|
+
def test_empty_graph_plus_sample_graph
|
383
|
+
g = @@sample_graph
|
384
|
+
|
385
|
+
assert_equal(g, @@empty+g)
|
386
|
+
assert_equal(g, g+@@empty)
|
387
|
+
end
|
388
|
+
|
389
|
+
def test_sample_graph_plus_itself
|
390
|
+
g = @@sample_graph
|
391
|
+
g2 = Graph.new(g.nodes+g.nodes, g.edges+g.edges)
|
392
|
+
|
393
|
+
assert_equal(g2, g+g)
|
394
|
+
end
|
395
|
+
|
396
|
+
def test_graph_plus_non_graph
|
397
|
+
|
398
|
+
g = @@sample_graph
|
399
|
+
|
400
|
+
assert_equal(nil, g+42)
|
401
|
+
assert_equal(nil, g+[])
|
402
|
+
assert_equal(nil, g+{})
|
403
|
+
assert_equal(nil, g+Graph::Node.new)
|
404
|
+
|
405
|
+
end
|
406
|
+
|
407
|
+
# == Graph#| == #
|
408
|
+
|
409
|
+
def test_empty_graph_OR_empty_graph
|
410
|
+
assert_equal(@@empty, @@empty|@@empty)
|
411
|
+
end
|
412
|
+
|
413
|
+
def test_empty_graph_OR_sample_graph
|
414
|
+
g = @@sample_graph
|
415
|
+
|
416
|
+
assert_equal(g, @@empty|g)
|
417
|
+
assert_equal(g, g|@@empty)
|
418
|
+
end
|
419
|
+
|
420
|
+
def test_sample_graph_OR_itself
|
421
|
+
g = @@sample_graph
|
422
|
+
|
423
|
+
assert_equal(g, g|g)
|
424
|
+
end
|
425
|
+
|
426
|
+
def test_sample_graph_OR_other_sample_graph
|
427
|
+
g1 = @@sample_graph
|
428
|
+
g2 = @@sample_graph_1
|
429
|
+
g3 = Graph.new(g1.nodes|g2.nodes, g1.edges|g2.edges)
|
430
|
+
g4 = Graph.new(g2.nodes|g1.nodes, g2.edges|g1.edges)
|
431
|
+
|
432
|
+
assert_equal(g3, g1|g2)
|
433
|
+
assert_equal(g4, g2|g1)
|
434
|
+
end
|
435
|
+
|
436
|
+
def test_graph_OR_non_graph
|
437
|
+
|
438
|
+
g = @@sample_graph
|
439
|
+
|
440
|
+
assert_equal(nil, g|42)
|
441
|
+
assert_equal(nil, g|[])
|
442
|
+
assert_equal(nil, g|{})
|
443
|
+
assert_equal(nil, g|Graph::Node.new)
|
444
|
+
|
445
|
+
end
|
446
|
+
|
447
|
+
# == Graph#- == #
|
448
|
+
|
449
|
+
def test_empty_graph_minus_empty_graph
|
450
|
+
assert_equal(@@empty, @@empty-@@empty)
|
451
|
+
end
|
452
|
+
|
453
|
+
def test_empty_graph_minus_sample_graph
|
454
|
+
g = @@sample_graph
|
455
|
+
|
456
|
+
assert_equal(@@empty, @@empty-g)
|
457
|
+
end
|
458
|
+
|
459
|
+
def test_sample_graph_minus_empty_graph
|
460
|
+
g = @@sample_graph
|
461
|
+
|
462
|
+
assert_equal(g, g-@@empty)
|
463
|
+
end
|
464
|
+
|
465
|
+
def test_sample_graph_minus_itself
|
466
|
+
g = @@sample_graph
|
467
|
+
|
468
|
+
assert_equal(@@empty, g-g)
|
469
|
+
end
|
470
|
+
|
471
|
+
def test_graph_minus_non_graph
|
472
|
+
|
473
|
+
g = @@sample_graph
|
474
|
+
|
475
|
+
assert_equal(nil, g-42)
|
476
|
+
assert_equal(nil, g-[])
|
477
|
+
assert_equal(nil, g-{})
|
478
|
+
assert_equal(nil, g-Graph::Node.new)
|
479
|
+
|
480
|
+
end
|
481
|
+
|
482
|
+
# == Graph#not == #
|
483
|
+
|
484
|
+
def test_empty_graph_NOT_empty_graph
|
485
|
+
assert_equal(@@empty, @@empty.not(@@empty))
|
486
|
+
end
|
487
|
+
|
488
|
+
def test_empty_graph_NOT_sample_graph
|
489
|
+
g = @@sample_graph
|
490
|
+
|
491
|
+
assert_equal(@@empty, @@empty.not(g))
|
492
|
+
end
|
493
|
+
|
494
|
+
def test_sample_graph_NOT_empty_graph
|
495
|
+
g = @@sample_graph
|
496
|
+
|
497
|
+
assert_equal(g, g.not(@@empty))
|
498
|
+
end
|
499
|
+
|
500
|
+
def test_sample_graph_NOT_itself
|
501
|
+
g = @@sample_graph
|
502
|
+
|
503
|
+
assert_equal(@@empty, g.not(g))
|
504
|
+
end
|
505
|
+
|
506
|
+
# == Graph::union == #
|
507
|
+
|
508
|
+
def test_union_one_empty_graph
|
509
|
+
assert_equal(@@empty, Graph::union(@@empty))
|
510
|
+
end
|
511
|
+
|
512
|
+
def test_union_3_empty_graph
|
513
|
+
assert_equal(@@empty, Graph::union(@@empty, @@empty, @@empty))
|
514
|
+
end
|
515
|
+
|
516
|
+
def test_union_empty_graph_and_sample_graph
|
517
|
+
g = @@sample_graph
|
518
|
+
|
519
|
+
assert_equal(g, Graph::union(@@empty, g))
|
520
|
+
assert_equal(g, Graph::union(g, @@empty))
|
521
|
+
end
|
522
|
+
|
523
|
+
def test_union_sample_graph_and_itself
|
524
|
+
g = @@sample_graph
|
525
|
+
|
526
|
+
assert_equal(g, Graph::union(g, g))
|
527
|
+
assert_equal(g, Graph::union(g, g, g, g))
|
528
|
+
end
|
529
|
+
|
530
|
+
def test_union_sample_graph_and_other_sample_graph
|
531
|
+
g1 = @@sample_graph
|
532
|
+
g2 = @@sample_graph_1
|
533
|
+
g3 = Graph.new(g1.nodes|g2.nodes, g1.edges|g2.edges)
|
534
|
+
g4 = Graph.new(g2.nodes|g1.nodes, g2.edges|g1.edges)
|
535
|
+
|
536
|
+
assert_equal(g3, Graph::union(g1, g2))
|
537
|
+
assert_equal(g3, Graph::union(g1, g1, g2))
|
538
|
+
assert_equal(g3, Graph::union(g1, g2, g2))
|
539
|
+
|
540
|
+
assert_equal(g4, Graph::union(g2, g1))
|
541
|
+
assert_equal(g4, Graph::union(g2, g2, g1))
|
542
|
+
assert_equal(g4, Graph::union(g2, g1, g1))
|
543
|
+
end
|
544
|
+
|
545
|
+
# == Graph::xor == #
|
546
|
+
|
547
|
+
def test_xor_one_empty_graph
|
548
|
+
assert_equal(@@empty, Graph::xor(@@empty))
|
549
|
+
end
|
550
|
+
|
551
|
+
def test_xor_3_empty_graph
|
552
|
+
assert_equal(@@empty, Graph::xor(@@empty, @@empty, @@empty))
|
553
|
+
end
|
554
|
+
|
555
|
+
def test_xor_empty_graph_and_sample_graph
|
556
|
+
g = @@sample_graph
|
557
|
+
|
558
|
+
assert_equal(g, Graph::xor(@@empty, g))
|
559
|
+
assert_equal(g, Graph::xor(g, @@empty))
|
560
|
+
end
|
561
|
+
|
562
|
+
def test_xor_sample_graph_and_itself
|
563
|
+
g = @@sample_graph
|
564
|
+
|
565
|
+
assert_equal(@@empty, Graph::xor(g, g))
|
566
|
+
assert_equal(@@empty, Graph::xor(g, g, g, g))
|
567
|
+
end
|
568
|
+
|
569
|
+
def test_xor_sample_graph_and_other_sample_graph
|
570
|
+
g1 = @@sample_graph
|
571
|
+
g2 = @@sample_graph_1
|
572
|
+
|
573
|
+
def _xor(g,h)
|
574
|
+
[(g.nodes|h.nodes)-(g.nodes&h.nodes),
|
575
|
+
(g.edges|h.edges)-(g.edges&h.edges)]
|
576
|
+
end
|
577
|
+
|
578
|
+
g_1_2 = Graph.new(*_xor(g1,g2))
|
579
|
+
g_2_1 = Graph.new(*_xor(g2,g1))
|
580
|
+
|
581
|
+
assert_equal(g_1_2, Graph::xor(g1, g2))
|
582
|
+
assert_equal(g_2_1, Graph::xor(g2, g1))
|
583
|
+
end
|
584
|
+
|
585
|
+
# == Graph#write == #
|
586
|
+
|
587
|
+
def test_graph_write_no_ext
|
588
|
+
g = @@sample_graph
|
589
|
+
f = '/tmp/_graph_test'
|
590
|
+
g.write(f)
|
591
|
+
assert_equal(true, File.exists?(f))
|
592
|
+
|
593
|
+
dict = YAML.load(File.open(f))
|
594
|
+
assert_equal(g.nodes, dict['nodes'])
|
595
|
+
assert_equal(g.edges, dict['edges'])
|
596
|
+
end
|
597
|
+
|
598
|
+
def test_graph_write_unknow_ext
|
599
|
+
g = @@sample_graph
|
600
|
+
f = '/tmp/_graph_test.foo'
|
601
|
+
assert_raise(NoMethodError) do
|
602
|
+
g.write(f)
|
603
|
+
end
|
604
|
+
assert_equal(false, File.exists?(f))
|
605
|
+
end
|
606
|
+
|
607
|
+
# == Graph#get_node == #
|
608
|
+
|
609
|
+
def test_graph_get_node_unexisting_label
|
610
|
+
|
611
|
+
n = @@sample_graph.get_node 'foobar'
|
612
|
+
|
613
|
+
assert_equal(nil, n)
|
614
|
+
end
|
615
|
+
|
616
|
+
def test_graph_get_node_existing_label
|
617
|
+
|
618
|
+
g = @@sample_graph;
|
619
|
+
|
620
|
+
n = g.get_node 'foo'
|
621
|
+
|
622
|
+
assert_equal(g.nodes[0], n)
|
623
|
+
end
|
624
|
+
|
625
|
+
# == Graph#get_neighbours == #
|
626
|
+
|
627
|
+
def test_graph_get_neighbours_unexisting_node_label
|
628
|
+
|
629
|
+
n = @@sample_graph.get_neighbours 'moo'
|
630
|
+
|
631
|
+
assert_equal([], n)
|
632
|
+
|
633
|
+
end
|
634
|
+
|
635
|
+
def test_graph_get_neighbours_unexisting_node_object
|
636
|
+
|
637
|
+
n = @@sample_graph.get_neighbours Graph::Node.new( :label => 'moo' )
|
638
|
+
|
639
|
+
assert_equal([], n)
|
640
|
+
|
641
|
+
end
|
642
|
+
|
643
|
+
def test_graph_get_neighbours_undirected_graph
|
644
|
+
|
645
|
+
g = @@sample_graph
|
646
|
+
g.attrs[:directed] = false
|
647
|
+
|
648
|
+
n1 = g.get_neighbours 'chuck'
|
649
|
+
n2 = g.get_neighbours 'foo'
|
650
|
+
|
651
|
+
assert_equal([ 'bar', 'foo' ], n1.map { |m| m.label })
|
652
|
+
assert_equal([ 'bar', 'chuck' ], n2.map { |m| m.label })
|
653
|
+
|
654
|
+
end
|
655
|
+
|
656
|
+
def test_graph_get_neighbours_directed_graph
|
657
|
+
|
658
|
+
g = @@directed
|
659
|
+
|
660
|
+
n = g.get_neighbours 'foo'
|
661
|
+
assert_equal([ 'bar' ], n.map { |m| m.label })
|
662
|
+
|
663
|
+
n = g.get_neighbours 'bar'
|
664
|
+
assert_equal([], n)
|
665
|
+
|
666
|
+
end
|
667
|
+
|
668
|
+
def test_graph_get_neighbours_bad_edges
|
669
|
+
|
670
|
+
g = Graph.new(
|
671
|
+
[ { :label => 'foo' },
|
672
|
+
{ :label => 'bar' },
|
673
|
+
{ :label => 'moo' }],
|
674
|
+
|
675
|
+
[ { :node1 => 'foo' }, # missing :node2 attr
|
676
|
+
{ :node1 => 'foo', :node2 => 'moo' }])
|
677
|
+
|
678
|
+
n = g.get_neighbours 'foo'
|
679
|
+
|
680
|
+
assert_equal(1, n.length)
|
681
|
+
assert_equal('moo', n[0].label)
|
682
|
+
|
683
|
+
end
|
684
|
+
|
685
|
+
# == Graph#get_label == #
|
686
|
+
|
687
|
+
def test_graph_get_label_string
|
688
|
+
s = "foo"
|
689
|
+
assert_equal(s, Graph::get_label(s))
|
690
|
+
end
|
691
|
+
|
692
|
+
def test_graph_get_label_node
|
693
|
+
s = "foo"
|
694
|
+
assert_equal(s, Graph::get_label(Graph::Node.new({:label => s})))
|
695
|
+
end
|
696
|
+
|
697
|
+
def test_graph_get_label_not_a_string_nor_a_node
|
698
|
+
assert_raise(TypeError) { Graph::get_label(42) }
|
699
|
+
assert_raise(TypeError) { Graph::get_label([]) }
|
700
|
+
assert_raise(TypeError) { Graph::get_label(nil) }
|
701
|
+
end
|
702
|
+
|
703
|
+
end
|