agenda: Move install-suspendable-ports! call to start-agenda.
[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 ;; TODO: We need to rewrite the whole lot here...
261
262 ;;; Agenda tests
263 ;;; ============
264
265 ;; helpers
266
267 (define (true-after-n-times n)
268   (let ((count 0))
269     (lambda _
270       (set! count (+ count 1))
271       (if (>= count n) #t #f))))
272
273 ;; the dummy test
274
275 (define speaker (speak-it))
276
277 (define (dummy-func)
278   (speaker "I'm a dummy\n"))
279
280 (define (run-dummy)
281   (speaker "I bet I can make you say you're a dummy!\n")
282   (run-it dummy-func))
283
284 (begin
285   (set! speaker (speak-it))  ; reset the speaker
286   (start-agenda (make-agenda #:queue (make-q* run-dummy))
287                 #:stop-condition (true-after-n-times 2))
288   (test-equal (speaker)
289     '("I bet I can make you say you're a dummy!\n"
290       "I'm a dummy\n")))
291
292 ;; should only do the first one after one round though
293 (begin
294   (set! speaker (speak-it))  ; reset the speaker
295   (start-agenda (make-agenda #:queue (make-q* run-dummy))
296                 #:stop-condition (true-after-n-times 1))
297   (test-equal (speaker)
298     '("I bet I can make you say you're a dummy!\n")))
299
300
301 ;; End tests
302
303 (test-end "test-agenda")
304
305 ;; @@: A better way to handle this at the repl?
306 (test-exit)
307