File: directed_graph_spec.rb

package info (click to toggle)
dirgra 0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208 kB
  • sloc: ruby: 658; java: 539; xml: 130; makefile: 8
file content (101 lines) | stat: -rw-r--r-- 2,821 bytes parent folder | download
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
require 'dirgra-0.3.jar'
import 'org.jruby.dirgra.DirectedGraph'

require 'vertex_id_helper'

# This is spec for Directed Graph Library

describe "Directed Graph Utility" do

  let(:graph) { DirectedGraph.new }
  let(:one) { VertexID.new(1) }
  let(:two) { VertexID.new(2) }
  let(:three) { VertexID.new(3) }
  let(:four) { VertexID.new(4) }
  let(:five) { VertexID.new(5) }
  let(:six) { VertexID.new(6) }
  let(:hundred) { VertexID.new(100) }
  let(:foo) { VertexID.new("foo") }
  let(:bar) { VertexID.new("bar") }

  before do
    @edge_count = 0
  end

  it "adds an edge to newly created graph" do
    expect(graph.edges.size).to eq 0
    add_edge(one,two,'foo')
    add_edge(four,five,'bar')
    expect(graph.edges.size).to eq 2
  end

  it "removes an existing edge from a graph" do
    add_edge(one,two,'foo')
    add_edge(four,five,'bar')
    remove_edge(four,five)
    expect(graph.edges.size).to eq 1
    graph.removeEdge(graph.edges.to_a.last)
    expect(graph.edges.size).to eq 0
  end

  it "does not delete a non-existent edge from the graph" do
    remove_edge(two,one)
    expect(graph.edges.size).to eq 0
  end

  it "removes a vertex and its associated edges" do
    graph.removeVertexFor(three)
    expect(graph.vertices.size).to eq 0
    add_edge(one,two,'foo')
    add_edge(four,five,'bar')
    graph.removeVertexFor(two)
    expect(graph.vertices.size).to eq 3
    expect(graph.edges.size).to eq 1
  end

  it "gives vertex for given data" do
    add_edge(one,two,'foo')
    expect(graph.findOrCreateVertexFor(two).getData()).to eq two
  end

  it "creates a new vertex if it is not present" do
    expect(graph.findOrCreateVertexFor(hundred).getData()).to eq hundred
  end

  it "finds already existing vertex" do
    expect(graph.findVertexFor(hundred)).to eq nil
    add_edge(one,two,'foo')
    expect(graph.findVertexFor(one).getData()).to eq one
  end

  it "gives correct size of graph" do
    remove_edge(one,two)
    expect(graph.size).to eq 0
    add_edge(five,six,'baz')
    expect(graph.size).to eq 2
    add_edge(foo,bar,'baz')
    expect(graph.size).to eq 4
  end

  it "gives all data in the graph" do
    expect(graph.allData.size).to eq 0
    add_edge(one,two,'baz')
    graph.allData.each do |key|
      expect(graph.findVertexFor(key)).to_not eq nil
    end
    graph.removeVertexFor(one)
    graph.allData.each do |key|
      expect(graph.findVertexFor(key)).to_not eq nil
    end
  end

  it "gives data in the graph in the order in which it was inserted" do
    expect(graph.getInorderData.to_a.size).to eq 0
    graph.findOrCreateVertexFor(one)
    expect(graph.getInorderData.to_a).to eq [one]
    add_edge(foo,bar,'baz')
    expect(graph.getInorderData.to_a).to eq [one,foo,bar]
    graph.removeVertexFor(foo)
    expect(graph.getInorderData.to_a).to eq [one,bar]
  end
end