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