aboutsummaryrefslogtreecommitdiff
path: root/tests/run.scm
blob: ed2cd8ffe3c9df3eba3e75ab5518bbe60cf1e611 (about) (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
(import (chicken load) (test))

(test-begin "algebraic-structures")

(import (prefix (algebraic-structures monoid list) list:))

(import (only (algebraic-structures monoid make fold)))
(import (prefix (algebraic-structures foldable list) list:))
(import (prefix (algebraic-structures foldable vector) vector:))
(import (prefix (algebraic-structures monoid number sum) sum:))
(import (prefix (algebraic-structures monoid number product) product:))

(import (prefix (algebraic-structures functor list) list:))
(import (prefix (algebraic-structures applicative list) list:))
(import (prefix (algebraic-structures monad list) list:))
(import (prefix (algebraic-structures alternative list) list:))

(import (prefix (only (scheme) apply) list:))

(test-begin "foldable")

(test-begin "foldable.list")

(test '(a b c d e) (list:foldr cons '() '(a b c d e)))
(test '(((((() a) b) c) d) e) (list:foldl list '() '(a b c d e)))

(test 0 (list:length '()))
(test 5 (list:length '(a b c d e)))

(test #f (list:find (constantly #t) '()))
(test #f (list:find even? '(1 3 5 7)))
(test 4 (list:find even? '(1 3 4 7 8)))

(test #f (list:any (constantly #t) '()))
(test #f (list:any (cut member 'x <>) '((a b c) (d e f))))
(test '(x f) (list:any (cut member 'x <>) '((a b c) (d x f))))

(test #t (list:every (constantly #f) '()))
(test #f (list:every (cut member 'x <>) '((a b c) (d x f))))
(test '(x f) (list:every (cut member 'x <>) '((a x c) (d x f))))


(test '(a b c d e) (list:->list '(a b c d e)))

(test-end "foldable.list")

(test-begin "foldable.vector")

(test '(a b c d e) (vector:foldr cons '() #(a b c d e)))
(test '(((((() a) b) c) d) e) (vector:foldl list '() #(a b c d e)))

(test 0 (vector:length #()))
(test 5 (vector:length #(a b c d e)))

(test #f (vector:find (constantly #t) #()))
(test #f (vector:find even? #(1 3 5 7)))
(test 4 (vector:find even? #(1 3 4 7 8)))

(test #f (vector:any (constantly #t) #()))
(test #f (vector:any (cut member 'x <>) #((a b c) (d e f))))
(test '(x f) (vector:any (cut member 'x <>) #((a b c) (d x f))))

(test #t (vector:every (constantly #f) #()))
(test #f (vector:every (cut member 'x <>) #((a b c) (d x f))))
(test '(x f) (vector:every (cut member 'x <>) #((a x c) (d x f))))

(test '(a b c d e) (vector:->list #(a b c d e)))

(test-end "foldable.vector")

(test-end "foldable")

(test-begin "monoid")

(test-begin "monoid.list")

(test '(a b c 1 2 3) (list:<> '(a b c) '(1 2 3)))
(test '(a b c x y z 1 2 3) (list:<> (list:<> '(a b c) '(x y z)) '(1 2 3)))
(test '(a b c x y z 1 2 3) (list:<> '(a b c) (list:<> '(x y z) '(1 2 3))))

(test-end "monoid.list")

(test-begin "monoid.sum.fold.list")

(module sum-fold = ((algebraic-structures monoid make fold)
                    (algebraic-structures monoid number sum)
                    (algebraic-structures foldable list)))
(import (prefix sum-fold sum:))

(test 15 (sum:fold '(1 2 3 4 5)))
(test 0 (sum:fold '()))


(test-end "monoid.sum.fold.list")

(test-begin "monoid.product.fold.vector")

(module product-fold = ((algebraic-structures monoid make fold)
                        (algebraic-structures monoid number product)
                        (algebraic-structures foldable vector)))
(import (prefix product-fold product:))

(test 120 (product:fold #(1 2 3 4 5)))
(test 1 (product:fold #()))

(test-end "monoid.product.fold.vector")

(test-end "monoid")

(test-begin "functor")

(test '((a) (b) (c)) (list:map list '(a b c)))

(test-end "functor")

(test-begin "applicative")

(test '(a) (list:pure 'a))

(test '((a 1) (a 2) (b 1) (b 2) (c 1) (c 2))
      (list:map2 list '(a b c) '(1 2)))

(test '((a 1 z) (a 2 z) (b 1 z) (b 2 z) (c 1 z) (c 2 z))
      (list:map* list '(a b c) '(1 2) '(z)))

(test-end "applicative")

(test-begin "monad")

(test '((1 a) (2 a))
      (list:>>= (list:pure 'a)
                (lambda (x)
                  (list (list 1 x)
                        (list 2 x)))))

(test '(210 330 350 550)
      (list:do (x <- '(3 5))
               (let y = 10)
               (z <- '(7 11))
               (list 2)
               (list:pure (* x y z))))

(test-end "monad")

(test-begin "alternative")

(test '(9 25)
      (list:do (x <- '(2 3 4 5))
               (list:guard (odd? x))
               (list:pure (* x x))))

(test-end "alternative")

(test-end "algebraic-structures")

(test-exit)