|
| 1 | +-- Tests for bfs.lua |
| 2 | +local BFS = require 'bfs' |
| 3 | + |
| 4 | +local total, pass = 0, 0 |
| 5 | + |
| 6 | +local function dec(str, len) |
| 7 | + return #str < len |
| 8 | + and str .. (('.'):rep(len-#str)) |
| 9 | + or str:sub(1,len) |
| 10 | +end |
| 11 | + |
| 12 | +local function same(t, p, comp) |
| 13 | + for k,v in ipairs(t) do |
| 14 | + if not comp(v, p[k]) then return false end |
| 15 | + end |
| 16 | + return true |
| 17 | +end |
| 18 | + |
| 19 | +local function run(message, f) |
| 20 | + total = total + 1 |
| 21 | + local ok, err = pcall(f) |
| 22 | + if ok then pass = pass + 1 end |
| 23 | + local status = ok and 'PASSED' or 'FAILED' |
| 24 | + print(('%02d. %68s: %s'):format(total, dec(message,68), status)) |
| 25 | +end |
| 26 | + |
| 27 | +run('Testing BFS search on linear graph', function() |
| 28 | + local comp = function(a, b) return a.value == b end |
| 29 | + local ln_handler = require 'linear_handler' |
| 30 | + local bfs = BFS(ln_handler) |
| 31 | + local start, goal = ln_handler.getNode(0), ln_handler.getNode(5) |
| 32 | + assert(same(bfs:findPath(start, goal), {0,1,2,3,4,5}, comp)) |
| 33 | + |
| 34 | + start, goal = ln_handler.getNode(-2), ln_handler.getNode(2) |
| 35 | + assert(same(bfs:findPath(start, goal), {-2,-1,0,1,2}, comp)) |
| 36 | +end) |
| 37 | + |
| 38 | +run('Testing Greedy BFS search on linear graph', function() |
| 39 | + local comp = function(a, b) return a.value == b end |
| 40 | + local ln_handler = require 'linear_handler' |
| 41 | + local bfs = BFS(ln_handler) |
| 42 | + local start, goal = ln_handler.getNode(0), ln_handler.getNode(5) |
| 43 | + assert(same(bfs:findPath(start, goal, true), {0,1,2,3,4,5}, comp)) |
| 44 | + |
| 45 | + start, goal = ln_handler.getNode(-2), ln_handler.getNode(2) |
| 46 | + assert(same(bfs:findPath(start, goal, true), {-2,-1,0,1,2}, comp)) |
| 47 | +end) |
| 48 | + |
| 49 | +run('Testing BFS search on grid graph', function() |
| 50 | + local comp = function(a, b) return a.x == b[1] and a.y == b[2] end |
| 51 | + local gm_handler = require 'gridmap_handler' |
| 52 | + local bfs = BFS(gm_handler) |
| 53 | + gm_handler.map = {{0,0,0,0,0},{0,1,1,1,1},{0,0,0,0,0}} |
| 54 | + |
| 55 | + gm_handler.diagonal = false |
| 56 | + local start, goal = gm_handler.getNode(1,1), gm_handler.getNode(5,3) |
| 57 | + assert(same(bfs:findPath(start, goal), {{1,1},{1,2},{1,3},{2,3},{3,3},{4,3},{5,3}}, comp)) |
| 58 | + |
| 59 | + gm_handler.diagonal = true |
| 60 | + assert(same(bfs:findPath(start, goal), {{1,1},{1,2},{2,3},{3,3},{4,3},{5,3}}, comp)) |
| 61 | +end) |
| 62 | + |
| 63 | +run('Testing Greedy BFS search on grid graph', function() |
| 64 | + local comp = function(a, b) return a.x == b[1] and a.y == b[2] end |
| 65 | + local gm_handler = require 'gridmap_handler' |
| 66 | + local bfs = BFS(gm_handler) |
| 67 | + gm_handler.map = {{0,0,0,0,0},{0,1,1,1,1},{0,0,0,0,0}} |
| 68 | + |
| 69 | + gm_handler.diagonal = false |
| 70 | + local start, goal = gm_handler.getNode(1,1), gm_handler.getNode(5,3) |
| 71 | + assert(same(bfs:findPath(start, goal,true), {{1,1},{1,2},{1,3},{2,3},{3,3},{4,3},{5,3}}, comp)) |
| 72 | + |
| 73 | + gm_handler.diagonal = true |
| 74 | + assert(same(bfs:findPath(start, goal,true), {{1,1},{1,2},{2,3},{3,3},{4,3},{5,3}}, comp)) |
| 75 | +end) |
| 76 | + |
| 77 | +run('Testing BFS search on point graph', function() |
| 78 | + local comp = function(a, b) return a.x == b[1] and a.y == b[2] end |
| 79 | + local pg_handler = require 'point_graph_handler' |
| 80 | + local bfs = BFS(pg_handler) |
| 81 | + |
| 82 | + pg_handler.addNode('a') |
| 83 | + pg_handler.addNode('b') |
| 84 | + pg_handler.addNode('c') |
| 85 | + pg_handler.addNode('d') |
| 86 | + pg_handler.addNode('e') |
| 87 | + pg_handler.addEdge('a', 'b', 10) |
| 88 | + pg_handler.addEdge('b', 'e', 10) |
| 89 | + pg_handler.addEdge('a', 'c', 5) |
| 90 | + pg_handler.addEdge('c', 'd', 5) |
| 91 | + pg_handler.addEdge('d', 'e', 5) |
| 92 | + |
| 93 | + local comp = function(a, b) return a.name == b end |
| 94 | + local start, goal = pg_handler.getNode('a'), pg_handler.getNode('e') |
| 95 | + assert(same(bfs:findPath(start, goal), {'a','c','d','e'}, comp)) |
| 96 | + |
| 97 | + pg_handler.setEdgeWeight('a', 'b', 1) |
| 98 | + pg_handler.setEdgeWeight('b', 'e', 1) |
| 99 | + |
| 100 | + assert(same(bfs:findPath(start, goal), {'a','b','e'}, comp)) |
| 101 | +end) |
| 102 | + |
| 103 | +run('Testing Greedy BFS search on point graph', function() |
| 104 | + local comp = function(a, b) return a.x == b[1] and a.y == b[2] end |
| 105 | + local pg_handler = require 'point_graph_handler' |
| 106 | + local bfs = BFS(pg_handler) |
| 107 | + |
| 108 | + pg_handler.addNode('a') |
| 109 | + pg_handler.addNode('b') |
| 110 | + pg_handler.addNode('c') |
| 111 | + pg_handler.addNode('d') |
| 112 | + pg_handler.addNode('e') |
| 113 | + pg_handler.addEdge('a', 'b', 10) |
| 114 | + pg_handler.addEdge('b', 'e', 10) |
| 115 | + pg_handler.addEdge('a', 'c', 5) |
| 116 | + pg_handler.addEdge('c', 'd', 5) |
| 117 | + pg_handler.addEdge('d', 'e', 5) |
| 118 | + |
| 119 | + local comp = function(a, b) return a.name == b end |
| 120 | + local start, goal = pg_handler.getNode('a'), pg_handler.getNode('e') |
| 121 | + assert(same(bfs:findPath(start, goal,true), {'a','c','d','e'}, comp)) |
| 122 | + |
| 123 | + pg_handler.setEdgeWeight('a', 'b', 1) |
| 124 | + pg_handler.setEdgeWeight('b', 'e', 1) |
| 125 | + |
| 126 | + assert(same(bfs:findPath(start, goal,true), {'a','b','e'}, comp)) |
| 127 | +end) |
| 128 | + |
| 129 | +print(('-'):rep(80)) |
| 130 | +print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%') |
| 131 | + :format(total, pass, total-pass, (pass*100/total))) |
0 commit comments