1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
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.
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.
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
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))
27 (test-begin "test-agenda")
29 (define-syntax-rule (%import var)
31 (@@ (8sync agenda) var)))
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))
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))
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))))
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))))
69 (test-equal (time-minus '(100 . 100) '(50 . 66))
71 (test-equal (time-minus '(2 . 0) '(0 . 1))
74 (test-equal (time-plus '(50 . 34) '(50 . 66))
76 (test-equal (time-plus '(1 . 999999) '(1 . 2))
84 (%import time-segment-time)
85 (%import time-segment-queue)
88 (define (assert-times-expected time-segments expected-times)
89 (test-equal (map time-segment-time time-segments)
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))
99 (define sched (make-schedule))
100 (test-assert (schedule-empty? sched))
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)))
108 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
110 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
112 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
114 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
116 (assert-times-expected (schedule-segments sched)
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)))
125 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
127 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
129 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
131 (assert-times-expected (schedule-segments sched)
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)))
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)))
150 (test-split-at sched '(0 . 0)
152 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
153 (test-split-at sched '(8 . 0)
155 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
156 (test-split-at sched '(8 . 1)
158 '((10 . 0) (10 . 10) (11 . 0)))
159 (test-split-at sched '(9 . 0)
161 '((10 . 0) (10 . 10) (11 . 0)))
162 (test-split-at sched '(10 . 0)
164 '((10 . 10) (11 . 0)))
165 (test-split-at sched '(9000 . 0)
166 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
168 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
169 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
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))
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))
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))
210 ;;; Run/wrap request stuff
211 ;;; ======================
213 (let ((wrapped (wrap (+ 1 2))))
214 (test-assert (procedure? wrapped))
215 (test-equal (wrapped) 3))
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))
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)))
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))
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)))
242 ;;; %run, 8sync and friends tests
243 ;;; ==============================
245 ;; TODO: We need to rewrite the whole lot here...
252 (define (true-after-n-times n)
256 (if (>= count n) #t #f))
257 (set! count (+ count 1))
263 (speak "I'm a dummy\n"))
266 (speak "I bet I can make you say you're a dummy!\n")
270 (run-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"
276 ;; should only do the first one after one round though
278 (run-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")))
286 (test-end "test-agenda")
288 ;; @@: A better way to handle this at the repl?