Skip to content

Commit

Permalink
Fixed specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonaba committed Mar 17, 2013
1 parent b4c0d2c commit 9c558f5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 42 deletions.
38 changes: 19 additions & 19 deletions specs/grid_specs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ context('Module Grid', function()
local map = {{0,0},{0,0}}
local grid = Grid(map)
local grid2 = Grid(map)
assert_equal(grid.width, 2)
assert_equal(grid2.width, 2)
assert_equal(grid:getWidth(), 2)
assert_equal(grid2:getWidth(), 2)
end)

test('Grid and map should have the same height', function()
local map = '00\n00\n00'
local map2 = {{0,0},{0,0},{0,0},{0,0}}
local grid = Grid(map)
local grid2 = Grid(map2)
assert_equal(grid.height, 3)
assert_equal(grid2.height, 4)
assert_equal(grid:getHeight(), 3)
assert_equal(grid2:getHeight(), 4)
end)

test('Each value on the map matches a node on the grid', function()
Expand Down Expand Up @@ -100,7 +100,7 @@ context('Module Grid', function()
assert_equal(getmetatable(node), Node)
count = count+1
end
assert_equal(count, pgrid.width*pgrid.height)
assert_equal(count, pgrid:getWidth()*pgrid:getHeight())
end)

end)
Expand Down Expand Up @@ -181,8 +181,8 @@ context('Module Grid', function()
test('returns the node at a given position', function()
local node = grid:getNodeAt(1,1)
assert_equal(getmetatable(node),Node)
assert_equal(node.x,1)
assert_equal(node.y,1)
assert_equal(node._x,1)
assert_equal(node._y,1)
end)

test('returns nil if the node does not exist', function()
Expand Down Expand Up @@ -259,7 +259,7 @@ context('Module Grid', function()
local record = {}
for n in grid:iter() do
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n.y] and map[n.y][n.x])
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_nil(record[n])
record[n] = true
end
Expand All @@ -276,11 +276,11 @@ context('Module Grid', function()
local record = {}
for n in grid:iter(2,2,3,3) do
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n.y] and map[n.y][n.x])
assert_gte(n.x, 2)
assert_gte(n.y, 2)
assert_lte(n.x, 3)
assert_lte(n.y, 3)
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_gte(n._x, 2)
assert_gte(n._y, 2)
assert_lte(n._x, 3)
assert_lte(n._y, 3)
assert_nil(record[n])
record[n] = true
end
Expand All @@ -303,7 +303,7 @@ context('Module Grid', function()
grid:each(f, 3)
for n in grid:iter() do
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n.y] and map[n.y][n.x])
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_equal(n.value,3)
assert_nil(record[n])
record[n] = true
Expand All @@ -326,13 +326,13 @@ context('Module Grid', function()
local function f(node,i) node.value = i end
grid:eachRange(1,1,2,2,f,3)
for n in grid:iter() do
if n.x <= 2 and n.y <= 2 then
if n._x <= 2 and n._y <= 2 then
assert_equal(n.value,3)
else
assert_nil(n.value)
end
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n.y] and map[n.y][n.x])
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_nil(record[n])
record[n] = true
end
Expand All @@ -358,7 +358,7 @@ context('Module Grid', function()
grid:imap(f, 5)
for n in grid:iter() do
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n.y] and map[n.y][n.x])
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_equal(n.v,5)
assert_nil(record[n])
record[n] = true
Expand All @@ -384,13 +384,13 @@ context('Module Grid', function()
end
grid:imapRange(3,3,4,4,f,7)
for n in grid:iter() do
if n.x >= 3 and n.y >= 3 then
if n._x >= 3 and n._y >= 3 then
assert_equal(n.v,7)
else
assert_nil(n.v)
end
assert_equal(getmetatable(n), Node)
assert_not_nil(map[n.y] and map[n.y][n.x])
assert_not_nil(map[n._y] and map[n._y][n._x])
assert_nil(record[n])
record[n] = true
end
Expand Down
18 changes: 14 additions & 4 deletions specs/node_specs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,26 @@ context('Module Node', function()

test('A Node has x and y attributes', function()
local node = Node:new(1,3)
assert_equal(node.x, 1)
assert_equal(node.y, 3)
assert_equal(node._x, 1)
assert_equal(node._y, 3)
end)

test('x and y attributes can be retrieved through methods', function()
local node = Node:new(5,6)
assert_equal(node:getX(), 5)
assert_equal(node:getY(), 6)

local x, y = node:getPos()
assert_equal(x, 5)
assert_equal(y, 6)
end)

test('Nodes can be compared, if they both have an F-cost', function()
local nodeA, nodeB = Node(1,2), Node(1,2)
nodeA.f, nodeB.f = 1, 2
nodeA._f, nodeB._f = 1, 2
assert_less_than(nodeA, nodeB)

nodeA.f = 3
nodeA._f = 3
assert_less_than(nodeB, nodeA)
end)

Expand Down
38 changes: 19 additions & 19 deletions specs/path_specs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ context('Module Path', function()

test('Path:iter() iterates on nodes forming the path',function()
local p = Path()
for i = 1,10 do p[#p+1] = Node(i,i) end
for i = 1,10 do p._nodes[#p._nodes+1] = Node(i,i) end

local i = 0
for node, count in p:iter() do
i = i+1
assert_equal(getmetatable(node), Node)
assert_equal(node.x, i)
assert_equal(node.y, i)
assert_equal(node._x, i)
assert_equal(node._y, i)
assert_equal(count, i)
end
end)
Expand All @@ -33,62 +33,62 @@ context('Module Path', function()

test('Path:getLength() returns the length of the path', function()
local p = Path()
for i = 1,10 do p[#p+1] = Node(i,0) end
for i = 1,10 do p._nodes[#p._nodes+1] = Node(i,0) end
assert_equal(p:getLength(),9)

p = Path()
for j = 1,10 do p[#p+1] = Node(0,j) end
for j = 1,10 do p._nodes[#p._nodes+1] = Node(0,j) end
assert_equal(p:getLength(),9)

p = Path()
for i = 1,10 do p[#p+1] = Node(i,i) end
for i = 1,10 do p._nodes[#p._nodes+1] = Node(i,i) end
assert_less_than(p:getLength()-9*math.sqrt(2),1e-6)
end)

test('Path:fill() interpolates a path', function()
local p = Path()
for i = 1,9,2 do p[#p+1] = Node(i,i) end
p.grid = {getNodeAt = function(self,x,y) return {x = x, y = y} end}
for i = 1,9,2 do p._nodes[#p._nodes+1] = Node(i,i) end
p._grid = {getNodeAt = function(self,x,y) return {_x = x, _y = y} end}
p:fill()

local i = 0
for node, count in p:iter() do
i = i+1
assert_equal(node.x, i)
assert_equal(node.y, i)
assert_equal(node._x, i)
assert_equal(node._y, i)
assert_equal(count, i)
end

end)

test('Interpolation does not affect the total path length', function()
local p = Path()
for i = 1,10,3 do p[#p+1] = Node(i,i) end
for i = 1,10,3 do p._nodes[#p._nodes+1] = Node(i,i) end
local len = p:getLength()
p.grid = {getNodeAt = function(self,x,y) return {x = x, y = y} end}
p._grid = {getNodeAt = function(self,x,y) return {_x = x, _y = y} end}
p:fill()

assert_less_than(p:getLength()-len,1e-6)
end)

test('Path:filter() compresses a path', function()
local p = Path()
for i = 1,10 do p[#p+1] = Node(i,i) end
for i = 1,10 do p._nodes[#p._nodes+1] = Node(i,i) end
p:filter()

assert_equal(p[1].x,1)
assert_equal(p[1].y,1)
assert_equal(p[2].x,10)
assert_equal(p[2].y,10)
assert_equal(p._nodes[1]._x,1)
assert_equal(p._nodes[1]._y,1)
assert_equal(p._nodes[2]._x,10)
assert_equal(p._nodes[2]._y,10)
for i = 3,10 do
assert_nil(p[i])
assert_nil(p._nodes[i])
end

end)

test('Compression does not affect the total path length', function()
local p = Path()
for i = 1,10 do p[#p+1] = Node(i,i) end
for i = 1,10 do p._nodes[#p._nodes+1] = Node(i,i) end
local len = p:getLength()
p:fill()

Expand Down

0 comments on commit 9c558f5

Please sign in to comment.