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