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)
77 (define (assert-times-expected time-segments expected-times)
78 (test-equal (map time-segment-time time-segments)
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))
88 (define sched (make-schedule))
89 (test-assert (schedule-empty? sched))
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)))
97 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
99 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
101 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
103 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
105 (assert-times-expected (schedule-segments sched)
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)))
114 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
116 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
118 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
120 (assert-times-expected (schedule-segments sched)
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)))
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)))
139 (test-split-at sched 0
141 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
142 (test-split-at sched '(8 . 0)
144 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
145 (test-split-at sched '(8 . 1)
147 '((10 . 0) (10 . 10) (11 . 0)))
148 (test-split-at sched 9
150 '((10 . 0) (10 . 10) (11 . 0)))
151 (test-split-at sched 10
153 '((10 . 10) (11 . 0)))
154 (test-split-at sched 9000
155 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
157 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
158 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
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))
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))
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))
199 ;; Run/wrap request stuff
200 ;; ----------------------
202 (let ((wrapped (wrap (+ 1 2))))
203 (test-assert (procedure? wrapped))
204 (test-equal (wrapped) 3))
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))
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)))
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))
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)))
231 ;;; %run, %sync and friends tests
232 ;;; -----------------------------
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)
245 (test-%run-and-friends (%run (string-concatenate '("apple" "sauce")))
248 (test-%run-and-friends (%run-at (string-concatenate '("apple" "sauce"))
252 (test-%run-and-friends (%run-delay (string-concatenate '("apple" "sauce"))
254 ;; whoa, I'm surprised equal? can
255 ;; compare records like this
258 ;; TODO: test %port-request
259 ;; TODO: test %sync and friends!
267 (define (true-after-n-times n)
270 (set! count (+ count 1))
271 (if (>= count n) #t #f))))
275 (define speaker (speak-it))
278 (speaker "I'm a dummy\n"))
281 (speaker "I bet I can make you say you're a dummy!\n")
285 (set! speaker (speak-it)) ; reset the speaker
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"
293 ;; should only do the first one after one round though
295 (set! speaker (speak-it)) ; reset the speaker
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")))
302 ;; delimited continuation tests
304 (define (return-monkey)
305 (speaker "(Hint, it's a monkey...)\n")
308 (define (talk-about-the-zoo)
309 (speaker "Today I went to the zoo and I saw...\n")
312 `("A " ,(symbol->string (%sync (%run (return-monkey)))) "!\n"))))
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"
327 (test-end "tests-agenda")