File: directed_graph_spec.rb

package info (click to toggle)
jruby 9.4.8.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 89,244 kB
  • sloc: ruby: 548,574; java: 276,189; yacc: 25,873; ansic: 6,178; xml: 6,111; sh: 1,855; sed: 94; makefile: 78; jsp: 48; tcl: 40; exp: 12
file content (88 lines) | stat: -rw-r--r-- 2,390 bytes parent folder | download | duplicates (4)
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
import 'org.jruby.ir.util.DirectedGraph'

# This is spec for Directed Graph Library

describe "Directed Graph Utility" do

  before do
    @graph = DirectedGraph.new
  end

  it "adds an edge to newly created graph" do
    @graph.edges.size.should be 0
    @graph.addEdge(1,2,'foo')
    @graph.addEdge(4,5,'bar')
    @graph.edges.size.should be 2
  end

  it "removes an existing edge from a graph" do
    @graph.addEdge(1,2,'foo')
    @graph.addEdge(4,5,'bar')
    @graph.removeEdge(4,5)
    @graph.edges.size.should be 1
    @graph.removeEdge(@graph.edges.to_a.last)
    @graph.edges.size.should be 0
  end

  it "does not delete a non-existent edge from the graph" do
    @graph.removeEdge(2,1)
    @graph.edges.size.should be 0
  end

  it "removes a vertex and its associated edges" do
    @graph.removeVertexFor(3)
    @graph.vertices.size.should be 0
    @graph.addEdge(1,2,'foo')
    @graph.addEdge(4,5,'bar')
    @graph.removeVertexFor(2)
    @graph.vertices.size.should be 3
    @graph.edges.size.should be 1
  end

  it "gives vertex for given data" do
    @graph.addEdge(1,2,'foo')
    @graph.findOrCreateVertexFor(2).getData().should be 2
  end

  it "creates a new vertex if it is not present" do
    @graph.findOrCreateVertexFor(100).getData().should be 100
  end

  it "finds already existing vertex" do
    @graph.findVertexFor(100).should be_nil
    @graph.addEdge(1,2,'foo')
    @graph.findVertexFor(1).getData().should be 1
  end

  it "gives correct size of graph" do
    @graph.removeEdge(1,2)
    @graph.size.should be 0
    @graph.addEdge(5,6,'baz')
    @graph.size.should be 2
    @graph.addEdge('foo','bar','baz')
    @graph.size.should be 4
  end

  it "gives all data in the graph" do
    @graph.allData.size.should be 0
    @graph.addEdge(1,2,'baz')
    @graph.allData.each do |key|
      @graph.findVertexFor(key).should_not be_nil
    end
    @graph.removeVertexFor(1)
    @graph.allData.each do |key|
      @graph.findVertexFor(key).should_not be_nil
    end
  end

  it "gives data in the graph in the order in which it was inserted" do
    @graph.getInorderData.to_a.size.should be 0
    @graph.findOrCreateVertexFor(1)
    @graph.getInorderData.to_a.should eq [1]
    @graph.addEdge('foo','bar','baz')
    @graph.getInorderData.to_a.should eq [1,'foo','bar']
    @graph.removeVertexFor('foo')
    @graph.getInorderData.to_a.should eq [1,'bar']
  end

end