5 (define-module (tests test-core)
6 #:use-module (srfi srfi-64)
8 #:use-module (ice-9 receive)
9 #:use-module (loopy agenda))
17 (test-assert (time= '(1 . 1) '(1 . 1)))
18 (test-assert (not (time= '(1 . 1) '(1 . 0))))
19 (test-assert (not (time= '(0 . 1) '(1 . 1))))
21 (test-assert (time< '(1 . 1) '(1 . 2)))
22 (test-assert (time< '(7 . 2) '(8 . 2)))
23 (test-assert (not (time< '(7 . 2) '(7 . 2))))
24 (test-assert (not (time< '(7 . 8) '(7 . 2))))
25 (test-assert (not (time< '(8 . 2) '(7 . 2))))
27 (let ((tdelta (make-time-delta 8)))
28 (test-assert (time-delta? tdelta))
29 (test-eqv (time-delta-sec tdelta) 8)
30 (test-eqv (time-delta-usec tdelta) 0)
32 (time-delta+ '(2 . 3) tdelta)
35 (let ((tdelta (make-time-delta 10 1)))
36 (test-assert (time-delta? tdelta))
37 (test-eqv (time-delta-sec tdelta) 10)
38 (test-eqv (time-delta-usec tdelta) 1)
40 (time-delta+ '(2 . 3) tdelta)
49 (define (assert-times-expected time-segments expected-times)
50 (test-equal (map time-segment-time time-segments)
53 (define a-proc (const 'a))
54 (define b-proc (const 'b))
55 (define c-proc (const 'c))
56 (define d-proc (const 'd))
57 (define e-proc (const 'e))
58 (define f-proc (const 'f))
60 (define sched (make-schedule))
61 (test-assert (schedule-empty? sched))
63 ;; Add a segment at (10 . 0)
64 (schedule-add! sched 10 a-proc)
65 (test-assert (not (schedule-empty? sched)))
66 (test-equal (length (schedule-segments sched)) 1)
67 (test-equal (time-segment-time (car (schedule-segments sched)))
69 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
71 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
73 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
75 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
77 (assert-times-expected (schedule-segments sched)
80 ;; Add another segment at (10 . 0)
81 (schedule-add! sched '(10 . 0) b-proc)
82 (test-assert (not (schedule-empty? sched)))
83 (test-equal (length (schedule-segments sched)) 1)
84 (test-equal (time-segment-time (car (schedule-segments sched)))
86 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
88 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
90 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
92 (assert-times-expected (schedule-segments sched)
95 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
96 (schedule-add! sched 11 c-proc)
97 (schedule-add! sched '(8 . 1) d-proc)
98 (schedule-add! sched '(10 . 10) e-proc)
99 (test-assert (not (schedule-empty? sched)))
100 (test-equal (length (schedule-segments sched)) 4)
101 (assert-times-expected (schedule-segments sched)
102 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
105 (define (test-split-at schedule time expected-before expected-after)
106 (receive (segments-before segments-after)
107 (schedule-segments-split schedule time)
108 (assert-times-expected segments-before expected-before)
109 (assert-times-expected segments-after expected-after)))
111 (test-split-at sched 0
113 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
114 (test-split-at sched '(8 . 0)
116 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
117 (test-split-at sched '(8 . 1)
119 '((10 . 0) (10 . 10) (11 . 0)))
120 (test-split-at sched 9
122 '((10 . 0) (10 . 10) (11 . 0)))
123 (test-split-at sched 10
125 '((10 . 10) (11 . 0)))
126 (test-split-at sched 9000
127 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
129 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
130 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
133 ;; Break off half of those and do some tests on them
134 (define some-extracted
135 (schedule-extract-until! sched 10))
136 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
137 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
138 (define first-extracted-queue
139 (time-segment-queue (car some-extracted)))
140 (define second-extracted-queue
141 (time-segment-queue (cadr some-extracted)))
142 (test-assert (not (q-empty? first-extracted-queue)))
143 (test-equal ((deq! first-extracted-queue)) 'd)
144 (test-assert (q-empty? first-extracted-queue))
146 (test-assert (not (q-empty? second-extracted-queue)))
147 (test-equal ((deq! second-extracted-queue)) 'a)
148 (test-equal ((deq! second-extracted-queue)) 'b)
149 (test-assert (q-empty? second-extracted-queue))
151 ;; Add one more and test flattening to a queue
152 (test-assert (not (schedule-empty? sched)))
153 (schedule-add! sched '(10 . 10) f-proc)
154 (define remaining-segments
155 (schedule-extract-until! sched '(9000 . 1)))
156 (test-assert (schedule-empty? sched))
157 (define some-queue (make-q))
158 (enq! some-queue (const 'ho-ho))
159 (enq! some-queue (const 'ha-ha))
160 (add-segments-contents-to-queue! remaining-segments some-queue)
161 (test-assert (not (q-empty? some-queue)))
162 (test-equal 'ho-ho ((deq! some-queue)))
163 (test-equal 'ha-ha ((deq! some-queue)))
164 (test-equal 'e ((deq! some-queue)))
165 (test-equal 'f ((deq! some-queue)))
166 (test-equal 'c ((deq! some-queue)))
167 (test-assert (q-empty? some-queue))
171 ;; Run/wrap request stuff
172 ;; ----------------------
174 (let ((wrapped (wrap (+ 1 2))))
175 (test-assert (procedure? wrapped))
176 (test-equal (wrapped) 3))
178 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
179 (test-assert (run-request? run-two-squared))
180 (test-assert (procedure? (run-request-proc run-two-squared)))
181 (test-equal ((run-request-proc run-two-squared)) 4)
182 (test-eq (run-request-when run-two-squared) #f))
184 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
185 (test-assert (run-request? run-two-squared))
186 (test-assert (procedure? (run-request-proc run-two-squared)))
187 (test-equal ((run-request-proc run-two-squared)) 4)
188 (test-equal (run-request-when run-two-squared) '(88 . 0)))
190 (let ((run-two-squared (run (* 2 2))))
191 (test-assert (run-request? run-two-squared))
192 (test-assert (procedure? (run-request-proc run-two-squared)))
193 (test-equal ((run-request-proc run-two-squared)) 4)
194 (test-eq (run-request-when run-two-squared) #f))
196 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
197 (test-assert (run-request? run-two-squared))
198 (test-assert (procedure? (run-request-proc run-two-squared)))
199 (test-equal ((run-request-proc run-two-squared)) 4)
200 (test-equal (run-request-when run-two-squared) '(88 . 0)))
209 (let ((messages '()))
210 (lambda* (#:optional message)
211 (if message (set! messages (append messages (list message))))
214 (define (true-after-n-times n)
217 (set! count (+ count 1))
218 (if (>= count n) #t #f))))
222 (define speaker (speak-it))
225 (speaker "I'm a dummy\n"))
228 (speaker "I bet I can make you say you're a dummy!\n")
232 (set! speaker (speak-it)) ; reset the speaker
234 (start-agenda (make-agenda #:queue q)
235 #:stop-condition (true-after-n-times 2))
236 (test-equal (speaker)
237 '("I bet I can make you say you're a dummy!\n"
240 ;; should only do the first one after one round though
242 (set! speaker (speak-it)) ; reset the speaker
244 (start-agenda (make-agenda #:queue q)
245 #:stop-condition (true-after-n-times 1))
246 (test-equal (speaker)
247 '("I bet I can make you say you're a dummy!\n")))