Debian Codegraph @

test/include/srfi-235-test.scm

raw
1 (cond-expand
2 (guile
3 (import (scheme base)
4 (srfi 235)
5 (srfi srfi-1)
6 (srfi srfi-64)))
7 (chibi
8 (import (scheme base)
9 (srfi 235)
10 (srfi 1)
11 (rename (except (chibi test) test-equal)
12 (test test-equal))))
13 (else
14 (import (scheme base)
15 (srfi 235)
16 (srfi 1)
17 (srfi 64))))
18
19
20 (test-begin "Combinators")
21
22 (test-group
23 "constantly"
24
25 (test-equal '(1 2)
26 (call-with-values
27 (lambda () ((constantly 1 2) 'a 'b))
28 list))
29
30 (test-equal '(1)
31 (call-with-values
32 (lambda () ((constantly 1) 'a 'b))
33 list))
34
35 (test-equal '()
36 (call-with-values
37 (lambda () ((constantly) 'a 'b))
38 list)))
39
40
41
42 (test-group
43 "complement"
44
45 (test-equal #f
46 ((complement symbol?) 'a))
47
48 (test-equal #t
49 ((complement symbol?) 1)))
50
51
52
53 (test-group
54 "swap"
55
56 (test-equal '(2 1 3 4)
57 ((swap list) 1 2 3 4)))
58
59
60
61 (test-group
62 "flip"
63
64 (test-equal '(4 3 2 1)
65 ((flip list) 1 2 3 4)))
66
67
68
69 (test-group
70 "on-left"
71
72 (test-equal '(1)
73 ((on-left list) 1 2)))
74
75
76
77 (test-group
78 "on-right"
79
80 (test-equal '(2)
81 ((on-right list) 1 2)))
82
83
84
85 (test-group
86 "conjoin"
87
88 (test-assert
89 ((conjoin number? exact?)))
90
91 (test-assert
92 ((conjoin number? exact?) 1 2))
93
94 (test-assert
95 (not ((conjoin number? exact?) 1 2.)))
96
97 (test-assert
98 ((conjoin) 1 2)))
99
100
101
102 (test-group
103 "disjoin"
104
105 (test-assert
106 ((disjoin number? string?)))
107
108 (test-assert
109 ((disjoin number? string?) 1 "a"))
110
111 (test-assert
112 (not ((disjoin number? string?) 'a 'b)))
113
114 (test-assert
115 (not ((disjoin) 1 2))))
116
117
118
119 (test-group
120 "each-of"
121
122 (let ((r1 #f)
123 (r2 #f))
124 ((each-of
125 (lambda args (set! r1 args))
126 (lambda args (set! r2 args)))
127 1 2)
128 (test-equal r1 '(1 2))
129 (test-equal r2 '(1 2))))
130
131
132
133 (test-group
134 "all-of"
135
136 (test-assert
137 ((all-of string?) '()))
138
139 (test-assert
140 ((all-of string?) '("a" "b")))
141
142 (test-equal
143 "b"
144 ((all-of values) '("a" "b")))
145
146 (test-assert
147 (not ((all-of string?) '("a" b))))
148
149 (test-assert
150 (not ((all-of (lambda (x)
151 (when (equal? x 'c)
152 ;; should short circuit before this point
153 (test-assert #f))
154 (string? x)))
155 '("a" b c)))))
156
157
158
159 (test-group
160 "any-of"
161
162 (test-assert
163 (not ((any-of string?) '())))
164
165 (test-assert
166 ((any-of string?) '("a" b)))
167
168 (test-equal
169 "a"
170 ((any-of values) '("a" "b")))
171
172 (test-assert
173 (not ((any-of string?) '(a b))))
174
175 (test-assert
176 ((any-of (lambda (x)
177 (when (equal? x 'b)
178 ;; should short circuit before this point
179 (test-assert #f))
180 (string? x)))
181 '("a" b))))
182
183
184
185 (test-group
186 "on"
187
188 (test-equal '(2 3 4)
189 ((on list (lambda (x) (+ 1 x))) 1 2 3)))
190
191
192
193 (test-group
194 "left-section"
195
196 (test-equal '(1 2 3 4)
197 ((left-section list 1 2) 3 4)))
198
199
200
201 (test-group
202 "right-section"
203
204 (test-equal '(3 4 2 1)
205 ((right-section list 1 2) 3 4)))
206
207
208
209 (test-group
210 "apply-chain"
211 (define cadr* (apply-chain car cdr))
212 (define factorial ;;test multivalue
213 (apply-chain
214 *
215 (lambda (n) (apply values (cdr (iota (+ 1 n)))))))
216
217 (test-equal 2 (cadr* (list 1 2 3)))
218 (test-equal 120 (factorial 5)))
219
220
221
222 (test-group
223 "arguments-drop"
224
225 (test-equal
226 '(4)
227 ((arguments-drop list 3) 1 2 3 4)))
228
229
230
231 (test-group
232 "arguments-drop-right"
233
234 (test-equal
235 '(1)
236 ((arguments-drop-right list 3) 1 2 3 4)))
237
238
239
240 (test-group
241 "arguments-take"
242
243 (test-equal
244 '(1 2 3)
245 ((arguments-take list 3) 1 2 3 4)))
246
247
248
249 (test-group
250 "arguments-take-right"
251
252 (test-equal
253 '(2 3 4)
254 ((arguments-take-right list 3) 1 2 3 4)))
255
256
257 (test-group
258 "group-by"
259
260 (test-equal
261 '((1 3)
262 (2 4))
263 ((group-by odd?) '(1 2 3 4)))
264
265 (test-equal
266 '(("aa" "ab")
267 ("ba" "bb"))
268 ((group-by (lambda (str) (string-ref str 0))
269 char=?)
270 (list "aa" "ba" "bb" "ab"))))
271
272
273
274
275 (test-group
276 "begin-procedure"
277
278 (test-equal 2
279 (begin-procedure
280 (lambda () 1)
281 (lambda () 2))))
282
283
284
285 (test-group
286 "if-procedure"
287
288 (test-equal 1
289 (if-procedure #t
290 (lambda () 1)
291 (lambda () (test-assert #f))))
292
293 (test-equal 2
294 (if-procedure #f
295 (lambda () (test-assert #f))
296 (lambda () 2))))
297
298
299
300 (test-group
301 "when-procedure"
302
303 (define lst1 '())
304 (define lst2 '())
305
306 (when-procedure #t
307 (lambda () (set! lst1 (cons 1 lst1)))
308 (lambda () (set! lst1 (cons 2 lst1))))
309
310 (when-procedure #f
311 (lambda () (set! lst2 (cons 1 lst2)))
312 (lambda () (set! lst2 (cons 2 lst2))))
313
314 (test-equal '(2 1) lst1)
315 (test-equal '() lst2))
316
317
318
319 (test-group
320 "unless-procedure"
321
322 (define lst1 '())
323 (define lst2 '())
324
325 (unless-procedure #t
326 (lambda () (set! lst1 (cons 1 lst1)))
327 (lambda () (set! lst1 (cons 2 lst1))))
328
329 (unless-procedure #f
330 (lambda () (set! lst2 (cons 1 lst2)))
331 (lambda () (set! lst2 (cons 2 lst2))))
332
333 (test-equal '() lst1)
334 (test-equal '(2 1) lst2))
335
336
337
338 (test-group
339 "value-procedure"
340
341 (test-equal "1"
342 (value-procedure 1
343 number->string
344 (lambda () (test-assert #f))))
345
346 (test-equal 2
347 (value-procedure #f
348 (lambda args (test-assert #f))
349 (lambda () 2))))
350
351
352
353 (test-group
354 "case-procedure"
355
356 (test-equal 2
357 (case-procedure 'b
358 `((a . ,(lambda () 1))
359 (b . ,(lambda () 2)))))
360
361 (test-equal 3
362 (case-procedure 'c
363 `((a . ,(lambda () 1))
364 (b . ,(lambda () 2)))
365 (lambda () 3))))
366
367
368
369 (test-group
370 "and-procedure"
371
372 (test-assert
373 (and-procedure))
374
375 (test-equal 2
376 (and-procedure (lambda () 1)
377 (lambda () 2)))
378
379 (test-assert
380 (not (and-procedure (lambda () #f)
381 (lambda () (test-assert #f))))))
382
383
384
385 (test-group
386 "eager-and-procedure"
387
388 (test-assert
389 (eager-and-procedure))
390
391 (test-equal 2
392 (eager-and-procedure (lambda () 1)
393 (lambda () 2)))
394
395 (let ((second-called? #f))
396 (test-assert
397 (not (eager-and-procedure (lambda () #f)
398 (lambda ()
399 (set! second-called? #t)
400 #t))))
401 (test-assert second-called?)))
402
403
404
405 (test-group
406 "or-procedure"
407
408 (test-assert
409 (not (or-procedure)))
410
411 (test-equal 2
412 (or-procedure (lambda () #f)
413 (lambda () 2)))
414
415 (test-assert
416 (or-procedure (lambda () 1)
417 (lambda () (test-assert #f)))))
418
419
420
421 (test-group
422 "eager-or-procedure"
423
424 (test-assert
425 (not (eager-or-procedure)))
426
427 (test-equal 2
428 (eager-or-procedure (lambda () #f)
429 (lambda () 2)))
430
431 (let ((second-called? #f))
432 (test-equal 1
433 (eager-or-procedure (lambda () 1)
434 (lambda ()
435 (set! second-called? #t)
436 #f)))
437 (test-assert second-called?)))
438
439 (test-group
440 "funcall-procedure"
441
442 (test-equal 1
443 (funcall-procedure (lambda () 1))))
444
445 (test-group
446 "loop-procedure"
447
448 (call/cc (lambda (k)
449 (define v 0)
450 (define (thunk)
451 (when (> v 5)
452 (k #t))
453 (set! v (+ 1 v)))
454 (loop-procedure thunk)
455 (test-assert #t))))
456
457
458
459 (test-group
460 "while-procedure"
461
462 (define v 0)
463 (define (thunk)
464 (set! v (+ 1 v))
465 (< v 5))
466 (while-procedure thunk)
467 (test-equal 5 v))
468
469
470
471 (test-group
472 "until-procedure"
473
474 (define v 0)
475 (define (thunk)
476 (set! v (+ 1 v))
477 (>= v 5))
478 (until-procedure thunk)
479 (test-equal 5 v))
480
481
482 (test-group
483 "always"
484
485 (test-assert (always))
486 (test-assert (always 'a)))
487
488
489
490 (test-group
491 "never"
492
493 (test-assert (not (never)))
494 (test-assert (not (never 'a))))
495
496
497
498 (test-group
499 "boolean"
500
501 (test-equal #t (boolean 1))
502 (test-equal #f (boolean #f)))
503
504
505
506 (test-group
507 "values"
508
509 (test-equal 1 (values 1))
510 (test-equal 'a (values 'a)))
511
512
513 (test-end)