-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizzbuzz_test.lua
89 lines (67 loc) · 2.74 KB
/
fizzbuzz_test.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
#!/usr/bin/env luax
local F = require "F"
local fs = require "fs"
local result_file = arg[1]
local N = tonumber(arg[2])
assert(result_file and N, "Wrong arguments")
local indices, fizzbuzzes = fs.read(result_file)
: lines()
: map(string.words)
: unzip()
indices = indices:map(tonumber)
local tests = F{}
-- fizzbuzz list used to render the test results
tests.fizzbuzz = fizzbuzzes
---------------------------------------------------------------------------
-- The number of line shall be N
---------------------------------------------------------------------------
tests.valid_number_of_lines =
#indices == N and #fizzbuzzes == N
and F.op.ueq(indices, F.range(N))
---------------------------------------------------------------------------
-- Multiples of 3 but not 5 are "fizz"
---------------------------------------------------------------------------
tests.valid_fizz =
fizzbuzzes
: filteri(function(i, _) return i%3 == 0 and i%5 ~= 0 end)
: all(F.partial(F.op.eq, "fizz"))
---------------------------------------------------------------------------
-- Multiples of 5 but not 3 are "buzz"
---------------------------------------------------------------------------
tests.valid_buzz =
fizzbuzzes
: filteri(function(i, _) return i%3 ~= 0 and i%5 == 0 end)
: all(F.partial(F.op.eq, "buzz"))
---------------------------------------------------------------------------
-- Multiples of 3 and 5 are "fizzbuzz"
---------------------------------------------------------------------------
tests.valid_fizzbuzz =
fizzbuzzes
: filteri(function(i, _) return i%3 == 0 and i%5 == 0 end)
: all(F.partial(F.op.eq, "fizzbuzz"))
---------------------------------------------------------------------------
-- Non multiples of 3 and 5 are themselves
---------------------------------------------------------------------------
tests.valid_numbers =
fizzbuzzes
: mapi(function(i, s)
return i%3 == 0 or i%5 == 0 or F.read(s) == i
end)
: land()
---------------------------------------------------------------------------
-- Statistics
---------------------------------------------------------------------------
local results = tests
: filtert(function(res) return type(res) == "boolean" end)
: values()
tests.nb = #results
tests.nb_pass = #results:filter(F.partial(F.op.eq, true))
tests.nb_fail = #results:filter(F.partial(F.op.eq, false))
---------------------------------------------------------------------------
-- Format test results
---------------------------------------------------------------------------
print("--[[ Fizzbuzz output")
print("indices", F.show(indices))
print("fizzbuzzes", F.show(fizzbuzzes))
print("]]")
print("return", F.show(tests, {indent=4}))