Skip to content

Commit

Permalink
fix: add test case for bfs_search_tree_from (fixes #99)
Browse files Browse the repository at this point in the history
  • Loading branch information
monora committed Jun 17, 2023
1 parent 0ef2ce5 commit 54f92e4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ Rake::TestTask.new do |t|
t.verbose = true
end

# Test bfs_search_tree_from in isolation, to ensure that adjacency is not loaded by other tests.
Rake::TestTask.new do |t|
t.libs << 'test'
t.pattern = 'test/traversal_bfs_require.rb'
t.verbose = true
end

# Git tagging

desc 'Commit all changes as a new version commit. Tag the commit with v<version> tag'
Expand Down
6 changes: 3 additions & 3 deletions lib/rgl/traversal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ def bfs_iterator(v = self.detect { |x| true })
#
# @return [DirectedAdjacencyGraph] which represents a BFS search tree starting at _v_.
def bfs_search_tree_from(v)
unless defined?(DirectedAdjacencyGraph)
require 'rgl/adjyceny'
end
unless defined?(DirectedAdjacencyGraph)
require 'rgl/adjacency'
end
bfs = bfs_iterator(v)
tree = DirectedAdjacencyGraph.new

Expand Down
29 changes: 29 additions & 0 deletions test/traversal_bfs_require.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'test/unit'

# Do not require rgl/adjacency !
require 'rgl/traversal'
require 'rgl/implicit'

include RGL

# Cyclic graph with _n_ vertices. Need a concrete graph, that is not an AdjacencyGraph
def cycle(n)
RGL::ImplicitGraph.new { |g|
g.vertex_iterator { |b| 0.upto(n - 1, &b) }
g.adjacent_iterator { |x, b| b.call((x + 1) % n) }
g.directed = true
}
end

class TestAdjacencyNotRequired < Test::Unit::TestCase

def setup
@dg = cycle(4)
end

def test_bfs_search_tree
# bfs_search_tree_from requires rgl/adjacency if not yet loaded.
assert_equal("(1-2)(2-3)(3-0)", @dg.bfs_search_tree_from(1).edges.sort.join)
end

end

0 comments on commit 54f92e4

Please sign in to comment.