-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.coffee
331 lines (294 loc) · 6.54 KB
/
test.coffee
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
Test = require './testFramework'
_ = require './common'
{
identity,
None,
isFunction,
delay,
deadline,
retry,
allAwait,
raceAwait,
sequence,
isGenerator,
tco,
BinaryHeap,
genWrap,
genLog,
logInfo,
logError,
newMessageArray,
newPool
} = _
logInfo = genLog 2
Test('lazy Monad') ({assert, assertSeq}) =>
status = 1
seq = 3
res = []
f = _.M (x) => x + 1
.then (x) =>
assertSeq ++seq
x
.then (x) => assert x is 2
.then => assert status is 2
.then =>
throw new Error 'wrong'
.then => assert false
.catch (e) => assert e.message is 'wrong'
.then => assertSeq ++seq
res.push assertSeq 1
res.push f 1
res.push assertSeq 2
status = 2
r = f 1
.then => assertSeq Infinity
res.push r
res.push assertSeq 3
Promise.all res
Test('Monad handler type check') (report) =>
try
f = _.M (x) => x + 1
.then null, logError
.then do Promise.resolve
.then => throw new Error 'wrong'
f 1
catch e
report.assert e instanceof TypeError
Test('Immutable Monad') (report) =>
f = _.M identity
g = f.then => 1
h = f.then => 2
Promise.all [f, g, h].map (fn, i) =>
fn i
.then (x) => report.assert x is i
Test('identity') (report) =>
x = {}
Promise.all [
report.assert x is identity.apply @, [x, 1]
report.assert undefined is do identity
]
Test('None') (report) =>
x = {}
Promise.all [
report.assert undefined is None.apply @, [x, {}]
report.assert undefined is do None
]
Test('wait with delay and deadline') (report) =>
start = Date.now()
timing = (time) => do delay time
interval = (k) => (time) =>
time = time.message if time.message?
report.assert Date.now() - start >= time * k
time
f = _.M timing
.then interval 1
.then timing
.then interval 2
.then (time) => do deadline time
.catch interval 3
f 100
Test('death race') ({assert}) =>
life = 1000
f = (time) =>
a = allAwait [identity, delay(time), delay time * 2]
g = _.M a
.then None
.then a
.then None
.then a
.then =>
assert Date.now() - start >= time * 6
h = raceAwait [g, deadline life]
logInfo "race began with time interval #{time}ms"
start = Date.now()
h().then =>
assert Date.now() - start < life
logInfo "after #{Date.now() - start}ms,
should resolve when time interval < #{life} / 6"
.catch (e) =>
e = +e.message
assert e is life
assert Date.now() - start >= e
logInfo "after #{Date.now() - start}ms,
should reject when time interval > #{e} / 6"
Promise.all [
f 100
f 200
]
Test('retry') ({assert}) =>
f = do (times = 3) => (count = 0) => =>
count += 1
if count < times
throw new Error "#{count} < #{times}"
return count
g = retry(f()) 3
Promise.all [
retry(f())(2)()
.then => assert false
.catch (e) => assert e.message is '2 < 3'
g()
.then (r) => assert r is 3
.catch => assert false
delay(1000)()
.then g
.then (r) => assert r is 4
.catch => assert false
]
Test('sequence') ({assert, assertSeq}) =>
number = ->
n = 1
k = 1
while true
k = yield n + k
return
f = (x) ->
if x < 5
assertSeq x
x
else undefined
g = (x) ->
delay(1000)()
.then => f x
s = sequence(g) number()
Promise.all [
assertSeq 0
a = await s
assert a.every (x, i) => x is i + 2
]
Test('isGenerator') (report) =>
number = ->
n = 1
while true
yield n++
return
Promise.all [
report.assert not isGenerator number
report.assert isGenerator number()
]
Test('tail call optimization') ({assert}) =>
countFn = (n, res = 0) ->
if n <= 1
yield res + n
else
yield countFn(n - 1, res + 1)
countR = (n) =>
if n <= 1 then n
else 1 + countR n - 1
try
tco identity
await assert false
catch e
await assert e.name is 'TypeError'
count = tco countFn
await assert count 1e7
try
countR 1e7
await assert false
catch e
await assert e.name is 'RangeError'
Test('Priority Queue') ({assert}) =>
queue = [2, 1, 4].reduce ((queue, x) => queue.push x)
, new BinaryHeap simple: true
assert isFunction queue.push
assert isFunction queue.peek
assert isFunction queue.pop
queue.push 3
assert queue.peek() is 1
assert [1, 2, 3, 4].reduce ((flag, x) => flag and x is queue.pop())
, true
N = 1e6
data = ({v: Math.random()} for i in [1..N])
compare = (a, b) => a.v - b.v
queue = new BinaryHeap()
console.time "Heapsort #{N}"
data.forEach (x) => queue.push x.v, x
res1 = (queue.pop() for i in [1..N])
console.timeEnd "Heapsort #{N}"
console.time "Array sort #{N}"
resA = data.sort compare
console.timeEnd "Array sort #{N}"
assert resA.every (item, i) => item is res1[i][1]
Test('Wrapper Generator') ({assert}) =>
f = (x) ->
@x = x.length
return
w = genWrap f
r = w 'aaa'
rr = w r
assert r.x is 3
assert r is rr
try
genWrap 'wrong'
assert false
catch e
assert e instanceof TypeError
Test('Message Array') ({assert}) =>
items = []
[newItem, popItem, peek] = newMessageArray 2, items
newItem() for i in [1..4]
id = items[2].id
t = peek id
assert items[2] is t
item = popItem id
assert item.id is id
assert t.id is id
assert items[2] is undefined
item = popItem id
assert item is undefined
item = newItem()
assert (item.id & 3) is 2
assert item.id > 2
try
newItem()
assert false
catch e
assert e.message is 'Full'
Test('Pool') ({assert, idle}) =>
pool = [1, 2, 3]
s = new Set()
[ac, release] = newPool pool
acquire = do (g = do (o = ac()) => o.next.bind o) => => (await g()).value
c = 0
timeout = 20
d = delay(timeout)
times = 99
f = =>
if c > times
return
c += 1
v = await acquire()
assert not s.has v
s.add v
d()
.then ->
assert s.has v
s.delete v
release v
f()
await idle()
start = Date.now()
Promise.all [f(), f(), f()]
.then => Date.now() - start
.then (elapse) =>
assert elapse * 3 >= timeout * times > elapse * 2
Test('Pool with timeout') ({assert}) =>
pool = [1, 2, 3]
s = new Set pool
[ac, release] = newPool pool, 10, true
acquire = do (g = do (o = ac()) => o.next.bind o) => => (await g()).value
c = 0
f = =>
res = await acquire()
if res instanceof Error
c = 1
assert pool.length is 0
assert +res.message is 10
return
await delay(20)()
assert s.has res
s.delete res
release res
Promise.all (f() for _ in [1..4])
.then =>
assert s.size is 0
assert c is 1