tests: Add "speaker" utility.
[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 (define-syntax-rule (%import var)
30   (define var
31     (@@ (8sync agenda) var)))
32
33 \f
34 ;;; queue helpers
35 ;;; =============
36
37 (define test-q (list->q '(1 2 3)))
38 (test-equal (deq! test-q) 1)
39 (test-equal (deq! test-q) 2)
40 (test-equal (deq! test-q) 3)
41 (test-assert (q-empty? test-q))
42
43 (define test-q (make-q* 'apple 'banana 'carrot))
44 (test-equal (deq! test-q) 'apple)
45 (test-equal (deq! test-q) 'banana)
46 (test-equal (deq! test-q) 'carrot)
47 (test-assert (q-empty? test-q))
48
49
50 \f
51 ;;; Timer tests
52 ;;; ===========
53
54 (%import time=)
55 (%import time<)
56 (%import time-minus)
57 (%import time-plus)
58
59 (test-assert (time= '(1 . 1) '(1 . 1)))
60 (test-assert (not (time= '(1 . 1) '(1 . 0))))
61 (test-assert (not (time= '(0 . 1) '(1 . 1))))
62
63 (test-assert (time< '(1 . 1) '(1 . 2)))
64 (test-assert (time< '(7 . 2) '(8 . 2)))
65 (test-assert (not (time< '(7 . 2) '(7 . 2))))
66 (test-assert (not (time< '(7 . 8) '(7 . 2))))
67 (test-assert (not (time< '(8 . 2) '(7 . 2))))
68
69 (test-equal (time-minus '(100 . 100) '(50 . 66))
70             '(50 . 34))
71 (test-equal (time-minus '(2 . 0) '(0 . 1))
72             '(1 . 999999))
73
74 (test-equal (time-plus '(50 . 34) '(50 . 66))
75             '(100 . 100))
76 (test-equal (time-plus '(1 . 999999) '(1 . 2))
77             '(3 . 1))
78
79
80 \f
81 ;;; Schedule tests
82 ;;; ==============
83
84 (%import time-segment-time)
85 (%import time-segment-queue)
86
87 ;; helpers
88 (define (assert-times-expected time-segments expected-times)
89   (test-equal (map time-segment-time time-segments)
90     expected-times))
91
92 (define a-proc (const 'a))
93 (define b-proc (const 'b))
94 (define c-proc (const 'c))
95 (define d-proc (const 'd))
96 (define e-proc (const 'e))
97 (define f-proc (const 'f))
98
99 (define sched (make-schedule))
100 (test-assert (schedule-empty? sched))
101
102 ;; Add a segment at (10 . 0)
103 (schedule-add! sched '(10 . 0) a-proc)
104 (test-assert (not (schedule-empty? sched)))
105 (test-equal (length (schedule-segments sched)) 1)
106 (test-equal (time-segment-time (car (schedule-segments sched)))
107   '(10 . 0))
108 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
109   1)
110 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
111   a-proc)
112 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
113   a-proc)
114 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
115   'a) ;; why not
116 (assert-times-expected (schedule-segments sched)
117                        '((10 . 0)))
118
119 ;; Add another segment at (10 . 0)
120 (schedule-add! sched '(10 . 0) b-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   2)
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   b-proc)
131 (assert-times-expected (schedule-segments sched)
132                        '((10 . 0)))
133
134 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
135 (schedule-add! sched '(11 . 0) c-proc)
136 (schedule-add! sched '(8 . 1) d-proc)
137 (schedule-add! sched '(10 . 10) e-proc)
138 (test-assert (not (schedule-empty? sched)))
139 (test-equal (length (schedule-segments sched)) 4)
140 (assert-times-expected (schedule-segments sched)
141                        '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
142
143 ;; Splitting 
144 (define (test-split-at schedule time expected-before expected-after)
145   (receive (segments-before segments-after)
146       (schedule-segments-split schedule time)
147     (assert-times-expected segments-before expected-before)
148     (assert-times-expected segments-after expected-after)))
149
150 (test-split-at sched '(0 . 0)
151                '()
152                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
153 (test-split-at sched '(8 . 0)
154                '()
155                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
156 (test-split-at sched '(8 . 1)
157                '((8 . 1))
158                '((10 . 0) (10 . 10) (11 . 0)))
159 (test-split-at sched '(9 . 0)
160                '((8 . 1))
161                '((10 . 0) (10 . 10) (11 . 0)))
162 (test-split-at sched '(10 . 0)
163                '((8 . 1) (10 . 0))
164                '((10 . 10) (11 . 0)))
165 (test-split-at sched '(9000 . 0)
166                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
167                '())
168 (test-split-at sched '(9000 . 1)    ; over nine thousaaaaaaand
169                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
170                '())
171
172 ;; Break off half of those and do some tests on them
173 (define some-extracted
174   (schedule-extract-until! sched '(10 . 0)))
175 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
176 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
177 (define first-extracted-queue
178   (time-segment-queue (car some-extracted)))
179 (define second-extracted-queue
180   (time-segment-queue (cadr some-extracted)))
181 (test-assert (not (q-empty? first-extracted-queue)))
182 (test-equal ((deq! first-extracted-queue)) 'd)
183 (test-assert (q-empty? first-extracted-queue))
184
185 (test-assert (not (q-empty? second-extracted-queue)))
186 (test-equal ((deq! second-extracted-queue)) 'a)
187 (test-equal ((deq! second-extracted-queue)) 'b)
188 (test-assert (q-empty? second-extracted-queue))
189
190 ;; Add one more and test flattening to a queue
191 (test-assert (not (schedule-empty? sched)))
192 (schedule-add! sched '(10 . 10) f-proc)
193 (define remaining-segments
194   (schedule-extract-until! sched '(9000 . 1)))
195 (test-assert (schedule-empty? sched))
196 (define some-queue (make-q))
197 (enq! some-queue (const 'ho-ho))
198 (enq! some-queue (const 'ha-ha))
199 (add-segments-contents-to-queue! remaining-segments some-queue)
200 (test-assert (not (q-empty? some-queue)))
201 (test-equal 'ho-ho ((deq! some-queue)))
202 (test-equal 'ha-ha ((deq! some-queue)))
203 (test-equal 'e ((deq! some-queue)))
204 (test-equal 'f ((deq! some-queue)))
205 (test-equal 'c ((deq! some-queue)))
206 (test-assert (q-empty? some-queue))
207
208 ;; ... whew!
209
210 ;;; Run/wrap request stuff
211 ;;; ======================
212
213 (let ((wrapped (wrap (+ 1 2))))
214   (test-assert (procedure? wrapped))
215   (test-equal (wrapped) 3))
216
217 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
218   (test-assert (run-request? run-two-squared))
219   (test-assert (procedure? (run-request-proc run-two-squared)))
220   (test-equal ((run-request-proc run-two-squared)) 4)
221   (test-eq (run-request-when run-two-squared) #f))
222
223 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
224   (test-assert (run-request? run-two-squared))
225   (test-assert (procedure? (run-request-proc run-two-squared)))
226   (test-equal ((run-request-proc run-two-squared)) 4)
227   (test-equal (run-request-when run-two-squared) '(88 . 0)))
228
229 (let ((run-two-squared (run (* 2 2))))
230   (test-assert (run-request? run-two-squared))
231   (test-assert (procedure? (run-request-proc run-two-squared)))
232   (test-equal ((run-request-proc run-two-squared)) 4)
233   (test-eq (run-request-when run-two-squared) #f))
234
235 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
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-equal (run-request-when run-two-squared) '(88 . 0)))
240
241
242 ;;; %run, 8sync and friends tests
243 ;;; ==============================
244
245 ;; TODO: We need to rewrite the whole lot here...
246
247 ;;; Agenda tests
248 ;;; ============
249
250 ;; helpers
251
252 (define (true-after-n-times n)
253   (let ((count 0))
254     (lambda _
255       (define ans
256         (if (>= count n) #t #f))
257       (set! count (+ count 1))
258       ans)))
259
260 ;; the dummy test
261
262 (define (dummy-func)
263   (speak "I'm a dummy\n"))
264
265 (define (run-dummy)
266   (speak "I bet I can make you say you're a dummy!\n")
267   (run-it dummy-func))
268
269 (with-fresh-speaker
270  (start-agenda (make-agenda #:queue (make-q* run-dummy))
271                #:stop-condition (true-after-n-times 2))
272  (test-equal (get-spoken)
273    '("I bet I can make you say you're a dummy!\n"
274      "I'm a dummy\n")))
275
276 ;; should only do the first one after one round though
277 (with-fresh-speaker
278  (start-agenda (make-agenda #:queue (make-q* run-dummy))
279                #:stop-condition (true-after-n-times 1))
280  (test-equal (get-spoken)
281    '("I bet I can make you say you're a dummy!\n")))
282
283
284 ;; End tests
285
286 (test-end "test-agenda")
287
288 ;; @@: A better way to handle this at the repl?
289 (test-exit)
290