]> gitweb.factorcode.org Git - factor.git/blob - basis/sequences/generalizations/generalizations-tests.factor
2e0c2150c3bf18efdb2079f6c543a0783b821cf2
[factor.git] / basis / sequences / generalizations / generalizations-tests.factor
1 ! Copyright (C) 2009 Joe Groff.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: io io.streams.string kernel math sequences
4 sequences.generalizations tools.test ;
5 IN: sequences.generalizations.tests
6
7 { 1 2 3 4 } [ { 1 2 3 4 } 4 firstn ] unit-test
8 { { 1 2 3 4 } } [ 1 2 3 4 { f f f f } [ 4 set-firstn ] keep ] unit-test
9 [ 1 2 3 4 { f f f } [ 4 set-firstn ] keep ] must-fail
10 { } [ { } 0 firstn ] unit-test
11 { "a" } [ { "a" } 1 firstn ] unit-test
12
13 { } [ { } 0 ?firstn ] unit-test
14 { f } [ { } 1 ?firstn ] unit-test
15 { f f } [ { } 2 ?firstn ] unit-test
16 { 1 f } [ { 1 } 2 ?firstn ] unit-test
17 { 1 2 } [ { 1 2 } 2 ?firstn ] unit-test
18 { 1 2 } [ { 1 2 3 } 2 ?firstn ] unit-test
19
20 { [ 1 2 ] } [ 1 2 2 [ ] nsequence ] unit-test
21 { { 1 2 3 4 5 } } [ 1 2 3 4 5 { 0 0 0 0 0 } 5 (nsequence) ] unit-test
22
23 { { 1 2 3 4 } } [ { 1 } { 2 } { 3 } { 4 } 4 nappend ] unit-test
24 { V{ 1 2 3 4 } } [ { 1 } { 2 } { 3 } { 4 } 4 V{ } nappend-as ] unit-test
25
26 [ 4 nappend ] must-infer
27 [ 4 { } nappend-as ] must-infer
28
29 : neach-test ( a b c d -- )
30     [ 4 nappend print ] 4 neach ;
31 : nmap-test ( a b c d -- e )
32     [ 4 nappend ] 4 nmap ;
33 : nmap-as-test ( a b c d -- e )
34     [ 4 nappend ] [ ] 4 nmap-as ;
35 : mnmap-3-test ( a b c d -- e f g )
36     [ append ] 4 3 mnmap ;
37 : mnmap-2-test ( a b c d -- e f )
38     [ [ append ] 2bi@ ] 4 2 mnmap ;
39 : mnmap-as-test ( a b c d -- e f )
40     [ [ append ] 2bi@ ] { } [ ] 4 2 mnmap-as ;
41 : mnmap-1-test ( a b c d -- e )
42     [ 4 nappend ] 4 1 mnmap ;
43 : mnmap-0-test ( a b c d -- )
44     [ 4 nappend print ] 4 0 mnmap ;
45 : nproduce-as-test ( n -- a b )
46     [ dup zero? not ]
47     [ [ 2 - ] [ ] [ 1 - ] tri ] { } B{ } 2 nproduce-as nipd ;
48 : nproduce-test ( n -- a b )
49     [ dup zero? not ]
50     [ [ 2 - ] [ ] [ 1 - ] tri ] 2 nproduce nipd ;
51
52 { "A1a!
53 B2b@
54 C3c#
55 D4d$
56 " } [
57     { "A" "B" "C" "D" }
58     { "1" "2" "3" "4" }
59     { "a" "b" "c" "d" }
60     { "!" "@" "#" "$" }
61     [ neach-test ] with-string-writer
62 ] unit-test
63
64 { { "A1a!" "B2b@" "C3c#" "D4d$" } }
65 [
66     { "A" "B" "C" "D" }
67     { "1" "2" "3" "4" }
68     { "a" "b" "c" "d" }
69     { "!" "@" "#" "$" }
70     nmap-test
71 ] unit-test
72
73 { [ "A1a!" "B2b@" "C3c#" "D4d$" ] }
74 [
75     { "A" "B" "C" "D" }
76     { "1" "2" "3" "4" }
77     { "a" "b" "c" "d" }
78     { "!" "@" "#" "$" }
79     nmap-as-test
80 ] unit-test
81
82 {
83     { "A" "B" "C" "D" }
84     { "1" "2" "3" "4" }
85     { "a!" "b@" "c#" "d$" }
86 } [
87     { "A" "B" "C" "D" }
88     { "1" "2" "3" "4" }
89     { "a" "b" "c" "d" }
90     { "!" "@" "#" "$" }
91     mnmap-3-test
92 ] unit-test
93
94 {
95     { "A1" "B2" "C3" "D4" }
96     { "a!" "b@" "c#" "d$" }
97 } [
98     { "A" "B" "C" "D" }
99     { "1" "2" "3" "4" }
100     { "a" "b" "c" "d" }
101     { "!" "@" "#" "$" }
102     mnmap-2-test
103 ] unit-test
104
105 {
106     { "A1" "B2" "C3" "D4" }
107     [ "a!" "b@" "c#" "d$" ]
108 } [
109     { "A" "B" "C" "D" }
110     { "1" "2" "3" "4" }
111     { "a" "b" "c" "d" }
112     { "!" "@" "#" "$" }
113     mnmap-as-test
114 ] unit-test
115
116 { { "A1a!" "B2b@" "C3c#" "D4d$" } }
117 [
118     { "A" "B" "C" "D" }
119     { "1" "2" "3" "4" }
120     { "a" "b" "c" "d" }
121     { "!" "@" "#" "$" }
122     mnmap-1-test
123 ] unit-test
124
125 { "A1a!
126 B2b@
127 C3c#
128 D4d$
129 " } [
130     { "A" "B" "C" "D" }
131     { "1" "2" "3" "4" }
132     { "a" "b" "c" "d" }
133     { "!" "@" "#" "$" }
134     [ mnmap-0-test ] with-string-writer
135 ] unit-test
136
137 { { 10 8 6 4 2 } B{ 9 7 5 3 1 } }
138 [ 10 nproduce-as-test ] unit-test
139
140 { { 10 8 6 4 2 } { 9 7 5 3 1 } }
141 [ 10 nproduce-test ] unit-test
142
143 { 45 } [
144     { 1 2 3 } { 4 5 6 } { 7 8 9 } [ + + ] [ + ] 3 nmap-reduce
145 ] unit-test
146
147 { t } [
148     { 1 3 5 } { 2 4 6 } { 4 8 12 } [ + + odd? ] 3 nall?
149 ] unit-test
150
151 { t } [
152     { 2 4 5 } { 4 6 7 } { 6 8 9 }
153     [ [ odd? ] tri@ and and ] 3 nany?
154 ] unit-test
155
156 { f } [
157     { 1 2 3 } { 4 5 6 } { 7 8 9 }
158     [ [ odd? ] tri@ and and ] 3 nany?
159 ] unit-test