1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;; Copyright (C) 2021 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 3 of the License, or (at your option) any later version.
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 (define-module (tests test-agenda)
21 #:use-module (srfi srfi-64)
22 #:use-module (ice-9 q)
23 #:use-module (ice-9 match)
24 #:use-module (ice-9 receive)
25 #:use-module (8sync agenda)
26 #:use-module (tests utils))
28 (test-begin "test-agenda")
30 (define-syntax-rule (%import var)
32 (@@ (8sync agenda) var)))
38 (define test-q (list->q '(1 2 3)))
39 (test-equal (deq! test-q) 1)
40 (test-equal (deq! test-q) 2)
41 (test-equal (deq! test-q) 3)
42 (test-assert (q-empty? test-q))
44 (define test-q (make-q* 'apple 'banana 'carrot))
45 (test-equal (deq! test-q) 'apple)
46 (test-equal (deq! test-q) 'banana)
47 (test-equal (deq! test-q) 'carrot)
48 (test-assert (q-empty? test-q))
60 (test-assert (time= '(1 . 1) '(1 . 1)))
61 (test-assert (not (time= '(1 . 1) '(1 . 0))))
62 (test-assert (not (time= '(0 . 1) '(1 . 1))))
64 (test-assert (time< '(1 . 1) '(1 . 2)))
65 (test-assert (time< '(7 . 2) '(8 . 2)))
66 (test-assert (not (time< '(7 . 2) '(7 . 2))))
67 (test-assert (not (time< '(7 . 8) '(7 . 2))))
68 (test-assert (not (time< '(8 . 2) '(7 . 2))))
70 (test-equal (time-minus '(100 . 100) '(50 . 66))
72 (test-equal (time-minus '(2 . 0) '(0 . 1))
75 (test-equal (time-plus '(50 . 34) '(50 . 66))
77 (test-equal (time-plus '(1 . 999999) '(1 . 2))
85 (%import time-segment-time)
86 (%import time-segment-queue)
89 (define (assert-times-expected time-segments expected-times)
90 (test-equal (map time-segment-time time-segments)
93 (define a-proc (const 'a))
94 (define b-proc (const 'b))
95 (define c-proc (const 'c))
96 (define d-proc (const 'd))
97 (define e-proc (const 'e))
98 (define f-proc (const 'f))
100 (define sched (make-schedule))
101 (test-assert (schedule-empty? sched))
103 ;; Add a segment at (10 . 0)
104 (schedule-add! sched '(10 . 0) a-proc)
105 (test-assert (not (schedule-empty? sched)))
106 (test-equal (length (schedule-segments sched)) 1)
107 (test-equal (time-segment-time (car (schedule-segments sched)))
109 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
111 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
113 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
115 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
117 (assert-times-expected (schedule-segments sched)
120 ;; Add another segment at (10 . 0)
121 (schedule-add! sched '(10 . 0) b-proc)
122 (test-assert (not (schedule-empty? sched)))
123 (test-equal (length (schedule-segments sched)) 1)
124 (test-equal (time-segment-time (car (schedule-segments sched)))
126 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
128 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
130 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
132 (assert-times-expected (schedule-segments sched)
135 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
136 (schedule-add! sched '(11 . 0) c-proc)
137 (schedule-add! sched '(8 . 1) d-proc)
138 (schedule-add! sched '(10 . 10) e-proc)
139 (test-assert (not (schedule-empty? sched)))
140 (test-equal (length (schedule-segments sched)) 4)
141 (assert-times-expected (schedule-segments sched)
142 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
145 (define (test-split-at schedule time expected-before expected-after)
146 (receive (segments-before segments-after)
147 (schedule-segments-split schedule time)
148 (assert-times-expected segments-before expected-before)
149 (assert-times-expected segments-after expected-after)))
151 (test-split-at sched '(0 . 0)
153 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
154 (test-split-at sched '(8 . 0)
156 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
157 (test-split-at sched '(8 . 1)
159 '((10 . 0) (10 . 10) (11 . 0)))
160 (test-split-at sched '(9 . 0)
162 '((10 . 0) (10 . 10) (11 . 0)))
163 (test-split-at sched '(10 . 0)
165 '((10 . 10) (11 . 0)))
166 (test-split-at sched '(9000 . 0)
167 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
169 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
170 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
173 ;; Break off half of those and do some tests on them
174 (define some-extracted
175 (schedule-extract-until! sched '(10 . 0)))
176 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
177 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
178 (define first-extracted-queue
179 (time-segment-queue (car some-extracted)))
180 (define second-extracted-queue
181 (time-segment-queue (cadr some-extracted)))
182 (test-assert (not (q-empty? first-extracted-queue)))
183 (test-equal ((deq! first-extracted-queue)) 'd)
184 (test-assert (q-empty? first-extracted-queue))
186 (test-assert (not (q-empty? second-extracted-queue)))
187 (test-equal ((deq! second-extracted-queue)) 'a)
188 (test-equal ((deq! second-extracted-queue)) 'b)
189 (test-assert (q-empty? second-extracted-queue))
191 ;; Add one more and test flattening to a queue
192 (test-assert (not (schedule-empty? sched)))
193 (schedule-add! sched '(10 . 10) f-proc)
194 (define remaining-segments
195 (schedule-extract-until! sched '(9000 . 1)))
196 (test-assert (schedule-empty? sched))
197 (define some-queue (make-q))
198 (enq! some-queue (const 'ho-ho))
199 (enq! some-queue (const 'ha-ha))
200 (add-segments-contents-to-queue! remaining-segments some-queue)
201 (test-assert (not (q-empty? some-queue)))
202 (test-equal 'ho-ho ((deq! some-queue)))
203 (test-equal 'ha-ha ((deq! some-queue)))
204 (test-equal 'e ((deq! some-queue)))
205 (test-equal 'f ((deq! some-queue)))
206 (test-equal 'c ((deq! some-queue)))
207 (test-assert (q-empty? some-queue))
211 ;;; Run/wrap request stuff
212 ;;; ======================
214 (let ((wrapped (wrap (+ 1 2))))
215 (test-assert (procedure? wrapped))
216 (test-equal (wrapped) 3))
218 (let ((run-two-squared (run-it (lambda () (* 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-it (lambda () (* 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)))
230 (let ((run-two-squared (run (* 2 2))))
231 (test-assert (run-request? run-two-squared))
232 (test-assert (procedure? (run-request-proc run-two-squared)))
233 (test-equal ((run-request-proc run-two-squared)) 4)
234 (test-eq (run-request-when run-two-squared) #f))
236 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
237 (test-assert (run-request? run-two-squared))
238 (test-assert (procedure? (run-request-proc run-two-squared)))
239 (test-equal ((run-request-proc run-two-squared)) 4)
240 (test-equal (run-request-when run-two-squared) '(88 . 0)))
243 ;;; %run, 8sync and friends tests
244 ;;; ==============================
246 ;; TODO: We need to rewrite the whole lot here...
253 (define (true-after-n-times n)
257 (if (>= count n) #t #f))
258 (set! count (+ count 1))
264 (speak "I'm a dummy\n"))
267 (speak "I bet I can make you say you're a dummy!\n")
271 (run-agenda (make-agenda #:queue (make-q* run-dummy))
272 #:stop-condition (true-after-n-times 2))
273 (test-equal (get-spoken)
274 '("I bet I can make you say you're a dummy!\n"
277 ;; should only do the first one after one round though
279 (run-agenda (make-agenda #:queue (make-q* run-dummy))
280 #:stop-condition (true-after-n-times 1))
281 (test-equal (get-spoken)
282 '("I bet I can make you say you're a dummy!\n")))
287 ;; @@: A better way to handle this at the repl?
289 (test-end "test-agenda")