tests: test-agenda: Remove broken shebang.
[8sync.git] / tests / test-agenda.scm
1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
4 ;;; This library is free software; you can redistribute it and/or
5 ;;; modify it under the terms of the GNU Lesser General Public
6 ;;; License as published by the Free Software Foundation; either
7 ;;; version 3 of the License, or (at your option) any later version.
8 ;;;
9 ;;; This library is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ;;; Lesser General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU Lesser General Public
15 ;;; License along with this library; if not, write to the Free Software
16 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 ;;; 02110-1301 USA
18
19 (define-module (tests test-agenda)
20   #:use-module (srfi srfi-64)
21   #:use-module (ice-9 q)
22   #:use-module (ice-9 match)
23   #:use-module (ice-9 receive)
24   #:use-module (8sync agenda)
25   #:use-module (tests utils))
26
27 (test-begin "test-agenda")
28
29 \f
30
31 ;;; Helpers
32 ;;; =======
33
34 (define (speak-it)
35   (let ((messages '()))
36     (lambda* (#:optional message)
37       (if message (set! messages (append messages (list message))))
38       messages)))
39
40 \f
41 ;;; queue helpers
42 ;;; =============
43
44 (define test-q (list->q '(1 2 3)))
45 (test-equal (deq! test-q) 1)
46 (test-equal (deq! test-q) 2)
47 (test-equal (deq! test-q) 3)
48 (test-assert (q-empty? test-q))
49
50 (define test-q (make-q* 'apple 'banana 'carrot))
51 (test-equal (deq! test-q) 'apple)
52 (test-equal (deq! test-q) 'banana)
53 (test-equal (deq! test-q) 'carrot)
54 (test-assert (q-empty? test-q))
55
56
57 \f
58 ;;; Timer tests
59 ;;; ===========
60
61 (test-assert (time= '(1 . 1) '(1 . 1)))
62 (test-assert (not (time= '(1 . 1) '(1 . 0))))
63 (test-assert (not (time= '(0 . 1) '(1 . 1))))
64
65 (test-assert (time< '(1 . 1) '(1 . 2)))
66 (test-assert (time< '(7 . 2) '(8 . 2)))
67 (test-assert (not (time< '(7 . 2) '(7 . 2))))
68 (test-assert (not (time< '(7 . 8) '(7 . 2))))
69 (test-assert (not (time< '(8 . 2) '(7 . 2))))
70
71 (let ((tdelta (make-time-delta 8)))
72   (test-assert (time-delta? tdelta))
73   (test-eqv (time-delta-sec tdelta) 8)
74   (test-eqv (time-delta-usec tdelta) 0)
75   (test-equal
76       (time-delta+ '(2 . 3) tdelta)
77     '(10 . 3)))
78
79 (let ((tdelta (make-time-delta '(10 . 1))))
80   (test-assert (time-delta? tdelta))
81   (test-eqv (time-delta-sec tdelta) 10)
82   (test-eqv (time-delta-usec tdelta) 1)
83   (test-equal
84       (time-delta+ '(2 . 3) tdelta)
85     '(12 . 4)))
86
87 (test-equal (time-minus '(100 . 100) '(50 . 66))
88             '(50 . 34))
89 (test-equal (time-minus '(2 . 0) '(0 . 1))
90             '(1 . 999999))
91
92 (test-equal (time-plus '(50 . 34) '(50 . 66))
93             '(100 . 100))
94 (test-equal (time-plus '(1 . 999999) '(1 . 2))
95             '(3 . 1))
96
97
98 \f
99 ;;; Schedule tests
100 ;;; ==============
101
102 ;; helpers
103 (define (assert-times-expected time-segments expected-times)
104   (test-equal (map time-segment-time time-segments)
105     expected-times))
106
107 (define a-proc (const 'a))
108 (define b-proc (const 'b))
109 (define c-proc (const 'c))
110 (define d-proc (const 'd))
111 (define e-proc (const 'e))
112 (define f-proc (const 'f))
113
114 (define sched (make-schedule))
115 (test-assert (schedule-empty? sched))
116
117 ;; Add a segment at (10 . 0)
118 (schedule-add! sched 10 a-proc)
119 (test-assert (not (schedule-empty? sched)))
120 (test-equal (length (schedule-segments sched)) 1)
121 (test-equal (time-segment-time (car (schedule-segments sched)))
122   '(10 . 0))
123 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
124   1)
125 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
126   a-proc)
127 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
128   a-proc)
129 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
130   'a) ;; why not
131 (assert-times-expected (schedule-segments sched)
132                        '((10 . 0)))
133
134 ;; Add another segment at (10 . 0)
135 (schedule-add! sched '(10 . 0) b-proc)
136 (test-assert (not (schedule-empty? sched)))
137 (test-equal (length (schedule-segments sched)) 1)
138 (test-equal (time-segment-time (car (schedule-segments sched)))
139   '(10 . 0))
140 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
141   2)
142 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
143   a-proc)
144 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
145   b-proc)
146 (assert-times-expected (schedule-segments sched)
147                        '((10 . 0)))
148
149 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
150 (schedule-add! sched 11 c-proc)
151 (schedule-add! sched '(8 . 1) d-proc)
152 (schedule-add! sched '(10 . 10) e-proc)
153 (test-assert (not (schedule-empty? sched)))
154 (test-equal (length (schedule-segments sched)) 4)
155 (assert-times-expected (schedule-segments sched)
156                        '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
157
158 ;; Splitting 
159 (define (test-split-at schedule time expected-before expected-after)
160   (receive (segments-before segments-after)
161       (schedule-segments-split schedule time)
162     (assert-times-expected segments-before expected-before)
163     (assert-times-expected segments-after expected-after)))
164
165 (test-split-at sched 0
166                '()
167                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
168 (test-split-at sched '(8 . 0)
169                '()
170                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
171 (test-split-at sched '(8 . 1)
172                '((8 . 1))
173                '((10 . 0) (10 . 10) (11 . 0)))
174 (test-split-at sched 9
175                '((8 . 1))
176                '((10 . 0) (10 . 10) (11 . 0)))
177 (test-split-at sched 10
178                '((8 . 1) (10 . 0))
179                '((10 . 10) (11 . 0)))
180 (test-split-at sched 9000
181                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
182                '())
183 (test-split-at sched '(9000 . 1)    ; over nine thousaaaaaaand
184                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
185                '())
186
187 ;; Break off half of those and do some tests on them
188 (define some-extracted
189   (schedule-extract-until! sched 10))
190 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
191 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
192 (define first-extracted-queue
193   (time-segment-queue (car some-extracted)))
194 (define second-extracted-queue
195   (time-segment-queue (cadr some-extracted)))
196 (test-assert (not (q-empty? first-extracted-queue)))
197 (test-equal ((deq! first-extracted-queue)) 'd)
198 (test-assert (q-empty? first-extracted-queue))
199
200 (test-assert (not (q-empty? second-extracted-queue)))
201 (test-equal ((deq! second-extracted-queue)) 'a)
202 (test-equal ((deq! second-extracted-queue)) 'b)
203 (test-assert (q-empty? second-extracted-queue))
204
205 ;; Add one more and test flattening to a queue
206 (test-assert (not (schedule-empty? sched)))
207 (schedule-add! sched '(10 . 10) f-proc)
208 (define remaining-segments
209   (schedule-extract-until! sched '(9000 . 1)))
210 (test-assert (schedule-empty? sched))
211 (define some-queue (make-q))
212 (enq! some-queue (const 'ho-ho))
213 (enq! some-queue (const 'ha-ha))
214 (add-segments-contents-to-queue! remaining-segments some-queue)
215 (test-assert (not (q-empty? some-queue)))
216 (test-equal 'ho-ho ((deq! some-queue)))
217 (test-equal 'ha-ha ((deq! some-queue)))
218 (test-equal 'e ((deq! some-queue)))
219 (test-equal 'f ((deq! some-queue)))
220 (test-equal 'c ((deq! some-queue)))
221 (test-assert (q-empty? some-queue))
222
223 ;; ... whew!
224
225 ;;; Run/wrap request stuff
226 ;;; ======================
227
228 (let ((wrapped (wrap (+ 1 2))))
229   (test-assert (procedure? wrapped))
230   (test-equal (wrapped) 3))
231
232 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
233   (test-assert (run-request? run-two-squared))
234   (test-assert (procedure? (run-request-proc run-two-squared)))
235   (test-equal ((run-request-proc run-two-squared)) 4)
236   (test-eq (run-request-when run-two-squared) #f))
237
238 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
239   (test-assert (run-request? run-two-squared))
240   (test-assert (procedure? (run-request-proc run-two-squared)))
241   (test-equal ((run-request-proc run-two-squared)) 4)
242   (test-equal (run-request-when run-two-squared) '(88 . 0)))
243
244 (let ((run-two-squared (run (* 2 2))))
245   (test-assert (run-request? run-two-squared))
246   (test-assert (procedure? (run-request-proc run-two-squared)))
247   (test-equal ((run-request-proc run-two-squared)) 4)
248   (test-eq (run-request-when run-two-squared) #f))
249
250 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
251   (test-assert (run-request? run-two-squared))
252   (test-assert (procedure? (run-request-proc run-two-squared)))
253   (test-equal ((run-request-proc run-two-squared)) 4)
254   (test-equal (run-request-when run-two-squared) '(88 . 0)))
255
256
257 ;;; %run, 8sync and friends tests
258 ;;; ==============================
259
260 (define-syntax-rule (run-in-fake-agenda
261                      code-to-run)
262   (let ((agenda (make-agenda)))
263     (parameterize ((%current-agenda agenda))
264       (call-with-prompt
265        (agenda-prompt-tag agenda)
266        (lambda ()
267          (list '*normal-result* code-to-run))
268        (lambda (kont async-request)
269          (list '*caught-kont*
270                kont async-request
271                ((@@ (8sync agenda) setup-async-request)
272                 kont async-request)))))))
273
274 (define (test-%run-and-friends run-result expected-when)
275   (match run-result
276     (('*caught-kont* kont async-request setup-request)
277      (let* ((fake-kont (speak-it))
278             (run-request ((@@ (8sync agenda) setup-async-request)
279                           fake-kont async-request)))
280        (test-equal (car async-request) '*async-request*)
281        (test-equal (run-request-when run-request) expected-when)
282        ;; we're using speaker as a fake continuation ;p
283        ((run-request-proc run-request))
284        (test-equal (fake-kont)
285                    '("applesauce"))))))
286
287 (test-%run-and-friends (run-in-fake-agenda
288                         (8sync (string-concatenate '("apple" "sauce"))))
289                        #f)
290
291 (test-%run-and-friends (run-in-fake-agenda
292                         (8sync (string-concatenate '("apple" "sauce"))
293                                 '(8 . 0)))
294                        '(8 . 0))
295
296 (test-%run-and-friends (run-in-fake-agenda
297                         (8sync-delay (string-concatenate '("apple" "sauce"))
298                                       8))
299                        ;; whoa, I'm surprised equal? can
300                        ;; compare records like this
301                        (tdelta 8))
302
303 ;; TODO: test %port-request
304 ;; TODO: test 8sync and friends!
305
306
307 ;;; Agenda tests
308 ;;; ============
309
310 ;; helpers
311
312 (define (true-after-n-times n)
313   (let ((count 0))
314     (lambda _
315       (set! count (+ count 1))
316       (if (>= count n) #t #f))))
317
318 ;; the dummy test
319
320 (define speaker (speak-it))
321
322 (define (dummy-func)
323   (speaker "I'm a dummy\n"))
324
325 (define (run-dummy)
326   (speaker "I bet I can make you say you're a dummy!\n")
327   (run-it dummy-func))
328
329 (begin
330   (set! speaker (speak-it))  ; reset the speaker
331   (start-agenda (make-agenda #:queue (make-q* run-dummy))
332                 #:stop-condition (true-after-n-times 2))
333   (test-equal (speaker)
334     '("I bet I can make you say you're a dummy!\n"
335       "I'm a dummy\n")))
336
337 ;; should only do the first one after one round though
338 (begin
339   (set! speaker (speak-it))  ; reset the speaker
340   (start-agenda (make-agenda #:queue (make-q* run-dummy))
341                 #:stop-condition (true-after-n-times 1))
342   (test-equal (speaker)
343     '("I bet I can make you say you're a dummy!\n")))
344
345 ;; delimited continuation tests
346
347 (define (return-monkey)
348   (speaker "(Hint, it's a monkey...)\n")
349   'monkey)
350
351 (define (talk-about-the-zoo)
352   (speaker "Today I went to the zoo and I saw...\n")
353   (speaker
354    (string-concatenate
355     `("A " ,(symbol->string (8sync (return-monkey))) "!\n"))))
356
357 (begin
358   (set! speaker (speak-it))
359   ;; (enq! q talk-about-the-zoo-but-wait)
360   (start-agenda (make-agenda #:queue (make-q* talk-about-the-zoo))
361                 #:stop-condition (true-after-n-times 10))
362   (test-equal (speaker)
363               '("Today I went to the zoo and I saw...\n"
364                 "(Hint, it's a monkey...)\n"
365                 "A monkey!\n")))
366
367
368 ;; Error handling tests
369 ;; --------------------
370
371 (define (remote-func-breaks)
372   (speaker "Here we go...\n")
373   (+ 1 2 (/ 1 0))
374   (speaker "SHOULD NOT HAPPEN\n"))
375
376 (define (indirection-remote-func-breaks)
377   (speaker "bebop\n")
378   (8sync (remote-func-breaks))
379   (speaker "bidop\n"))
380
381 (define* (local-func-gets-break #:key with-indirection)
382   (speaker "Time for exception fun!\n")
383   (let ((caught-exception #f))
384     (catch-8sync
385      (8sync-run (if with-indirection
386                          (indirection-remote-func-breaks)
387                          (remote-func-breaks)))
388       ('numerical-overflow
389        (lambda (orig-stacks . orig-args)
390          (set! caught-exception #t)
391          (speaker "in here now!\n")
392          (test-equal orig-args '("/" "Numerical overflow" #f #f))
393          (test-assert (list? orig-stacks))
394          (test-equal (length orig-stacks)
395                      (if with-indirection 2 1))
396          (for-each
397           (lambda (x)
398             (test-assert (stack? x)))
399           orig-stacks))))
400     (test-assert caught-exception))
401   (speaker "Well that was fun :)\n"))
402
403
404 (begin
405   (set! speaker (speak-it))
406   (start-agenda (make-agenda #:queue (make-q* local-func-gets-break))
407                 #:stop-condition (true-after-n-times 10))
408   (test-equal (speaker)
409               '("Time for exception fun!\n"
410                 "Here we go...\n"
411                 "in here now!\n"
412                 "Well that was fun :)\n")))
413
414 (begin
415   (set! speaker (speak-it))
416   (start-agenda (make-agenda
417                  #:queue (make-q* (wrap (local-func-gets-break
418                                          #:with-indirection #t))))
419                 #:stop-condition (true-after-n-times 10))
420   (test-equal (speaker)
421               '("Time for exception fun!\n"
422                 "bebop\n"
423                 "Here we go...\n"
424                 "in here now!\n"
425                 "Well that was fun :)\n")))
426
427 ;; Make sure catching tools work
428
429 (let ((speaker (speak-it))
430       (catch-result #f))
431   (catch-8sync
432    (begin
433      (speaker "hello")
434      (throw '8sync-caught-error
435             'my-orig-key '(apple orange banana) '(*fake-stack* *fake-stack* *fake-stack*))
436      (speaker "no goodbyes"))
437    ('some-key
438     (lambda (stacks . rest)
439       (speaker "should not happen")))
440    ('my-orig-key
441     (lambda (stacks fruit1 fruit2 fruit3)
442       (set! catch-result
443             `((fruit1 ,fruit1)
444               (fruit2 ,fruit2)
445               (fruit3 ,fruit3))))))
446   (test-equal (speaker) '("hello"))
447   (test-equal catch-result '((fruit1 apple)
448                              (fruit2 orange)
449                              (fruit3 banana))))
450
451 ;; End tests
452
453 (test-end "test-agenda")
454
455 ;; @@: A better way to handle this at the repl?
456 (test-exit)
457