-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathheuristics_specs.lua
100 lines (74 loc) · 2.86 KB
/
heuristics_specs.lua
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
context('Module Heuristics', function()
local H, Node
before(function()
H = require ('jumper.core.heuristics')
Node = require ('jumper.core.node')
end)
context('MANHATTAN distance', function()
test('is a function',function()
assert_type(H.MANHATTAN, 'function')
end)
test('evaluates as |dx|+|dy|', function()
assert_equal(H.MANHATTAN(Node(0,0), Node(0,0)), 0)
assert_equal(H.MANHATTAN(Node(1,1), Node(1,3)), 2)
assert_equal(H.MANHATTAN(Node(0,0), Node(2,1)), 3)
end)
test('calling the function with one arg raises an error', function()
assert_error(pcall(H.MANHATTAN,Node(0,0)))
end)
test('calling the function with no args raises an error', function()
assert_error(pcall(H.MANHATTAN))
end)
end)
context('EUCLIDIAN distance', function()
test('is a function',function()
assert_type(H.EUCLIDIAN, 'function')
end)
test('evaluates as SQUAREROOT(dx*dx + dy*dy)', function()
assert_equal(H.EUCLIDIAN(Node(0,0), Node(0,0)), 0)
assert_equal(H.EUCLIDIAN(Node(0,0), Node(2,2)), math.sqrt(8))
assert_equal(H.EUCLIDIAN(Node(0,0), Node(5,3)), math.sqrt(34))
end)
test('calling the function with one arg raises an error', function()
assert_error(pcall(H.EUCLIDIAN,Node(0,0)))
end)
test('calling the function with no args raises an error', function()
assert_error(pcall(H.EUCLIDIAN))
end)
end)
context('DIAGONAL distance', function()
test('is a function',function()
assert_type(H.DIAGONAL, 'function')
end)
test('evaluates as MAX(|dx|+|dy|)', function()
assert_equal(H.DIAGONAL(Node(0,0), Node(0,0)), 0)
assert_equal(H.DIAGONAL(Node(0,0), Node(2,2)), 2)
assert_equal(H.DIAGONAL(Node(0,0), Node(1,2)), 2)
assert_equal(H.DIAGONAL(Node(0,0), Node(3,1)), 3)
end)
test('calling the function with one arg raises an error', function()
assert_error(pcall(H.DIAGONAL,Node(0,0)))
end)
test('calling the function with no args raises an error', function()
assert_error(pcall(H.DIAGONAL))
end)
end)
context('CARDINTCARD distance', function()
test('is a function',function()
assert_type(H.CARDINTCARD, 'function')
end)
test('evaluates as (SQRT(2)-1)*MIN(|dx|+|dy|)+MAX(|dx|+|dy|)', function()
assert_equal(H.CARDINTCARD(Node(0,0), Node(0,0)), 0)
assert_less_than(H.CARDINTCARD(Node(0,0), Node(1,1))-(math.sqrt(2)),1e-6)
assert_less_than(H.CARDINTCARD(Node(0,0), Node(1,2))-(1+math.sqrt(2)),1e-6)
assert_less_than(H.CARDINTCARD(Node(0,0), Node(-3,1))-(2+math.sqrt(2)),1e-6)
assert_less_than(H.CARDINTCARD(Node(0,0), Node(2,2))-(2*math.sqrt(2)),1e-6)
end)
test('calling the function with one arg raises an error', function()
assert_error(pcall(H.CARDINTCARD,Node(0,0)))
end)
test('calling the function with no args raises an error', function()
assert_error(pcall(H.CARDINTCARD))
end)
end)
end)