Skip to content

Commit 673cffb

Browse files
committed
Added tests
1 parent 52224cf commit 673cffb

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Tests for 10harshadnumber.lua
2+
local is_10_harshad = require 'harshad'
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+
local harshad = {
21+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
22+
12, 18, 20, 21, 24, 27, 30, 36,
23+
40, 42, 45, 48, 50, 54, 60, 63,
24+
70, 72, 80, 81, 84, 90, 100, 102,
25+
108, 110, 111, 112, 114, 117, 120,
26+
126, 132, 133, 135, 140, 144, 150, 152,
27+
153, 156, 162, 171, 180, 190, 192, 195, 198, 200, 201
28+
}
29+
30+
run('Harshad Numbers', function()
31+
for _, n in ipairs(harshad) do assert(is_10_harshad(n)) end
32+
end)
33+
34+
print(('-'):rep(80))
35+
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
36+
:format(total, pass, total-pass, (pass*100/total)))

0 commit comments

Comments
 (0)