-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance debugging functions of Graph.lua
- Loading branch information
1 parent
2d263c0
commit 01a2b43
Showing
4 changed files
with
146 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,64 @@ | ||
local Graph = require "algo.Graph" | ||
|
||
local G = Graph:new() | ||
local function load_input(input) | ||
local G = Graph:new() | ||
for _, edge in ipairs(input) do | ||
local from, to, cost = edge:match('(%w-)%-(%w+)=(%d+)') | ||
G:add(from, to, cost) | ||
end | ||
return G | ||
end | ||
|
||
G:dump('x') | ||
local function basic_tests() | ||
print("basic_tests...") | ||
local G = Graph:new() | ||
|
||
G:add('s', 'v', 1) | ||
G:add('s', 'w', 4) | ||
G:add('v', 'w', 2) | ||
print(G:outgoing_str('x')) | ||
|
||
G:dump() | ||
G:add('s', 'v', 1) | ||
G:add('s', 'w', 4) | ||
G:add('v', 'w', 2) | ||
|
||
local edges = G:edges('s') | ||
for v, weight in edges:outgoings() do | ||
print('s', v, weight) | ||
end | ||
print(G:outgoing_str()) | ||
|
||
local s = G:vertex('s') | ||
for v, weight in s:outgoings() do | ||
print('s', v, weight) | ||
end | ||
|
||
print(G:outgoing_str('s')) | ||
|
||
G:dump('s') | ||
local x = {} | ||
setmetatable(x, x) | ||
assert(not Graph.isGraph(x)) -- self reference metatable | ||
|
||
local x = {} | ||
setmetatable(x, x) | ||
assert(not Graph.isGraph(x)) -- self reference metatable | ||
assert(Graph.isGraph(G)) -- the metatable of G is Graph | ||
assert(not Graph.isGraph {}) -- no metatable | ||
|
||
assert(Graph.isGraph(G)) -- the metatable of G is Graph | ||
assert(not Graph.isGraph{}) -- no metatable | ||
local child = G:new() -- the metatable of the metatable of child is Graph | ||
assert(Graph.isGraph(child)) | ||
|
||
local child = G:new() -- the metatable of the metatable of child is Graph | ||
assert(Graph.isGraph(child)) | ||
assert(not Graph.isGraph(1)) | ||
assert(not Graph.isGraph("")) | ||
|
||
assert(not Graph.isGraph(1)) | ||
assert(not Graph.isGraph("")) | ||
assert(not Graph.isGraph()) | ||
end | ||
|
||
local function test_build_ingress() | ||
print("test_build_ingress ... ") | ||
local input<const> = {'A-B=2', 'A-C=4', 'A-D=4', 'B-F=6', 'C-F=5', 'C-E=7', 'C-D=1', 'D-E=5', 'D-H=11', 'E-G=3', | ||
'E-H=4', 'F-H=5', 'F-G=2', 'F-E=1', 'G-H=2'} | ||
local source<const> = 'A' | ||
local G = load_input(input) | ||
G:build_ingress() | ||
local incoming, count_i = G:incoming_str() | ||
-- print(incoming) | ||
local outgoing, count_e = G:outgoing_str() | ||
-- print(outgoing) | ||
assert(count_e == count_i) | ||
end | ||
|
||
assert(not Graph.isGraph()) | ||
basic_tests() | ||
test_build_ingress() | ||
|
||
os.exit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters