-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtests.lua
487 lines (427 loc) · 10.6 KB
/
tests.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
-- Test cases for Lua Stream API
require "stream"
function check(cond, format, ...)
local message = string.format("Test Failed! "..format, ...)
assert(cond, message)
end
function assert_equals(aact, aexp)
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing toarray")
local aexp = {1,2,3,4,5}
local aact = stream({1,2,3,4,5}).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing iter")
local exp = 0
for act in stream({1,2,3,4,5}).iter() do
exp = exp + 1
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing foreach")
local aexp = {1,2,3,4,5}
local aact = {}
local function consume(x)
aact[#aact+1] = x
end
stream({1,2,3,4,5}).foreach(consume)
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,5 do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing filter")
local function isEven(x)
return x % 2 == 0
end
local aexp = {2,4,6,8}
local aact = stream({1,2,3,4,5,6,7,8,9}).filter(isEven).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing reverse")
local aexp = {5,4,3,2,1}
local aact = stream({1,2,3,4,5}).reverse().toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing sort")
local aexp = {1,2,3,4,5,6,7,8,9}
local aact = stream({5,7,6,3,4,1,2,8,9}).sort().toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing map")
local function square(x)
return x*x
end
local aexp = {1,4,9,16,25}
local aact = stream({1,2,3,4,5}).map(square).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing next")
local aexp = {1,2,3}
local aact = {}
local s = stream({1,2,3})
local e = nil
for i=1,#aexp do
aact[#aact+1] = s.next()
end
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing last")
local act = stream({2,2,2,2,1}).last()
local exp = 1
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing count")
local act = stream({1,2,3,4,5,6,7,8,9}).count()
local exp = 9
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing max")
local act = stream({5,7,6,3,4,1,2,8,9}).max()
local exp = 9
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing min")
local act = stream({5,7,6,3,4,1,2,8,9}).min()
local exp = 1
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing sum")
local act = stream({1,2,3,4,5}).sum()
local exp = 15
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing avg")
local act = stream({1,2,3,4,5,6,7,8,9}).avg()
local exp = 5
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing collect")
local aexp = {1,2,3,4,5}
local aact = {}
local function read(iter)
for x in iter do
aact[#aact+1] = x
end
end
stream({1,2,3,4,5}).collect(read)
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,5 do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing limit")
local aexp = {1,2,3}
local aact = stream({1,2,3,4,5}).limit(3).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing skip")
local aexp = {4,5}
local aact = stream({1,2,3,4,5}).skip(3).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing reverse")
local aexp = {5,4,3,2,1}
local aact = stream({1,2,3,4,5}).reverse().toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing distinct")
local aexp = {1,2,4,5,3}
local aact = stream({1,2,4,2,4,2,5,3,5,1}).distinct().toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing peek")
local aexp = {1,2,3,4,5}
local aact = {}
local function consume(x)
aact[#aact+1] = x
end
stream({1,2,3,4,5}).peek(consume).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,5 do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing allmatch true")
local function is_odd(x)
return x%2==0
end
local act = stream({2,4,6,8,10}).allmatch(is_odd)
local exp = true
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing allmatch false")
local function is_odd(x)
return x%2==0
end
local act = stream({2,4,6,8,11}).allmatch(is_odd)
local exp = false
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing anymatch true")
local function is_odd(x)
return x%2==0
end
local act = stream({1,2,3}).anymatch(is_odd)
local exp = true
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing anymatch false")
local function is_odd(x)
return x%2==0
end
local act = stream({1,3,5,7}).anymatch(is_odd)
local exp = false
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing nonematch true")
local function is_odd(x)
return x%2==0
end
local act = stream({1,3,5,7}).nonematch(is_odd)
local exp = true
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing nonematch false")
local function is_odd(x)
return x%2==0
end
local act = stream({1,2,3}).nonematch(is_odd)
local exp = false
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing flatmap")
local function duplicate(x)
return {x,x}
end
local aexp = {1,1,2,2,3,3,4,4,5,5}
local aact = stream({1,2,3,4,5}).flatmap(duplicate).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing flatten")
local aexp = {1,2,3,4,5,6,7,8,9}
local aact = stream({{1,2},{3,4,5,6},{7},{},{8,9}}).flatten().toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing concat 1")
local aexp = {1,2,3,4,5,6,7,8,9}
local aact = stream({1,2,3,4}).concat(stream({5,6,7,8,9})).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing concat 2")
local aexp = {1,2,3,4,5,6,7,8,9}
local aact = stream({1,2,3,4}).concat(stream({5,6}),stream({7,8,9})).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing merge 1")
local aexp = {1,5,2,6,3,7,4,8,9}
local aact = stream({1,2,3,4}).merge(stream({5,6,7,8,9})).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing merge 2")
local aexp = {1,5,7,2,6,8,3,9,4}
local aact = stream({1,2,3,4}).merge(stream({5,6}),stream({7,8,9})).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
print("Testing group")
local function is_odd(x)
return x%2==0
end
local aexp1 = {2,4}
local aexp2 = {1,3}
local mact = stream({1,2,3,4}).group(is_odd)
do
local aact = mact[true]
local aexp = aexp1
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
local aact = mact[false]
local aexp = aexp2
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
end
do
print("Testing split")
local function is_odd(x)
return x%2==0
end
local aexp1 = {2,4}
local aexp2 = {1,3}
local s1,s2 = stream({1,2,3,4}).split(is_odd)
do
local aexp = aexp1
local aact = s1.toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
do
local aexp = aexp2
local aact = s2.toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
check(act == exp, "act=%s, exp=%s", act, exp)
end
end
end
do
print("Testing reduce")
local function add(a,b)
return a+b
end
local act = stream({1,2,3,4,5}).reduce(0,add)
local exp = 15
check(act == exp, "act=%s, exp=%s", act, exp)
end
do
print("Testing pack")
local aexp = {{1,2},{3,4},{5,6},{7,8},{9}}
local aact = stream({1,2,3,4,5,6,7,8,9}).pack(2).toarray()
check(#aact == #aexp, "#aact=%s, #aexp=%s", #aact, #aexp)
for i=1,#aact do
local exp = aexp[i]
local act = aact[i]
assert_equals(act,exp)
end
end