forked from EvandroLG/array.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
355 lines (275 loc) · 9.32 KB
/
Copy pathtest.lua
File metadata and controls
355 lines (275 loc) · 9.32 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
local array = require 'array'
local test = require 'simple_test'
test('meta infos', function(a)
a.equal(array.__VERSION, '1.2.6')
a.equal(array.__DESCRIPTION,
"A small library with useful methods to handle Lua's table when it's working like an Array")
end)
test('is_array should return false when object is not a table', function(a)
a.equal(array.is_array('lua'), false)
end)
test('is_array should return false when the table is working like a dictionary', function(a)
a.equal(array.is_array({ language='lua' }), false)
end)
test('is_array should return true when table is empty', function(a)
a.equal(array.is_array({}), true)
end)
test('is_array should return true when table is working like an array', function(a)
a.equal(array.is_array({ 'a', 'b', 'c', 'd' }), true)
end)
test('is_empty should return false when table has at least one item', function(a)
a.equal(array.is_empty({ 'a' }), false)
end)
test('is_empty should return false when table does not have any item', function(a)
a.equal(array.is_empty({}), true)
end)
test('first should return first item from table', function(a)
a.equal(array.first({ 'a', 'b', 'c', 'd' }), 'a')
end)
test('last should return last item from table', function(a)
a.equal(array.last({ 'a', 'b', 'c', 'd' }), 'd')
end)
test('slice should return an empty table when it does not have any element', function(a)
a.equal(#array.slice({}, 1, 2), 0)
end)
test('slice should return a table with values between start index and end index', function(a)
local result = array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2, 4)
a.equal(type(result), 'table')
a.equal(#result, 3)
a.equal(result[1], 'javascript')
a.equal(result[2], 'python')
a.equal(result[3], 'ruby')
end)
test('slice should return a table with every values from start index until last index', function(a)
local result = array.slice({ 'lua', 'javascript', 'python', 'ruby', 'c' }, 2)
a.equal(type(result), 'table')
a.equal(#result, 4)
a.equal(result[1], 'javascript')
a.equal(result[2], 'python')
a.equal(result[3], 'ruby')
a.equal(result[4], 'c')
end)
test('reverse should return an inverted table', function(a)
local result = array.reverse({ 'lua', 'javascript', 'python' })
a.equal(type(result), 'table')
a.equal(#result, 3)
a.equal(result[1], 'python')
a.equal(result[2], 'javascript')
a.equal(result[3], 'lua')
end)
test('map should return a table with 2, 4, 6 values', function(a)
local result = array.map({ 1, 2, 3 }, function(value)
return value * 2
end)
a.equal(type(result), 'table')
a.equal(#result, 3)
a.equal(result[1], 2)
a.equal(result[2], 4)
a.equal(result[3], 6)
end)
test('filter should return a table with 10, 15, 20 values', function(a)
local result = array.filter({ 15, 10, 5, 3, 20 }, function(value)
return value >= 10
end)
a.equal(type(result), 'table')
a.equal(#result, 3)
a.equal(result[1], 15)
a.equal(result[2], 10)
a.equal(result[3], 20)
end)
test('max should return the biggest value from a table', function(a)
a.equal(array.max({ 20, 22, 1, 3, 30, 42 }), 42)
end)
test('max should return nil when table is empty', function(a)
a.equal(array.max({}), nil)
end)
test('min should return the smallest value from a table', function(a)
a.equal(array.min({ 20, 22, 1, 3, 30, 42 }), 1)
end)
test('min should return nil when table is empty', function(a)
a.equal(array.min({}), nil)
end)
test('reduce should return 90', function(a)
local result = array.reduce({ 20, 30, 40 }, function(memo, value)
return memo + value
end)
a.equal(result, 90)
end)
test('reduce should return 100', function(a)
local result = array.reduce({ 20, 30, 40 }, function(memo, value)
return memo + value
end, 10)
a.equal(result, 100)
end)
test('reduce should return first item', function(a)
local result = array.reduce({ 20 }, function(memo, value)
return memo + value
end)
a.equal(result, 20)
end)
test('reduce should concatenate all items', function(a)
local result = array.reduce({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value)
return memo .. value
end)
a.equal(result, 'abcde')
end)
test('reduce_right should concatenate all items starting from right to left', function(a)
local result = array.reduce_right({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value)
return memo .. value
end)
a.equal(result, 'edcba')
end)
test('reduce_right should return 100', function(a)
local result = array.reduce_right({ 20, 30, 40 }, function(memo, value)
return memo + value
end, 10)
a.equal(result, 100)
end)
test('reduce_right', function(a)
local result = array.reduce_right({ 'a', 'b', 'c', 'd', 'e' }, function(memo, value)
return memo .. value
end)
a.equal(result, 'edcba')
end)
test('sum should return sum of the values in table', function(a)
local result = array.sum({10, 20, 30, 40, 50})
a.equal(result, 150)
end)
test('index_of should return correct position of value in the table', function(a)
a.equal(array.index_of({ 20, 30, 40, 50 }, 40), 3)
end)
test('index_of should return -1 when the value is not in the table', function(a)
a.equal(array.index_of({ 20, 30, 40 }, 50), -1)
end)
test('concat should join the two array', function(a)
local result = array.concat({ 1, 2, 3 }, { 4, 5, 6 })
a.equal(#result, 6)
a.equal(result[4], 4)
end)
test('uniq should return every value once', function(a)
local result = array.uniq({ 'a', 'b', 'a', 'b', 'c', 'd' })
a.equal(#result, 4)
a.equal(result[1], 'a')
a.equal(result[2], 'b')
a.equal(result[3], 'c')
a.equal(result[4], 'd')
end)
test('without should return all values less the items from second array', function(a)
local result = array.without({ 10, 20, 30, 10, 4 }, { 10, 4 })
a.equal(#result, 2)
a.equal(result[1], 20)
a.equal(result[2], 30)
end)
test('some should return true when exist at least one match', function(a)
local result = array.some({'a', 'b', 'c'}, function(element)
if (element == 'b') then return true end
end)
a.ok(result)
end)
test('some should return true when exist at least one match', function(a)
local result = array.some({'a', 'b', 'c'}, function(element)
if (element == 'd') then return true end
end)
a.not_ok(result)
end)
test('zip should return a new table with the correct pairs', function(a)
local result = array.zip({ 'a', 'b' }, { 'A', 'B' })
a.equal(result[1][1], 'a')
a.equal(result[1][2], 'A')
a.equal(result[2][1], 'b')
a.equal(result[2][2], 'B')
end)
test('zip should not consider values without pairs', function(a)
local result = array.zip({ 'a', 'b' }, { 'A', 'B', 'C' })
a.equal(#result, 2)
a.equal(#result[1], 2)
a.equal(#result[2], 2)
a.equal(result[1][1], 'a')
a.equal(result[1][2], 'A')
a.equal(result[2][1], 'b')
a.equal(result[2][2], 'B')
end)
test('every should return true when all elements in the table pass the callback test', function(a)
local result = array.every({ 10, 20, 30 }, function(value)
return value >= 10
end)
a.ok(result)
end)
test('every should return false when at least a match fails', function(a)
local result = array.every({ 10, 20, 30 }, function(value)
return value > 10
end)
a.not_ok(result)
end)
test('shallow_copy should return a shallow copy of the array passed as parameter', function(a)
local obj = {
{ 'a' },
{ 'b' }
}
local result = array.shallow_copy(obj)
a.equal(#result, #obj)
a.equal(result[1][1], obj[1][1])
a.equal(result[2][1], obj[2][1])
obj[1][1] = 'c'
a.equal(result[1][1], obj[1][1])
obj[1] = 'c'
a.equal(type(result[1]), 'table')
end)
test('deep_copy should return a deep copy of the array passed as parameter', function(a)
local obj = {
{ 'a' },
{ 'b' }
}
local result = array.deep_copy(obj)
a.equal(#result, #obj)
a.equal(result[1][1], obj[1][1])
a.equal(result[2][1], obj[2][1])
obj[1][1] = 'c'
a.equal(result[1][1], 'a')
obj[1] = 'c'
a.equal(type(result[1]), 'table')
end)
test('diff should return a new table with the items which exist only in first table', function(a)
local result = array.diff(
{ 'a', 'b' },
{ 'a', 'c', 'd' }
)
a.equal(#result, 1)
a.equal(result[1], 'b')
end)
test('flat should return a new table that is an one-dimensional flatting of the table passed by parameter', function(a)
local obj = { 'a', 'b', 'c', { 'd', 'e', 'f', { 'g', 'h' } }, { 'i' }, 'j' }
local result = array.flat(obj)
a.equal(#result, 10)
end)
test('fill should create a table with the size passed by parameter and fill every item using the value passed as argument', function(a)
local value = 1
local size = 3
local result = array.fill(value, size)
a.equal(#result, size)
a.equal(result[1], value)
a.equal(result[2], value)
a.equal(result[3], value)
end)
test('fill should create a table using the value passed by argument from start to end', function(a)
local value = 'a'
local start = 3
local finish = 4
local result = array.fill(value, start, finish)
a.equal(result[1], nil)
a.equal(result[2], nil)
a.equal(result[3], value)
a.equal(result[4], value)
end)
test('remove should delete items that callback returns thruthy and should return a new table with the removed items', function(a)
local list = { 1, 2, 3, 4 }
local result = array.remove(list, function(value)
return math.fmod(value, 2) == 0
end)
a.equal(#list, 2)
a.equal(list[1], 1)
a.equal(list[2], 3)
a.equal(#result, 2)
a.equal(result[1], 2)
a.equal(result[2], 4)
end)