1 ;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
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.
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.
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
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))
28 (test-begin "tests-agenda")
37 (lambda* (#:optional message)
38 (if message (set! messages (append messages (list message))))
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))))
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))))
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)
60 (time-delta+ '(2 . 3) tdelta)
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)
68 (time-delta+ '(2 . 3) tdelta)
71 (test-equal (time-minus '(100 . 100) '(50 . 66))
73 (test-equal (time-minus '(2 . 0) '(0 . 1))
76 (test-equal (time-plus '(50 . 34) '(50 . 66))
78 (test-equal (time-plus '(1 . 999999) '(1 . 2))
87 (define (assert-times-expected time-segments expected-times)
88 (test-equal (map time-segment-time time-segments)
91 (define a-proc (const 'a))
92 (define b-proc (const 'b))
93 (define c-proc (const 'c))
94 (define d-proc (const 'd))
95 (define e-proc (const 'e))
96 (define f-proc (const 'f))
98 (define sched (make-schedule))
99 (test-assert (schedule-empty? sched))
101 ;; Add a segment at (10 . 0)
102 (schedule-add! sched 10 a-proc)
103 (test-assert (not (schedule-empty? sched)))
104 (test-equal (length (schedule-segments sched)) 1)
105 (test-equal (time-segment-time (car (schedule-segments sched)))
107 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
109 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
111 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
113 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
115 (assert-times-expected (schedule-segments sched)
118 ;; Add another segment at (10 . 0)
119 (schedule-add! sched '(10 . 0) b-proc)
120 (test-assert (not (schedule-empty? sched)))
121 (test-equal (length (schedule-segments sched)) 1)
122 (test-equal (time-segment-time (car (schedule-segments sched)))
124 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
126 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
128 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
130 (assert-times-expected (schedule-segments sched)
133 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
134 (schedule-add! sched 11 c-proc)
135 (schedule-add! sched '(8 . 1) d-proc)
136 (schedule-add! sched '(10 . 10) e-proc)
137 (test-assert (not (schedule-empty? sched)))
138 (test-equal (length (schedule-segments sched)) 4)
139 (assert-times-expected (schedule-segments sched)
140 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
143 (define (test-split-at schedule time expected-before expected-after)
144 (receive (segments-before segments-after)
145 (schedule-segments-split schedule time)
146 (assert-times-expected segments-before expected-before)
147 (assert-times-expected segments-after expected-after)))
149 (test-split-at sched 0
151 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
152 (test-split-at sched '(8 . 0)
154 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
155 (test-split-at sched '(8 . 1)
157 '((10 . 0) (10 . 10) (11 . 0)))
158 (test-split-at sched 9
160 '((10 . 0) (10 . 10) (11 . 0)))
161 (test-split-at sched 10
163 '((10 . 10) (11 . 0)))
164 (test-split-at sched 9000
165 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
167 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
168 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
171 ;; Break off half of those and do some tests on them
172 (define some-extracted
173 (schedule-extract-until! sched 10))
174 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
175 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
176 (define first-extracted-queue
177 (time-segment-queue (car some-extracted)))
178 (define second-extracted-queue
179 (time-segment-queue (cadr some-extracted)))
180 (test-assert (not (q-empty? first-extracted-queue)))
181 (test-equal ((deq! first-extracted-queue)) 'd)
182 (test-assert (q-empty? first-extracted-queue))
184 (test-assert (not (q-empty? second-extracted-queue)))
185 (test-equal ((deq! second-extracted-queue)) 'a)
186 (test-equal ((deq! second-extracted-queue)) 'b)
187 (test-assert (q-empty? second-extracted-queue))
189 ;; Add one more and test flattening to a queue
190 (test-assert (not (schedule-empty? sched)))
191 (schedule-add! sched '(10 . 10) f-proc)
192 (define remaining-segments
193 (schedule-extract-until! sched '(9000 . 1)))
194 (test-assert (schedule-empty? sched))
195 (define some-queue (make-q))
196 (enq! some-queue (const 'ho-ho))
197 (enq! some-queue (const 'ha-ha))
198 (add-segments-contents-to-queue! remaining-segments some-queue)
199 (test-assert (not (q-empty? some-queue)))
200 (test-equal 'ho-ho ((deq! some-queue)))
201 (test-equal 'ha-ha ((deq! some-queue)))
202 (test-equal 'e ((deq! some-queue)))
203 (test-equal 'f ((deq! some-queue)))
204 (test-equal 'c ((deq! some-queue)))
205 (test-assert (q-empty? some-queue))
209 ;; Run/wrap request stuff
210 ;; ----------------------
212 (let ((wrapped (wrap (+ 1 2))))
213 (test-assert (procedure? wrapped))
214 (test-equal (wrapped) 3))
216 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
217 (test-assert (run-request? run-two-squared))
218 (test-assert (procedure? (run-request-proc run-two-squared)))
219 (test-equal ((run-request-proc run-two-squared)) 4)
220 (test-eq (run-request-when run-two-squared) #f))
222 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
223 (test-assert (run-request? run-two-squared))
224 (test-assert (procedure? (run-request-proc run-two-squared)))
225 (test-equal ((run-request-proc run-two-squared)) 4)
226 (test-equal (run-request-when run-two-squared) '(88 . 0)))
228 (let ((run-two-squared (run (* 2 2))))
229 (test-assert (run-request? run-two-squared))
230 (test-assert (procedure? (run-request-proc run-two-squared)))
231 (test-equal ((run-request-proc run-two-squared)) 4)
232 (test-eq (run-request-when run-two-squared) #f))
234 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
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-equal (run-request-when run-two-squared) '(88 . 0)))
241 ;;; %run, %sync and friends tests
242 ;;; -----------------------------
244 (define (test-%run-and-friends async-request expected-when)
245 (let* ((fake-kont (speak-it))
246 (run-request ((@@ (eightsync agenda) setup-async-request)
247 fake-kont async-request)))
248 (test-equal (car async-request) '*async-request*)
249 (test-equal (run-request-when run-request) expected-when)
250 ;; we're using speaker as a fake continuation ;p
251 ((run-request-proc run-request))
252 (test-equal (fake-kont)
255 (test-%run-and-friends (%run (string-concatenate '("apple" "sauce")))
258 (test-%run-and-friends (%run-at (string-concatenate '("apple" "sauce"))
262 (test-%run-and-friends (%run-delay (string-concatenate '("apple" "sauce"))
264 ;; whoa, I'm surprised equal? can
265 ;; compare records like this
268 ;; TODO: test %port-request
269 ;; TODO: test %sync and friends!
277 (define (true-after-n-times n)
280 (set! count (+ count 1))
281 (if (>= count n) #t #f))))
285 (define speaker (speak-it))
288 (speaker "I'm a dummy\n"))
291 (speaker "I bet I can make you say you're a dummy!\n")
295 (set! speaker (speak-it)) ; reset the speaker
297 (start-agenda (make-agenda #:queue q)
298 #:stop-condition (true-after-n-times 2))
299 (test-equal (speaker)
300 '("I bet I can make you say you're a dummy!\n"
303 ;; should only do the first one after one round though
305 (set! speaker (speak-it)) ; reset the speaker
307 (start-agenda (make-agenda #:queue q)
308 #:stop-condition (true-after-n-times 1))
309 (test-equal (speaker)
310 '("I bet I can make you say you're a dummy!\n")))
312 ;; delimited continuation tests
314 (define (return-monkey)
315 (speaker "(Hint, it's a monkey...)\n")
318 (define (talk-about-the-zoo)
319 (speaker "Today I went to the zoo and I saw...\n")
322 `("A " ,(symbol->string (%sync (%run (return-monkey)))) "!\n"))))
325 (set! speaker (speak-it))
326 (enq! q talk-about-the-zoo)
327 ;; (enq! q talk-about-the-zoo-but-wait)
328 (start-agenda (make-agenda #:queue q)
329 #:stop-condition (true-after-n-times 10))
330 (test-equal (speaker)
331 '("Today I went to the zoo and I saw...\n"
332 "(Hint, it's a monkey...)\n"
337 (test-end "tests-agenda")