forked from mighty-gerbils/gerbil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter-test.ss
91 lines (74 loc) · 2.57 KB
/
iter-test.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
;;; -*- Gerbil -*-
;;; (C) vyzo at hackzen.org
;;; :std/iter unit-tests
(import :std/test
:std/iter)
(export iter-test)
(def (my-generator n)
(lambda ()
(let lp ((k 0))
(when (< k n)
(yield k)
(lp (1+ k))))))
(def iter-test
(test-suite "test :std/iter library"
(test-case "test imperative for"
(def (test-for-1)
(for (x '(1 2 3))
(displayln x)))
(check-output (test-for-1) "1\n2\n3\n")
(def (test-for-2)
(for ((x '(1 2 3))
(y '#(a b c d)))
(displayln x " " y)))
(check-output (test-for-2) "1 a\n2 b\n3 c\n")
(def (test-for-3)
(for (x (in-range 3))
(displayln x)))
(check-output (test-for-3) "0\n1\n2\n")
(def (test-for-4)
(for (x (in-range 3 3))
(displayln x)))
(check-output (test-for-4) "3\n4\n5\n")
(def (test-for-5)
(for (x (in-range 3 3 2))
(displayln x)))
(check-output (test-for-5) "3\n5\n7\n")
(def (test-for-6)
(for (x (my-generator 3))
(displayln x)))
(check-output (test-for-6) "0\n1\n2\n"))
(test-case "test folding macros"
(def (test-for/collect-1)
(for/collect ((x '(1 2 3))
(y '#(a b c d)))
(cons x y)))
(check (test-for/collect-1) => '((1 . a) (2 . b) (3 . c)))
(def (test-for/collect-2)
(for/collect ((x (in-naturals 1))
(y '#(a b c d)))
(cons x y)))
(check (test-for/collect-2) => '((1 . a) (2 . b) (3 . c) (4 . d)))
(def (test-for/collect-3)
(for/collect (x (my-generator 3)) x))
(check (test-for/collect-3) => '(0 1 2))
(def (test-for/fold)
(for/fold (r []) ((x '(1 2 3)) (y '#(a b c d)))
(cons* x y r)))
(check (test-for/fold) => '(3 c 2 b 1 a)))
(test-case "test iter xforms"
(def (test-xform-when)
(for/collect (x (in-range 5) when (odd? x)) x))
(check (test-xform-when) => '(1 3))
(def (test-xform-unless)
(for/collect (x (in-range 5) unless (odd? x)) x))
(check (test-xform-unless) => '(0 2 4))
(def (test-xform-filter)
(for/collect (x (iter-filter odd? (in-range 5))) x))
(check (test-xform-filter) => '(1 3))
(def (test-xform-map)
(for/collect (x (iter-map (cut * <> 2) (in-range 5))) x))
(check (test-xform-map) => '(0 2 4 6 8))
(def (test-xform-filter-map)
(for/collect (x (iter-filter-map (lambda (x) (and (odd? x) (* x 2))) (in-range 5))) x))
(check (test-xform-filter-map) => '(2 6)))))