4a87ad8dbea4eb7ebbaa6d2d1f957b1e99fd1e33
[8sync.git] / tests.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-core)
23   #:use-module (srfi srfi-64)
24   #:use-module (ice-9 q)
25   #:use-module (ice-9 receive)
26   #:use-module (eightsync agenda))
27
28 (test-begin "tests")
29
30 \f
31
32 ;;; Helpers
33 ;;; =======
34
35 (define (speak-it)
36   (let ((messages '()))
37     (lambda* (#:optional message)
38       (if message (set! messages (append messages (list message))))
39       messages)))
40
41 \f
42 ;; Timer tests
43 ;; ===========
44
45 (test-assert (time= '(1 . 1) '(1 . 1)))
46 (test-assert (not (time= '(1 . 1) '(1 . 0))))
47 (test-assert (not (time= '(0 . 1) '(1 . 1))))
48
49 (test-assert (time< '(1 . 1) '(1 . 2)))
50 (test-assert (time< '(7 . 2) '(8 . 2)))
51 (test-assert (not (time< '(7 . 2) '(7 . 2))))
52 (test-assert (not (time< '(7 . 8) '(7 . 2))))
53 (test-assert (not (time< '(8 . 2) '(7 . 2))))
54
55 (let ((tdelta (make-time-delta 8)))
56   (test-assert (time-delta? tdelta))
57   (test-eqv (time-delta-sec tdelta) 8)
58   (test-eqv (time-delta-usec tdelta) 0)
59   (test-equal
60       (time-delta+ '(2 . 3) tdelta)
61     '(10 . 3)))
62
63 (let ((tdelta (make-time-delta 10 1)))
64   (test-assert (time-delta? tdelta))
65   (test-eqv (time-delta-sec tdelta) 10)
66   (test-eqv (time-delta-usec tdelta) 1)
67   (test-equal
68       (time-delta+ '(2 . 3) tdelta)
69     '(12 . 4)))
70
71
72 \f
73 ;;; Schedule tests
74 ;;; ==============
75
76 ;; helpers
77 (define (assert-times-expected time-segments expected-times)
78   (test-equal (map time-segment-time time-segments)
79     expected-times))
80
81 (define a-proc (const 'a))
82 (define b-proc (const 'b))
83 (define c-proc (const 'c))
84 (define d-proc (const 'd))
85 (define e-proc (const 'e))
86 (define f-proc (const 'f))
87
88 (define sched (make-schedule))
89 (test-assert (schedule-empty? sched))
90
91 ;; Add a segment at (10 . 0)
92 (schedule-add! sched 10 a-proc)
93 (test-assert (not (schedule-empty? sched)))
94 (test-equal (length (schedule-segments sched)) 1)
95 (test-equal (time-segment-time (car (schedule-segments sched)))
96   '(10 . 0))
97 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
98   1)
99 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
100   a-proc)
101 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
102   a-proc)
103 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
104   'a) ;; why not
105 (assert-times-expected (schedule-segments sched)
106                        '((10 . 0)))
107
108 ;; Add another segment at (10 . 0)
109 (schedule-add! sched '(10 . 0) b-proc)
110 (test-assert (not (schedule-empty? sched)))
111 (test-equal (length (schedule-segments sched)) 1)
112 (test-equal (time-segment-time (car (schedule-segments sched)))
113   '(10 . 0))
114 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
115   2)
116 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
117   a-proc)
118 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
119   b-proc)
120 (assert-times-expected (schedule-segments sched)
121                        '((10 . 0)))
122
123 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
124 (schedule-add! sched 11 c-proc)
125 (schedule-add! sched '(8 . 1) d-proc)
126 (schedule-add! sched '(10 . 10) e-proc)
127 (test-assert (not (schedule-empty? sched)))
128 (test-equal (length (schedule-segments sched)) 4)
129 (assert-times-expected (schedule-segments sched)
130                        '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
131
132 ;; Splitting 
133 (define (test-split-at schedule time expected-before expected-after)
134   (receive (segments-before segments-after)
135       (schedule-segments-split schedule time)
136     (assert-times-expected segments-before expected-before)
137     (assert-times-expected segments-after expected-after)))
138
139 (test-split-at sched 0
140                '()
141                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
142 (test-split-at sched '(8 . 0)
143                '()
144                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
145 (test-split-at sched '(8 . 1)
146                '((8 . 1))
147                '((10 . 0) (10 . 10) (11 . 0)))
148 (test-split-at sched 9
149                '((8 . 1))
150                '((10 . 0) (10 . 10) (11 . 0)))
151 (test-split-at sched 10
152                '((8 . 1) (10 . 0))
153                '((10 . 10) (11 . 0)))
154 (test-split-at sched 9000
155                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
156                '())
157 (test-split-at sched '(9000 . 1)    ; over nine thousaaaaaaand
158                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
159                '())
160
161 ;; Break off half of those and do some tests on them
162 (define some-extracted
163   (schedule-extract-until! sched 10))
164 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
165 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
166 (define first-extracted-queue
167   (time-segment-queue (car some-extracted)))
168 (define second-extracted-queue
169   (time-segment-queue (cadr some-extracted)))
170 (test-assert (not (q-empty? first-extracted-queue)))
171 (test-equal ((deq! first-extracted-queue)) 'd)
172 (test-assert (q-empty? first-extracted-queue))
173
174 (test-assert (not (q-empty? second-extracted-queue)))
175 (test-equal ((deq! second-extracted-queue)) 'a)
176 (test-equal ((deq! second-extracted-queue)) 'b)
177 (test-assert (q-empty? second-extracted-queue))
178
179 ;; Add one more and test flattening to a queue
180 (test-assert (not (schedule-empty? sched)))
181 (schedule-add! sched '(10 . 10) f-proc)
182 (define remaining-segments
183   (schedule-extract-until! sched '(9000 . 1)))
184 (test-assert (schedule-empty? sched))
185 (define some-queue (make-q))
186 (enq! some-queue (const 'ho-ho))
187 (enq! some-queue (const 'ha-ha))
188 (add-segments-contents-to-queue! remaining-segments some-queue)
189 (test-assert (not (q-empty? some-queue)))
190 (test-equal 'ho-ho ((deq! some-queue)))
191 (test-equal 'ha-ha ((deq! some-queue)))
192 (test-equal 'e ((deq! some-queue)))
193 (test-equal 'f ((deq! some-queue)))
194 (test-equal 'c ((deq! some-queue)))
195 (test-assert (q-empty? some-queue))
196
197 ;; ... whew!
198
199 ;; Run/wrap request stuff
200 ;; ----------------------
201
202 (let ((wrapped (wrap (+ 1 2))))
203   (test-assert (procedure? wrapped))
204   (test-equal (wrapped) 3))
205
206 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
207   (test-assert (run-request? run-two-squared))
208   (test-assert (procedure? (run-request-proc run-two-squared)))
209   (test-equal ((run-request-proc run-two-squared)) 4)
210   (test-eq (run-request-when run-two-squared) #f))
211
212 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
213   (test-assert (run-request? run-two-squared))
214   (test-assert (procedure? (run-request-proc run-two-squared)))
215   (test-equal ((run-request-proc run-two-squared)) 4)
216   (test-equal (run-request-when run-two-squared) '(88 . 0)))
217
218 (let ((run-two-squared (run (* 2 2))))
219   (test-assert (run-request? run-two-squared))
220   (test-assert (procedure? (run-request-proc run-two-squared)))
221   (test-equal ((run-request-proc run-two-squared)) 4)
222   (test-eq (run-request-when run-two-squared) #f))
223
224 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
225   (test-assert (run-request? run-two-squared))
226   (test-assert (procedure? (run-request-proc run-two-squared)))
227   (test-equal ((run-request-proc run-two-squared)) 4)
228   (test-equal (run-request-when run-two-squared) '(88 . 0)))
229
230
231 ;;; %run, %sync and friends tests
232 ;;; -----------------------------
233
234 (define (test-%run-and-friends async-request expected-when)
235   (let* ((fake-kont (speak-it))
236          (run-request ((@@ (eightsync agenda) setup-async-request)
237                        fake-kont async-request)))
238     (test-equal (car async-request) '*async-request*)
239     (test-equal (run-request-when run-request) expected-when)
240     ;; we're using speaker as a fake continuation ;p
241     ((run-request-proc run-request))
242     (test-equal (fake-kont)
243                 '("applesauce"))))
244
245 (test-%run-and-friends (%run (string-concatenate '("apple" "sauce")))
246                        #f)
247
248 (test-%run-and-friends (%run-at (string-concatenate '("apple" "sauce"))
249                                 '(8 . 0))
250                        '(8 . 0))
251
252 (test-%run-and-friends (%run-delay (string-concatenate '("apple" "sauce"))
253                                    8)
254                        ;; whoa, I'm surprised equal? can
255                        ;; compare records like this
256                        (tdelta 8 0))
257
258 ;; TODO: test %port-request
259 ;; TODO: test %sync and friends!
260
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 (let ((q (make-q)))
285   (set! speaker (speak-it))  ; reset the speaker
286   (enq! q run-dummy)
287   (start-agenda (make-agenda #:queue q)
288                 #:stop-condition (true-after-n-times 2))
289   (test-equal (speaker)
290     '("I bet I can make you say you're a dummy!\n"
291       "I'm a dummy\n")))
292
293 ;; should only do the first one after one round though
294 (let ((q (make-q)))
295   (set! speaker (speak-it))  ; reset the speaker
296   (enq! q run-dummy)
297   (start-agenda (make-agenda #:queue q)
298                 #:stop-condition (true-after-n-times 1))
299   (test-equal (speaker)
300     '("I bet I can make you say you're a dummy!\n")))
301
302 ;; delimited continuation tests
303
304 (define (return-monkey)
305   (speaker "(Hint, it's a monkey...)\n")
306   'monkey)
307
308 (define (talk-about-the-zoo)
309   (speaker "Today I went to the zoo and I saw...\n")
310   (speaker
311    (string-concatenate
312     `("A " ,(symbol->string (%sync (%run (return-monkey)))) "!\n"))))
313
314 (let ((q (make-q)))
315   (set! speaker (speak-it))
316   (enq! q talk-about-the-zoo)
317   ;; (enq! q talk-about-the-zoo-but-wait)
318   (start-agenda (make-agenda #:queue q)
319                 #:stop-condition (true-after-n-times 10))
320   (test-equal (speaker)
321               '("Today I went to the zoo and I saw...\n"
322                 "(Hint, it's a monkey...)\n"
323                 "A monkey!\n")))
324
325 ;; End tests
326
327 (test-end "tests")
328 ;; (test-exit)
329