guix: Use guile-3.0.
[8sync.git] / tests / test-agenda.scm
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>
4 ;;;
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.
9 ;;;
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.
14 ;;;
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
18 ;;; 02110-1301 USA
19
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))
27
28 (test-begin "test-agenda")
29
30 (define-syntax-rule (%import var)
31   (define var
32     (@@ (8sync agenda) var)))
33
34 \f
35 ;;; queue helpers
36 ;;; =============
37
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))
43
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))
49
50
51 \f
52 ;;; Timer tests
53 ;;; ===========
54
55 (%import time=)
56 (%import time<)
57 (%import time-minus)
58 (%import time-plus)
59
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))))
63
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))))
69
70 (test-equal (time-minus '(100 . 100) '(50 . 66))
71             '(50 . 34))
72 (test-equal (time-minus '(2 . 0) '(0 . 1))
73             '(1 . 999999))
74
75 (test-equal (time-plus '(50 . 34) '(50 . 66))
76             '(100 . 100))
77 (test-equal (time-plus '(1 . 999999) '(1 . 2))
78             '(3 . 1))
79
80
81 \f
82 ;;; Schedule tests
83 ;;; ==============
84
85 (%import time-segment-time)
86 (%import time-segment-queue)
87
88 ;; helpers
89 (define (assert-times-expected time-segments expected-times)
90   (test-equal (map time-segment-time time-segments)
91     expected-times))
92
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))
99
100 (define sched (make-schedule))
101 (test-assert (schedule-empty? sched))
102
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)))
108   '(10 . 0))
109 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
110   1)
111 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
112   a-proc)
113 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
114   a-proc)
115 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
116   'a) ;; why not
117 (assert-times-expected (schedule-segments sched)
118                        '((10 . 0)))
119
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)))
125   '(10 . 0))
126 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
127   2)
128 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
129   a-proc)
130 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
131   b-proc)
132 (assert-times-expected (schedule-segments sched)
133                        '((10 . 0)))
134
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)))
143
144 ;; Splitting 
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)))
150
151 (test-split-at sched '(0 . 0)
152                '()
153                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
154 (test-split-at sched '(8 . 0)
155                '()
156                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
157 (test-split-at sched '(8 . 1)
158                '((8 . 1))
159                '((10 . 0) (10 . 10) (11 . 0)))
160 (test-split-at sched '(9 . 0)
161                '((8 . 1))
162                '((10 . 0) (10 . 10) (11 . 0)))
163 (test-split-at sched '(10 . 0)
164                '((8 . 1) (10 . 0))
165                '((10 . 10) (11 . 0)))
166 (test-split-at sched '(9000 . 0)
167                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
168                '())
169 (test-split-at sched '(9000 . 1)    ; over nine thousaaaaaaand
170                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
171                '())
172
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))
185
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))
190
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))
208
209 ;; ... whew!
210
211 ;;; Run/wrap request stuff
212 ;;; ======================
213
214 (let ((wrapped (wrap (+ 1 2))))
215   (test-assert (procedure? wrapped))
216   (test-equal (wrapped) 3))
217
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))
223
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)))
229
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))
235
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)))
241
242
243 ;;; %run, 8sync and friends tests
244 ;;; ==============================
245
246 ;; TODO: We need to rewrite the whole lot here...
247
248 ;;; Agenda tests
249 ;;; ============
250
251 ;; helpers
252
253 (define (true-after-n-times n)
254   (let ((count 0))
255     (lambda _
256       (define ans
257         (if (>= count n) #t #f))
258       (set! count (+ count 1))
259       ans)))
260
261 ;; the dummy test
262
263 (define (dummy-func)
264   (speak "I'm a dummy\n"))
265
266 (define (run-dummy)
267   (speak "I bet I can make you say you're a dummy!\n")
268   (run-it dummy-func))
269
270 (with-fresh-speaker
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"
275      "I'm a dummy\n")))
276
277 ;; should only do the first one after one round though
278 (with-fresh-speaker
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")))
283
284
285 ;; End tests
286
287 ;; @@: A better way to handle this at the repl?
288 (test-exit)
289 (test-end "test-agenda")