]> gitweb.factorcode.org Git - factor.git/blob - extra/combinators/extras/extras-tests.factor
combinators.extras: adding sequence-case
[factor.git] / extra / combinators / extras / extras-tests.factor
1 ! Copyright (C) 2013 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types arrays assocs combinators.extras io.files
4 kernel math modern.slices parser ranges sequences splitting
5 tools.test ;
6 IN: combinators.extras.tests
7
8
9 { "a b" }
10 [ "a" "b" [ " " glue ] once ] unit-test
11
12 { "a b c" }
13 [ "a" "b" "c" [ " " glue ] twice ] unit-test
14
15 { "a b c d" }
16 [ "a" "b" "c" "d" [ " " glue ] thrice ] unit-test
17
18 { { "negative" 0 "positive" } } [
19     { -1 0 1 } [
20         {
21             { [ 0 > ] [ "positive" ] }
22             { [ 0 < ] [ "negative" ] }
23             [ ]
24         } cond-case
25     ] map
26 ] unit-test
27
28 <<
29 SYNTAX: ..= dup pop scan-object [a..b] suffix! ;
30 SYNTAX: ..< dup pop scan-object [a..b) suffix! ;
31 >>
32
33 <<
34 : describe-number ( n -- str )
35     {
36         { 0 [ "no" ] }
37         { 1 ..= 3 [ "a few" ] }
38         { 4 ..= 9 [ "several" ] }
39         { 12 [ "twelve" ] }
40         { 10 ..= 99 [ "tens of" ] }
41         { 100 ..= 999 [ "hundreds of" ] }
42         { 1000 ..= 999,999 [ "thousands of" ] }
43         [ drop "millions and millions of" ]
44     } sequence-case ;
45 >>
46
47 { "twelve" } [ 12 describe-number ] unit-test
48 { "several" } [ 5 describe-number ] unit-test
49 { "millions and millions of" } [ 1,000,000 describe-number ] unit-test
50
51 { { 1 2 3 } } [ 1 { [ ] [ 1 + ] [ 2 + ] } cleave-array ] unit-test
52
53 { 2 15 } [ 1 2 3 4 5 6 [ - - ] [ + + ] 3bi* ] unit-test
54
55 { 2 5 } [ 1 2 3 4 5 6 [ - - ] 3bi@ ] unit-test
56
57 { 3 1 } [ 1 2 [ + ] keepd ] unit-test
58
59 { "1" "123" } [ "1" "123" [ length ] [ > ] swap-when ] unit-test
60 { "123" "1" } [ "1" "123" [ length ] [ < ] swap-when ] unit-test
61
62
63 { t } [ "resource:" [ file-exists? ] ?1arg >boolean ] unit-test
64 { f } [ f [ file-exists? ] ?1arg ] unit-test
65 { f } [ "/homeasdfasdf123123" [ file-exists? ] ?1arg ] unit-test
66
67 { "hi " "there" } [
68     "hi there" {
69         { [ "there" over subseq-start ] [ cut ] }
70         [ f ]
71     } cond*
72 ] unit-test
73
74 { "hi " "there" } [
75     "hi there" {
76         { [ "foo" over subseq-start ] [ head f ] }
77         { [ "there" over subseq-start ] [ cut ] }
78         [ f ]
79     } cond*
80 ] unit-test
81
82 { "hi there" f } [
83     "hi there" {
84         { [ "foo" over subseq-start ] [ head f ] }
85         { [ "bar" over subseq-start ] [ cut ] }
86         [ f ]
87     } cond*
88 ] unit-test
89
90 { "hi " "there" } [
91     "hi there" {
92         { [ dup "there" subseq-index ] [ cut ] }
93         [ f ]
94     } cond*
95 ] unit-test
96
97 { "hi " "there" } [
98     "hi there" {
99         { [ dup "foo" subseq-index ] [ head f ] }
100         { [ dup "there" subseq-index ] [ cut ] }
101         [ f ]
102     } cond*
103 ] unit-test
104
105 { "hi there" f } [
106     "hi there" {
107         { [ dup "foo" subseq-index ] [ head f ] }
108         { [ dup "bar" subseq-index ] [ cut ] }
109         [ f ]
110     } cond*
111 ] unit-test
112
113 { f } [ f { } chain ] unit-test
114 { 3 } [ H{ { 1 H{ { 2 3 } } } } { [ 1 of ] [ 2 of ] } chain ] unit-test
115 { f } [ H{ { 1 H{ { 3 4 } } } } { [ 1 of ] [ 2 of ] } chain ] unit-test
116 { f } [ H{ { 2 H{ { 3 4 } } } } { [ 1 of ] [ 2 of ] } chain ] unit-test
117 { 5 } [
118     "hello factor!" { [ split-words ] [ first ] [ length ] } chain
119 ] unit-test
120
121 {
122     { 1 2 3 4 }
123     { 1 2 3 4 }
124     { 1 2 3 4 }
125     { 1 2 3 4 }
126 } [
127     1 2 3 4
128     [ 4array ] [ 4array ] [ 4array ] [ 4array ] 4quad
129 ] unit-test
130
131 {
132     { 1 2 3 4 }
133     { 5 6 7 8 }
134     { 9 10 11 12 }
135 } [
136     1 2 3 4  5 6 7 8  9 10 11 12
137     [ 4array ] [ 4array ] [ 4array ] 4tri*
138 ] unit-test
139
140 {
141     { 1 2 3 4 }
142     { 5 6 7 8 }
143     { 9 10 11 12 }
144 } [
145     1 2 3 4  5 6 7 8  9 10 11 12
146     [ 4array ] 4tri@
147 ] unit-test
148
149 { 1 2 3 } [ 1 2 [ 3 ] dip-1up ] unit-test
150 { 2 2 } [ 1 2 [ 1 + ] dip-1up ] unit-test
151 { 20 11 } [ 10 20 [ 1 + ] dip-1up ] unit-test
152
153 { 0 10 20 30 40 50 60 80 71 } [ 0 10 20 30 40 50 60 70 80 [ 1 + ]  dip-1up ] unit-test
154 { 0 10 20 30 40 50 70 80 61 } [ 0 10 20 30 40 50 60 70 80 [ 1 + ] 2dip-1up ] unit-test
155 { 0 10 20 30 40 60 70 80 51 } [ 0 10 20 30 40 50 60 70 80 [ 1 + ] 3dip-1up ] unit-test
156
157
158 { 0 10 20 30 40 50 80 61 71 } [ 0 10 20 30 40 50 60 70 80 [ [ 1 + ] bi@ ]  dip-2up ] unit-test
159 { 0 10 20 30 40 70 80 51 61 } [ 0 10 20 30 40 50 60 70 80 [ [ 1 + ] bi@ ] 2dip-2up ] unit-test
160 { 0 10 20 30 60 70 80 41 51 } [ 0 10 20 30 40 50 60 70 80 [ [ 1 + ] bi@ ] 3dip-2up ] unit-test
161
162 { 0 10 20 60 70 80 31 41 51 } [ 0 10 20 30 40 50 60 70 80 [ [ 1 + ] tri@ ] 3dip-3up ] unit-test
163
164 { 4 "abcd" 97 98 99 100 } [
165     0 "abcd"
166     [ [ CHAR: a = ] accept1 ]
167     [ [ CHAR: b = ] accept1 ]
168     [ [ CHAR: c = ] accept1 ]
169     [ [ CHAR: d = ] accept1 ] 4craft-1up
170 ] unit-test
171
172 { 20 30 2500 } [ 20 30 [ + sq ] 2keep-1up ] unit-test
173
174 { 10 1 } [ 10 [ drop 1 ] keep-1up ] unit-test
175 { 10 20 1 } [ 10 20 [ 2drop 1 ] 2keep-1up ] unit-test
176 { 10 20 30 1 } [ 10 20 30 [ 3drop 1 ] 3keep-1up ] unit-test
177
178
179 { 10 1 } [ 10 [ drop 1 ] keep-1up ] unit-test
180 { 10 20 1 } [ 10 20 [ 2drop 1 ] 2keep-1up ] unit-test
181 { 10 20 30 1 } [ 10 20 30 [ 3drop 1 ] 3keep-1up ] unit-test
182
183 { 10 1 2 } [ 10 [ drop 1 2 ] keep-2up ] unit-test
184 { 10 20 1 2 } [ 10 20 [ 2drop 1 2 ] 2keep-2up ] unit-test
185 { 10 20 30 1 2 } [ 10 20 30 [ 3drop 1 2 ] 3keep-2up ] unit-test
186
187 { 10 1 2 3 } [ 10 [ drop 1 2 3 ] keep-3up ] unit-test
188 { 10 20 1 2 3 } [ 10 20 [ 2drop 1 2 3 ] 2keep-3up ] unit-test
189 { 10 20 30 1 2 3 } [ 10 20 30 [ 3drop 1 2 3 ] 3keep-3up ] unit-test
190
191 : test-keep-under ( -- a b c d e ) 1 [ [ 5 + ] call 10 20 30 ] keep-under ;
192 : test-2keep-under ( -- a b c d e f g ) 1 2 [ [ 5 + ] bi@ 10 20 30 ] 2keep-under ;
193 : test-3keep-under ( -- a b c d e f g h i ) 1 2 3 [ [ 5 + ] tri@ 10 20 30 ] 3keep-under ;
194 : test-4keep-under ( -- a b c d e f g h i j k l ) 1 2 3 4 [ [ 5 + ] quad@ 10 20 30 40 ] 4keep-under ;
195
196 { 1 6 10 20 30 } [ test-keep-under ] unit-test
197 { 1 2 6 7 10 20 30 } [ test-2keep-under ] unit-test
198 { 1 2 3 6 7 8 10 20 30 } [ test-3keep-under ] unit-test
199 { 1 2 3 4  6 7 8 9 10 20 30 40 } [ test-4keep-under ] unit-test
200
201 { 1 2 3 4 1 2 3 4 5 } [ 1 2 3 4 [ 5 ] 4keep-under ] unit-test
202 { 1 2 3 4 1 2 3 4 5 6 7 8 9 10 } [ 1 2 3 4 [ 5 6 7 8 9 10 ] 4keep-under ] unit-test
203
204
205 { 3 { 1 2 3 } }
206 [ 0 { 1 2 3 } [ 1 + ] 1temp1d map ] unit-test
207
208 { 3 { { 1 1 } { 2 2 } { 3 3 } } }
209 [ 0 { { 1 1 } { 2 2 } { 3 3 } } [ 1 + ] 1temp2d assoc-map ] unit-test
210
211 { 103 203 { { 1 1 } { 2 2 } { 3 3 } } }
212 [ 100 200 { { 1 1 } { 2 2 } { 3 3 } } [ [ 1 + ] bi@ ] 2temp2d assoc-map ] unit-test
213
214 { t } [ int [ c-type-name? ] [ lookup-c-type ] 1check-when c-type? ] unit-test
215
216 { 111 112 113 114 } [ 10 100 [ 1 + + ] [ 2  + + ] [ 3 + + ] [ 4 + + ] 2quad ] unit-test