| @@ -0,0 +1,9 @@ | |||
| #include "igraph.h" | |||
| #include "ruby.h" | |||
| #include "cIGraph.h" | |||
| VALUE cIGraph_add_op(VALUE self, VALUE graph){ | |||
| return Qnil; | |||
| } | |||
| @@ -0,0 +1,93 @@ | |||
| #include "igraph.h" | |||
| #include "ruby.h" | |||
| #include "cIGraph.h" | |||
| /* call-seq: | |||
| * graph.vertices -> Array | |||
| * | |||
| * Returns an Array containing all the vertices in the graph. Also aliased | |||
| * to IGraph#all_vertices | |||
| * | |||
| * Example: | |||
| * | |||
| * g = IGraph.new([1,2,3,4],true) | |||
| * g.vertices #returns [1,2,3,4] | |||
| * | |||
| */ | |||
| VALUE cIGraph_all_v(VALUE self){ | |||
| igraph_t *graph; | |||
| Data_Get_Struct(self, igraph_t, graph); | |||
| return rb_funcall(rb_iv_get(self,"@object_ids"),rb_intern("keys"),0); | |||
| } | |||
| /* call-seq: | |||
| * graph.adjacent_vertices(v,mode) -> Array | |||
| * | |||
| * Returns an Array containing all the vertices in the graph that are | |||
| * adjacent to vertex v. mode decides the type of the neighborhood for | |||
| * directed graphs. Possible values: IGRAPH_OUT, all vertices to which | |||
| * there is a directed edge from vid. IGRAPH_IN, all vertices from which | |||
| * there is a directed edge from vid. IGRAPH_ALL, all vertices to which | |||
| * or from which there is a directed edge from/to vid. This parameter is | |||
| * ignored for undirected graphs. | |||
| * | |||
| * Example: | |||
| * | |||
| * g = IGraph.new([1,2,3,4],true) | |||
| * g.adjacent_vertices(1,IGraph::ALL) #returns [2] | |||
| * | |||
| */ | |||
| VALUE cIGraph_adj_v(VALUE self, VALUE v, VALUE mode){ | |||
| igraph_t *graph; | |||
| igraph_integer_t pnode; | |||
| VALUE adjacent = rb_ary_new(); | |||
| igraph_neimode_t pmode = NUM2INT(mode); | |||
| igraph_vs_t vs; | |||
| igraph_vit_t vit; | |||
| Data_Get_Struct(self, igraph_t, graph); | |||
| pnode = cIGraph_get_vertex_id(self,v); | |||
| igraph_vs_adj(&vs,pnode,pmode); | |||
| igraph_vit_create(graph, vs, &vit); | |||
| while(!IGRAPH_VIT_END(vit)) { | |||
| rb_ary_push(adjacent,cIGraph_get_vertex_object(self,IGRAPH_VIT_GET(vit))); | |||
| IGRAPH_VIT_NEXT(vit); | |||
| } | |||
| igraph_vit_destroy(&vit); | |||
| igraph_vs_destroy(&vs); | |||
| return adjacent; | |||
| } | |||
| VALUE cIGraph_nonadj_v(VALUE self, VALUE v, VALUE mode){ | |||
| return Qnil; | |||
| } | |||
| VALUE cIGraph_all_e(VALUE self, VALUE mode){ | |||
| return Qnil; | |||
| } | |||
| VALUE cIGraph_adj_e(VALUE self, VALUE v, VALUE mode){ | |||
| return Qnil; | |||
| } | |||
| VALUE cIGraph_nonadj_e(VALUE self, VALUE v, VALUE mode){ | |||
| return Qnil; | |||
| } | |||
| @@ -0,0 +1,44 @@ | |||
| require 'test/unit' | |||
| require 'igraph' | |||
| class TestGraph < Test::Unit::TestCase | |||
| def test_each_vertex | |||
| graph = IGraph.new(['A','B','C','D'],true) | |||
| assert_nothing_raised do | |||
| graph.each_vertex do |v| | |||
| end | |||
| end | |||
| assert_nothing_raised do | |||
| graph.each do |v| | |||
| end | |||
| end | |||
| end | |||
| def test_each_edge | |||
| graph = IGraph.new(['A','B','C','D'],true) | |||
| assert_nothing_raised do | |||
| graph.each_edge(IGraph::EDGEORDER_ID) do |v,w| | |||
| end | |||
| end | |||
| assert_nothing_raised do | |||
| graph.each_edge_eid(IGraph::EDGEORDER_ID) do |v| | |||
| end | |||
| end | |||
| edges = [] | |||
| graph.each_edge(IGraph::EDGEORDER_ID){|v,w| edges.push([v,w])} | |||
| assert_equal [['A','B'],['C','D']], edges | |||
| edges = [] | |||
| graph.each_edge_eid(IGraph::EDGEORDER_ID){|v| edges.push(v)} | |||
| assert_equal [0,1], edges | |||
| end | |||
| def test_enumerable | |||
| graph = IGraph.new(['A','B','C','D'],true) | |||
| assert graph.all?{|v| v.kind_of? String} | |||
| assert graph.any?{|v| v == 'B'} | |||
| assert_equal ['A','B','C','D'], graph.collect{|v| v} | |||
| assert graph.detect(Proc.new{true}){|v| } | |||
| assert_equal ['A'], graph.find_all{|v| v < 'B'} | |||
| end | |||
| end | |||
| @@ -0,0 +1,14 @@ | |||
| require 'test/unit' | |||
| require 'igraph' | |||
| class TestGraph < Test::Unit::TestCase | |||
| def test_select_all | |||
| graph = IGraph.new(['A','B','C','D'],true) | |||
| assert_equal ['A','B','C','D'], graph.all_vertices | |||
| assert_equal ['A','B','C','D'], graph.vertices | |||
| end | |||
| def test_adj | |||
| graph = IGraph.new(['A','B','C','D'],true) | |||
| assert_equal ['B'], graph.adjacent_vertices('A',IGraph::ALL) | |||
| end | |||
| end | |||
| @@ -0,0 +1,28 @@ | |||
| require 'test/unit' | |||
| require 'igraph' | |||
| require 'mocha' | |||
| class TestGraph < Test::Unit::TestCase | |||
| def test_shortest_paths | |||
| graph = IGraph.new(['A','B','C','D'],true) | |||
| m = graph.shortest_paths(['A'],IGraph::ALL) | |||
| assert_equal 1, m[0][1] | |||
| assert_equal 0, m[0][0] | |||
| assert_equal graph.vcount, m[0].size | |||
| assert_equal nil, m[0][2] | |||
| end | |||
| def test_get_shortest_paths_warn | |||
| graph = IGraph.new(['A','B','C','D'],true) | |||
| graph.expects(:warn).with("Couldn't reach some vertices") | |||
| graph.get_shortest_paths('A',['B','C'],IGraph::ALL) | |||
| end | |||
| def test_get_shortest_paths | |||
| graph = IGraph.new(['A','B','C','D'],true) | |||
| m = graph.get_shortest_paths('A',['B','C'],IGraph::ALL) | |||
| assert_equal ['A','B'], m[0] | |||
| assert_equal [], m[1] | |||
| end | |||
| end | |||