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))
82 (define (assert-times-expected time-segments expected-times)
83 (test-equal (map time-segment-time time-segments)
86 (define a-proc (const 'a))
87 (define b-proc (const 'b))
88 (define c-proc (const 'c))
89 (define d-proc (const 'd))
90 (define e-proc (const 'e))
91 (define f-proc (const 'f))
93 (define sched (make-schedule))
94 (test-assert (schedule-empty? sched))
96 ;; Add a segment at (10 . 0)
97 (schedule-add! sched 10 a-proc)
98 (test-assert (not (schedule-empty? sched)))
99 (test-equal (length (schedule-segments sched)) 1)
100 (test-equal (time-segment-time (car (schedule-segments sched)))
102 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
104 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
106 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
108 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
110 (assert-times-expected (schedule-segments sched)
113 ;; Add another segment at (10 . 0)
114 (schedule-add! sched '(10 . 0) b-proc)
115 (test-assert (not (schedule-empty? sched)))
116 (test-equal (length (schedule-segments sched)) 1)
117 (test-equal (time-segment-time (car (schedule-segments sched)))
119 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
121 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
123 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
125 (assert-times-expected (schedule-segments sched)
128 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
129 (schedule-add! sched 11 c-proc)
130 (schedule-add! sched '(8 . 1) d-proc)
131 (schedule-add! sched '(10 . 10) e-proc)
132 (test-assert (not (schedule-empty? sched)))
133 (test-equal (length (schedule-segments sched)) 4)
134 (assert-times-expected (schedule-segments sched)
135 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
138 (define (test-split-at schedule time expected-before expected-after)
139 (receive (segments-before segments-after)
140 (schedule-segments-split schedule time)
141 (assert-times-expected segments-before expected-before)
142 (assert-times-expected segments-after expected-after)))
144 (test-split-at sched 0
146 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
147 (test-split-at sched '(8 . 0)
149 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
150 (test-split-at sched '(8 . 1)
152 '((10 . 0) (10 . 10) (11 . 0)))
153 (test-split-at sched 9
155 '((10 . 0) (10 . 10) (11 . 0)))
156 (test-split-at sched 10
158 '((10 . 10) (11 . 0)))
159 (test-split-at sched 9000
160 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
162 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
163 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
166 ;; Break off half of those and do some tests on them
167 (define some-extracted
168 (schedule-extract-until! sched 10))
169 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
170 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
171 (define first-extracted-queue
172 (time-segment-queue (car some-extracted)))
173 (define second-extracted-queue
174 (time-segment-queue (cadr some-extracted)))
175 (test-assert (not (q-empty? first-extracted-queue)))
176 (test-equal ((deq! first-extracted-queue)) 'd)
177 (test-assert (q-empty? first-extracted-queue))
179 (test-assert (not (q-empty? second-extracted-queue)))
180 (test-equal ((deq! second-extracted-queue)) 'a)
181 (test-equal ((deq! second-extracted-queue)) 'b)
182 (test-assert (q-empty? second-extracted-queue))
184 ;; Add one more and test flattening to a queue
185 (test-assert (not (schedule-empty? sched)))
186 (schedule-add! sched '(10 . 10) f-proc)
187 (define remaining-segments
188 (schedule-extract-until! sched '(9000 . 1)))
189 (test-assert (schedule-empty? sched))
190 (define some-queue (make-q))
191 (enq! some-queue (const 'ho-ho))
192 (enq! some-queue (const 'ha-ha))
193 (add-segments-contents-to-queue! remaining-segments some-queue)
194 (test-assert (not (q-empty? some-queue)))
195 (test-equal 'ho-ho ((deq! some-queue)))
196 (test-equal 'ha-ha ((deq! some-queue)))
197 (test-equal 'e ((deq! some-queue)))
198 (test-equal 'f ((deq! some-queue)))
199 (test-equal 'c ((deq! some-queue)))
200 (test-assert (q-empty? some-queue))
204 ;; Run/wrap request stuff
205 ;; ----------------------
207 (let ((wrapped (wrap (+ 1 2))))
208 (test-assert (procedure? wrapped))
209 (test-equal (wrapped) 3))
211 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
212 (test-assert (run-request? run-two-squared))
213 (test-assert (procedure? (run-request-proc run-two-squared)))
214 (test-equal ((run-request-proc run-two-squared)) 4)
215 (test-eq (run-request-when run-two-squared) #f))
217 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
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-equal (run-request-when run-two-squared) '(88 . 0)))
223 (let ((run-two-squared (run (* 2 2))))
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-eq (run-request-when run-two-squared) #f))
229 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
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-equal (run-request-when run-two-squared) '(88 . 0)))
236 ;;; %run, %sync and friends tests
237 ;;; -----------------------------
239 (define (test-%run-and-friends async-request expected-when)
240 (let* ((fake-kont (speak-it))
241 (run-request ((@@ (eightsync agenda) setup-async-request)
242 fake-kont async-request)))
243 (test-equal (car async-request) '*async-request*)
244 (test-equal (run-request-when run-request) expected-when)
245 ;; we're using speaker as a fake continuation ;p
246 ((run-request-proc run-request))
247 (test-equal (fake-kont)
250 (test-%run-and-friends (%run (string-concatenate '("apple" "sauce")))
253 (test-%run-and-friends (%run-at (string-concatenate '("apple" "sauce"))
257 (test-%run-and-friends (%run-delay (string-concatenate '("apple" "sauce"))
259 ;; whoa, I'm surprised equal? can
260 ;; compare records like this
263 ;; TODO: test %port-request
264 ;; TODO: test %sync and friends!
272 (define (true-after-n-times n)
275 (set! count (+ count 1))
276 (if (>= count n) #t #f))))
280 (define speaker (speak-it))
283 (speaker "I'm a dummy\n"))
286 (speaker "I bet I can make you say you're a dummy!\n")
290 (set! speaker (speak-it)) ; reset the speaker
292 (start-agenda (make-agenda #:queue q)
293 #:stop-condition (true-after-n-times 2))
294 (test-equal (speaker)
295 '("I bet I can make you say you're a dummy!\n"
298 ;; should only do the first one after one round though
300 (set! speaker (speak-it)) ; reset the speaker
302 (start-agenda (make-agenda #:queue q)
303 #:stop-condition (true-after-n-times 1))
304 (test-equal (speaker)
305 '("I bet I can make you say you're a dummy!\n")))
307 ;; delimited continuation tests
309 (define (return-monkey)
310 (speaker "(Hint, it's a monkey...)\n")
313 (define (talk-about-the-zoo)
314 (speaker "Today I went to the zoo and I saw...\n")
317 `("A " ,(symbol->string (%sync (%run (return-monkey)))) "!\n"))))
320 (set! speaker (speak-it))
321 (enq! q talk-about-the-zoo)
322 ;; (enq! q talk-about-the-zoo-but-wait)
323 (start-agenda (make-agenda #:queue q)
324 #:stop-condition (true-after-n-times 10))
325 (test-equal (speaker)
326 '("Today I went to the zoo and I saw...\n"
327 "(Hint, it's a monkey...)\n"
332 (test-end "tests-agenda")