Skip to content

Commit 4d0e263

Browse files
committed
Added tests for Manhattan
Added tests for Chebyshev Added tests for Minkowski Added tests for Euclidean
1 parent 12c08b0 commit 4d0e263

4 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- Tests for chebyshev_distance.lua
2+
local cdist = require 'chebyshev_distance'
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+
13+
local function run(message, f)
14+
total = total + 1
15+
local ok, err = pcall(f)
16+
if ok then pass = pass + 1 end
17+
local status = ok and 'PASSED' or 'FAILED'
18+
print(('%02d. %68s: %s'):format(total, dec(message,68), status))
19+
end
20+
21+
-- Fuzzy equality test
22+
local function fuzzyEqual(a, b)
23+
return math.abs(a-b) < 1e-8
24+
end
25+
26+
run('Chebyshev distance test', function()
27+
assert(cdist({},{}), 0)
28+
assert(cdist({1,2,3},{4,5,6}), 3)
29+
assert(cdist({1,2,-1},{4,4,6}), 7)
30+
assert(not pcall(cdist,{1,2,3},{4,5}))
31+
end)
32+
33+
print(('-'):rep(80))
34+
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
35+
:format(total, pass, total-pass, (pass*100/total)))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
-- Tests for euclidean_distance.lua
2+
local edist = require 'euclidean_distance'
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+
13+
local function run(message, f)
14+
total = total + 1
15+
local ok, err = pcall(f)
16+
if ok then pass = pass + 1 end
17+
local status = ok and 'PASSED' or 'FAILED'
18+
print(('%02d. %68s: %s'):format(total, dec(message,68), status))
19+
end
20+
21+
-- Fuzzy equality test
22+
local function fuzzyEqual(a, b)
23+
return math.abs(a-b) < 1e-8
24+
end
25+
26+
run('Euclidian distance test', function()
27+
assert(fuzzyEqual(edist({},{}), 0))
28+
assert(fuzzyEqual(edist({1,2,3},{4,5,6}), math.sqrt(3*3^2)))
29+
assert(not pcall(edist,{1,2,3},{4,5}))
30+
end)
31+
32+
print(('-'):rep(80))
33+
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
34+
:format(total, pass, total-pass, (pass*100/total)))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-- Tests for manhattan_distance.lua
2+
local mdist = require 'manhattan_distance'
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 run(message, f)
13+
total = total + 1
14+
local ok, err = pcall(f)
15+
if ok then pass = pass + 1 end
16+
local status = ok and 'PASSED' or 'FAILED'
17+
print(('%02d. %68s: %s'):format(total, dec(message,68), status))
18+
end
19+
20+
-- Fuzzy equality test
21+
local function fuzzyEqual(a, b)
22+
return math.abs(a-b) < 1e-8
23+
end
24+
25+
run('Manhattan distance test', function()
26+
assert(mdist({},{}), 0)
27+
assert(mdist({1,2,3},{4,5,6}), 9)
28+
assert(not pcall(mdist,{1,2,3},{4,5}))
29+
end)
30+
31+
print(('-'):rep(80))
32+
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
33+
:format(total, pass, total-pass, (pass*100/total)))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
-- Tests for minkowski_distance.lua
2+
local mdist = require 'minkowski_distance'
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 run(message, f)
13+
total = total + 1
14+
local ok, err = pcall(f)
15+
if ok then pass = pass + 1 end
16+
local status = ok and 'PASSED' or 'FAILED'
17+
print(('%02d. %68s: %s'):format(total, dec(message,68), status))
18+
end
19+
20+
-- Fuzzy equality test
21+
local function fuzzyEqual(a, b)
22+
return math.abs(a-b) < 1e-8
23+
end
24+
25+
run('Minkowski distance test', function()
26+
assert(mdist({},{}, 1), 0)
27+
assert(mdist({1,2,3},{4,5,6}, 1), 9)
28+
assert(fuzzyEqual(mdist({1,2,3},{4,5,6},2), 27^(1/2)))
29+
assert(not pcall(mdist,{1,2,3},{4,5}))
30+
end)
31+
32+
print(('-'):rep(80))
33+
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
34+
:format(total, pass, total-pass, (pass*100/total)))

0 commit comments

Comments
 (0)