public
Description: A library of higher-order functions for Lua
Homepage: http://www.samsarin.com/blog/lua-functional
Clone URL: git://github.com/samsarin/lua-functional.git
lua-functional / functional_tests.lua
100644 153 lines (124 sloc) 4.284 kb
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require 'functional'
 
functional.add_to_env(getfenv())
 
function do_assert(condition, message, depth)
if not condition then
error(message, depth)
end
end
 
function assert_equals(expected, actual, depth)
depth = depth or 3
do_assert(expected == actual, "Values do not match. Expected: " .. tostring(expected)
.. " Actual: " .. tostring(actual), depth)
end
 
function assert_array_equals(expected, actual)
assert_equals("table", type(expected), 4)
assert_equals("table", type(actual), 4)
 
local expected_arr = {}
for _, v in ipairs(expected) do table.insert(expected_arr, v) end
 
local actual_arr = {}
for _, v in ipairs(actual) do table.insert(actual_arr, v) end
 
assert_table_equals(expected_arr, actual_arr)
end
 
function assert_table_equals(expected, actual)
assert_equals("table", type(expected), 4)
assert_equals("table", type(actual), 4)
 
for k, v in pairs(expected) do
do_assert(v == actual[k], "Values do not match. Key: " .. k .. " Expected: "
.. tostring(v) .. " Actual: " .. tostring(actual[k]))
end
for k, v in pairs(actual) do
do_assert(v == expected[k], "Values do not match. Key: " .. k .. " Expected: "
.. tostring(v) .. " Actual: " .. tostring(expected[k]))
end
end
 
function test_nipairs()
local a = { 1, 2, nil, 4, a = 25 }
local expected = { 1, 2, nil, 4 }
 
local visited = 0
for k, v in nipairs(a) do
assert_equals(expected[k], v)
visited = visited + 1
end
end
 
function test_map()
mapped = map(function(x) return x + 1 end, { 1, 2, 3, n = 4 })
assert_array_equals({ 2, 3, 4 }, mapped)
assert_equals(5, mapped.n)
end
 
function test_map_empty()
assert_array_equals({}, map(function(x) return x + 1 end, {}))
end
 
function test_reduce()
assert_equals(16, reduce(function(t, v) return t + v end, 10, { 1, 2, 3 }))
end
 
function test_reduce_empty()
assert_equals(5, reduce(function(t, v) return t + v end, 5, {}))
end
 
function test_reduce_with_nipairs()
local a = { 1, nil, 5 }
local addButResetForNil = function(t, v)
return v and t + v or 0
end
assert_equals(5, reduce(addButResetForNil, 0, wrap_iter(nipairs(a))))
end
 
function test_sort_by()
assert_array_equals({ 1, 2, 3}, sort_by(function(x) return x end, { 1, 3, 2 }))
end
 
function test_sort_by_empty()
assert_array_equals({}, sort_by(function(x) return x end, {}))
end
 
function test_partial()
local func = function(x, y, z) return { x, y, z } end
local partial = partial(func, PLACEHOLDER, 'b', PLACEHOLDER)
assert_equals("function", type(partial))
assert_equals("function", type(partial('a')))
assert_array_equals({ 'a', 'b', 'c' }, partial('a')('c'))
assert_array_equals({ 'a', 'b', 'c' }, partial('a', 'c'))
end
 
function test_partial_with_nil()
local func = function(x, y) return { x, y } end
local partial = partial(func, nil, PLACEHOLDER)
assert_equals("function", type(partial))
assert_table_equals({ nil, 'a' }, partial('a'))
end
 
function test_curry()
local func = function(x, y, z) return { x, y, z } end
local curried = curry(func, 3, 'a')
assert_equals("function", type(curried))
assert_equals("function", type(curried('b')))
assert_array_equals({ 'a', 'b', 'c' }, curried('b')('c'))
assert_array_equals({ 'a', 'b', 'c' }, curried('b', 'c'))
end
 
function test_curry_with_zero_arity()
assert_equals(5, curry(function() return 5 end, 0))
end
 
function test_filter_none()
assert_array_equals({}, filter(function() return false end, { 1, 2, 3, 4 }))
end
 
function test_filter_all()
assert_array_equals({ 1, 2, 3, 4 }, filter(function() return true end, { 1, 2, 3, 4 }))
end
 
function test_filter_even()
local even = function(x) return x % 2 == 0 end
assert_array_equals({ 2, 4 }, filter(even, { 1, 2, 3, 4 }))
end
 
function test_copy()
local array = { 'a', 'b', 'c' }
local copy = copy(array)
assert(array ~= copy, "array and it's copy should not be the same object")
assert_array_equals(array, copy)
end
 
function test_wrap_iter_example()
local array = { 'a', 'b', 'c', nil, 'e' }
local sequence = wrap_iter(nipairs(array))
local concatenate = function(t, v) return v and t .. v or t end
local result = reduce(concatenate, "", sequence)
assert_equals("abce", result)
end
 
-- Run the tests
for test_name, test in pairs(getfenv()) do
if string.match(test_name, "test_.*") then
print(test_name)
test()
end
end