git-svn-id: http://igraph.rubyforge.org/svn/trunk@60 71f48855-0bbf-4aa5-930d-4df415e86613

This commit is contained in:
alexgutteridge 2007-11-14 05:18:20 +00:00
parent 882b23031d
commit 700278bd95
7 changed files with 269 additions and 0 deletions

32
test/tc_community.rb Normal file
View File

@ -0,0 +1,32 @@
require 'test/unit'
require 'igraph'
class TestGraph < Test::Unit::TestCase
def test_modularity
g = IGraph.new(['A','B','B','C','A','C','C','D','D','E','E','F','D','F'])
assert_in_delta 0.357, g.modularity([['A','B','C'],['D','E','F']]), 0.001
end
def test_spinglass
g = IGraph.new(['A','B','B','C','A','C','C','D','D','E','E','F','D','F'])
groups,mod,temp = g.community_spinglass([],25,false,1,0.01,0.99,IGraph::SPINCOMM_UPDATE_SIMPLE,1.0)
assert_in_delta 0.357, mod, 0.001
assert_in_delta 0.200, temp, 0.100
assert_equal [['A','B','C','D','E','F']], groups
commun,coh,adh = g.community_spinglass_single([],'A',25,IGraph::SPINCOMM_UPDATE_SIMPLE,1.0)
assert_in_delta 1.25, coh, 0.001
assert_in_delta(-2.5, adh, 0.100)
assert_equal ['A','B','C'], commun
end
def test_eigen
g = IGraph.new(['A','B','B','C','A','C','C','D','D','E','E','F','D','F'],false)
groups,merges = g.community_leading_eigenvector(6)
assert_equal [['A','B','C'],['D','E','F']], groups
assert_equal [[0,1]], merges.to_a
groups,merges = g.community_leading_eigenvector_naive(6)
assert_equal [['A','B','C'],['D','E','F']], groups
assert_equal [[0,1]], merges.to_a
groups,split,eigenvec,eigenval = g.community_leading_eigenvector_step([['A','B','C','D','E','F']],0)
end
end

22
test/tc_connectivity.rb Normal file
View File

@ -0,0 +1,22 @@
require 'test/unit'
require 'igraph'
class TestGraph < Test::Unit::TestCase
def test_connecitivity
g = IGraph.new(['A','B','B','C','C','D'],true)
assert_equal 1, g.st_edge_connectivity('A','B')
assert_equal 0, g.edge_connectivity
assert_equal 1, g.st_vertex_connectivity('A','C',IGraph::VCONN_NEI_ERROR)
assert_equal 0, g.vertex_connectivity
end
def test_disjoint
g = IGraph.new(['A','B','B','C','C','D','A','E','E','D'],true)
assert_equal 2, g.edge_disjoint_paths('A','D')
assert_equal 2, g.vertex_disjoint_paths('A','D')
end
def test_adhesion
g = IGraph.new(['A','B','B','C','C','D'],true)
assert_equal 0, g.adhesion
assert_equal 0, g.cohesion
end
end

View File

@ -0,0 +1,61 @@
require 'test/unit'
require 'igraph'
class TestGraph < Test::Unit::TestCase
def test_adjacency
m = IGraphMatrix.new([0,1,1,0],[1,0,0,0],[1,0,0,1],[0,0,1,0])
g = IGraph.adjacency(m,IGraph::ADJ_MAX)
assert_equal 4, g.vcount
assert_equal 3, g.ecount
end
def test_star
g = IGraph.star(10,IGraph::STAR_UNDIRECTED,0)
assert_equal 10, g.vcount
assert_equal 9, g.ecount
end
def test_lattice
g = IGraph.lattice([2,2],false,false,false)
assert_equal 4, g.vcount
assert_equal 4, g.ecount
end
def test_ring
g = IGraph.ring(10,false,false,false)
assert_equal 10, g.vcount
assert_equal 9, g.ecount
end
def test_tree
g = IGraph.tree(13,3,IGraph::TREE_UNDIRECTED)
assert_equal 13, g.vcount
assert_equal 12, g.ecount
end
def test_full
g = IGraph.full(10,false,false)
assert_equal 10, g.vcount
assert_equal 45, g.ecount
end
def test_atlas
g = IGraph.atlas(10)
assert_equal 4, g.vcount
assert_equal 2, g.ecount
end
def test_extended_chordal_ring
g = IGraph.extended_chordal_ring(3,IGraphMatrix.new([1,2,3],[1,2,3],[1,2,3]))
assert_equal 3, g.vcount
assert_equal 6, g.ecount
end
def test_connect_neighborhood
g = IGraph.new([1,2,1,3,3,4],false)
g.connect_neighborhood(2,IGraph::ALL)
assert g.are_connected?(2,3)
end
end

34
test/tc_layout3d.rb Normal file
View File

@ -0,0 +1,34 @@
require 'test/unit'
require 'igraph'
class TestGraph < Test::Unit::TestCase
def test_random_3d
g = IGraph.new([1,2,3,4],true)
l = g.layout_random_3d
assert_instance_of IGraphMatrix, l
assert_equal g.vcount, l.nrow
assert_equal 3, l.ncol
end
def test_sphere
g = IGraph.new([1,2,3,4],true)
l = g.layout_sphere
assert_instance_of IGraphMatrix, l
assert_equal g.vcount, l.nrow
assert_equal 3, l.ncol
end
def test_fruchterman_reingold_3d
g = IGraph.new([1,2,3,4],true)
l = g.layout_fruchterman_reingold_3d(10,1,1,2,1,false)
assert_instance_of IGraphMatrix, l
assert_equal g.vcount, l.nrow
assert_equal 3, l.ncol
end
def test_kamada_kawai_3d
g = IGraph.new([1,2,3,4],true)
l = g.layout_kamada_kawai_3d(10,1,1,2,1)
assert_instance_of IGraphMatrix, l
assert_equal g.vcount, l.nrow
assert_equal 3, l.ncol
end
end

20
test/tc_mincuts.rb Normal file
View File

@ -0,0 +1,20 @@
require 'test/unit'
require 'igraph'
class TestGraph < Test::Unit::TestCase
def test_maxflow_mincut
g = IGraph.new([1,2,2,3,3,4])
assert_equal 3, g.maxflow_value(1,4,[5,4,3])
assert_equal 3, g.st_mincut_value(1,4,[5,4,3])
end
def test_mincut
g = IGraph.new([1,2,2,3,3,4,2,1,3,2,4,3],true)
assert_equal 3, g.mincut_value([5,4,3,3,4,5])
g = IGraph.new([1,2,2,3,1,3,3,4,4,5,4,6,5,6],false)
val,p1,p2,cut_eid = g.mincut(Array.new(7,1))
assert_equal 1, val
assert_equal [1,2,3], p2.sort
assert_equal [4,5,6], p1.sort
assert_equal [3], cut_eid
end
end

15
test/tc_randomisation.rb Executable file
View File

@ -0,0 +1,15 @@
require 'test/unit'
require 'igraph'
class TestGraph < Test::Unit::TestCase
def test_rewire_edges
g = IGraph.grg_game(10,0.1,false)
h = g.rewire_edges(0.5)
assert_equal 10, h.to_a.size
end
def test_rewire
g = IGraph.grg_game(10,0.1,false)
h = g.rewire(0.5)
assert_equal 10, h.to_a.size
end
end

85
test/test_draw_atlas.rb Normal file
View File

@ -0,0 +1,85 @@
require 'igraph'
require 'cairo'
gs = []
vs = []
ARGV[0].to_i.times do |n|
gs << IGraph.atlas(n+1)
vs += gs[-1].vertices
end
ls = gs.map{|g| g.layout_fruchterman_reingold(10,1,1,2,1,false)}
layout = IGraph.layout_merge_dla(gs,ls).to_a
format = Cairo::FORMAT_ARGB32
width = 1000
height = 1000
surface = Cairo::ImageSurface.new(format, width, height)
cr = Cairo::Context.new(surface)
# fill background with white
cr.set_source_rgba(1.0, 1.0, 1.0, 0.8)
cr.paint
max_x = layout.map{|a| a[0]}.max
min_x = layout.map{|a| a[0]}.min
max_y = layout.map{|a| a[1]}.max
min_y = layout.map{|a| a[1]}.min
x_var = max_x - min_x
y_var = max_y - min_y
max_var = [x_var,y_var].max
layout.each_with_index do |a,i|
x,y = *a
x = (x - min_x)/max_var
y = (y - min_y)/max_var
x *= (width)
y *= (height)
layout[i] = [x,y]
puts "#{x} #{y}"
end
vn = 1
gi = 0
g = gs[gi]
layout.each_with_index do |a,i|
sub_layout = layout[start..finish]
v = vs[i]
x,y = *a
if vn > g.vcount
gi += 1
g = gs[gi]
vn = 1
end
puts "Looking for: " + v.inspect
puts "In: #{g.to_a.inspect}"
puts "Graph number: #{gi}"
puts "Vertex number: #{vn}"
vn += 1
cr.set_source_rgba(0,0,0,0.5)
cr.circle(x,y,10).fill
g.adjacent_vertices(v,IGraph::OUT).each do |w|
cr.move_to(x,y)
wx,wy = *layout[vs.index(w)]
cr.line_to(wx,wy)
cr.stroke
end
end
cr.target.write_to_png("test.png")