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